我的程序,首先有一点,肯定是调试通过的,不像我以前找的一些网络程序,貌似可以,就是不能执行结果。
/*DS1302时钟芯片显示设置
功能:显示 时间 日期 星期
引脚说明:DS1302三引脚 CLK = P1^5IO=P1^6RST=P1^7
设计整理:创达科技 陈仲库
技术服务:kuzi00@163.com QQ:86828048
整理时间:2007年5月26日
*/
#define uchar unsigned char
#define uint unsigned int
sbit DS1302_CLK = P1^5 //实时时钟时钟线引脚
sbit DS1302_IO = P1^6 //实时时钟数据线引脚
sbit DS1302_RST = P1^7 //实时时钟复位线引脚
sbit ACC0 = ACC^0
sbit ACC7 = ACC^7
typedef struct __SYSTEMTIME__
{
uchar Second
uchar Minute
uchar Hour
uchar Week
uchar Day
uchar Month
uchar Year
uchar DateString[9]
uchar TimeString[9]
}SYSTEMTIME//定义的时间类型
void DS1302_WR_Byte(uchar d) //实时时钟写入一字节(内部函数)
{
uchar i
ACC = d
for(i=8i>0i--)
{
DS1302_IO = ACC0 //相当于汇编中的 RRC
DS1302_CLK = 1
DS1302_CLK = 0
ACC = ACC >>1
}
}
uchar DS1302_RD_Byte(void) //实时时钟读取一字节(内部函数)
{
uchar i
for(i=8i>0i--)
{
ACC = ACC >>1 //相当于汇编中的 RRC
ACC7 = DS1302_IO
DS1302_CLK = 1
DS1302_CLK = 0
}
return(ACC)
}
void DS1302_Write(uchar ucAddr, uchar ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0
DS1302_CLK = 0
DS1302_RST = 1
DS1302_WR_Byte(ucAddr) // 地址,命令
DS1302_WR_Byte(ucDa) // 写1Byte数据
DS1302_CLK = 1
DS1302_RST = 0
}
uchar DS1302_Read(uchar ucAddr) //读取DS1302某地址的数据
{
uchar ucData
DS1302_RST = 0
DS1302_CLK = 0
DS1302_RST = 1
DS1302_WR_Byte(ucAddr|0x01) // 地址,命令
ucData = DS1302_RD_Byte()// 读1Byte数据
DS1302_CLK = 1
DS1302_RST = 0
return(ucData)
}
void DS1302_SetTime(uchar Address, uchar Value)// 设置时间函数
{
DS1302_Write(0x8E,0x00) //写保护
DS1302_Write(Address, ((Value/10)<<4 | (Value%10)))
}
void DS1302_GetTime(SYSTEMTIME *Time)
{
uchar ReadValue
ReadValue = DS1302_Read(0x80)
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x82)
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x84)
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x86)
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x8a)
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x88)
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
ReadValue = DS1302_Read(0x8c)
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)
}
void DateToStr(SYSTEMTIME *Time)
{
Time->DateString[0] = Time->Year/10 + '0'
Time->DateString[1] = Time->Year%10 + '0'
Time->DateString[2] = '-'
Time->DateString[3] = Time->Month/10 + '0'
Time->DateString[4] = Time->Month%10 + '0'
Time->DateString[5] = '-'
Time->DateString[6] = Time->Day/10 + '0'
Time->DateString[7] = Time->Day%10 + '0'
Time->DateString[8] = '\0'
}
void TimeToStr(SYSTEMTIME *Time)
{
Time->TimeString[0] = Time->Hour/10 + '0'
Time->TimeString[1] = Time->Hour%10 + '0'
Time->TimeString[2] = ':'
Time->TimeString[3] = Time->Minute/10 + '0'
Time->TimeString[4] = Time->Minute%10 + '0'
Time->TimeString[5] = ':'
Time->TimeString[6] = Time->Second/10 + '0'
Time->TimeString[7] = Time->Second%10 + '0'
Time->DateString[8] = '\0'
}
void DS1302_Init(void)
{
uchar Second=DS1302_Read(0x80)
if(Second&0x80) DS1302_SetTime(0x80,0)
//时间设置初始化 秒 分 时
DS1302_SetTime(0x80,30)
DS1302_SetTime(0x82,59)
DS1302_SetTime(0x84,23)
DS1302_SetTime(0x8A,6) //周
DS1302_SetTime(0x86,26) //天
DS1302_SetTime(0x88,5) //月
DS1302_SetTime(0x8C,7) //年
}
/*调用说明
DS1302_Init()初始化
DS1302_GetTime(&CurrentTime)//取得当前时间
DateToStr(&CurrentTime)//取得当前日期
TimeToStr(&CurrentTime)//取得当前时间
// 数据处理 CurrentTime.DateString 格式"07-05-26"
// CurrentTime.TimeString 格式"000000"
*/
刚写的程序,可以通过S0(功能键),S1(加键),S2(减键)设定初值,倒数为00-00-00时蜂鸣器响,通过八位LED显示;#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}//八位位选码表
uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}//段选码表
sbit s0=P3^3 //设置功能键
sbit s1=P3^5 //设置‘加’键
sbit s2=P3^6 //设置‘减’键
sbit beep=P3^7//设置蜂鸣器
void delay(uint z)
void display()
void init()
void keyscan()
uchar shi,fen,miao,nam
void main()
{
init()
while(1)
{
display()
keyscan()
beep=1
if (miao==0&&fen==0&&shi==0)//当倒数计时为00-00-00时蜂鸣器响
{
beep=0
TR0=0
}
}
}
void delay(uint z)
{
uint i,j
for (i=zi>0i--)
for (j=110j>0j--)
}
void display() //我这里P2为位选,P0为段选,显示格式为 时-分-秒
{
uchar s,g
s=shi/10
g=shi%10
P2=wei[7]
P0=duan[s]
delay(1)
P2=wei[6]
P0=duan[g]
delay(1)
P2=wei[5]
P0=0xbf
delay(1)
s=fen/10
g=fen%10
P2=wei[4]
P0=duan[s]
delay(1)
P2=wei[3]
P0=duan[g]
delay(1)
P2=wei[2]
P0=0xbf
delay(1)
s=miao/10
g=miao%10
P2=wei[1]
P0=duan[s]
delay(1)
P2=wei[0]
P0=duan[g]
delay(1)
}
void init()
{
miao=10//设定初值为 00-01-10
fen=1
shi=0
EA=1
ET0=1
TMOD=0X01
TH0=(65536-50000)/256
TL0=(65536-50000)%256
TR0=1
}
void timer() interrupt 1
{
TH0=(65536-50000)/256
TL0=(65536-50000)%256
nam++
if (nam==20)
{
nam=0
if (miao==0)
{
miao=60
if (fen==0)
{
fen=60
if (shi==0)
shi=24
shi--
}
fen--
}
miao--
}
}
void keyscan()
{
uchar key,num
if (s0==0)
{
delay(5)
if (s0==0)
{
key++
if (key==4)
key=0
}
while(!s0)
display()
}
if (s1==0)
{
delay(5)
if (s1==0)
{
num=1
}
while(!s1)
display()
}
if (s2==0)
{
delay(5)
if (s2==0)
{
num=2
}
while(!s2)
display()
}
switch(key)
{
case 0:TR0=1break
case 1:TR0=0
if (num==1)
{
num=0
miao++
if (miao>=60)
miao=0
display()
}
if (num==2)
{
num=0
if (miao==0)
miao=60
miao--
display()
}
break
case 2:
if (num==1)
{
num=0
fen++
if (fen>=60)
fen=0
display()
}
if (num==2)
{
num=0
if (fen==0)
fen=60
fen--
display()
}
break
case 3:
if (num==1)
{
num=0
shi++
if (shi>=24)
shi=0
display()
}
if (num==2)
{
num=0
if (shi==0)
shi=24
shi--
display()
}
break
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)