本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
我想让时钟停走,但是在控制CH停止位的时候,出现问题了,按一下key1(停止键),数码管会减一个数,我怀疑是秒寄存器的CH影响了数码管。。具体程序如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
uchar miao,fen,shi,flag,temp;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit IO=P1^5;
sbit SCLK=P1^6;
sbit RST=P1^7;
uchar code tab[]={ 0x3F,/*0*/
0x06,/*1*/
0x5B,/*2*/
0x4F,/*3*/
0x66,/*4*/
0x6D,/*5*/
0x7D,/*6*/
0x07,/*7*/
0x7F,/*8*/
0x6F,/*9*/
} ;//共阴码
void delay(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
//1302写入一字节
void write_byte(uchar dat)
{
uchar a;
ACC=dat;
for(a=8;a>0;a--)
{
IO=ACC0;
SCLK=0;
//delayus(10);
SCLK=1;
ACC=ACC>>1;
}
}
//向1302读出一字节
uchar read_byte()
{
uchar a;
for(a=0;a<8;a++)
{
ACC7=IO;
SCLK=1;
SCLK=0;
ACC=ACC>>1;
}
return(ACC);
}
//向1302写入数据
void write_1302(uchar add,uchar dat)
{
RST=0;
SCLK=0;
//delayus(5);
RST=1;
write_byte(add);
write_byte(dat);
//delayus(5);
SCLK=1;
RST=0;
}
//向1302读出数据
uchar read_1302(uchar add)
{
uchar temp;
RST=0;
SCLK=0;
//delayus(5);
RST=1;
write_byte(add);
temp=read_byte();
//delayus(5);
SCLK=1;
RST=0;
return (temp);
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
void init()//初始化
{
//RST=0;
//SCLK=0;
//write_1302(0x80,0x00);//写控制字,允许写
//write_1302(0x90,0x00);//禁止涡流充电
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void display(uchar miaog,uchar miaos,uchar feng,uchar fens,uchar shig,uchar shis)//数码管动态显示
{
P2=0x7f;
P0=tab[miaog];
delay(5);
P2=0xbf;
P0=tab[miaos];
delay(5);
P2=0xdf;
P0=0xc0;
delay(5);
P2=0xef;
P0=tab[feng];
delay(5);
P2=0xf7;
P0=tab[fens];
delay(5);
P2=0xfb;
P0=0xc0;
delay(5);
P2=0xfd;
P0=tab[shig];
delay(5);
P2=0xfe;
P0=tab[shis];
delay(5);
}
write_sfm(uchar miao,uchar fen,uchar shi) //写入到数码管
{
uchar miaog,miaos,feng,fens,shig,shis;
miaog=miao%10;
miaos=miao/10;
feng=fen%10;
fens=fen/10;
shig=shi%10;
shis=shi/10;
display(miaog,miaos,feng,fens,shig,shis);
}
void keyscan()
{
if(key1==0)
{
if(key1==0)
{
while(!key2);
write_1302(0x80,miao|0x80);//停止1302振荡器
}
}
//write_1302(0x80,miao|0x80);
//miao++;
if(key2==0)
{
if(key2==0)
{
while(!key2);
write_1302(0x80,miao|0x00);//打开1302振荡器
}
}
}
void main()
{
init();
//miao=20;
//fen=10;
//shi=23;
while(1)
{
miao = BCD_Decimal(read_1302(0x81));
fen = BCD_Decimal(read_1302(0x83));
shi = BCD_Decimal(read_1302(0x85));
keyscan();
}
}
void tiemr0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
write_sfm(miao,fen,shi);
} |