📄 main.lst
字号:
C51 COMPILER V6.10 MAIN 04/19/2001 12:10:44 PAGE 1
C51 COMPILER V6.10, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN .\MAIN.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE .\MAIN.C OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 Main.c (v1.00)
4
5 ------------------------------------------------------------------
6
7 Automatic light example.
8
9
10 COPYRIGHT
11 ---------
12
13 This code is from the book:
14
15 PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
16 [Pearson Education, 2001; ISBN: 0-201-33138-1].
17
18 This code is copyright (c) 2001 by Michael J. Pont.
19
20 See book for copyright details and other information.
21
22 -*------------------------------------------------------------------*/
23
24 #include "Main.h"
25
26 // ------ Port pins ------------------------------------------------
27
28 // Omit Port.H in this simple (one-file) example
29 // Don抰 use pins 1^0, 1^1 - NO INTERNAL PULL-UP RESISTORS
30 sbit Switch_pin_G = P1^2;
31 sbit Light_pin_G = P1^3;
32
33 // ------ Private function prototypes ------------------------------
34
35 // Function prototypes
36 // NOTE: ISR is not explictly called and does not require a prototype
37 void Timer_1_Init(void);
38 void Timer_1_Manual_Reload(void);
39 void Light_Init(void);
40
41 // ------ Private constants ----------------------------------------
42
43 #define SWITCH_PRESSED 0
44
45 #define LIGHT_ON 0
46 #define LIGHT_OFF 1
47
48 // ------ Private variable definitions------------------------------
49
50 static tByte Switch_count_G = 0;
51 static tByte Auto_switch_off_count_G = 0;
52 static tByte Switch_blocked_G = 0;
53
54 static bit LED_state_G = 0;
55 static tByte Call_count_G = 0;
C51 COMPILER V6.10 MAIN 04/19/2001 12:10:44 PAGE 2
56
57 /* --------------------------------------------------------------- */
58
59 void main(void)
60 {
61 1 Timer_1_Init(); // Set up Timer 2
62 1 Light_Init(); // Prepare to flash the LED
63 1
64 1 EA = 1; // Globally enable interrupts
65 1
66 1 while(1)
67 1 {
68 2 PCON |= 0x01; // Go to sleep (idle mode)
69 2 }
70 1 }
71
72 /* --------------------------------------------------------------- */
73
74 void Timer_1_Init(void)
75 {
76 1 // Timer 1 is configured as a 16-bit timer,
77 1 // which is manually reloaded when it overflows
78 1 TMOD &= 0x0F; // Clear all T1 bits (T0 left unchanged)
79 1 TMOD |= 0x10; // Set required T1 bits (T0 left unchanged)
80 1
81 1 // Sets up timer reload values
82 1 Timer_1_Manual_Reload();
83 1
84 1 // Interrupt Timer 1 enabled
85 1 ET1 = 1;
86 1 }
87
88 /*------------------------------------------------------------------*-
89
90 Timer_1_Manual_Reload()
91
92 This 'One-Year Scheduler' uses a (manually reloaded) 16-bit timer.
93 The manual reload means that all timings are approximate.
94 THIS SCHEDULER IS NOT SUITABLE FOR APPLICATIONS WHERE
95 ACCURATE TIMING IS REQUIRED!!!
96 Timer reload is carried out in this function.
97
98 -*------------------------------------------------------------------*/
99 void Timer_1_Manual_Reload(void)
100 {
101 1 // Stop Timer 1
102 1 TR1 = 0;
103 1
104 1 // This code (generic 8051/52) assumes a 4 MHz system osc.
105 1 // The Timer 1 resolution is then 0.000003 seconds
106 1 // (see Chapter 11 for details)
107 1 //
108 1 // We want to generate an interrupt every 200 ms (approx):
109 1 // this takes 0.2 / 0.000003 timer increments
110 1 // i.e. 66666 timer increments
111 1 //
112 1 // Reload value of 0x00 gives 65536 increments, which is
113 1 // sufficiently close for our purposes here (around 2% out)
114 1 TL1 = 0x00;
115 1 TH1 = 0x00;
116 1
117 1 // Start Timer 1
C51 COMPILER V6.10 MAIN 04/19/2001 12:10:44 PAGE 3
118 1 TR1 = 1;
119 1 }
120
121 /*------------------------------------------------------------------*-
122
123 Light_Init()
124
125 -*------------------------------------------------------------------*/
126 void Light_Init(void)
127 {
128 1 Switch_count_G = 0;
129 1 Auto_switch_off_count_G = 0;
130 1 Switch_blocked_G = 0;
131 1
132 1 // Write 1 to switch pin (to set it up for reading)
133 1 Switch_pin_G = 1;
134 1 }
135
136 /*------------------------------------------------------------------*-
137
138 Check_Switch()
139
140 -*------------------------------------------------------------------*/
141 void Check_Switch(void) interrupt INTERRUPT_Timer_1_Overflow
142 {
143 1 // This function is an implementation of the pattern On-Off Switch
144 1
145 1 // If the light is on, 'Auto_switch_off_count' will be > 0
146 1 // Decrement here - and switch the light off when it reaches zero.
147 1 if (Auto_switch_off_count_G > 0)
148 1 {
149 2 Auto_switch_off_count_G--;
150 2
151 2 if (Auto_switch_off_count_G == 0)
152 2 {
153 3 Light_pin_G = LIGHT_OFF;
154 3 }
155 2 }
156 1
157 1 // The switch is 'blocked' after each switch press,
158 1 // to give the user time to remove their finger:
159 1 // If this is not done, the light will switch off again
160 1 // when the user presses the switch for more than 0.4 seconds.
161 1 //
162 1 // If the switch is blocked, decrement the block count and return
163 1 // without checking the switch pin status.
164 1 if (Switch_blocked_G > 0)
165 1 {
166 2 Switch_blocked_G--;
167 2 return;
168 2 }
169 1
170 1 // Now read switch pin
171 1 if (Switch_pin_G == SWITCH_PRESSED)
172 1 {
173 2 // If the switch pin is pressed, increment the switch count.
174 2 if (++Switch_count_G == 2)
175 2 {
176 3 // If Switch_count_G == 2, this means that the pin has been active
177 3 // for two consecutive calls to this task, i.e. it is a
178 3 // genuine switch press rather than a bounce.
179 3
C51 COMPILER V6.10 MAIN 04/19/2001 12:10:44 PAGE 4
180 3 // The variable Auto_switch_off_count_G acts both as
181 3 // an indication the light is on (if it is non-zero)
182 3 // and a counter of the number of task calls the
183 3 // light will remain on for.
184 3 if (Auto_switch_off_count_G > 0)
185 3 {
186 4 // The light is currently ON
187 4 // -> switch it off.
188 4 Light_pin_G = LIGHT_OFF;
189 4 Auto_switch_off_count_G = 0;
190 4 }
191 3 else
192 3 {
193 4 // The light is currently OFF
194 4 // -> switch it on and set the counter to 150
195 4 // (task is called every 0.2s so this gives 30 seconds delay).
196 4 Light_pin_G = LIGHT_ON;
197 4 Auto_switch_off_count_G = 150;
198 4 }
199 3
200 3 // Reset the switch count, and block the switch for the next
201 3 // second (5 calls to this task).
202 3 Switch_count_G = 0;
203 3 Switch_blocked_G = 5;
204 3 }
205 2 }
206 1 else
207 1 {
208 2 Switch_count_G = 0;
209 2 }
210 1 }
211
212 /*------------------------------------------------------------------*-
213 ---- END OF FILE -------------------------------------------------
214 -*------------------------------------------------------------------*/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 118 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 4 ----
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -