📄 switch_count.lst
字号:
C51 COMPILER V6.21 SWITCH_COUNT 01/23/2002 17:21:00 PAGE 1
C51 COMPILER V6.21, COMPILATION OF MODULE SWITCH_COUNT
OBJECT MODULE PLACED IN Switch_count.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE Switch_count.c OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 Switch_count.C (v1.00)
4
5 ------------------------------------------------------------------
6
7 A 'goat counting' program for the 8051 (see text for details...).
8
9
10 COPYRIGHT
11 ---------
12
13 This code is associated with the book:
14
15 EMBEDDED C by Michael J. Pont
16 [Pearson Education, 2002: ISBN: 0-201-79523-X].
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 <reg52.h>
25
26 // Connect switch to this pin
27 sbit Switch_pin = P1^0;
28
29 // Display count (binary) on this port
30 #define Count_port P3
31
32 // Return values from Switch_Get_Input()
33 #define SWITCH_NOT_PRESSED (bit) 0
34 #define SWITCH_PRESSED (bit) 1
35
36 // Function prototypes
37 void SWITCH_Init(void);
38 bit SWITCH_Get_Input(const unsigned char);
39 void DISPLAY_COUNT_Init(void);
40 void DISPLAY_COUNT_Update(const unsigned char);
41 void DELAY_LOOP_Wait(const unsigned int);
42
43 /* ---------------------------------------------------------------- */
44 void main(void)
45 {
46 1 unsigned char Switch_presses = 0;
47 1
48 1 // Init functions
49 1 SWITCH_Init();
50 1 DISPLAY_COUNT_Init();
51 1
52 1 while(1)
53 1 {
54 2 if (SWITCH_Get_Input(30) == SWITCH_PRESSED)
55 2 {
C51 COMPILER V6.21 SWITCH_COUNT 01/23/2002 17:21:00 PAGE 2
56 3 Switch_presses++;
57 3 }
58 2
59 2 DISPLAY_COUNT_Update(Switch_presses);
60 2 }
61 1 }
62
63 /*------------------------------------------------------------------*-
64
65 SWITCH_Init()
66
67 Initialization function for the switch library.
68
69 -*------------------------------------------------------------------*/
70 void SWITCH_Init(void)
71 {
72 1 Switch_pin = 1; // Use this pin for input
73 1 }
74
75 /*------------------------------------------------------------------*-
76
77 SWITCH_Get_Input()
78
79 Reads and debounces a mechanical switch as follows:
80
81 1. If switch is not pressed, return SWITCH_NOT_PRESSED.
82
83 2. If switch is pressed, wait for DEBOUNCE_PERIOD (in ms).
84 a. If switch is not pressed, return SWITCH_NOT_PRESSED.
85 b. If switch is pressed, wait (indefinitely) for
86 switch to be released, then return SWITCH_PRESSED
87
88 See Switch_Wait.H for details of return values.
89
90 -*------------------------------------------------------------------*/
91 bit SWITCH_Get_Input(const unsigned char DEBOUNCE_PERIOD)
92 {
93 1 bit Return_value = SWITCH_NOT_PRESSED;
94 1
95 1 if (Switch_pin == 0)
96 1 {
97 2 // Switch is pressed
98 2
99 2 // Debounce - just wait...
100 2 DELAY_LOOP_Wait(DEBOUNCE_PERIOD);
101 2
102 2 // Check switch again
103 2 if (Switch_pin == 0)
104 2 {
105 3 // Wait until the switch is released.
106 3 while (Switch_pin == 0);
107 3 Return_value = SWITCH_PRESSED;
108 3 }
109 2 }
110 1
111 1 // Now (finally) return switch value
112 1 return Return_value;
113 1 }
114 /*------------------------------------------------------------------*-
115
116 DISPLAY_COUNT_Init()
117
C51 COMPILER V6.21 SWITCH_COUNT 01/23/2002 17:21:00 PAGE 3
118 Initialization function for the DISPLAY COUNT library.
119
120 -*------------------------------------------------------------------*/
121 void DISPLAY_COUNT_Init(void)
122 {
123 1 Count_port = 0x00;
124 1 }
125
126 /*------------------------------------------------------------------*-
127
128 DISPLAY_COUNT_Update()
129
130 Simple function to display tByte data (COUNT)
131 on LEDs connected to port (Count_Port)
132
133 -*------------------------------------------------------------------*/
134 void DISPLAY_COUNT_Update(const unsigned char COUNT)
135 {
136 1 Count_port = COUNT;
137 1 }
138
139 /*------------------------------------------------------------------*-
140
141 DELAY_LOOP_Wait()
142
143 Delay duration varies with parameter.
144
145 Parameter is, *ROUGHLY*, the delay, in milliseconds,
146 on 12MHz 8051 (12 osc cycles).
147
148 You need to adjust the timing for your application!
149
150 -*------------------------------------------------------------------*/
151 void DELAY_LOOP_Wait(const unsigned int DELAY_MS)
152 {
153 1 unsigned int x, y;
154 1
155 1 for (x = 0; x <= DELAY_MS; x++)
156 1 {
157 2 for (y = 0; y <= 120; y++);
158 2 }
159 1 }
160
161
162 /*------------------------------------------------------------------*-
163 ---- END OF FILE -------------------------------------------------
164 -*------------------------------------------------------------------*/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 89 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 1
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 + -