第四位是数字的十位 应该在第一位
第一位是数字的个位 应该在第二位
第二位是显示组别 应该在第四位
程序内部调换 怎么调换 位 都是乱的
除非我把底下接线改了
这是动态显示的程序
void display(uchar number,uchar second) //动态显示
{
uchar second_first,second_second;
second_first = second / 10;
second_second = second % 10;
P2 = 0xfe;
P0 = table[number];
delay(2);
P2 = 0xfd;
P0 = 0xbf;
delay(2);
P2 = 0xfb;
P0 = table[second_first];
delay(2);
P2 = 0xf7;
P0 = table[second_second];
delay(2);
}
全部程序在下边
#include <reg52.h>
/*-----------------------------------------------------------
宏定义
------------------------------------------------------------*/
#define uint unsigned int
#define uchar unsigned char
/*-----------------------------------------------------------
共阴极数码管编码表 0 - f显示
------------------------------------------------------------*/
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
/*-----------------------------------------------------------
变量定义
------------------------------------------------------------*/
sbit buzz=P3^5;
sbit start_stop = P3^6;
sbit reset = P3^7;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
sbit key5 = P1^4;
sbit key6 = P1^5;
sbit key7 = P1^6;
sbit key8 = P1^7;
sbit state = P3^4;
sbit kaishi=P3^1;
bit start_stop_flag = 0;
bit key1_flag = 0;
bit key2_flag = 0;
bit key3_flag = 0;
bit key4_flag = 0;
bit key5_flag = 0;
bit key6_flag = 0;
bit key7_flag = 0;
bit key8_flag = 0;
bit reset_flag = 0;
bit ac
tion = 0;
bit kaishi_flag=0;
uchar second = 20;
uchar timer0_count = 0;
uchar number = 0;
uchar number_display = 0;
/*-----------------------------------------------------------
延时函数
------------------------------------------------------------*/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*-----------------------------------------------------------
数码管显示驱动函数
------------------------------------------------------------*/
void display(uchar number,uchar second) //动态显示
{
uchar second_first,second_second;
second_first = second / 10;
second_second = second % 10;
P2 = 0xfe;
P0 = table[number];
delay(2);
P2 = 0xfd;
P0 = 0xbf;
delay(2);
P2 = 0xfb;
P0 = table[second_first];
delay(2);
P2 = 0xf7;
P0 = table[second_second];
delay(2);
}
/*-----------------------------------------------------------
开始键扫描函数
------------------------------------------------------------*/
void start_stop_keyscan()
{
if(start_stop == 0)
{
delay(8);
if((start_stop == 0)&&(!start_stop_flag))
{
start_stop_flag = 1;
action = 1;
TR0 = 1;
state = 0;
}
}
else
{
start_stop_flag = 0;
}
}
/*-----------------------------------------------------------
八位抢答键扫描函数
------------------------------------------------------------*/
uchar key_scan8()
{
if(key1 == 0)
{
delay(8);
if((key1 == 0)&&(!key1_flag))
{
key1_flag = 1;
number = 1;
number_display = number;
}
}
else
{
key1_flag = 0;
number = 0;
}
if(key2 == 0)
{
delay(8);
if((key2 == 0)&&(!key2_flag))
{
key2_flag = 1;
number = 2;
number_display = number;
}
}
else
{
key2_flag = 0;
number = 0;
}
if(key3 == 0)
{
delay(8);
if((key3 == 0)&&(!key3_flag))
{
key3_flag = 1;
number = 3;
number_display = number;
}
}
else
{
key3_flag = 0;
number = 0;
}
if(key4 == 0)
{
delay(8);
if((key4 == 0)&&(!key4_flag))
{
key4_flag = 1;
number = 4;
number_display = number;
}
}
else
{
key4_flag = 0;
number = 0;
}
if(key5 == 0)
{
delay(8);
if((key5 == 0)&&(!key5_flag))
{
key5_flag = 1;
number = 5;
number_display = number;
}
}
else
{
key5_flag = 0;
number = 0;
}
if(key6 == 0)
{
delay(8);
if((key6 == 0)&&(!key6_flag))
{
key6_flag = 1;
number = 6;
number_display = number;
}
}
else
{
key6_flag = 0;
number = 0;
}
if(key7 == 0)
{
delay(8);
if((key7 == 0)&&(!key7_flag))
{
key7_flag = 1;
number = 7;
number_display = number;
}
}
else
{
key7_flag = 0;
number = 0;
}
if(key8 == 0)
{
delay(8);
if((key8 == 0)&&(!key8_flag))
{
key8_flag = 1;
number = 8;
number_display = number;
}
}
else
{
key8_flag = 0;
number = 0;
}
if(number_display != 0)
{
return 1;
}
else
{
return 00;
}
}
/*-----------------------------------------------------------
复位键扫描函数
------------------------------------------------------------*/
void reset_keyscan()
{
if(reset == 0)
{
delay(8);
if((reset == 0)&&(!reset_flag))
{ second=20;
reset_flag = 1;
number_display = 0;
state = 1;
}
}
else
{
reset_flag = 0;
}
}
/*
开机函数*/
void kaishi_keyscan()
{
if(kaishi == 0)
{
delay(8);
if((kaishi == 0)&&(!kaishi_flag))
{
kaishi_flag = 1;
number_display = 0;
second=0;
state = 1;
}
}
else
{
kaishi_flag = 0;
}
}
/*-----------------------------------------------------------
主函数
------------------------------------------------------------*/
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=0;
while(1)
{
while((kaishi == 0)&&(!kaishi_flag))
{
start_stop_keyscan();
reset_keyscan();
while(action)
{
while(!key_scan8())
{
display(number_display,second);
if(second == 0)
{
second = 20;
break;
}
}
buzz=1;
delay(50);
TR0 = 0;
second = 0;
display(number_display,second);
action = 0;
break;
}
display(number_display,second);
}
}
}
/*-----------------------------------------------------------
中断服务函数
------------------------------------------------------------*/
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
timer0_count ++;
if(timer0_count == 20)
{
timer0_count = 0;
second --;
if(second == 0)
{
TR0 = 0;
number_display = 0;
state = 1;
action = 0;
}
}
}
一周热门 更多>