单独的计时显示模块可以运行,单独的按键扫描模块也可以运行,就是凑起来的时候就出现问题。 看了好久没找出问题所在。#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit du = P2^0;
sbit we = P2^1;
sbit keyout0 = P3^0;
sbit keylie0 = P3^4;
sbit keylie1 = P3^5;
sbit keylie2 = P3^6;
sbit keylie3 = P3^7;
sbit led = P1^0;
uchar T0RL, T0RH;
ulong Integer = 0;
uchar decimal = 0;
void ledscan();
void ledaccount();
void keyinit_1();
void keyscan();
void keydriver();
uchar code dutable[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code wetable[] = { 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar ledbuff[] = {0, 0, 0, 0, 0, 0};
uchar keysta
tic[] = {1,1,1,1};
bit stopkey = 0;
bit keyinit = 0;
void Timer_init(uint ms)
{
ulong tmp=0;
TMOD = TMOD & 0XF0;
TMOD = TMOD | 0X01;
tmp = 11059200 / 12 ;
tmp = tmp * ms / 1000;
tmp = 65535 - tmp;
T0RH = (uchar)(tmp >> 8);
T0RL = (uchar)tmp;
TH0 = T0RH;
TL0 = T0RL;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void main()
{
Timer_init(2);
keyout0 = 0;
while(1)
{
if(keyinit == 1)
{
keyinit_1();
}
keydriver();
}
}
void keydriver()
{ static uchar update[] ={1,1,1,1};
uchar i = 0;
for(i=0; i<4; i++)
{
if(update[i] != keystatic[i])
{
if(update[i] != 0)
{
if( i == 0)
{
stopkey = ~stopkey;
}
if(i == 1)
{
keyinit = 1;
}
}
update[i] = keystatic[i];
}
}
}
void keyscan()
{ uchar i = 0;
static uchar keybuff[] = {0xff,0xff,0xff,0xff};
keybuff[0] = (keybuff[0]<<1) | keylie0;
keybuff[1] = (keybuff[1]<<1) | keylie1;
keybuff[2] = (keybuff[2]<<1) | keylie2;
keybuff[3] = (keybuff[3]<<1) | keylie3;
for(i=0; i<4; i++)
{
if(keybuff[i] == 0x00)
{
keystatic[i] = 0;
}
else if(keybuff[i] == 0xff)
{
keystatic[i] = 1;
}
}
}
void keyinit_1()
{
if(keyinit == 1)
{
Integer = 0;
decimal = 0;
stopkey = 0;
keyinit = 0;
}
else
keyinit = 0;
}
void ledscan()
{ static uchar i=0;
P0 = 0x00;
switch(i)
{
case 0: du = 1; P0=ledbuff[0]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 1: du = 1; P0=ledbuff[1]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 2: du = 1; P0=ledbuff[2]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 3: du = 1; P0=ledbuff[3]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 4: du = 1; P0=ledbuff[4]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 5: du = 1; P0=ledbuff[5]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 6: du = 1; P0=ledbuff[6]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i++; break;
case 7: du = 1; P0=ledbuff[7]; du = 0; P0 = 0XFF; we = 1; P0 = wetable[i]; we = 0; i=0; break;
default : break;
}
}
void ledaccount()
{
if(stopkey == 0)
{
decimal++;
ledbuff[0] = dutable[(decimal%10)];
ledbuff[1] = dutable[(decimal/10)];
if(decimal>=100)
{
decimal=0;
Integer++;
ledbuff[2] = dutable[(Integer%10 )]|0x80;
ledbuff[3] = dutable[(Integer/10%10)];
ledbuff[4] = dutable[(Integer/100%10)];
ledbuff[5] = dutable[(Integer/1000%10 )];
ledbuff[6] = dutable[(Integer/10000%10 )];
ledbuff[7] = dutable[(Integer/100000%10 )];
if(Integer == 100000)
{
Integer = 0;
}
}
}
}
void interrupTimer0() interrupt 1
{ static uchar mun=0;
TH0 = T0RH;
TL0 = T0RL;
mun++;
ledscan();
if(mun == 10)
{
mun =0 ;
ledaccount();
}
keyscan();
}
一周热门 更多>