~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to tterev3/pic10f322-examples/clicky-switch-with-strobe.c

  • Committer: Selene Scriven
  • Date: 2015-09-14 19:23:29 UTC
  • mto: (153.1.18 tiny25)
  • mto: This revision was merged to the branch mainline in revision 156.
  • Revision ID: ubuntu@toykeeper.net-20150914192329-0ean5s8qpnnkdbub
updated to BLF-VLD 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//v0 4/21/2014 by Everett
 
2
        //initial version
 
3
        //simple flashlight controller. mode change on power cycle
 
4
        
 
5
 
 
6
#include <htc.h>
 
7
#include <pic12f1822.h>
 
8
 
 
9
#define _XTAL_FREQ 8000000
 
10
 
 
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);
 
13
 
 
14
 
 
15
persistent unsigned char mode;  //must be declared persistent for ram retention trick to work
 
16
enum mode{
 
17
        max=0,
 
18
        med=1,
 
19
        low=2,
 
20
        strobe=3,
 
21
};
 
22
#define max_mode 3
 
23
#define default_mode 0
 
24
 
 
25
#define on_time 15      //milliseconds
 
26
 
 
27
unsigned int strobe_timer;
 
28
unsigned char strobe_position;
 
29
bit on_off;
 
30
persistent unsigned int key;
 
31
 
 
32
void delayms(int milliseconds);
 
33
void configure(void);
 
34
unsigned char stun_rate_lookup(unsigned char input);
 
35
 
 
36
void interrupt isr(void)
 
37
{
 
38
        if(TMR1IF){
 
39
                TMR1IF=0;
 
40
                }
 
41
 
 
42
        if(TMR0IF){     //fires at 1kHz
 
43
                TMR0IF=0;
 
44
                if(strobe_timer){strobe_timer--;}       //count down milliseconds
 
45
        }
 
46
}
 
47
 
 
48
 
 
49
 
 
50
 
 
51
void main(void)
 
52
{
 
53
        configure();    //set up hardware peripherals
 
54
        
 
55
        delayms(15);    //short delay to avoid power glitches incrementing mode
 
56
        
 
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;}
 
60
        }       
 
61
        else{                   //long power loss. default to first mode
 
62
                mode=default_mode;
 
63
                key=12345;
 
64
        }               
 
65
 
 
66
 
 
67
        switch(mode){   //initialize current mode
 
68
                default:
 
69
                case max:
 
70
                        CCPR1L=255;
 
71
                        break;
 
72
                case med:
 
73
                        CCPR1L=25;
 
74
                        break;
 
75
                case low:
 
76
                        CCPR1L=1;
 
77
                        break;
 
78
                case strobe:
 
79
                        strobe_timer=0;
 
80
                        on_off=0;
 
81
                        CCPR1L=0;
 
82
                        break;
 
83
        }               
 
84
 
 
85
        
 
86
        GIE=1;  //turn on interrupts
 
87
 
 
88
        while(1){
 
89
                
 
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
 
93
                                if(on_off){
 
94
                                        CCPR1L=255;
 
95
                                        strobe_timer=on_time;   //set on time
 
96
                                }
 
97
                                else{
 
98
                                        CCPR1L=0;
 
99
                                        strobe_position++;
 
100
                                        strobe_timer=stun_rate_lookup(strobe_position); //set off time
 
101
                                }       //set output
 
102
                        }       
 
103
                }
 
104
                
 
105
                
 
106
                
 
107
        }       
 
108
}
 
109
 
 
110
 
 
111
unsigned char stun_rate_lookup(unsigned char input)
 
112
{
 
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;}
 
117
        return table[input];    
 
118
}       
 
119
 
 
120
 
 
121
 
 
122
void delayms(int milliseconds)
 
123
{
 
124
        while(milliseconds!=0){ __delay_ms(1); milliseconds--;}
 
125
}
 
126
 
 
127
 
 
128
 
 
129
void configure(void)
 
130
{
 
131
        INTCON=0b01100000;
 
132
        PIR1=0;
 
133
        PIR2=0;
 
134
        T1CON=0b00110001;
 
135
        T2CON=0b00000101; 
 
136
        PR2=255; 
 
137
        TMR2=0;
 
138
                
 
139
        LATA=0;
 
140
        TRISA=0b11111011;
 
141
        ANSELA=0b00000000;      //
 
142
        WPUA=0b11111011;
 
143
        APFCON=0;
 
144
 
 
145
        OSCCON=0b01110011;      //8MHz
 
146
        PIE1=0b00000000;
 
147
        PIE2=0;
 
148
        OPTION_REG=0b00000010;  //8 prescale for 1ms interrupts
 
149
 
 
150
//      FVRCON=0b11000001;      //1.024v to adc
 
151
//      ADCON0=0b00001101;      //
 
152
//      ADCON1=0b00010000;      //1/8, left justify, vref from vdd
 
153
 
 
154
        CCPR1L=0;
 
155
        CCP1CON=0b00001100;
 
156
}
 
157
 
 
158
 
 
159
__EEPROM_DATA(0,0,0,0,0,0,0,0); 
 
160
 
 
161
__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);
 
162
__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);
 
163
 
 
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',' ',' ',' ');