~toykeeper/flashlight-firmware/anduril2

« back to all changes in this revision

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

  • Committer: Selene Scriven
  • Date: 2014-07-23 02:26:31 UTC
  • Revision ID: ubuntu@toykeeper.net-20140723022631-mjl3ukeb9za20lob
Added PIC10F322 C code examples from tterev3.
(one for clicky/hard switches, one for momentary/electronic switches)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//v0 5/1/2014 by Everett
 
2
        //initial version
 
3
        //simple flashlight controller. mode change on power cycle
 
4
        
 
5
 
 
6
#include <htc.h>
 
7
#include <pic10f322.h>
 
8
#define _XTAL_FREQ 8000000
 
9
__CONFIG(CP_OFF & BOREN_OFF & LVP_OFF  & MCLRE_ON & WDTE_OFF & PWRTE_OFF & FOSC_INTOSC & WRT_HALF);
 
10
 
 
11
#define pwm PWM1DCH
 
12
 
 
13
persistent unsigned char mode;  //must be declared persistent for ram retention trick to work
 
14
enum mode{
 
15
        max=0,
 
16
        med=1,
 
17
        low=2,
 
18
};
 
19
#define max_mode 2
 
20
#define default_mode 0
 
21
 
 
22
persistent unsigned int key;
 
23
unsigned int v_timer; 
 
24
bit v_sample;
 
25
#define voltage_rate 100 //milliseconds
 
26
 
 
27
void delayms(int milliseconds);
 
28
void configure(void);
 
29
unsigned char read_voltage(void);
 
30
void initialize_mode(void);
 
31
 
 
32
void interrupt isr(void)
 
33
{
 
34
 
 
35
        if(TMR0IF){     //fires at 1kHz
 
36
                TMR0IF=0;
 
37
                if(++v_timer==voltage_rate){v_timer=0; v_sample=1;}
 
38
        }
 
39
}
 
40
 
 
41
 
 
42
void main(void)
 
43
{
 
44
        configure();    //set up hardware peripherals
 
45
        delayms(15);    //short delay to avoid power glitches incrementing mode
 
46
        if(key==12345){         //RAM retention trick to detect quick power cycles
 
47
                mode++;                 //go to next mode
 
48
                if(mode>max_mode){mode=0;}
 
49
        }       
 
50
        else{                   //long power loss. default to first mode
 
51
                mode=default_mode;
 
52
                key=12345;
 
53
        }               
 
54
 
 
55
        initialize_mode();      
 
56
        GIE=1;  //turn on interrupts
 
57
 
 
58
        while(1){
 
59
                
 
60
                if(v_sample){
 
61
                        v_sample=0;
 
62
                        if(mode==max){                          //if battery goes below 3.0V in max mode, force down to medium mode
 
63
                                if(read_voltage()>87){
 
64
                                        mode=med;
 
65
                                        initialize_mode();
 
66
                                }
 
67
                        }
 
68
                }                       
 
69
                
 
70
        }       
 
71
}
 
72
 
 
73
void initialize_mode(void)
 
74
{
 
75
                switch(mode){   //initialize current mode
 
76
                default:
 
77
                case max:
 
78
                        pwm=255;
 
79
                        break;
 
80
                case med:
 
81
                        pwm=25;
 
82
                        break;
 
83
                case low:
 
84
                        pwm=1;
 
85
                        break;
 
86
        }       
 
87
}       
 
88
 
 
89
unsigned char read_voltage(void)        
 
90
{
 
91
//fvr is at 1.024V. ADRES = 1.024/Vin*255. Vin = 1.024/(ADRES/255). voltage limit set to 3.0V -> 87. values below this correspond to voltage above 3.0V
 
92
        GO_nDONE=1;
 
93
        while(GO_nDONE);
 
94
        return ADRES;
 
95
}
 
96
 
 
97
        
 
98
void delayms(int milliseconds)
 
99
{
 
100
        while(milliseconds!=0){ __delay_ms(1); milliseconds--;}
 
101
}
 
102
 
 
103
void configure(void)
 
104
{
 
105
        INTCON=0b00100000;      //tmr0 only
 
106
 
 
107
        T2CON=0b00000100; //on, no prescale
 
108
        PR2=255; 
 
109
        TMR2=0;
 
110
                
 
111
        LATA=0;
 
112
        TRISA=0b11111110;       //GP0 output
 
113
        ANSELA=0b11110000;      //
 
114
        WPUA=0b11111110;
 
115
 
 
116
        OSCCON=0b01100000;      //8MHz
 
117
 
 
118
        OPTION_REG=0b00000010;  //8 prescale for 1ms interrupts
 
119
 
 
120
        FVRCON=0b10000001;      //1.024v to adc
 
121
        ADCON=0b10111101;       // fvr
 
122
 
 
123
        PWM1DCH=0;
 
124
        PWM1CON=0b11000000;     //on, output
 
125
}
 
126
 
 
127