本帖最后由 suoma 于 2009-11-16 21:51 编辑
//基于单片机串行口的电子钟的设计
//请大家帮忙看一下,提提建议!
//我采用的是74HC595芯片。程序如下:
//基于单片机串行口的电子钟的设计
//利用74HC595
//下面有几个不太明白的地方,请高手指点指点!!
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define NOP _nop_()
sbit SDA=P1^1;
sbit SHIFT=P1^2;
sbit ST=P1^0;
sbit P0_0=P1^3;
sbit P0_1=P1^4;
sbit P0_2=P1^5;
sbit P0_3=P1^6;
sbit P1_7=P1^7;
//共阳极数码管
uchar duan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0x0c};
uchar wei[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0x80};
//我想在P2口接pnp三极管,是否要改变位码。我自己改成 {0xfe,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd}这个口,发现P2口高电平时就通了!
//如果我仿真的时候不接三极管,实物制作的时候加上会不会出现问题。
uchar dispbuf[]={0,0,16,0,0,16,0,0};
void key(void);
uchar i;
uchar second;
uchar minite;
uchar hour;
uchar mstcnt;
uchar V=0;
void Init( )
{
P1_7=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=0;
ET0=1;
EA=1;
SDA = 0;
SHIFT = 0;
ST = 0;
}
void delay(uint m)
{
while(m--);
}
void display(uchar dat)
{
uchar ii;
uchar sdata=dat;
for(ii=0;ii<8;ii++)
{
if(sdata&0x80)
SDA=1;
else
SDA = 0;
sdata<<=1;
SHIFT =0;
NOP;
NOP;
SHIFT = 1;
NOP;
NOP;
}
ST = 1;
NOP;
NOP;
ST = 0;
}
void main()
{
Init();
while(1)
{
delay(360);
P2 = wei[8];
display(duan[11]);
if(P0_0==0)
{
while(1)
{
key();
for(i=0;i<8;i++)
{
delay(360);
P2 = wei;
display(duan[dispbuf]);
}
}
}
}
}
void key(void)
{
dispbuf[2]=10;
dispbuf[5]=10;
if(P0_0==0)
{
delay(50);
if(P0_0==0)
{
V++;
if(V==2)
{
V=0;
}
switch(V)
{
case 0:TR0=0;break;
case 1:TR0=1;break;
}
}
}
if(P0_1==0)
{
delay(50);
if(P0_1==0)
{
second++;
if(second==60)
{
second=0;
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
while(P0_1==0);
}
}
if(P0_2==0)
{
delay(100);
if(P0_2==0)
{
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(P0_2==0);
}
}
if(P0_3==0)
{
delay(50);
if(P0_3==0)
{
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(P0_3==0);
}
}
}
void t0(void) interrupt 1 using 0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
mstcnt++;
if(mstcnt==15)
{
mstcnt=0;
second++;
if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
}
|
|