pyro1son

X6R Firmware V1.1

Oct 8th, 2015
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* PYRO_momentary_charger version 1.1
  2.  * Designed for use with X6R driver designed by pilotdog68, integrating original charger circuit.
  3.  * Changelog
  4.  *
  5.  * 1.0 First stab at this based on MTN_momentary_temp version 1.0
  6.  * 1.1 Addition of Hold to Strobe function 1.1
  7.  */
  8.  
  9. /*
  10.  * NANJG 105C Diagram
  11.  *                ---
  12.  *              -|   |- VCC
  13.  *       Switch -|   |- Voltage ADC
  14.  *  CHARGER MON -|   |- PWM
  15.  *          GND -|   |- ALT PWM
  16.  *                ---
  17.  *
  18.  * FUSES
  19.  *      I use these fuse settings
  20.  *      Low:  0x75  (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
  21.  *      High: 0xff
  22.  *
  23.  *      For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
  24.  *
  25.  * STARS
  26.  *      Star 2 = Alt PWM output
  27.  *      Star 3 = Charger on? input
  28.  *      Star 4 = Switch input
  29.  *
  30.  * VOLTAGE
  31.  *      Resistor values for voltage divider (reference BLF-VLD README for more info)
  32.  *      Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
  33.  *
  34.  *           VCC
  35.  *            |
  36.  *           Vd (~.25 v drop from protection diode)
  37.  *            |
  38.  *          1912 (R1 19,100 ohms)
  39.  *            |
  40.  *            |---- PB2 from MCU
  41.  *            |
  42.  *          4701 (R2 4,700 ohms)
  43.  *            |
  44.  *           GND
  45.  *
  46.  *      ADC = ((V_bat - V_diode) * R2   * 255) / ((R1    + R2  ) * V_ref)
  47.  *      125 = ((3.0   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
  48.  *      121 = ((2.9   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
  49.  *
  50.  *      Well 125 and 121 were too close, so it shut off right after lowering to low mode, so I went with
  51.  *      130 and 120
  52.  *
  53.  *      To find out what value to use, plug in the target voltage (V) to this equation
  54.  *          value = (V * 4700 * 255) / (23800 * 1.1)
  55.  *      
  56.  */
  57. #define F_CPU 4800000UL
  58.  
  59. // PWM Mode
  60. #define PHASE 0b00000001
  61. #define FAST  0b00000011
  62.  
  63. /*
  64.  * =========================================================================
  65.  * Settings to modify per driver
  66.  */
  67.  
  68. #define VOLTAGE_MON         // Comment out to disable - ramp down and eventual shutoff when battery is low
  69. #define CHARGER_MON            // Comment out to disable - is charging cable connected
  70. #define MODES           0,0,0,17,100,255        // Must be low to high, and must start with 0
  71. #define ALT_MODES       0,3,17,0,0,0    // Must be low to high, and must start with 0, the defines the level for the secondary output. Comment out if no secondary output
  72. #define MODE_PWM        0,PHASE,FAST,FAST,FAST,FAST     // Define one per mode above. 0 tells the light to go to sleep
  73. #define TURBO               // Comment out to disable - full output with a step down after n number of seconds
  74.                             // If turbo is enabled, it will be where 255 is listed in the modes above
  75. #define TURBO_TIMEOUT   5625 // How many WTD ticks before before dropping down (.016 sec each)
  76.                             // 90  = 5625
  77.                             // 120 = 7500
  78.                            
  79. #define BATT_LOW        140 // When do we start ramping
  80. #define BATT_CRIT       130 // When do we shut the light off
  81. #define CHARGER_ON       250 // When we should lower the output
  82. #define BATT_ADC_DELAY  188 // Delay in ticks between low-bat rampdowns (188 ~= 3s)
  83. #define CHARGER_ADC_DELAY   15  // Delay in ticks before checking for charging cable (188 ~= 3s)
  84.  
  85.  
  86. /*
  87.  * =========================================================================
  88.  */
  89.  
  90. #include <avr/pgmspace.h>
  91. #include <avr/io.h>
  92. #include <util/delay.h>
  93. #include <avr/interrupt.h>
  94. #include <avr/wdt.h>   
  95. #include <avr/eeprom.h>
  96. #include <avr/sleep.h>
  97. //#include <avr/power.h>
  98.  
  99. #define SWITCH_PIN  PB3     // what pin the switch is connected to, which is Star 4
  100. #define PWM_PIN     PB1
  101. #define ALT_PWM_PIN PB0
  102. #define ADC_DIDR    ADC1D   // Digital input disable bit corresponding with PB2
  103. #define ADC_PRSCL   0x06    // clk/64
  104.  
  105. #define PWM_LVL     OCR0B   // OCR0B is the output compare register for PB1
  106. #define ALT_PWM_LVL OCR0A   // OCR0A is the output compare register for PB0
  107.  
  108. //#define DEBOUNCE_BOTH          // Comment out if you don't want to debounce the PRESS along with the RELEASE
  109.                                // PRESS debounce is only needed in special cases where the switch can experience errant signals
  110. #define DB_PRES_DUR 0b00000001 // time before we consider the switch pressed (after first realizing it was pressed)
  111. #define DB_REL_DUR  0b00001111 // time before we consider the switch released
  112.                                // each bit of 1 from the right equals 16ms, so 0x0f = 64ms
  113.  
  114. // Switch handling
  115. #define LONG_PRESS_DUR   32 // How many WDT ticks until we consider a press a long press
  116.                             // 32 is roughly .5 s
  117. #define STROBE_DUR       96 // How many WDT ticks until we enter the hold-to-strobe mode
  118.  
  119.  
  120. /*
  121.  * The actual program
  122.  * =========================================================================
  123.  */
  124.  
  125. /*
  126.  * global variables
  127.  */
  128. const uint8_t modes[]     = { MODES    };
  129. #ifdef ALT_MODES
  130. const uint8_t alt_modes[] = { ALT_MODES };
  131. #endif
  132. const uint8_t mode_pwm[] = { MODE_PWM };
  133. volatile uint8_t mode_idx = 0;
  134. volatile uint8_t press_duration = 0;
  135. volatile uint8_t in_momentary = 0;
  136. #ifdef VOLTAGE_MON
  137. volatile uint8_t adc_channel = 1;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  138. #else
  139. volatile uint8_t adc_channel = 2;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  140. #endif
  141.  
  142. // Debounce switch press value
  143. #ifdef DEBOUNCE_BOTH
  144. int is_pressed()
  145. {
  146.     static uint8_t pressed = 0;
  147.     // Keep track of last switch values polled
  148.     static uint8_t buffer = 0x00;
  149.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  150.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  151.    
  152.     if (pressed) {
  153.         // Need to look for a release indicator by seeing if the last switch status has been 0 for n number of polls
  154.         pressed = (buffer & DB_REL_DUR);
  155.     } else {
  156.         // Need to look for pressed indicator by seeing if the last switch status was 1 for n number of polls
  157.         pressed = ((buffer & DB_PRES_DUR) == DB_PRES_DUR);
  158.     }
  159.  
  160.     return pressed;
  161. }
  162. #else
  163. int is_pressed()
  164. {
  165.     // Keep track of last switch values polled
  166.     static uint8_t buffer = 0x00;
  167.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  168.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  169.    
  170.     return (buffer & DB_REL_DUR);
  171. }
  172. #endif
  173.  
  174. void next_mode() {
  175.     if (++mode_idx >= sizeof(modes)) {
  176.         // Wrap around
  177.         mode_idx = 2;
  178.     }  
  179. }
  180.  
  181. void prev_mode() {
  182.     if (mode_idx == 0) {
  183.     } else {
  184.         --mode_idx;
  185.     }
  186. }
  187.  
  188. inline void PCINT_on() {
  189.     // Enable pin change interrupts
  190.     GIMSK |= (1 << PCIE);
  191. }
  192.  
  193. inline void PCINT_off() {
  194.     // Disable pin change interrupts
  195.     GIMSK &= ~(1 << PCIE);
  196. }
  197.  
  198. // Need an interrupt for when pin change is enabled to ONLY wake us from sleep.
  199. // All logic of what to do when we wake up will be handled in the main loop.
  200. EMPTY_INTERRUPT(PCINT0_vect);
  201.  
  202. inline void WDT_on() {
  203.     // Setup watchdog timer to only interrupt, not reset, every 16ms.
  204.     cli();                          // Disable interrupts
  205.     wdt_reset();                    // Reset the WDT
  206.     WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
  207.     WDTCR = (1<<WDTIE);             // Enable interrupt every 16ms
  208.     sei();                          // Enable interrupts
  209. }
  210.  
  211. inline void WDT_off()
  212. {
  213.     cli();                          // Disable interrupts
  214.     wdt_reset();                    // Reset the WDT
  215.     MCUSR &= ~(1<<WDRF);            // Clear Watchdog reset flag
  216.     WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
  217.     WDTCR = 0x00;                   // Disable WDT
  218.     sei();                          // Enable interrupts
  219. }
  220.  
  221. void ADC_on() {
  222.     ADMUX  = (1 << REFS0) | (1 << ADLAR) | adc_channel; // 1.1v reference, left-adjust, ADC1/PB2 or ADC2/PB3
  223.     DIDR0 |= (1 << ADC_DIDR);                           // disable digital input on ADC pin to reduce power consumption
  224.     ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale, Single Conversion mode
  225. }
  226.  
  227. void ADC_off() {
  228.     ADCSRA &= ~(1 << ADSC); //ADC off
  229. }
  230.  
  231. void sleep_until_switch_press()
  232. {
  233.     // This routine takes up a lot of program memory :(
  234.     // Turn the WDT off so it doesn't wake us from sleep
  235.     // Will also ensure interrupts are on or we will never wake up
  236.     WDT_off();
  237.     // Need to reset press duration since a button release wasn't recorded
  238.     press_duration = 0;
  239.     // Enable a pin change interrupt to wake us up
  240.     // However, we have to make sure the switch is released otherwise we will wake when the user releases the switch
  241.     while (is_pressed()) {
  242.         _delay_ms(16);
  243.     }
  244.     PCINT_on();
  245.     // Enable sleep mode set to Power Down that will be triggered by the sleep_mode() command.
  246.     //set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  247.     // Now go to sleep
  248.     sleep_mode();
  249.     // Hey, someone must have pressed the switch!!
  250.     // Disable pin change interrupt because it's only used to wake us up
  251.     PCINT_off();
  252.     // Turn the WDT back on to check for switch presses
  253.     WDT_on();
  254.     // Go back to main program
  255. }
  256.  
  257. // The watchdog timer is called every 16ms
  258. ISR(WDT_vect) {
  259.  
  260.     //static uint8_t  press_duration = 0;  // Pressed or not pressed
  261.     static uint16_t turbo_ticks = 0;
  262.     static uint16_t batt_adc_ticks = BATT_ADC_DELAY;
  263.     static uint16_t charger_adc_ticks = CHARGER_ADC_DELAY;
  264.     static uint8_t  lowbatt_cnt = 0;
  265.     static uint8_t  highest_mode_idx = 255;
  266.  
  267.     if (is_pressed()) {
  268.         if (press_duration < 255) {
  269.             press_duration++;
  270.         }
  271.        
  272.         if (press_duration == LONG_PRESS_DUR) {
  273.             // Long press
  274.                 prev_mode();
  275.             highest_mode_idx = mode_idx;
  276.             }
  277.         //hold-to-strobe code
  278.         if (press_duration == STROBE_DUR) {
  279.             while(is_pressed()){
  280.                             //Exit loop once switch is let go
  281.                 PWM_LVL = 255;
  282.                 _delay_ms(20);
  283.                 PWM_LVL=0;
  284.                 _delay_ms(60);
  285.                                 //Can alter values to change strobe duration
  286.             }
  287.         next_mode();
  288.                 //Return to the previous mode because we actually went back a
  289.                 //mode before entering strobe
  290.         }
  291.         // Just always reset turbo timer whenever the button is pressed
  292.         turbo_ticks = 0;
  293.         // Same with the ramp down delay
  294.         batt_adc_ticks = BATT_ADC_DELAY;
  295.         charger_adc_ticks = CHARGER_ADC_DELAY;
  296.    
  297.     } else {
  298.        
  299.         #ifdef MOM_ENTER_DUR
  300.         if (in_momentary) {
  301.             // Turn off the light
  302.             mode_idx = 0;
  303.             return;
  304.         }
  305.         #endif
  306.        
  307.         // Not pressed
  308.         if (press_duration > 0 && press_duration < LONG_PRESS_DUR) {
  309.             // Short press
  310.                 next_mode();
  311.             highest_mode_idx = mode_idx;
  312.         } else {
  313.             // Only do turbo check when switch isn't pressed
  314.         #ifdef TURBO
  315.             if (modes[mode_idx] == 255) {
  316.                 turbo_ticks++;
  317.                 if (turbo_ticks > TURBO_TIMEOUT) {
  318.                     // Go to the previous mode
  319.                     prev_mode();
  320.                 }
  321.             }
  322.         #endif
  323.             // Only do voltage monitoring when the switch isn't pressed
  324.             // See if conversion is done. We moved this up here because we want to stay on
  325.             // the current ADC input until the conversion is done, and then switch to the new
  326.             // input, start the monitoring
  327.             if (batt_adc_ticks > 0) {
  328.                 --batt_adc_ticks;
  329.             }
  330.             if (charger_adc_ticks > 0) {
  331.                 --charger_adc_ticks;
  332.             }
  333.             if (ADCSRA & (1 << ADIF)) {
  334.                 if (adc_channel == 0x01) {
  335.                    
  336.                     if (batt_adc_ticks == 0) {
  337.                         // See if voltage is lower than what we were looking for
  338.                         if (ADCH < ((mode_idx == 1) ? BATT_CRIT : BATT_LOW)) {
  339.                             ++lowbatt_cnt;
  340.                         } else {
  341.                             lowbatt_cnt = 0;
  342.                         }
  343.                    
  344.                         // See if it's been low for a while
  345.                         if (lowbatt_cnt >= 5) {
  346.                             prev_mode();
  347.                             highest_mode_idx = mode_idx;
  348.                             lowbatt_cnt = 0;
  349.                             // If we reach 0 here, main loop will go into sleep mode
  350.                             // Restart the counter to when we step down again
  351.                             batt_adc_ticks = BATT_ADC_DELAY;
  352.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  353.                         }
  354.                     }
  355.                     // Switch ADC to charger monitoring
  356.                     adc_channel = 0x02;
  357.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  358.                 } else if (adc_channel == 0x02) {
  359.                    
  360.                     if (charger_adc_ticks == 0) {
  361.                         // See if charger is higher than the high threshold
  362.                         if (ADCH > ((mode_idx == 1) ? 255 : CHARGER_ON)) {
  363.                             mode_idx = 0;
  364.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  365.                             batt_adc_ticks = BATT_ADC_DELAY;
  366.                         } else {
  367.                             mode_idx;
  368.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  369.                             batt_adc_ticks = BATT_ADC_DELAY;
  370.                         }
  371.                     }
  372.                     #ifdef VOLTAGE_MON
  373.                     // Switch ADC to battery monitoring
  374.                     adc_channel = 0x01;
  375.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  376.                     #endif;
  377.                 }
  378.             }
  379.             // Start conversion for next time through
  380.             ADCSRA |= (1 << ADSC);
  381.         }
  382.         press_duration = 0;
  383.     }
  384. }
  385.  
  386. int main(void)
  387. {  
  388.     // Set all ports to input, and turn pull-up resistors on for the inputs we are using
  389.     DDRB = 0x00;
  390.     PORTB = (1 << SWITCH_PIN);
  391.  
  392.     // Set the switch as an interrupt for when we turn pin change interrupts on
  393.     PCMSK = (1 << SWITCH_PIN);
  394.    
  395.     // Set PWM pin to output
  396.     #ifdef ALT_MODES
  397.     DDRB = (1 << PWM_PIN) | (1 << ALT_PWM_PIN);
  398.     #else
  399.     DDRB = (1 << PWM_PIN);
  400.     #endif
  401.  
  402.     // Set timer to do PWM for correct output pin and set prescaler timing
  403.     //TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
  404.     TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
  405.    
  406.     // Turn features on or off as needed
  407.     ADC_on();
  408.     ACSR   |=  (1<<7); //AC off
  409.    
  410.     // Enable sleep mode set to Power Down that will be triggered by the sleep_mode() command.
  411.     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  412.     sleep_until_switch_press();
  413.    
  414.     uint8_t last_mode_idx = 0;
  415.    
  416.     while(1) {
  417.         // We will never leave this loop.  The WDT will interrupt to check for switch presses and
  418.         // will change the mode if needed.  If this loop detects that the mode has changed, run the
  419.         // logic for that mode while continuing to check for a mode change.
  420.         if (mode_idx != last_mode_idx) {
  421.             // The WDT changed the mode.
  422.             if (mode_idx > 0) {
  423.                 // No need to change the mode if we are just turning the light off
  424.                 // Check if the PWM mode is different
  425.                 if (mode_pwm[last_mode_idx] != mode_pwm[mode_idx]) {
  426.                     #ifdef ALT_MODES
  427.                     TCCR0A = mode_pwm[mode_idx] | 0b10100000;  // Use both outputs
  428.                     #else
  429.                     TCCR0A = mode_pwm[mode_idx] | 0b00100000;  // Only use the normal output
  430.                     #endif
  431.                 }
  432.             }
  433.             PWM_LVL     = modes[mode_idx];
  434.             #ifdef ALT_MODES
  435.             ALT_PWM_LVL = alt_modes[mode_idx];
  436.             #endif
  437.             last_mode_idx = mode_idx;
  438.             if (mode_pwm[mode_idx] == 0) {
  439.                 _delay_ms(1); // Need this here, maybe instructions for PWM output not getting executed before shutdown?
  440.                 // Go to sleep
  441.                 sleep_until_switch_press();
  442.             }
  443.         }
  444.     }
  445.  
  446.     return 0; // Standard Return Code
  447. }
RAW Paste Data