c8051f020单片机数字时钟Word格式.docx

上传人:b****6 文档编号:16619376 上传时间:2022-11-24 格式:DOCX 页数:16 大小:19.92KB
下载 相关 举报
c8051f020单片机数字时钟Word格式.docx_第1页
第1页 / 共16页
c8051f020单片机数字时钟Word格式.docx_第2页
第2页 / 共16页
c8051f020单片机数字时钟Word格式.docx_第3页
第3页 / 共16页
c8051f020单片机数字时钟Word格式.docx_第4页
第4页 / 共16页
c8051f020单片机数字时钟Word格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

c8051f020单片机数字时钟Word格式.docx

《c8051f020单片机数字时钟Word格式.docx》由会员分享,可在线阅读,更多相关《c8051f020单片机数字时钟Word格式.docx(16页珍藏版)》请在冰豆网上搜索。

c8051f020单片机数字时钟Word格式.docx

i<

256。

i++>

while(!

(OSCXCN&

0x80>

>

//等待外部晶振稳定

OSCICN=0x88。

//选择外部晶振作系统时钟源,允许时钟丢失检测

}

//*****端口初始化*****//

XBR0

XBR1

XBR2

=0x07//允许SMBus、SPI0和UART0

=0x00。

=0x44。

//使能交叉开关和弱上拉

voidPORT_Init(void>

//*****定时器T0初始化*****//

voidTimer0_Init(void>

 

CKCON|=0x8。

//T0按系统时钟频率计数TMOD|=0x1。

//T0方式1Count1ms=10。

TR0=0。

//停止T0

TH0=(-SYSCLK/1000>>>。

8//定时1ms的时间常数

TLO=-SYSCLK/10Q0

TR0=1。

//启动T0

IE|=0x2。

//开T0中断

//*****定时器T0中断服务程序<每隔1ms中断1次)*****//voidTimer0_ISR(void>interrupt1

8//重新装入初值

TL0=-SYSCLK/1000

if(Count1ms>Count1ms--。

//定时时间减1

//******延时T卩s<软件实现)******//voidDelay1us(unsignedcharT>

while(T>

_nop_(>。

_nop_(>。

--T。

//******延时Tms<定时器T0实现)******//voidDelay1ms(unsignedcharT>

Count1ms=T。

while(Count1ms>。

//在T0中断服务程序中减1

//*****延时Ts碇时器T0实现)******//voidDelay1s(unsignedcharT>

Delay1ms(200>。

T--。

voidSend7279Byte(unsignedcharch>

chari。

SELECT7279//置CS低电平

Delay1us(50>

//延时50卩

for(i=0。

i<

8。

if(ch&

0x80>

//输出1位到HD7279A的DATA端

Set7279DAT。

elseClr7279DAT。

Set7279CLK。

ch=ch<

<

1。

//待发数据左移

Delay1us(8>

Clr7279CLK。

Clr7279DAT。

//发送完毕,DATA端置低,返回

//*****仿真I2C总线时序接收一字节*****//

unsignedcharReceive7279Byte(void>

unsignedchari,ch=0。

//DATA端置为高电平<

输入状态)

//接收数据左移1位

if(HD7279_DAT>

ch+=1。

//接收1位数据

//接收完毕,DATA端重新置成低电平(输出状态>

returnch。

//*****让第No(0至U5>

位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>

//发左移指令

〃******采用不译码方式显示时数字o到f的段码******〃

unsignedcharcodeBdSeg[]={

ox7e,ox3o,ox6d,ox79,//o123ox33,ox5b,ox5f,ox7o,//4567ox7f,ox7b,ox77,ox1f,//89abox4e,ox3d,ox4f,ox47,//cdefoxoo,oxo1}。

//*显示指针DispBuf所指6个单元数据,点亮第ShowDot<

1到6)个LED的小数点*//voidDispLED(unsignedchar*DispBuf,unsignedcharShowDot>

{chari,ch。

ShowDot--。

i<

6。

i++>

ch=DispBuf[i]。

//取一字符

if((ch>

='

a'

&

&

(ch<

f'

//转换成数字{

ch-='

ch+=oxa。

A'

F'

{ch-='

Send7279Byte(0x90+5-i>

//用不译码方式显示在第i位

if(ch=='

'

//发送不译码方式显示命令的第2字节

Send7279Byte(oxoo>

elseif(ch=='

-'

Send7279Byte(oxo1>

else

if(ShowDot==i>

//查表显示,同时点亮小数点Send7279Byte(0x80|BdSeg[ch&

0x0f]>

elseSend7279Byte(BdSeg[ch&

NOSELECT7279//置CS高电平

//*****读取按键值*****//unsignedcharGetKeyValue(void>

unsignedcharKeyValue。

if(CPT1CN&

0x40>

return-1。

//if(P17==1>

//无键按下

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>

//在最高位<第5位)LED显示小时的高位

//在第4位LED显示小时的低位

//在第3位LED显示分钟的高位

II在第2位LED显示分钟的低位

//在第1位LED显示秒钟的高位

II在第0位LED显示秒钟的低位

II在最高位<第5位)LED显示小时的高位

II在第4位LED显示小时的低位

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>。

WaitKeyOff(>。

Num*=10。

//转换成十进制

Num+=KeyValue。

Delay1ms(500>。

}voidDispTime(void>

Send7279Byte(0x85>。

Send7279Byte(HourH>。

Send7279Byte(0x84>。

Send7279Byte(HourL>。

Send7279Byte(0x83>。

Send7279Byte(MinuteH>。

Send7279Byte(0x82>。

Send7279Byte(MinuteL>。

Send7279Byte(0x81>。

Send7279Byte(SecondH>。

Send7279Byte(0x80>。

Send7279Byte(SecondL>。

}voidmDispTime(void>

Send7279Byte(mHourH>。

Send7279Byte(mHourL>

Send7279Byte(0x83>

//在第3位LED显示分钟的高位

Send7279Byte(mMinuteH>

Send7279Byte(0x82>

//在第2位LED显示分钟的低位

Send7279Byte(mMinuteL>

Send7279Byte(0x81>

//在第1位LED显示秒钟的高位

Send7279Byte(mSecondH>

Send7279Byte(0x80>

//在第0位LED显示秒钟的低位

Send7279Byte(mSecondL>

voidset_time(>

unsignedlongCurrentTime=0。

do{//从键盘输入时间,格式HHMMSS

CurrentTime=InputNum(>

DispValue(CurrentTime>

NowTime[0]=(unsignedchar>

((CurrentTime/100000>

4>

//时CurrentTime=CurrentTime%100000。

NowTime[0]=NowTime[0]|(CurrentTime/10000>

CurrentTime=CurrentTime%10000。

NowTime[1]=(unsignedchar>

((CurrentTime/1000>

//分CurrentTime=CurrentTime%1000。

NowTime[1]=NowTime[1]|(CurrentTime/100>

CurrentTime=CurrentTime%100。

NowTime[2]=(unsignedchar>

((CurrentTime/10>

//秒

CurrentTime=CurrentTime%10。

NowTime[2]=NowTime[2]|CurrentTime。

}while((NowTime[0]>

0x24>

|(NowTime[1]>

0x59>

|(NowTime[2]>

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。

//输入提示

FlashLED(0>

while(1>

KeyValue=GetKeyValue(>

if(i==6>

//6位数据输入完,返回

FlashLED(8>

if((KeyValue>

=0>

(KeyValue<

=9>

//只接收十进制的0到9

i++。

Send7279Byte(0xC8>

//发送键码值,按方式1译码下载显示

Send7279Byte(KeyValue>

MoveLeft(>

Send7279Byte(0x90>

Send7279Byte(0x01>

WaitKeyOff(>

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>

{HourH=0。

HourL=0。

c++。

Delay1ms(500>

if(c%2==0>

SecondL++。

voidset_ring(>

CurrentTime=set_InputNum(>

//时

CurrentTime=CurrentTime%100000。

NowTime[0]=NowTime[0]|(CurrentTime/10000>

//分

CurrentTime=CurrentTime%1000。

NowTime[2]=NowTime[2]|CurrentTime。

setHourH=NowTime[0]/16。

//转换成10进制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。

CurrentTime=CurrentTime%10000。

CurrentTime=CurrentTime%100。

mHourH=NowTime[0]/16。

//转换成10进制mHourL=NowTime[0]%16。

mMinuteH=NowTime[1]/16。

mMinuteL=NowTime[1]%16。

mSecondH=NowTime[2]/16。

mSecondL=NowTime[2]%16。

Delay1ms(200>

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>

3>

{mHourH=0。

}control_keyvalue=GetKeyValue(>

if(control_keyvalue==14>

break。

if(control_keyvalue==10>

a=-a。

if(a==-1&

ma%5==0>

mSecondL--。

mDispTime(>

}if(SecondH>

HourL=0。

}SecondL++。

voidmain(void>

i

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1