《atmega16单片机lcd显示时间及protues(正确).doc》由会员分享,可在线阅读,更多相关《atmega16单片机lcd显示时间及protues(正确).doc(4页珍藏版)》请在金锄头文库上搜索。
1、#include#include#include#define uint unsigned int#define uchar unsigned char/*/sbit LCDRS=P20;/LCD数据/命令选择端sbit LCDRW=P21;sbit LCDEN=P22;/LCD使能新号端/*/sbit IO=P10;/1302数据线sbit SCLK=P11;/1302时钟线sbit RST=P12;/复位uint num=0;uchar Date=Date: 2000-00-00 ;uchar Time=Time: *00:00-00* ;uchar date_time7;/从ds1302
2、读取的当前日期时间/*/void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);/*/void write_ds1302(uchar dat)uchar i;for(i=0;i= 1;/*/uchar read_ds1302()uchar i,b=0x00;for(i=0;i8;i+)b |= _crol_(uchar)IO,i);SCLK=1;delay(1);SCLK=0;return b/16*10+b%16;/与BCD码转换/*/uchar read_data(uchar addr)uchar dat;RST=0;SCLK=0
3、;RST=1;write_ds1302(addr);dat=read_ds1302();SCLK=1;RST=0;return dat;/*/void read_date_time()uchar i,addr=0x81;for(i=0;i7;i+)date_timei=read_data(addr);addr+=2;/*/void lcd_write_com(uchar com)LCDRS=0;/RS为0时,写指令,RS为1时,写数据P0=com;delay(5);LCDEN=1;delay(5);LCDEN=0;/*/void lcd_write_data(uchar dat)LCDRS=1
4、;P0=dat;delay(5);LCDEN=1;delay(5);LCDEN=0;/*/void lcd_init()LCDEN=0;lcd_write_com(0x38);/LCD显示模式设置lcd_write_com(0x0c);/LCD显示开/关及光标设置lcd_write_com(0x06);/当写一个字符后地址指针加1,且光标加1lcd_write_com(0x01);/显示清屏/*/void set_lcd_pos(uchar p)lcd_write_com(p | 0x80);/*/void lcd_print(uchar p,uchar *s)set_lcd_pos(p);f
5、or(num=0;num16;num+)lcd_write_data(snum);delay(10);/*/void format_datetime(uchar d,uchar *p)p0=d/10+0;p1=d%10+0;/*/void main()LCDRW=0;lcd_init();while(1)read_date_time();format_datetime(date_time6,Date+8);/年月日format_datetime(date_time4,Date+11);format_datetime(date_time3,Date+14);format_datetime(date_time2,Time+7);/时间转换format_datetime(date_time1,Time+10);format_datetime(date_time0,Time+13);lcd_print(0x00,Date);lcd_print(0x40,Time);