Skip to content

Commit 8fe9435

Browse files
committed
v.0.2 working low power mode
1 parent 744df8f commit 8fe9435

9 files changed

Lines changed: 318 additions & 287 deletions

File tree

Listings/main.lst

Lines changed: 154 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 13:33:18 PAGE 1
1+
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 23:46:49 PAGE 1
22

33

44
C51 COMPILER V9.59.0.0, COMPILATION OF MODULE MAIN
@@ -16,162 +16,172 @@ line level source
1616
6 #define FALSE 0
1717
7 #define TRUE 1
1818
8
19-
9 // I/O Ports
20-
10 #define I_BUTTON P34
21-
11 #define I_AVR_ONLINE P33 // PD2
22-
12 #define O_BOOST P35
23-
13 #define O_AVR_BUTTON P32 // PD1
24-
14
25-
15 // States
26-
16 #define SLEEP 0
27-
17 #define BOOT 1
28-
18 #define ONLINE 2
29-
19
30-
20 #define CLR_BIT(p,n) ((p) &= ~(1 << (n)))
31-
21 #define SET_BIT(p,n) ((p) |= (1 << (n)))
19+
9 #define CLR_BIT(p,n) ((p) &= ~(1 << (n)))
20+
10 #define SET_BIT(p,n) ((p) |= (1 << (n)))
21+
11
22+
12 // I/O Ports
23+
13 #define I_BUTTON P34
24+
14 #define I_AVR_ONLINE P33 // PD2
25+
15 #define O_BOOST P35
26+
16 #define O_AVR_BUTTON P32 // PD1
27+
17
28+
18 // States
29+
19 #define SLEEP 0
30+
20 #define BOOT 1
31+
21 #define ONLINE 2
3232
22
3333
23 volatile int state;
3434
24 volatile int button_changed;
3535
25 volatile int button_state;
3636
26 volatile int avr_online_state;
3737
27
38-
28 void timer0_ISR (void) interrupt 1
39-
29 {
40-
30 1 static float avg_button = 10;
41-
31 1 static float avg_avr_online = 10;
42-
32 1
43-
33 1 int new_button_state;
44-
34 1 int new_avr_online_state;
45-
35 1
46-
36 1 // Increment Counter to shorten overflow time
47-
37 1 TH0 = (65536 - 1000) / 256; // TODO: Calculation?! Old: 1ms = 1000MZ, davon das High-Byte in TH0
48-
38 1 TL0 = (65536 - 1000) % 256; // das Low-Byte in TL0
49-
39 1
50-
40 1 // Button debouncing using exponential moving average
51-
41 1 // avg = alpha * avg + (1 - alpha) * input, alpha < 1
52-
42 1 // TODO: inefficient, replace
53-
43 1 avg_button = 0.9 * avg_button + 0.1 * (I_BUTTON * 10);
54-
44 1 avg_avr_online = 0.9 * avg_avr_online + 0.1 * (I_AVR_ONLINE * 10);
38+
28
39+
29 void extInt2_ISR (void) interrupt 10
40+
30 {
41+
31 1 _nop_();
42+
32 1 }
43+
33
44+
34 void timer0_ISR (void) interrupt 1
45+
35 {
46+
36 1 static float avg_button = 10;
47+
37 1 static float avg_avr_online = 10;
48+
38 1
49+
39 1 int new_button_state;
50+
40 1 int new_avr_online_state;
51+
41 1
52+
42 1 // Increment Counter to shorten overflow time
53+
43 1 TH0 = (65536 - 100) / 256; // TODO: Calculation?! Old: 1ms = 1000MZ, davon das High-Byte in TH0
54+
44 1 TL0 = (65536 - 100) % 256; // das Low-Byte in TL0
5555
45 1
56-
46 1 (avg_button > 5) ? (new_button_state = HIGH) : (new_button_state = LOW);
57-
47 1 (avg_avr_online > 5) ? (new_avr_online_state = HIGH) : (new_avr_online_state = LOW);
58-
48 1
59-
49 1 if (new_button_state != button_state) {
60-
50 2 button_state = new_button_state;
61-
51 2 button_changed = 1;
62-
52 2 } else {
63-
53 2 button_changed = 0;
64-
54 2 }
65-
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 13:33:18 PAGE 2
56+
46 1 // Button debouncing using exponential moving average
57+
47 1 // avg = alpha * avg + (1 - alpha) * input, alpha < 1
58+
48 1 // TODO: inefficient, replace
59+
49 1 avg_button = 0.9 * avg_button + 0.1 * (I_BUTTON * 10);
60+
50 1 avg_avr_online = 0.9 * avg_avr_online + 0.1 * (I_AVR_ONLINE * 10);
61+
51 1
62+
52 1 (avg_button > 5) ? (new_button_state = HIGH) : (new_button_state = LOW);
63+
53 1 (avg_avr_online > 5) ? (new_avr_online_state = HIGH) : (new_avr_online_state = LOW);
64+
54 1
65+
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 23:46:49 PAGE 2
6666

67-
55 1
68-
56 1 if (new_avr_online_state != avr_online_state) {
69-
57 2 avr_online_state = new_avr_online_state;
70-
58 2 }
71-
59 1 }
72-
60
73-
61
74-
62 void setup()
75-
63 {
76-
64 1 // Keep P33 (I_AVR_ONLINE) and P34 (I_BUTTON) as Quasi-Bidirectional
77-
65 1 // since we need the internal PullUps
78-
66 1
79-
67 1 // P35 (O_BOOST): PushPull Output
80-
68 1 SET_BIT(P3M0, 5); // P3M0 |= 1 << 5;
81-
69 1 CLR_BIT(P3M1, 5); // P3M1 &= ~(0 << 5);
82-
70 1
83-
71 1 // P32 (O_AVR_BUTTON): PushPull Output
84-
72 1 SET_BIT(P3M1, 2); // P3M0 |= 1 << 2;
85-
73 1 CLR_BIT(P3M1, 2); // P3M1 &= ~(0 << 2);
86-
74 1
87-
75 1 // Init States
88-
76 1 state = SLEEP;
89-
77 1
90-
78 1 button_changed = FALSE;
91-
79 1 button_state = HIGH;
92-
80 1 avr_online_state = LOW;
93-
81 1
94-
82 1 // Init I/O's
95-
83 1 O_AVR_BUTTON = HIGH;
96-
84 1 O_BOOST = LOW;
97-
85 1 I_BUTTON = HIGH; // set to weak high => pullups!
98-
86 1 I_AVR_ONLINE = HIGH; // set to weak high => pullups!
67+
55 1 if (new_button_state != button_state) {
68+
56 2 button_state = new_button_state;
69+
57 2 button_changed = 1;
70+
58 2 } else {
71+
59 2 button_changed = 0;
72+
60 2 }
73+
61 1
74+
62 1 if (new_avr_online_state != avr_online_state) {
75+
63 2 avr_online_state = new_avr_online_state;
76+
64 2 }
77+
65 1 }
78+
66
79+
67
80+
68 void setup()
81+
69 {
82+
70 1 // Keep P33 (I_AVR_ONLINE) and P34 (I_BUTTON) as Quasi-Bidirectional
83+
71 1 // since we need the internal PullUps
84+
72 1
85+
73 1 // P35 (O_BOOST): PushPull Output
86+
74 1 SET_BIT(P3M0, 5);
87+
75 1 CLR_BIT(P3M1, 5);
88+
76 1
89+
77 1 // P32 (O_AVR_BUTTON): PushPull Output
90+
78 1 SET_BIT(P3M1, 2);
91+
79 1 CLR_BIT(P3M1, 2);
92+
80 1
93+
81 1 // Init States
94+
82 1 state = SLEEP;
95+
83 1
96+
84 1 button_changed = FALSE;
97+
85 1 button_state = HIGH;
98+
86 1 avr_online_state = LOW;
9999
87 1
100-
88 1 // Timers
101-
89 1 TMOD = (TMOD & 0xF0) | 0x01; // Set T/C0 Mode 1, 16Bit, Manual Reload
102-
90 1 ET0 = 1; // Enable Timer 0 Interrupts
103-
91 1 TR0 = 1; // Start Timer 0 Running
104-
92 1 EA = 1; // Global Interrupt Enable
105-
93 1 }
106-
94
107-
95 void delay(int ms)
108-
96 {
109-
97 1 unsigned int j = 0;
110-
98 1 unsigned int g = 0;
111-
99 1 for(j=0;j<ms;j++)
112-
100 1 {
113-
101 2 for(g=0;g<600;g++)
114-
102 2 {
115-
103 3 _nop_();
116-
104 3 _nop_();
117-
105 3 _nop_();
118-
106 3 _nop_();
119-
107 3 _nop_();
120-
108 3 }
121-
109 2 }
122-
110 1 }
123-
111
124-
112 void main()
125-
113 {
126-
114 1 setup();
127-
115 1
128-
116 1 while(1)
129-
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 13:33:18 PAGE 3
100+
88 1 // Init I/O's
101+
89 1 O_AVR_BUTTON = HIGH;
102+
90 1 O_BOOST = LOW;
103+
91 1 I_BUTTON = HIGH; // set to weak high => pullups!
104+
92 1 I_AVR_ONLINE = HIGH; // set to weak high => pullups!
105+
93 1
106+
94 1 // Timers & Interrupts
107+
95 1 SET_BIT(INT_CLKO, 4); // Interrupt on falling edge of INT2/P34 (Button)
108+
96 1 TMOD = (TMOD & 0xF0) | 0x01; // Set T/C0 Mode 1, 16Bit, Manual Reload
109+
97 1 ET0 = 1; // Enable Timer 0 Interrupts
110+
98 1 TR0 = 1; // Start Timer 0 Running
111+
99 1 }
112+
100
113+
101 void delay(int ms)
114+
102 {
115+
103 1 unsigned int j = 0;
116+
104 1 unsigned int g = 0;
117+
105 1 for(j=0;j<ms;j++)
118+
106 1 {
119+
107 2 for(g=0;g<600;g++)
120+
108 2 {
121+
109 3 _nop_();
122+
110 3 _nop_();
123+
111 3 _nop_();
124+
112 3 _nop_();
125+
113 3 _nop_();
126+
114 3 }
127+
115 2 }
128+
116 1 }
129+
C51 COMPILER V9.59.0.0 MAIN 09/10/2018 23:46:49 PAGE 3
130130

131-
117 1 {
132-
118 2 switch (state) {
133-
119 3 case SLEEP:
134-
120 3 // TODO: POWER SAVING OPTIONS
135-
121 3 // deactivate timer, enable interrupt2, ...
136-
122 3
137-
123 3 if (button_changed && button_state == LOW) {
138-
124 4 O_AVR_BUTTON = LOW;
139-
125 4 delay(1); // otherwise the fw hangs up
140-
126 4 O_BOOST = HIGH;
141-
127 4 state = BOOT;
142-
128 4 }
143-
129 3 break;
144-
130 3
145-
131 3 case BOOT:
146-
132 3 O_AVR_BUTTON = button_state;
147-
133 3
148-
134 3 if (avr_online_state == HIGH)
149-
135 3 state = ONLINE;
150-
136 3
151-
137 3 // TODO: Timeout
152-
138 3 break;
153-
139 3
154-
140 3 case ONLINE:
155-
141 3 O_AVR_BUTTON = button_state;
156-
142 3
157-
143 3 if (avr_online_state == LOW) {
158-
144 4 O_BOOST = LOW;
159-
145 4 state = SLEEP;
160-
146 4 }
161-
147 3 break;
162-
148 3
163-
149 3 default:
164-
150 3 state = SLEEP;
165-
151 3 break;
166-
152 3 }
167-
153 2
168-
154 2 }
169-
155 1
170-
156 1 }
131+
117
132+
118 void main()
133+
119 {
134+
120 1 setup();
135+
121 1 EA = 1; // Global Interrupt Enable
136+
122 1
137+
123 1 while(1)
138+
124 1 {
139+
125 2 switch (state) {
140+
126 3 case SLEEP:
141+
127 3
142+
128 3 PCON = 0x02; // Stop/PowerDown Mode
143+
129 3
144+
130 3 // Normally we would check here for (button_changed && button_state == LOW)
145+
131 3 // but debouncing would take too much time, we would enter powerDown mode again
146+
132 3 // before the button is debounced.
147+
133 3
148+
134 3 O_AVR_BUTTON = LOW;
149+
135 3 delay(1); // otherwise the fw hangs up
150+
136 3 O_BOOST = HIGH;
151+
137 3 state = BOOT;
152+
138 3
153+
139 3 break;
154+
140 3
155+
141 3 case BOOT:
156+
142 3 O_AVR_BUTTON = button_state;
157+
143 3
158+
144 3 if (avr_online_state == HIGH)
159+
145 3 state = ONLINE;
160+
146 3
161+
147 3 // TODO: Timeout
162+
148 3 break;
163+
149 3
164+
150 3 case ONLINE:
165+
151 3 O_AVR_BUTTON = button_state;
166+
152 3
167+
153 3 if (avr_online_state == LOW) {
168+
154 4 O_BOOST = LOW;
169+
155 4 state = SLEEP;
170+
156 4 }
171+
157 3 break;
172+
158 3
173+
159 3 default:
174+
160 3 state = SLEEP;
175+
161 3 break;
176+
162 3 }
177+
163 2
178+
164 2 }
179+
165 1
180+
166 1 }
171181

172182

173183
MODULE INFORMATION: STATIC OVERLAYABLE
174-
CODE SIZE = 529 ----
184+
CODE SIZE = 526 ----
175185
CONSTANT SIZE = ---- ----
176186
XDATA SIZE = ---- ----
177187
PDATA SIZE = ---- ----

0 commit comments

Comments
 (0)