c8051f020单片机数字时钟Word文档下载推荐.docx
《c8051f020单片机数字时钟Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《c8051f020单片机数字时钟Word文档下载推荐.docx(20页珍藏版)》请在冰点文库上搜索。
{
inti。
OSCXCN=0x67。
//外部晶振22.1184MHz
for(i=0。
i<
256。
i++>
。
while(!
(OSCXCN&
0x80>
>
//等待外部晶振稳定
OSCICN=0x88。
//选择外部晶振作系统时钟源,允许时钟丢失检测
}
//*****端口初始化*****//
voidPORT_Init(void>
XBR0=0x07。
//允许SMBus、SPI0和UART0
XBR1=0x00。
XBR2=0x44。
//使能交叉开关和弱上拉
//*****定时器T0初始化*****//
voidTimer0_Init(void>
CKCON|=0x8。
//T0按系统时钟频率计数
TMOD|=0x1。
//T0方式1
Count1ms=10。
TR0=0。
//停止T0
TH0=(-SYSCLK/1000>
>
8。
//定时1ms的时间常数
TL0=-SYSCLK/1000。
TR0=1。
//启动T0
IE|=0x2。
//开T0中断
//*****定时器T0中断服务程序<
每隔1ms中断1次)*****//
voidTimer0_ISR(void>
interrupt1
TH0=(-SYSCLK/1000>
//重新装入初值
if(Count1ms>
Count1ms--。
//定时时间减1
//******延时Tμs<
软件实现)******//
while(T>
{
_nop_(>
--T。
}
//******延时Tms<
定时器T0实现)******//
Count1ms=T。
while(Count1ms>
//在T0中断服务程序中减1
//*****延时Ts<
Delay1ms(200>
T--。
voidSend7279Byte(unsignedcharch>
{
chari。
SELECT7279。
//置CS低电平
Delay1us(50>
//延时50μ
i<
8。
i++>
{
if(ch&
0x80>
//输出1位到HD7279A的DATA端
Set7279DAT。
else
Clr7279DAT。
Set7279CLK。
ch=ch<
<
1。
//待发数据左移
Delay1us(8>
Clr7279CLK。
Clr7279DAT。
//发送完毕,DATA端置低,返回
//*****仿真I2C总线时序接收一字节*****//
unsignedcharReceive7279Byte(void>
unsignedchari,ch=0。
Set7279DAT。
//DATA端置为高电平<
输入状态)
//接收数据左移1位
if(HD7279_DAT>
ch+=1。
//接收1位数据
//接收完毕,DATA端重新置成低电平(输出状态>
returnch。
//*****让第No(0到5>
位LED闪烁*****//
voidFlashLED(unsignedcharNo>
unsignedchari。
Send7279Byte(0x88>
//发闪烁指令
i=0x01。
while(No>
//将1移到第No位
i=i<
No--。
Send7279Byte(~i>
//0闪烁、1不闪烁
NOSELECT7279。
}
//******HD7279A左移命令*****//
voidMoveLeft(void>
Send7279Byte(0xA1>
//发左移指令
//******采用不译码方式显示时数字0到F的段码******//
unsignedcharcodeBdSeg[]={
0x7e,0x30,0x6d,0x79,//0123
0x33,0x5b,0x5f,0x70,//4567
0x7f,0x7b,0x77,0x1f,//89ab
0x4e,0x3d,0x4f,0x47,//cdef
0x00,0x01}。
//*显示指针DispBuf所指6个单元数据,点亮第ShowDot<
1到6)个LED的小数点*//
voidDispLED(unsignedchar*DispBuf,unsignedcharShowDot>
RTCrpUDGiT
chari,ch。
ShowDot--。
6。
ch=DispBuf[i]。
//取一字符
if((ch>
='
a'
&
&
(ch<
f'
//转换成数字
{
ch-='
ch+=0xa。
}
A'
F'
Send7279Byte(0x90+5-i>
//用不译码方式显示在第i位
if(ch=='
'
//发送不译码方式显示命令的第2字节
Send7279Byte(0x00>
if(ch=='
-'
Send7279Byte(0x01>
else
{
if(ShowDot==i>
//查表显示,同时点亮小数点
Send7279Byte(0x80|BdSeg[ch&
0x0f]>
else
Send7279Byte(BdSeg[ch&
}
//置CS高电平
//*****读取按键值*****//
unsignedcharGetKeyValue(void>
unsignedcharKeyValue。
if(CPT1CN&
0x40>
return-1。
//if(P17==1>
//无键按下5PCzVD7HxA
Send7279Byte(0x15>
//发读键盘命令
KeyValue=Receive7279Byte(>
returnKeyValue。
//***等待按键释放,用C8051F020的比较器硬件检测,也可以用软件延时实现***//
voidWaitKeyOff(void>
(CPT1CN&
//*****显示任意长整型数据*****//
voidDispValue(unsignedlongxx>
unsignedcharbuf[6]。
buf[0]=(xx%1000000>
/100000。
//最高位
buf[1]=(xx%100000>
/10000。
buf[2]=(xx%10000>
/1000。
buf[3]=(xx%1000>
/100。
buf[4]=(xx%100>
/10。
buf[5]=(xx%10>
//最低位
DispLED(buf,0>
//调用显示函数,所有小数点都不点亮
//**从键盘读取6位数据,用于存储时间,格式是HHMMSS,所读数据转换成长整型**//
unsignedlongInputNum(void>
unsignedlongNum=0。
unsignedchari=0,KeyValue。
DispLED("
-"
0>
//输入提示
FlashLED(0>
//第一位闪烁
Delay1s(1>
while(1>
KeyValue=GetKeyValue(>
if(i==6>
//6位数据输入完,返回
FlashLED(8>
//关闪烁
returnNum。
if((KeyValue>
=0>
(KeyValue<
=9>
//只接收十进制的0到9
i++。
Send7279Byte(0xC8>
//发送键码值,按方式1译码下载显示
Send7279Byte(KeyValue>
MoveLeft(>
//显示并左移一位,仍使低位显示并闪烁。
Send7279Byte(0x90>
Send7279Byte(0x01>
NOSELECT7279。
WaitKeyOff(>
Num*=10。
//转换成十进制
Num+=KeyValue。
Delay1ms(500>
}
voidDispTime(void>
Send7279Byte(0x85>
//在最高位<
第5位)LED显示小时的高位
Send7279Byte(HourH>
Send7279Byte(0x84>
//在第4位LED显示小时的低位
Send7279Byte(HourL>
Send7279Byte(0x83>
//在第3位LED显示分钟的高位
Send7279Byte(MinuteH>
Send7279Byte(0x82>
//在第2位LED显示分钟的低位
Send7279Byte(MinuteL>
Send7279Byte(0x81>
//在第1位LED显示秒钟的高位
Send7279Byte(SecondH>
Send7279Byte(0x80>
//在第0位LED显示秒钟的低位
Send7279Byte(SecondL>
voidmDispTime(void>
Send7279Byte(mHourH>
Send7279Byte(mHourL>
Send7279Byte(mMinuteH>
Send7279Byte(mMinuteL>
Send7279Byte(mSecondH>
Send7279Byte(mSecondL>
voidset_time(>
unsignedlongCurrentTime=0。
do{//从键盘输入时间,格式HHMMSS
CurrentTime=InputNum(>
DispValue(CurrentTime>
NowTime[0]=(unsignedchar>
((CurrentTime/100000>
4>
//时jLBHrnAILg
CurrentTime=CurrentTime%100000。
NowTime[0]=NowTime[0]|(CurrentTime/10000>
CurrentTime=CurrentTime%10000。
NowTime[1]=(unsignedchar>
((CurrentTime/1000>
//分xHAQX74J0X
CurrentTime=CurrentTime%1000。
NowTime[1]=NowTime[1]|(CurrentTime/100>
CurrentTime=CurrentTime%100。
NowTime[2]=(unsignedchar>
((CurrentTime/10>
//秒LDAYtRyKfE
CurrentTime=CurrentTime%10。
NowTime[2]=NowTime[2]|CurrentTime。
}while((NowTime[0]>
0x24>
|(NowTime[1]>
0x59>
|(NowTime[2]>
Zzz6ZB2Ltk
HourH=NowTime[0]/16。
//转换成10进制
HourL=NowTime[0]%16。
MinuteH=NowTime[1]/16。
MinuteL=NowTime[1]%16。
SecondH=NowTime[2]/16。
SecondL=NowTime[2]%16。
unsignedlongset_InputNum(void>
intc=0。
while(1>
if(SecondL>
9>
{SecondH++,SecondL=0。
if(SecondH>
5>
{SecondH=0,MinuteL++。
if(MinuteL>
{MinuteH++,MinuteL=0。
if(MinuteH>
{MinuteH=0,HourL++。
if(HourL>
{HourH++。
HourL=0。
if(HourH>
2>
HourH=0。
if((HourH==2>
(HourL>
3>
{HourH=0。
c++。
Delay1ms(500>
if(c%2==0>
SecondL++。
voidset_ring(>
CurrentTime=set_InputNum(>
//时dvzfvkwMI1
//分rqyn14ZNXI
//秒EmxvxOtOco
SixE2yXPq5
setHourH=NowTime[0]/16。
setHourL=NowTime[0]%16。
setMinuteH=NowTime[1]/16。
setMinuteL=NowTime[1]%16。
setSecondH=NowTime[2]/16。
setSecondL=NowTime[2]%16。
voidm(>
unsignedcharcontrol_keyvalue。
intma=0。
inta=1。
//时6ewMyirQFL
//分kavU42VRUs
//秒y6v3ALoS89
mHourH=NowTime[0]/16。
mHourL=NowTime[0]%16。
mMinuteH=NowTime[1]/16。
mMinuteL=NowTime[1]%16。
mSecondH=NowTime[2]/16。
mSecondL=NowTime[2]%16。
ma++。
if(ma%5==0>
mSecondL++。
if(mSecondL>
{mSecondH++,mSecondL=0。
if(mSecondH>
{mSecondH=0,mMinuteL++。
if(mMinuteL>
{mMinuteH++,mMinuteL=0。
if(mMinuteH>
{mMinuteH=0,mHourL++。
if(mHourL>
{mHourH++。
mHourL=0。
if(mHourH>
mHourH=0。
if((mHourH==2>
(mHourL>
{mHourH=0。