文件目录
main.c
#include <REGX52.H>
#include "TIMER0.H"
#include "KEY.H"
#include "DELAY.H"
//void Timer0_Init() {
// TMOD = 0x01;
// TL0 = 64536 % 256;
// TH0 = 64536 / 256;
// ET0 = 1;
// EA = 1;
// TR0 = 1;
//}
unsigned char keyNum = 0; //链接文件会拼到一起,不要起一样的名字
void main() {
// Timer0_Init();
while(1) {
keyNum = Key();
if(keyNum) { //if来缓解闪动
if(keyNum == 1) P2_1 = ~P2_1;
if(keyNum == 2) P2_2 = ~P2_2;
if(keyNum == 3) P2_3 = ~P2_3;
if(keyNum == 4) P2_4 = ~P2_4;
}
}
}
//static unsigned int T0Count = 0;
//void Timer0_Routine() interrupt 1{
// TL0 = 64536 % 256;
// TH0 = 64536 / 256;
// T0Count ++;
// if(T0Count >= 1000) {
// T0Count = 0;
// P2_0 = ~P2_0;
// }
//}
Key.c
unsigned char Key() {
unsigned char keyNumber = 0; //不要放在外面,每次没按动的时候默认if全部经过就是 keyNumber
if(P3_1 == 0){Delay(20);while(P3_1 == 0);Delay(20);keyNumber = 1;}
if(P3_0 == 0){Delay(20);while(P3_0 == 0);Delay(20);keyNumber = 2;}
if(P3_2 == 0){Delay(20);while(P3_2 == 0);Delay(20);keyNumber = 3;}
if(P3_3 == 0){Delay(20);while(P3_3 == 0);Delay(20);keyNumber = 4;}
return keyNumber;
}
Key.h
#ifndef __KEY_H__
#define __KEY_H__
unsigned char Key(); //要有返回类型,要有“;”结尾
#endif
下面是对的
Timer0.c
#include <REGX52.H>
void Timer0_Init() {
TMOD = 0x01;
TL0 = 64536 % 256;
TH0 = 64536 / 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
Timer0.h
#ifndef __TIMER0_H__
#define __TIMER0_H__
void Timer0_Init(void);
#endif
Delay.c
void Delay(unsigned int xms)
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
Delay.h
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);
#endif