Untitled diff

建立於 差異永不過期
70 刪除
444 行
124 新增
478 行
/* STAR_momentary version 1.6
/* MTN_momentary_temp version 1.0
*
*
* Changelog
* Changelog
*
*
* 1.0 Initial version
* 1.0 First stab at this based on STAR_momentary v1.6
* 1.1 Added ability for star 2 to change mode order
* 1.2 Fix for newer version of AVR Studio
* 1.3 Added support for dual PWM outputs and selection of PWM mode per output level
* 1.4 Added ability to switch to a momentary mode
* 1.5 Added ability to set mode to 0 but not have it turn off
* 1.6 Added ability to debounce the press
*
*
*/
*/


/*
/*
* NANJG 105C Diagram
* NANJG 105C Diagram
* ---
* ---
* -| |- VCC
* -| |- VCC
* Star 4 -| |- Voltage ADC
* Star 4 -| |- Voltage ADC
* Star 3 -| |- PWM
* Star 3 -| |- PWM
* GND -| |- Star 2
* GND -| |- Star 2
* ---
* ---
*
*
* FUSES
* FUSES
* I use these fuse settings
* I use these fuse settings
* Low: 0x75 (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
* Low: 0x75 (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
* High: 0xff
* High: 0xff
*
*
* For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
* For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
*
*
* STARS
* STARS
* Star 2 = Not used
* Star 2 = Alt PWM output
* Star 3 = H-L if connected, L-H if not
* Star 3 = Temp sensor input
* Star 4 = Switch input
* Star 4 = Switch input
*
*
* VOLTAGE
* VOLTAGE
* Resistor values for voltage divider (reference BLF-VLD README for more info)
* Resistor values for voltage divider (reference BLF-VLD README for more info)
* Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
* Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
*
*
* VCC
* VCC
* |
* |
* Vd (~.25 v drop from protection diode)
* Vd (~.25 v drop from protection diode)
* |
* |
* 1912 (R1 19,100 ohms)
* 1912 (R1 19,100 ohms)
* |
* |
* |---- PB2 from MCU
* |---- PB2 from MCU
* |
* |
* 4701 (R2 4,700 ohms)
* 4701 (R2 4,700 ohms)
* |
* |
* GND
* GND
*
*
* ADC = ((V_bat - V_diode) * R2 * 255) / ((R1 + R2 ) * V_ref)
* ADC = ((V_bat - V_diode) * R2 * 255) / ((R1 + R2 ) * V_ref)
* 125 = ((3.0 - .25 ) * 4700 * 255) / ((19100 + 4700) * 1.1 )
* 125 = ((3.0 - .25 ) * 4700 * 255) / ((19100 + 4700) * 1.1 )
* 121 = ((2.9 - .25 ) * 4700 * 255) / ((19100 + 4700) * 1.1 )
* 121 = ((2.9 - .25 ) * 4700 * 255) / ((19100 + 4700) * 1.1 )
*
*
* Well 125 and 121 were too close, so it shut off right after lowering to low mode, so I went with
* Well 125 and 121 were too close, so it shut off right after lowering to low mode, so I went with
* 130 and 120
* 130 and 120
*
*
* To find out what value to use, plug in the target voltage (V) to this equation
* To find out what value to use, plug in the target voltage (V) to this equation
* value = (V * 4700 * 255) / (23800 * 1.1)
* value = (V * 4700 * 255) / (23800 * 1.1)
*
*
*/
*/
#define F_CPU 4800000UL
#define F_CPU 4800000UL


// PWM Mode
// PWM Mode
#define PHASE 0b00000001
#define PHASE 0b00000001
#define FAST 0b00000011
#define FAST 0b00000011


/*
/*
* =========================================================================
* =========================================================================
* Settings to modify per driver
* Settings to modify per driver
*/
*/


