11#include <STC15F2K60S2.h>
22#include <intrins.h>
33
4- #define O_BOOST P35
4+ #define LOW 0
5+ #define HIGH 1
6+
7+ // I/O Ports
8+ #define I_BUTTON P34
59#define I_AVR_ONLINE P33 // PD2
10+ #define O_BOOST P35
611#define O_AVR_BUTTON P32 // PD1
7- #define I_BUTTON P34
812
9- // states
13+ // States
1014#define SLEEP 0
1115#define BOOT 1
1216#define ONLINE 2
1317
14- #define LOW 0
15- #define HIGH 1
1618
1719int state ;
1820int button_changed ;
1921int button_state ;
20-
2122int avr_online_state ;
2223
2324
24-
25-
2625void timer0_ISR (void ) interrupt 1
2726{
2827 static float avg_button = 10 ;
2928 static float avg_avr_online = 10 ;
30-
29+
3130 int new_button_state ;
3231 int new_avr_online_state ;
33-
32+
3433 // Increment Counter to shorten overflow time
3534 //TH0 = (65536 - 1000)/256; // TODO: Calculation?! Old: 1ms = 1000MZ, davon das High-Byte in TH0
36- //TL0 = (65536 - 1000)%256; // das Low-Byte in TL0
37-
38- // Button entprellen
39-
35+ //TL0 = (65536 - 1000)%256; // das Low-Byte in TL0
4036
37+ // Button entprellen
4138
4239 avg_button = (avg_button * 9 + I_BUTTON * 10 ) / 10 ;
4340 avg_avr_online = (avg_avr_online * 9 + I_AVR_ONLINE * 10 ) / 10 ;
44-
41+
4542 if (avg_button > 5 ) {
46- new_button_state = HIGH ;
43+ new_button_state = HIGH ;
4744 } else {
48- new_button_state = LOW ;
45+ new_button_state = LOW ;
4946 }
50-
47+
5148 if (avg_avr_online > 5 ) {
52- new_avr_online_state = HIGH ;
49+ new_avr_online_state = HIGH ;
5350 } else {
54- new_avr_online_state = LOW ;
51+ new_avr_online_state = LOW ;
5552 }
56-
53+
5754 if (new_button_state != button_state ) {
5855 button_state = new_button_state ;
5956 button_changed = 1 ;
6057 }
61-
58+
6259 if (new_avr_online_state != avr_online_state ) {
6360 avr_online_state = new_avr_online_state ;
6461 }
@@ -68,31 +65,32 @@ void timer0_ISR (void) interrupt 1
6865
6966void setup ()
7067{
71- // P33 (I_AVR_ONLINE) to Input only
68+ // P33 (I_AVR_ONLINE) to Input only (HiZ)
7269 P3M0 &= ~(0 << 3 );
7370 P3M1 |= 1 << 3 ;
74-
75- // P34 (I_BUTTON) to Input only
71+
72+ // P34 (I_BUTTON) to Input only (HiZ)
7673 P3M0 &= ~(0 << 4 );
7774 P3M1 |= 1 << 4 ;
78-
75+
7976 // P35 (O_BOOST) to PushPull Output
8077 P3M0 |= 1 << 5 ;
8178 P3M1 &= ~(0 << 5 );
82-
79+
8380 // P32 (O_AVR_BUTTON) to PushPull Output
8481 P3M0 |= 1 << 2 ;
8582 P3M1 &= ~(0 << 2 );
8683
87- // initial states
84+ // Init States
8885 state = SLEEP ;
8986 button_changed = 0 ;
9087 button_state = HIGH ;
9188 avr_online_state = LOW ;
9289
90+ // Init Outputs
9391 O_AVR_BUTTON = HIGH ;
9492 O_BOOST = LOW ;
95-
93+
9694 // Timers
9795 TMOD = (TMOD & 0xF0 ) | 0x01 ; // Set T/C0 Mode 1, 16Bit, No Auto Reload
9896 ET0 = 1 ; // Enable Timer 0 Interrupts
@@ -121,57 +119,43 @@ void delay(int ms)
121119void main ()
122120{
123121 setup ();
124-
125- /*
126- delay(5000);
127- O_AVR_BUTTON = LOW;
128- O_BOOST = HIGH;
129- delay(500);
130- O_AVR_BUTTON = HIGH;
131-
132- while(1){}
133- */
134-
135122
136- while (1 )
137- {
138-
139- O_AVR_BUTTON = button_state ;
140-
141-
123+ while (1 )
124+ {
142125 switch (state ) {
143126 case SLEEP :
144-
145- // POWER SAVING OPTIONS,
146- // deactivate timer, enable interrupt2, ...
147-
127+ // TODO: POWER SAVING OPTIONS
128+ // deactivate timer, enable interrupt2, ...
129+
148130 if (button_changed && button_state == LOW ) {
149131 button_changed = 0 ;
132+ O_AVR_BUTTON = LOW ;
150133 O_BOOST = HIGH ;
151134 state = BOOT ;
152135 }
153136 break ;
154-
137+
155138 case BOOT :
156139 if (avr_online_state == HIGH )
157140 state = ONLINE ;
158-
141+
159142 // TODO: Timeout
160143
161144 break ;
162-
145+
163146 case ONLINE :
147+ O_AVR_BUTTON = button_state ;
148+
164149 if (avr_online_state == LOW ) {
165150 O_BOOST = LOW ;
166151 state = SLEEP ;
167152 }
168153 break ;
169-
154+
170155 default :
171156 break ;
172157 }
173-
174- }
158+ }
175159
176160}
177161
0 commit comments