Advertisement
pyro1son

X6R Firmware with independent RED channel V1.01

Oct 11th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.98 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.  * 1.01 Added independent secondary channel for RED led. 1.01
  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,3,17,50,125,255       // Must be low to high, and must start with 0
  71. #define ALT_MODES       0,STROBE,255,0,0,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,PHASE,PHASE,FAST,FAST,FAST,PHASE        // 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. #define BEACON    253       // Convenience code for beacon mode
  79.                            
  80. #define BATT_LOW        145 // When do we start ramping
  81. #define BATT_CRIT       135 // When do we shut the light off
  82. #define CHARGER_ON      190 // When we should lower the output
  83. #define BATT_ADC_DELAY  188 // Delay in ticks between low-bat rampdowns (188 ~= 3s)
  84. #define CHARGER_ADC_DELAY   5   // Delay in ticks before checking for charging cable (188 ~= 3s)
  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       70 // How many WDT ticks until we enter the hold-to-strobe mode
  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 = 3;
  133. volatile uint8_t press_duration = 0;
  134. #ifdef VOLTAGE_MON
  135. volatile uint8_t adc_channel = 1;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  136. #else
  137. volatile uint8_t adc_channel = 2;   // MUX 01 corresponds with PB2, 02 for PB4. Will switch back and forth
  138. #endif
  139.  
  140. // Debounce switch press value
  141. #ifdef DEBOUNCE_BOTH
  142. int is_pressed()
  143. {
  144.     static uint8_t pressed = 0;
  145.     // Keep track of last switch values polled
  146.     static uint8_t buffer = 0x00;
  147.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  148.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  149.    
  150.     if (pressed) {
  151.         // Need to look for a release indicator by seeing if the last switch status has been 0 for n number of polls
  152.         pressed = (buffer & DB_REL_DUR);
  153.     } else {
  154.         // Need to look for pressed indicator by seeing if the last switch status was 1 for n number of polls
  155.         pressed = ((buffer & DB_PRES_DUR) == DB_PRES_DUR);
  156.     }
  157.  
  158.     return pressed;
  159. }
  160. #else
  161. int is_pressed()
  162. {
  163.     // Keep track of last switch values polled
  164.     static uint8_t buffer = 0x00;
  165.     // Shift over and tack on the latest value, 0 being low for pressed, 1 for pulled-up for released
  166.     buffer = (buffer << 1) | ((PINB & (1 << SWITCH_PIN)) == 0);
  167.    
  168.     return (buffer & DB_REL_DUR);
  169. }
  170. #endif
  171.  
  172. void next_mode() {
  173.     if (++mode_idx >= sizeof(modes)) {
  174.         // Wrap around
  175.         mode_idx = 3;
  176.     }  
  177. }
  178.  
  179. void prev_mode() {
  180.     if (mode_idx == 0) {
  181.         // Wrap around
  182.         //mode_idx = sizeof(modes) - 1;
  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.         // 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.         // Not pressed
  286.         if (press_duration > 0 && press_duration < LONG_PRESS_DUR) {
  287.             // Short press
  288.                 next_mode();
  289.             highest_mode_idx = mode_idx;
  290.         } else {
  291.             // Only do turbo check when switch isn't pressed
  292.         #ifdef TURBO
  293.             if (modes[mode_idx] == 255) {
  294.                 turbo_ticks++;
  295.                 if (turbo_ticks > TURBO_TIMEOUT) {
  296.                     // Go to the previous mode
  297.                     prev_mode();
  298.                 }
  299.             }
  300.         #endif
  301.             // Only do voltage monitoring when the switch isn't pressed
  302.             // See if conversion is done. We moved this up here because we want to stay on
  303.             // the current ADC input until the conversion is done, and then switch to the new
  304.             // input, start the monitoring
  305.             if (batt_adc_ticks > 0) {
  306.                 --batt_adc_ticks;
  307.             }
  308.             if (charger_adc_ticks > 0) {
  309.                 --charger_adc_ticks;
  310.             }
  311.             if (ADCSRA & (1 << ADIF)) {
  312.                 if (adc_channel == 0x01) {
  313.                    
  314.                     if (batt_adc_ticks == 0) {
  315.                         // See if voltage is lower than what we were looking for
  316.                         if (ADCH < ((mode_idx == 1) ? BATT_CRIT : BATT_LOW)) {
  317.                             ++lowbatt_cnt;
  318.                         } else {
  319.                             lowbatt_cnt = 0;
  320.                         }
  321.                    
  322.                         // See if it's been low for a while
  323.                         if (lowbatt_cnt >= 4) {
  324.                             prev_mode();
  325.                             highest_mode_idx = mode_idx;
  326.                             lowbatt_cnt = 0;
  327.                             // If we reach 0 here, main loop will go into sleep mode
  328.                             // Restart the counter to when we step down again
  329.                             batt_adc_ticks = BATT_ADC_DELAY;
  330.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  331.                         }
  332.                     }
  333.                     // Switch ADC to charger monitoring
  334.                     adc_channel = 0x02;
  335.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  336.                 } else if (adc_channel == 0x02) {
  337.                    
  338.                     if (charger_adc_ticks == 0) {
  339.                         // See if charger is higher than the high threshold
  340.                         if (ADCH > ((mode_idx == 1) ? 255 : CHARGER_ON)) {
  341.                             mode_idx = 0;
  342.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  343.                         } else {
  344.                             mode_idx;
  345.                             charger_adc_ticks = CHARGER_ADC_DELAY;
  346.                         }
  347.                     }
  348.                     #ifdef VOLTAGE_MON
  349.                     // Switch ADC to battery monitoring
  350.                     adc_channel = 0x01;
  351.                     ADMUX = ((ADMUX & 0b11111100) | adc_channel);
  352.                     #endif;
  353.                 }
  354.             }
  355.             // Start conversion for next time through
  356.             ADCSRA |= (1 << ADSC);
  357.         }
  358.         press_duration = 0;
  359.     }
  360. }
  361.  
  362. int main(void)
  363. {  
  364.     // Set all ports to input, and turn pull-up resistors on for the inputs we are using
  365.     DDRB = 0x00;
  366.     PORTB = (1 << SWITCH_PIN);
  367.  
  368.     // Set the switch as an interrupt for when we turn pin change interrupts on
  369.     PCMSK = (1 << SWITCH_PIN);
  370.    
  371.     // Set PWM pin to output
  372.     #ifdef ALT_MODES
  373.     DDRB = (1 << PWM_PIN) | (1 << ALT_PWM_PIN);
  374.     #else
  375.     DDRB = (1 << PWM_PIN);
  376.     #endif
  377.  
  378.     // Set timer to do PWM for correct output pin and set prescaler timing
  379.     //TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
  380.     TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
  381.    
  382.     // Turn features on or off as needed
  383.     ADC_on();
  384.     ACSR   |=  (1<<7); //AC off
  385.    
  386.     #ifdef STROBE
  387.             else if (output == BEACON) {
  388.                 // 10Hz tactical strobe
  389.                 set_output(0,255);
  390.                 _delay_ms(500);
  391.                 set_output(0,0);
  392.                 _delay_ms(5000);
  393.             }
  394.     #endif // ifdef STROBE
  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