小弟做了个秒表,遇到了一些困难,向大神们求解
时间:10-02
整理:3721RD
点击:
单独的计时显示模块可以运行,单独的按键扫描模块也可以运行,就是凑起来的时候就出现问题。 看了好久没找出问题所在。#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 keystatic[] = {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();
}
#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 keystatic[] = {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();
}
你也没有说出是什么问题呀,
你想做的秒表是不是在键盘按下多少秒总数,按确定键之后就开始计数,然后到了设定值就提醒,是这个意思吗?
实现的功能其实很简单,就是有两个按键,一个是停止按键按下去就要停止,一个是复位清零。可是程序会跑飞
这程序是你自己写的吗?如果是我写的话就不会写的这么复杂,大概思路是:程序一开始就计数,用定时器1或者0都可以,然后再定时中断服务程序里设定终值(要看使用多少位数码管,2位的话就是99,3位就是999)等计数到了种植就清零从头开始计数!在主函数里不停的扫描按键函数(比如设定key1为停止键,key2为清零键)当按下key1时,只要使TR0=0就可以了!当按下key2时,只要使num=0就OK了(num是计数参数)!
这个问题我已经解决了,谢谢。其实如果按照你的说法不是说不行。只不过可能程序的移植性没那么好。