#define VOLTAGE_MON // Comment out to disable - ramp down and eventual shutoff when battery is low
//#define VOLTAGE_MON // Comment out to disable - ramp down and eventual shutoff when battery is low
#define MODES 0,3,14,39,125,255 // Must be low to high, and must start with 0
#define TEMP_MON // Comment out to disable - ramp down/up to stabilize temp (read as voltage)
#define ALT_MODES 0,3,14,39,125,255 // Must be low to high, and must start with 0, the defines the level for the secondary output. Comment out if no secondary output
#define MODES 0,2,32,125,255 // Must be low to high, and must start with 0
#define MODE_PWM 0,PHASE,FAST,FAST,FAST,PHASE // Define one per mode above. 0 tells the light to go to sleep
#define ALT_MODES 0,2,32,125,255 // Must be low to high, and must start with 0, the defines the level for the secondary output. Comment out if no secondary output
#define TURBO // Comment out to disable - full output with a step down after n number of seconds
#define MODE_PWM 0,PHASE,FAST,FAST,FAST // Define one per mode above. 0 tells the light to go to sleep
//#define TURBO // Comment out to disable - full output with a step down after n number of seconds
// If turbo is enabled, it will be where 255 is listed in the modes above
// If turbo is enabled, it will be where 255 is listed in the modes above
#define TURBO_TIMEOUT 5625 // How many WTD ticks before before dropping down (.016 sec each)
//#define TURBO_TIMEOUT 5625 // How many WTD ticks before before dropping down (.016 sec each)
// 90 = 5625
// 90 = 5625
// 120 = 7500
// 120 = 7500
#define ADC_LOW 130 // When do we start ramping
#define BATT_LOW 145 // When do we start ramping
#define ADC_CRIT 120 // When do we shut the light off
#define BATT_CRIT 135 // When do we shut the light off
#define ADC_DELAY 188 // Delay in ticks between low-bat rampdowns (188 ~= 3s)
#define TEMP_HIGH 120 // When we should lower the output
#define TEMP_LOW 100 // When we should raise the output
#define BATT_ADC_DELAY 188 // Delay in ticks between low-bat rampdowns (188 ~= 3s)
#define TEMP_ADC_DELAY 30 // Delay in ticks before checking temp (188 ~= 3s)
#define TEMP_ADC_DELAY_AFTER_RAMP 30 // Delay in ticks before checking temp again after a ramp


//#define MOM_ENTER_DUR 128 // .16ms each. Comment out to disable this feature
#define MOM_ENTER_DUR 128 // 16ms each. Comment out to disable this feature
#define MOM_EXIT_DUR 128 // .16ms each
#define MOM_EXIT_DUR 128 // 16ms each


#define MOM_MODE_IDX 4 // The index of the mode to use in MODES above if momementary feature enabled, starting at index of 0
#define MOM_MODE_IDX 4 // The index of the mode to use in MODES above, starting at index of 0


/*
/*
* =========================================================================
* =========================================================================
*/
*/


#include <avr/pgmspace.h>
#include <avr/pgmspace.h>
#include <avr/io.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
#include <avr/sleep.h>
//#include <avr/power.h>
//#include <avr/power.h>


#define STAR3_PIN PB4 // If not connected, will cycle L-H. Connected, H-L
#define SWITCH_PIN PB3 // what pin the switch is connected to, which is Star 4
#define SWITCH_PIN PB3 // what pin the switch is connected to, which is Star 4
#define PWM_PIN PB1
#define PWM_PIN PB1
#define ALT_PWM_PIN PB0
#define ALT_PWM_PIN PB0
#define VOLTAGE_PIN PB2
#define ADC_CHANNEL 0x01 // MUX 01 corresponds with PB2
#define ADC_DIDR ADC1D // Digital input disable bit corresponding with PB2
#define ADC_DIDR ADC1D // Digital input disable bit corresponding with PB2
#define ADC_PRSCL 0x06 // clk/64
#define ADC_PRSCL 0x06 // clk/64


#define PWM_LVL OCR0B // OCR0B is the output compare register for PB1
#define PWM_LVL OCR0B // OCR0B is the output compare register for PB1