Advertisement
pyro1son

X6R Firmware V1.0

Oct 8th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.73 KB | None | 0 0
  1. /* PYRO_momentary_charger version 1.0
  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.  *
  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.  
  118.  
  119. /*
  120.  * The actual program
  121.  * =========================================================================
  122.  */
  123.  
  124. /*
  125.  * global variables
  126.  */
  127. const uint8_t modes[]     = { MODES    };
  128. #ifdef ALT_MODES
  129. const uint8_t alt_modes[] = { ALT_MODES };
  130. #endif
  131. const uint8_t mode_pwm[] = { MODE_PWM };
  132. volatile uint8_t mode_idx = 0;
  133. volatile uint8_t press_duration = 0;
  134. volatile uint8_t in_momentary = 0;
  135. #ifdef VOLTAGE_MON
  136. volatile uint8_t adc_channel = 1;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  137. #else
  138. volatile uint8_t adc_channel = 2;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  139. #endif
  140.  
  141. // Debounce switch press value
  142. #ifdef DEBOUNCE_BOTH
  143. int is_pressed()
  144. {
  145.     static uint8_t pressed = 0;
  146.     // Keep track of last switch values polled
  147.     static uint8_t buffer = 0x00;
  148.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  149.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  150.    
  151.     if (pressed) {
  152.         // Need to look for a release indicator by seeing if the last switch status has been 0 for n number of polls
  153.         pressed = (buffer & DB_REL_DUR);
  154.     } else {
  155.         // Need to look for pressed indicator by seeing if the last switch status was 1 for n number of polls
  156.         pressed = ((buffer & DB_PRES_DUR) == DB_PRES_DUR);
  157.     }
  158.  
  159.     return pressed;
  160. }
  161. #else
  162. int is_pressed()
  163. {
  164.     // Keep track of last switch values polled
  165.     static uint8_t buffer = 0x00;
  166.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  167.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  168.    
  169.     return (buffer & DB_REL_DUR);
  170. }
  171. #endif
  172.  
  173. void next_mode() {
  174.     if (++mode_idx >= sizeof(modes)) {
  175.         // Wrap around
  176.         mode_idx = 2;
  177.     }  
  178. }
  179.  
  180. void prev_mode() {
  181.     if (mode_idx == 0) {
  182.     } else {
  183.         --mode_idx;
  184.     }
  185. }
  186.  
  187. inline void PCINT_on() {
  188.     // Enable pin change interrupts
  189.     GIMSK |= (1 << PCIE);
  190. }
  191.  
  192. inline void PCINT_off() {
  193.     // Disable pin change interrupts
  194.     GIMSK &= ~(1 << PCIE);
  195. }
  196.  
  197. // Need an interrupt for when pin change is enabled to ONLY wake us from sleep.
  198. // All logic of what to do when we wake up will be handled in the main loop.
  199. EMPTY_INTERRUPT(PCINT0_vect);
  200.  
  201. inline void WDT_on() {
  202.     // Setup watchdog timer to only interrupt, not reset, every 16ms.
  203.     cli();                          // Disable interrupts
  204.     wdt_reset();                    // Reset the WDT
  205.     WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
  206.     WDTCR = (1<<WDTIE);             // Enable interrupt every 16ms
  207.     sei();                          // Enable interrupts
  208. }
  209.  
  210. inline void WDT_off()
  211. {
  212.     cli();                          // Disable interrupts
  213.     wdt_reset();                    // Reset the WDT
  214.     MCUSR &= ~(1<<WDRF);            // Clear Watchdog reset flag
  215.     WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
  216.     WDTCR = 0x00;                   // Disable WDT
  217.     sei();                          // Enable interrupts
  218. }
  219.  
  220. void ADC_on() {
  221.     ADMUX  = (1 << REFS0) | (1 << ADLAR) | adc_channel; // 1.1v reference, left-adjust, ADC1/PB2 or ADC2/PB3
  222.     DIDR0 |= (1 << ADC_DIDR);                           // disable digital input on ADC pin to reduce power consumption
  223.     ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale, Single Conversion mode
  224. }
  225.  
  226. void ADC_off() {
  227.     ADCSRA &= ~(1 << ADSC); //ADC off
  228. }
  229.  
  230. void sleep_until_switch_press()
  231. {
  232.     // This routine takes up a lot of program memory :(
  233.     // Turn the WDT off so it doesn't wake us from sleep
  234.     // Will also ensure interrupts are on or we will never wake up
  235.     WDT_off();
  236.     // Need to reset press duration since a button release wasn't recorded
  237.     press_duration = 0;
  238.     // Enable a pin change interrupt to wake us up
  239.     // However, we have to make sure the switch is released otherwise we will wake when the user releases the switch
  240.     while (is_pressed()) {
  241.         _delay_ms(16);
  242.     }
  243.     PCINT_on();
  244.     // Enable sleep mode set to Power Down that will be triggered by the sleep_mode() command.
  245.     //set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  246.     // Now go to sleep
  247.     sleep_mode();
  248.     // Hey, someone must have pressed the switch!!
  249.     // Disable pin change interrupt because it's only used to wake us up
  250.     PCINT_off();
  251.     // Turn the WDT back on to check for switch presses
  252.     WDT_on();
  253.     // Go back to main program
  254. }
  255.  
  256. // The watchdog timer is called every 16ms
  257. ISR(WDT_vect) {
  258.  
  259.     //static uint8_t  press_duration = 0;  // Pressed or not pressed
  260.     static uint16_t turbo_ticks = 0;
  261.     static uint16_t batt_adc_ticks = BATT_ADC_DELAY;
  262.     static uint16_t charger_adc_ticks = CHARGER_ADC_DELAY;
  263.     static uint8_t  lowbatt_cnt = 0;
  264.     static uint8_t  highest_mode_idx = 255;
  265.  
  266.     if (is_pressed()) {
  267.         if (press_duration < 255) {
  268.             press_duration++;
  269.         }
  270.        
  271.         if (press_duration == LONG_PRESS_DUR) {
  272.             // Long press
  273.                 prev_mode();
  274.             highest_mode_idx = mode_idx;
  275.             }
  276.  
  277.         // Just always reset turbo timer whenever the button is pressed
  278.         turbo_ticks = 0;
  279.         // Same with the ramp down delay
  280.         batt_adc_ticks = BATT_ADC_DELAY;
  281.         charger_adc_ticks = CHARGER_ADC_DELAY;
  282.    
  283.     } else {
  284.        
  285.         #ifdef MOM_ENTER_DUR
  286.         if (in_momentary) {
  287.             // Turn off the light
  288.             mode_idx = 0;
  289.             return;
  290.         }
  291.         #endif
  292.        
  293.         // Not pressed
  294.         if (press_duration > 0 && press_duration < LONG_PRESS_DUR) {
  295.             // Short press
  296.                 next_mode();
  297.             highest_mode_idx = mode_idx;
  298.         } else {
  299.             // Only do turbo check when switch isn't pressed
  300.         #ifdef TURBO
  301.             if (modes[mode_idx] == 255) {
  302.                 turbo_ticks++;
  303.                 if (turbo_ticks > TURBO_TIMEOUT) {
  304.                     // Go to the previous mode
  305.                     prev_mode();
  306.                 }
  307.             }
  308.         #endif
  309.             // Only do voltage monitoring when the switch isn't pressed
  310.             // See if conversion is done. We moved this up here because we want to stay on
  311.             // the current ADC input until the conversion is done, and then switch to the new
  312.             // input, start the monitoring
  313.             if (batt_adc_ticks > 0) {
  314.                 --batt_adc_ticks;
  315.             }
  316.             if (charger_adc_ticks > 0) {
  317.                 --charger_adc_ticks;
  318.             }
  319.             if (ADCSRA & (1 << ADIF)) {
  320.                 if (adc_channel == 0x01) {
  321.                    
  322.                     if (batt_adc_ticks == 0) {
  323.                         // See if voltage is lower than what we were looking for
  324.                         if (ADCH < ((mode_idx == 1) ? BATT_CRIT : BATT_LOW)) {
  325.                             ++lowbatt_cnt;
  326.                         } else {
  327.                             lowbatt_cnt = 0;
  328.                         }
  329.                    
  330.                         // See if it's been low for a while
  331.                         if (lowbatt_cnt >= 5) {
  332.                             prev_mode();
  333.                             highest_mode_idx = mode_idx;
  334.                             lowbatt_cnt = 0;
  335.                             // If we reach 0 here, main loop will go into sleep mode
  336.                             // Restart the counter to when we step down again
  337.                             batt_adc_ticks = BATT_ADC_DELAY;
  338.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  339.                         }
  340.                     }
  341.                     // Switch ADC to charger monitoring
  342.                     adc_channel = 0x02;
  343.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  344.                 } else if (adc_channel == 0x02) {
  345.                    
  346.                     if (charger_adc_ticks == 0) {
  347.                         // See if charger is higher than the high threshold
  348.                         if (ADCH > ((mode_idx == 1) ? 255 : CHARGER_ON)) {
  349.                             mode_idx = 0;
  350.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  351.                             batt_adc_ticks = BATT_ADC_DELAY;
  352.                         } else {
  353.                             mode_idx;
  354.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  355.                             batt_adc_ticks = BATT_ADC_DELAY;
  356.                         }
  357.                     }
  358.                     #ifdef VOLTAGE_MON
  359.                     // Switch ADC to battery monitoring
  360.                     adc_channel = 0x01;
  361.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  362.                     #endif;
  363.                 }
  364.             }
  365.             // Start conversion for next time through
  366.             ADCSRA |= (1 << ADSC);
  367.         }
  368.         press_duration = 0;
  369.     }
  370. }
  371.  
  372. int main(void)
  373. {  
  374.     // Set all ports to input, and turn pull-up resistors on for the inputs we are using
  375.     DDRB = 0x00;
  376.     PORTB = (1 << SWITCH_PIN);
  377.  
  378.     // Set the switch as an interrupt for when we turn pin change interrupts on
  379.     PCMSK = (1 << SWITCH_PIN);
  380.    
  381.     // Set PWM pin to output
  382.     #ifdef ALT_MODES
  383.     DDRB = (1 << PWM_PIN) | (1 << ALT_PWM_PIN);
  384.     #else
  385.     DDRB = (1 << PWM_PIN);
  386.     #endif
  387.  
  388.     // Set timer to do PWM for correct output pin and set prescaler timing
  389.     //TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
  390.     TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
  391.    
  392.     // Turn features on or off as needed
  393.     ADC_on();
  394.     ACSR   |=  (1<<7); //AC off
  395.    
  396.     // Enable sleep mode set to Power Down that will be triggered by the sleep_mode() command.
  397.     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  398.     sleep_until_switch_press();
  399.    
  400.     uint8_t last_mode_idx = 0;
  401.    
  402.     while(1) {
  403.         // We will never leave this loop.  The WDT will interrupt to check for switch presses and
  404.         // will change the mode if needed.  If this loop detects that the mode has changed, run the
  405.         // logic for that mode while continuing to check for a mode change.
  406.         if (mode_idx != last_mode_idx) {
  407.             // The WDT changed the mode.
  408.             if (mode_idx > 0) {
  409.                 // No need to change the mode if we are just turning the light off
  410.                 // Check if the PWM mode is different
  411.                 if (mode_pwm[last_mode_idx] != mode_pwm[mode_idx]) {
  412.                     #ifdef ALT_MODES
  413.                     TCCR0A = mode_pwm[mode_idx] | 0b10100000;  // Use both outputs
  414.                     #else
  415.                     TCCR0A = mode_pwm[mode_idx] | 0b00100000;  // Only use the normal output
  416.                     #endif
  417.                 }
  418.             }
  419.             PWM_LVL     = modes[mode_idx];
  420.             #ifdef ALT_MODES
  421.             ALT_PWM_LVL = alt_modes[mode_idx];
  422.             #endif
  423.             last_mode_idx = mode_idx;
  424.             if (mode_pwm[mode_idx] == 0) {
  425.                 _delay_ms(1); // Need this here, maybe instructions for PWM output not getting executed before shutdown?
  426.                 // Go to sleep
  427.                 sleep_until_switch_press();
  428.             }
  429.         }
  430.     }
  431.  
  432.     return 0; // Standard Return Code
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement