📄 hello.lst
字号:
C51 COMPILER V8.05a HELLO 12/18/2006 18:13:10 PAGE 1
C51 COMPILER V8.05a, COMPILATION OF MODULE HELLO
OBJECT MODULE PLACED IN HELLO.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE HELLO.C BROWSE DEBUG OBJECTEXTEND
line level source
1 /*------------------------------------------------------------------------------
2 KB.C
3 说明:此程序为一简单键盘模拟程序.适用于8051系列单片机
4 扫描码标准为国际第二通用扫描码,无任何粘连键及扩展键.
5 Copyright 2006 chuangxin
6 ------------------------------------------------------------------------------*/
7
8
9 /* for the intended 8051 derivative */
10
11 #include <stdio.h> /* prototype declarations for I/O functions */
12 #include <AT89X51.h>
13 #define CLK P1_2
14 #define DATA P1_1
15
16
17
18 unsigned char Code = 0x00;
19 int enCode =0; //按键扫描成功标记
20 int realse =0;
21 unsigned char error = 0;
22 unsigned char RCode = 0x00;
23 unsigned char test = 0;
24 /*-----------------------------------------------
25 函数声明部分
26
27 ------------------------------------------------*/
28 void ScanCode();
29 void Delay10ms();
30 void SendByte(unsigned char a );
31 void KBClk();
32 void DelayNus(unsigned char i);
33 unsigned char RecieveByte();
34 unsigned char C_Command();
35 /*------------------------------------------------
36 The main C function. Program execution starts
37 here after stack initialization.
38 ------------------------------------------------*/
39 void main () {
40 1 //扫描码
41 1
42 1 int Command = 0; //命令输出标志
43 1
44 1 //TMOD = 0x61;//0x20; //定时器1定时方式2
45 1 SCON = 0x50;
46 1 //TCON = 0x40; //定时器1开始计数
47 1 //TH0 = 65490/256;//0xD7; //计数32us 此位保存TL1初始值
48 1 // TL0 = 65490%256;//0xD //起动定时器
49 1 CLK =1;
50 1 DATA =1;
51 1 P1_4 =0;
52 1 SendByte(0xAA); //键盘自检完毕
53 1
54 1
55 1 while (1) {
C51 COMPILER V8.05a HELLO 12/18/2006 18:13:10 PAGE 2
56 2 if(C_Command()){
57 3 RCode =RecieveByte();
58 3
59 3 switch(RCode){
60 4
61 4 case 0xF2:
62 4 SendByte(0xFA);
63 4 SendByte(0xAB);
64 4 SendByte(0x83);
65 4 break;
66 4 case 0xEE:
67 4 SendByte(0xEE);
68 4 break;
69 4 case 0x00:
70 4 SendByte(0xFA);
71 4 P1_4 = 1; //关掉指示灯
72 4 break;
73 4 case 0x02:
74 4 SendByte(0xFA);
75 4 P1_4 =0; //打开指示灯
76 4 break;
77 4 case 0xFF:
78 4 SendByte(0xFA);
79 4 SendByte(0xAA);
80 4 break;
81 4 case 0xFE:
82 4 SendByte(0xFA);
83 4 SendByte(Code);
84 4 break;
85 4 default:
86 4 SendByte(0xFA);
87 4 break;
88 4 }
89 3 }
90 2
91 2 enCode = 0;
92 2 if(CLK & DATA){
93 3 ScanCode();
94 3
95 3
96 3 if(enCode)
97 3
98 3 {
99 4 SendByte(Code);
100 4 }
101 3 if(realse){
102 4
103 4 SendByte(0xF0);
104 4 SendByte(Code);
105 4
106 4 realse = 0;
107 4 }
108 3
109 3 }
110 2 }
111 1
112 1
113 1 }
114 /*-----------------------------------------------
115 unsigned char ScanCode()
116 说明:按键扫描函数
117 参数:无
C51 COMPILER V8.05a HELLO 12/18/2006 18:13:10 PAGE 3
118 返回:unsigned char
119 功能:返回按键编码
120 ------------------------------------------------*/
121 void ScanCode(){
122 1 unsigned char i =0;
123 1 Code = 0x1C; //A键的扫描码因为只用一个K3按键
124 1 P3_4 = 1;
125 1 if(P3_4 == 0){ //如果有键按下
126 2 Delay10ms(); //延时10ms
127 2 if(P3_4 == 0) //确定有键按下
128 2 {
129 3 enCode = 1; //可以发送数据了
130 3
131 3 Delay10ms(); //等待10ms让释放按键
132 3 realse = 1;
133 3
134 3 }
135 2 else
136 2 return ;
137 2 }
138 1 return ;
139 1 }
140
141 /*-----------------------------------------------
142 void DelayNms()
143 说明:延时程序
144 参数:无
145 返回:无
146 功能:延时
147 ------------------------------------------------*/
148 void Delay10ms(){
149 1 unsigned char dly1 = 1;
150 1 unsigned char dly2 = 215;
151 1 while(dly1++){
152 2 while(dly2++);
153 2 }
154 1 }
155
156 void Delay40us(){
157 1 unsigned char i;
158 1 for(i = 59;i >0; i--);
159 1 }
160
161 void Delay20us(){
162 1 unsigned char i;
163 1 for(i = 30;i >0; i--);
164 1 }
165
166 void Delay50us(){
167 1 unsigned char i;
168 1 for(i = 74;i >0; i--);
169 1 }
170
171 void Delay5us(){
172 1 unsigned char i;
173 1 for(i = 8;i >0; i--);
174 1 }
175
176 void Delay15us(){
177 1 unsigned char i;
178 1 for(i = 23;i >0; i--);
179 1 }
C51 COMPILER V8.05a HELLO 12/18/2006 18:13:10 PAGE 4
180 /*-----------------------------------------------
181 void KBClk()
182 说明:键盘时钟脉冲生成函数
183 参数:无
184 返回:无
185 功能:生成键盘时钟脉冲,高40us,低40us
186 ------------------------------------------------*/
187 void KBClk() interrupt 1 using 1 {
188 1 unsigned int edgeCg = 1;
189 1 unsigned char a =1;
190 1 CLK = !CLK;
191 1 TH0 = 65490/256;//0xD7; //计数32us 此位保存TL1初始值
192 1 TL0 = 65490%256;
193 1 }
194 /*-----------------------------------------------
195 void SendByte(unsigned char ScanCode)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -