硬件设计
(末尾附文件)
功能:
1、8位数码管显示当前时间。
2、使用DS1302获得时钟信号,能够进行时钟设置
3、能够设置10组打铃时间,并具有临时打铃功能,打铃的时长为5S钟
4、使用1602液晶屏幕显示。
仿真图:
程序设计
#include <all.h>u8 LCD1602_Cursor_address=0;//光标标记//uchar code degree[]={0x16,0x09,0x08,0x08,0x08,0x09,0x06,0x00}; //摄氏度 符号bit lcd_busy_test(void) //忙状态监测{bit result;RS=0; RW=1;EN=1;_nop_();_nop_();_nop_();_nop_();result = (bit)(DB&0x80);EN=0;return result;}void lcd_write_cmd (u8 cmd)//写命令{while (lcd_busy_test());RS=0;RW=0;EN=0;_nop_();_nop_();DB=cmd ;_nop_();_nop_();_nop_();_nop_();EN=1;_nop_();_nop_();_nop_();_nop_();EN=0;}void lcd_position (u8 pos)//显示起始位置{if(pos<16)lcd_write_cmd (pos|0x80);elselcd_write_cmd (((pos)-16)|0xC0);}void lcd_write_data (u8 dat)//写数据{while (lcd_busy_test());RS=1;RW=0;EN=0;_nop_();_nop_();DB=dat ;_nop_();_nop_();_nop_();_nop_();EN=1;_nop_();_nop_();_nop_();_nop_();EN=0;}//初始化void lcd_start (void){lcd_write_cmd (0x01);delay_ms(1);lcd_write_cmd (0x02);delay_ms(1);lcd_write_cmd (0x06);delay_ms(1);lcd_write_cmd (0x0c);delay_ms(1);lcd_write_cmd (0x14);delay_ms(1);lcd_write_cmd (0x38);delay_ms(1);}void lcd_CLS(){data u8 i;lcd_position(0x80);for(i=0;i<16;i++)lcd_write_data(' ');lcd_position(0xc0);for(i=0;i<16;i++)lcd_write_data(' ');}void LCD_1602_Cursor_address()//光标状态{switch(LCD1602_Cursor_address){case 0:lcd_write_cmd(0x0c);break;//不显示//(先开启 0x0f,后地址,上0x8,下0xc)case 1:lcd_write_cmd(0x0f);lcd_write_cmd(0xcb);break;//时间秒case 2:lcd_write_cmd(0x0f);lcd_write_cmd(0xc8);break;//时间分case 3:lcd_write_cmd(0x0f);lcd_write_cmd(0xc5);break;//时间时case 4:lcd_write_cmd(0x0f);lcd_write_cmd(0x8c);break;//星期case 5:lcd_write_cmd(0x0f);lcd_write_cmd(0x8a);break;//时间日case 6:lcd_write_cmd(0x0f);lcd_write_cmd(0x87);break;//时间月case 7:lcd_write_cmd(0x0f);lcd_write_cmd(0x84);break;//时间年}}void lcd_printf_Ds1302()//正常模式显示{lcd_position(3);lcd_write_data(Time[6]/16%16+'0');//时间年显示lcd_write_data(Time[6]%16+'0');lcd_write_data('/');lcd_write_data(Time[5]/16%16+'0');//时间月显示lcd_write_data(Time[5]%16+'0');lcd_write_data('/');lcd_write_data(Time[4]/16%16+'0');//时间日显示lcd_write_data(Time[4]%16+'0');lcd_write_data('/');lcd_write_data(Time[3]%16+'0');//星期显示lcd_position(20);lcd_write_data(Time[2]/16%16+'0');//时间时显示lcd_write_data(Time[2]%16+'0');lcd_write_data('/');lcd_write_data(Time[1]/16%16+'0');//时间分显示lcd_write_data(Time[1]%16+'0');lcd_write_data('/');lcd_write_data(Time[0]/16%16+'0');//时间秒显示lcd_write_data(Time[0]%16+'0');}void lcd_printf_Ring()//铃声调整数据显示{lcd_position(20);lcd_write_data(Ring_Data[Ring][2]/16%16+'0');//时间时显示lcd_write_data(Ring_Data[Ring][2]%16+'0');lcd_write_data('/');lcd_write_data(Ring_Data[Ring][1]/16%16+'0');//时间分显示lcd_write_data(Ring_Data[Ring][1]%16+'0');lcd_write_data('/');lcd_write_data(Ring_Data[Ring][0]/16%16+'0');//时间秒显示lcd_write_data(Ring_Data[Ring][0]%16+'0');}void Alarm_Clock()//铃声调整数据处理{if(Key4x4A=='+'){Key4x4_tt=0;Key4x4A=0;Ring_Data[Ring][LCD1602_Cursor_address-1]++;if((Ring_Data[Ring][LCD1602_Cursor_address-1]&0x0f)>9) //十进制转BCD码(Ring_Data[Ring][LCD1602_Cursor_address-1])+=6;if(Ring_Data[Ring][0]>0x59)//数据限制Ring_Data[Ring][0]=0x00;if(Ring_Data[Ring][1]>0x59)//数据限制Ring_Data[Ring][1]=0x00;if(Ring_Data[Ring][2]>0x23)//数据限制Ring_Data[Ring][2]=0x00;lcd_printf_Ring();//铃声调整数据刷新}if(Key4x4A=='-'){Key4x4_tt=0;Key4x4A=0;if(Ring_Data[Ring][LCD1602_Cursor_address-1]>0)//数据限制Ring_Data[Ring][LCD1602_Cursor_address-1]--;if((Ring_Data[Ring][LCD1602_Cursor_address-1]&0x0f)>9)//十进制转BCD码(Ring_Data[Ring][LCD1602_Cursor_address-1])-=6;lcd_printf_Ring();//铃声调整数据刷新}}void Time_Settings()//时间调整数据处理{if(Key4x4A=='+'){Key4x4_tt=0;Key4x4A=0;Time[LCD1602_Cursor_address-1]++;if((Time[LCD1602_Cursor_address-1]&0x0f)>9)//十进制转BCD码(Time[LCD1602_Cursor_address-1])+=6;if(Time[0]>0x59)//数据限制Time[0]=0x00;if(Time[1]>0x59)//数据限制Time[1]=0x00;if(Time[2]>0x23)//数据限制Time[2]=0x00;if(Time[3]>0x07)//数据限制Time[3]=0x07;if(Time[4]>0x31)//数据限制Time[4]=0x00;if(Time[5]>0x12)//数据限制Time[5]=0x00;if(Time[6]>0x99)//数据限制Time[6]=0x00;lcd_printf_Ds1302();//时间调整数据刷新}
.
文件仅供参考:
链接:/s/1yUigOtG7_7a2sxrYD_KB-Q
提取码:tviy
.