📄 switch_read.lst
字号:
C51 COMPILER V7.06 SWITCH_READ 12/14/2004 09:43:15 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE SWITCH_READ
OBJECT MODULE PLACED IN Switch_read.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE Switch_read.c OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 Switch_read.C (v1.00)
4
5 ------------------------------------------------------------------
6
7 A simple 'switch input' program for the 8051.
8 - Reads (and debounces) switch input on Pin1^0
9 - If switch is pressed, changes Port 3 output.
10
11
12 COPYRIGHT
13 ---------
14
15 This code is associated with the book:
16
17 EMBEDDED C by Michael J. Pont
18 [Pearson Education, 2002: ISBN: 0-201-79523-X].
19
20 This code is copyright (c) 2001 by Michael J. Pont.
21
22 See book for copyright details and other information.
23
24 -*------------------------------------------------------------------*/
25
26 #include <reg52.h>
27
28 // Connect switch to this pin
29 sbit Switch_pin = P1^0;
30
31 // Display switch status on this port
32 #define Output_port P3
33
34 // Return values from Switch_Get_Input()
35 #define SWITCH_NOT_PRESSED (bit) 0
36 #define SWITCH_PRESSED (bit) 1
37
38 // Function prototypes
39 void SWITCH_Init(void);
40 bit SWITCH_Get_Input(const unsigned char);
41 void DISPLAY_SWITCH_STATUS_Init(void);
42 void DISPLAY_SWITCH_STATUS_Update(const bit);
43 void DELAY_LOOP_Wait(const unsigned int);
44
45 /* ---------------------------------------------------------------- */
46 void main(void)
47 {
48 1 bit Sw_state;
49 1
50 1 // Init functions
51 1 SWITCH_Init();
52 1 DISPLAY_SWITCH_STATUS_Init();
53 1
54 1 while(1)
55 1 {
C51 COMPILER V7.06 SWITCH_READ 12/14/2004 09:43:15 PAGE 2
56 2 Sw_state = SWITCH_Get_Input(30);
57 2
58 2 DISPLAY_SWITCH_STATUS_Update(Sw_state);
59 2 }
60 1 }
61
62 /*------------------------------------------------------------------*-
63
64 SWITCH_Init()
65
66 Initialization function for the switch library.
67
68 -*------------------------------------------------------------------*/
69 void SWITCH_Init(void)
70 {
71 1 Switch_pin = 1; // Use this pin for input
72 1 }
73
74 /*------------------------------------------------------------------*-
75
76 SWITCH_Get_Input()
77
78 Reads and debounces a mechanical switch as follows:
79
80 1. If switch is not pressed, return SWITCH_NOT_PRESSED.
81
82 2. If switch is pressed, wait for DEBOUNCE_PERIOD (in ms).
83 a. If switch is not pressed, return SWITCH_NOT_PRESSED.
84 b. If switch is pressed, wait (indefinitely) for
85 switch to be released, then return SWITCH_PRESSED
86
87 See Switch_Wait.H for details of return values.
88
89 -*------------------------------------------------------------------*/
90 bit SWITCH_Get_Input(const unsigned char DEBOUNCE_PERIOD)
91 {
92 1 bit Return_value = SWITCH_NOT_PRESSED;
93 1
94 1 if (Switch_pin == 0)
95 1 {
96 2 // Switch is pressed
97 2
98 2 // Debounce - just wait...
99 2 DELAY_LOOP_Wait(DEBOUNCE_PERIOD);
100 2
101 2 // Check switch again
102 2 if (Switch_pin == 0)
103 2 {
104 3 Return_value = SWITCH_PRESSED;
105 3 }
106 2 }
107 1
108 1 // Now return switch value
109 1 return Return_value;
110 1 }
111
112 /*------------------------------------------------------------------*-
113
114 DISPLAY_SWITCH_STATUS_Init()
115
116 Initialization function for the DISPLAY_SWITCH_STATUS library.
117
C51 COMPILER V7.06 SWITCH_READ 12/14/2004 09:43:15 PAGE 3
118 -*------------------------------------------------------------------*/
119 void DISPLAY_SWITCH_STATUS_Init(void)
120 {
121 1 Output_port = 0xF0;
122 1 }
123
124 /*------------------------------------------------------------------*-
125
126 DISPLAY_SWITCH_STATUS_Update()
127
128 Simple function to display data (SWITCH_STATUS)
129 on LEDs connected to port (Output_Port)
130
131 -*------------------------------------------------------------------*/
132 void DISPLAY_SWITCH_STATUS_Update(const bit SWITCH_STATUS)
133 {
134 1 if (SWITCH_STATUS == SWITCH_PRESSED)
135 1 {
136 2 Output_port = 0x0F;
137 2 }
138 1 else
139 1 {
140 2 Output_port = 0xF0;
141 2 }
142 1 }
143
144 /*------------------------------------------------------------------*-
145
146 DELAY_LOOP_Wait()
147
148 Delay duration varies with parameter.
149
150 Parameter is, *ROUGHLY*, the delay, in milliseconds,
151 on 12MHz 8051 (12 osc cycles).
152
153 You need to adjust the timing for your application!
154
155 -*------------------------------------------------------------------*/
156 void DELAY_LOOP_Wait(const unsigned int DELAY_MS)
157 {
158 1 unsigned int x, y;
159 1
160 1 for (x = 0; x <= DELAY_MS; x++)
161 1 {
162 2 for (y = 0; y <= 120; y++);
163 2 }
164 1 }
165
166
167 /*------------------------------------------------------------------*-
168 ---- END OF FILE -------------------------------------------------
169 -*------------------------------------------------------------------*/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 91 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 3
C51 COMPILER V7.06 SWITCH_READ 12/14/2004 09:43:15 PAGE 4
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -