经过几天的熟悉,基本上又拿起了原来的单片机知识,模仿着编了几个简单的小程序,虽然每次都出现不止一个错误,但毕竟还是在排错中收获了不少,现在基本上能自己写少于100行的程序了,呵呵,惭愧,继续努力!
键盘控制数码管加减一:
(现在编程看重的只是结果,还无法把注意力放到代码优化上!)
#include <at89x52.h>
#define uint unsigned int
#define uchar unsigned char
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit LED3=P1^2;
sbit LED4=P1^3;
sbit K1=P2^4;
uchar LEDMAP[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0x86,0x8e,0x82};
uchar one;
uchar twe;
uchar three;
uchar four;
uint D;
void Delay(uint T);
void Display();
void LED_analyze(uint a);
void GETKEY();
void main ()
{
uint i;
D=0;
while(1)
{
if((D>=0)&&(D<=9999))
{
GETKEY();
LED_analyze(D);
for(i=0;i<50;i++)
Display();
}
else
D="0";
}
}
void Delay(uint T)
{
uint i;
for(i=0;i<T;i++);
}
void Display()
{
P0=LEDMAP[one];
LED1=0;
Delay(100);
LED1=1;
P0=LEDMAP[twe];
LED2=0;
Delay(100);
LED2=1;
P0=LEDMAP[three];
LED3=0;
Delay(100);
LED3=1;
P0=LEDMAP[four];
LED4=0;
Delay(100);
LED4=1;
}
void GETKEY()
{
uchar i,j;
K1=0;
i=P2;
Delay(100);
j=P2;
K1=1;
if(j==i)
{
switch (j)
{
case 0xee: D++;break;
case 0xed: D--;break;
}
}
}
void LED_analyze(uint a)
{
one=a/1000;
twe=a%1000/100;
three=a%100/10;
four=a%10;
}
文章评论(0条评论)
登录后参与讨论