1
//v0 4/21/2014 by Everett
3
//simple flashlight controller. mode change on power cycle
7
#include <pic12f1822.h>
9
#define _XTAL_FREQ 8000000
11
__CONFIG(FOSC_INTOSC & WDTE_SWDTEN & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
12
__CONFIG(WRT_BOOT & PLLEN_OFF & STVREN_ON & BORV_LO & LVP_OFF);
15
persistent unsigned char mode; //must be declared persistent for ram retention trick to work
23
#define default_mode 0
25
#define on_time 15 //milliseconds
27
unsigned int strobe_timer;
28
unsigned char strobe_position;
30
persistent unsigned int key;
32
void delayms(int milliseconds);
34
unsigned char stun_rate_lookup(unsigned char input);
36
void interrupt isr(void)
42
if(TMR0IF){ //fires at 1kHz
44
if(strobe_timer){strobe_timer--;} //count down milliseconds
53
configure(); //set up hardware peripherals
55
delayms(15); //short delay to avoid power glitches incrementing mode
57
if(key==12345){ //RAM retention trick to detect quick power cycles
58
mode++; //go to next mode
59
if(mode>max_mode){mode=0;}
61
else{ //long power loss. default to first mode
67
switch(mode){ //initialize current mode
86
GIE=1; //turn on interrupts
90
if(mode==strobe){ //no other modes need active tasks while running
91
if(strobe_timer==0){ //timer ran out
92
on_off=~on_off; //flip it
95
strobe_timer=on_time; //set on time
100
strobe_timer=stun_rate_lookup(strobe_position); //set off time
111
unsigned char stun_rate_lookup(unsigned char input)
113
static const char table[72]=28,30,33,31,12,24,28,33,29,23,19,33,33,23,14,26,9,18,15,29,15,10,19,31,9,27,33,10,15,26,16,28,30,24,14,23,10,24,30,10,23,31,25,33,9,15,19,17,19,18,32,23,33,17,31,13,31,18,20,8,24,33,17,21,20,14,9,20,26,16,22,16;
114
if(input>71){input-=71;}
115
if(input>71){input-=71;}
116
if(input>71){input-=71;}
122
void delayms(int milliseconds)
124
while(milliseconds!=0){ __delay_ms(1); milliseconds--;}
141
ANSELA=0b00000000; //
145
OSCCON=0b01110011; //8MHz
148
OPTION_REG=0b00000010; //8 prescale for 1ms interrupts
150
// FVRCON=0b11000001; //1.024v to adc
151
// ADCON0=0b00001101; //
152
// ADCON1=0b00010000; //1/8, left justify, vref from vdd
159
__EEPROM_DATA(0,0,0,0,0,0,0,0);
161
__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);
162
__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);
164
__EEPROM_DATA('1','8','2','2',' ','f','l','a');
165
__EEPROM_DATA('s','h','l','i','g','h','t',' ');
166
__EEPROM_DATA('b','y',' ','E','v','e','r','e');
167
__EEPROM_DATA('t','t',' ','e','v','e','r','e');
168
__EEPROM_DATA('t','t','.','b','r','a','d','f');
169
__EEPROM_DATA('o','r','d','@','g','m','a','i');
170
__EEPROM_DATA('l','.','c','o','m',' ',' ',' ');