lcd1602显示实时时钟及模块化编程.docx

上传人:b****1 文档编号:1273222 上传时间:2023-04-30 格式:DOCX 页数:18 大小:232.54KB
下载 相关 举报
lcd1602显示实时时钟及模块化编程.docx_第1页
第1页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第2页
第2页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第3页
第3页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第4页
第4页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第5页
第5页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第6页
第6页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第7页
第7页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第8页
第8页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第9页
第9页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第10页
第10页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第11页
第11页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第12页
第12页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第13页
第13页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第14页
第14页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第15页
第15页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第16页
第16页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第17页
第17页 / 共18页
lcd1602显示实时时钟及模块化编程.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

lcd1602显示实时时钟及模块化编程.docx

《lcd1602显示实时时钟及模块化编程.docx》由会员分享,可在线阅读,更多相关《lcd1602显示实时时钟及模块化编程.docx(18页珍藏版)》请在冰点文库上搜索。

lcd1602显示实时时钟及模块化编程.docx

lcd1602显示实时时钟及模块化编程

本程序是用lcd1602显示实时时钟

Ds1302

Ds1302单字节读写顺序

程序如下:

#include

#include

#defineuintunsignedint

#defineucharunsignedchar

sbitsclk=P3^6;

sbitrst=P3^5;

sbitdsio=P3^4;

sbitrs=P2^6;

sbitrw=P2^5;

sbite=P2^7;

ucharbusy1;

ucharcodewrite_addr[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};

ucharcoderead_addr[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

uchartime[7]={0,0x49,0x14,0x22,0x02,0x01,0x16};

ucharDisplayData[8];

voiddelay(uintn)

{

ucharm;

while(--n)

{

for(m=0;m<40;m++);

}

}

unsignedcharbusy()

{

unsignedcharf;

rs=0;

rw=1;

e=1;

delay

(1);

f=P0;

e=0;

busy1=f&0x80;

returnbusy1;

}

voidlcd_write_com(unsignedcharcom1)//写命令

{

while(busy()==0x80);

rs=0;

rw=0;

e=0;

P0=com1;

delay(5);

e=1;

delay(5);

e=0;

}

voidlcdreset()//初始化

{

delay(10);

lcd_write_com(0x38);//设置显示模式

lcd_write_com(0x0c);

lcd_write_com(0x06);

lcd_write_com(0x01);

lcd_write_com(0x80);

}

voidlcd_write_data(unsignedchardata1)//写数据

{

while(busy()==0x80);

rs=1;

rw=0;

e=0;

P0=data1;

delay(5);

e=1;

delay(5);

e=0;

}

voidds1302_write(ucharaddr,uchardat)

{

ucharn;

sclk=0;

_nop_();

rst=1;

_nop_();

for(n=0;n<8;n++)

{

dsio=addr&0x01;

addr>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

for(n=0;n<8;n++)

{

dsio=dat&0x01;

dat>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

rst=0;

_nop_();

}

uchards1302_read(ucharaddr)

{

ucharn,dat,dat1;

sclk=0;

_nop_();

rst=1;

_nop_();

for(n=0;n<8;n++)

{

dsio=addr&0x01;

addr>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

_nop_();

for(n=0;n<8;n++)

{

dat1=dsio;

//dat=dat>>1;

//dat|=dat1<<7;

dat=(dat1<<7)|(dat>>1);

sclk=1;

_nop_();

sclk=0;

_nop_();

}

rst=0;

_nop_();

sclk=1;

_nop_();

dsio=0;

_nop_();

dsio=1;

_nop_();

returndat;

}

voidds1302_init()

{

ucharn;

ds1302_write(0x8e,0x00);

for(n=0;n<7;n++)

{

ds1302_write(write_addr[n],time[n]);

}

ds1302_write(0x8e,0x80);

}

voidds1302_readtime()

{

ucharn;

for(n=0;n<7;n++)

{

time[n]=ds1302_read(read_addr[n]);

}

}

voiddisplay()

{

lcd_write_com(0x80+0x40);

lcd_write_data('0'+time[2]/16);

lcd_write_data('0'+(time[2]&0x0f));

lcd_write_data('-');

lcd_write_data('0'+time[1]/16);

lcd_write_data('0'+(time[1]&0x0f));

lcd_write_data('-');

lcd_write_data('0'+time[0]/16);

lcd_write_data('0'+(time[0]&0x0f));

lcd_write_com(0x80);

lcd_write_data('2');

lcd_write_data('0');

lcd_write_data('0'+time[6]/16);

lcd_write_data('0'+(time[6]&0x0f));

lcd_write_data('-');

lcd_write_data('0'+time[4]/16);

lcd_write_data('0'+(time[4]&0x0f));

lcd_write_data('-');

lcd_write_data('0'+time[3]/16);

lcd_write_data('0'+(time[3]&0x0f));

}

voidmain()

{

//ds1302_init();//使用这条语句就可以显示设定的时间,不时用就会使用实时时钟。

_nop_();

lcdreset();

while

(1)

{

ds1302_readtime();

display();

}

}

模块化编程

Ds1302.h

#ifndef__DS1302_H__

#define__DS1302_H__

#include

#include

#defineucharunsignedchar

#defineuintunsignedint

uchardatatime[7],write_addr[7];

sbitio=P3^4;

sbitsclk=P3^6;

sbitrst=P3^5;

voidds1302_write(ucharaddr,uchardat);

uchards1302_read(ucharaddr);

voidds1302_init();

voidds1302_readtime();

#endif

Ds1302.c

#include

uchardatatime[7]={0,0x10,0x16,0x21,0x02,0x07,0x16};//存取数据

voidds1302_write(ucharaddr,uchardat)

{

ucharn;

sclk=0;

_nop_();

rst=1;

_nop_();

for(n=0;n<8;n++)

{

io=addr&0x01;

addr>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

for(n=0;n<8;n++)

{

io=dat&0x01;

dat>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

rst=0;

_nop_();

}

uchards1302_read(ucharaddr)

{

ucharn,dat,dat1;

sclk=0;

_nop_();

rst=1;

_nop_();

for(n=0;n<8;n++)

{

io=addr&0x01;

addr>>=1;

sclk=1;

_nop_();

sclk=0;

_nop_();

}

_nop_();

for(n=0;n<8;n++)

{

dat1=io;

//dat=dat>>1;

//dat|=dat1<<7;

dat=(dat1<<7)|(dat>>1);

sclk=1;

_nop_();

sclk=0;

_nop_();

}

rst=0;

_nop_();

sclk=1;

_nop_();

io=0;

_nop_();

io=1;

_nop_();

returndat;

}

voidds1302_init()

{

ucharn,addr=0x80;

ds1302_write(0x8e,0x00);

for(n=0;n<7;n++)

{

ds1302_write(addr,datatime[n]);

addr+=2;

}

ds1302_write(0x8e,0x80);

}

voidds1302_readtime()

{

ucharn,addr=0x81;

for(n=0;n<7;n++)

{

datatime[n]=ds1302_read(addr);

addr+=2;

}

}

Lcd1602.h

#ifndef__LCD1602_H__

#define__LCD1602_H__

#include

#include

#defineucharunsignedchar

#defineuintunsignedint

sbitrs=P2^6;

sbitrw=P2^5;

sbiten=P2^7;

voiddelay(ucharms);

voidbusy_wait();

voidwrite_lcd_command(ucharcmd);

voidwrite_lcd_data(uchardat);

voidinit_lcd();

voidlcd_showstring(ucharr,ucharc,uchar*str);

#endif

Lcd1602.c

#include

codeucharddram[]={0x80,0xc0};

voiddelay(ucharms)

{

uchari;

while(ms--)

for(i=0;i<10;i++);

}

voidbusy_wait()

{

ucharlcd_status;

do

{

P0=0xff;

en=0;rs=0;rw=1;

en=1;lcd_status=P0;

en=0;

}while(lcd_status&0x80);

}

voidwrite_lcd_command(ucharcmd)

{

busy_wait();

en=0;rs=0;rw=0;

P0=cmd;

en=1;_nop_();en=0;

}

voidwrite_lcd_data(uchardat)

{

busy_wait();

en=0;rs=1;rw=0;

P0=dat;

en=1;_nop_();en=0;

}

voidinit_lcd()

{

write_lcd_command(0x38);delay

(1);

write_lcd_command(0x01);delay

(1);

write_lcd_command(0x06);delay

(1);

write_lcd_command(0x0c);delay

(1);

}

voidlcd_showstring(ucharr,ucharc,uchar*str)

{

uchari=0;

write_lcd_command(ddram[r]|c);

for(i=0;i<16;i++)

write_lcd_data(str[i]);

//for(;i<16;i++)

//write_lcd_data('');

}

Main.c

#include

#defineucharunsignedchar

#defineuintunsignedint

uchar*week[]={"sun","mon","tus","wen","thu","fri","sat"};

ucharlcd_buf_1[]="data00-00-00";

ucharlcd_buf_2[]="time00:

00:

00";

externvoidds1302_readtime();

externds1302_init();

externvoidinit_lcd();

externvoidlcd_showstring(ucharr,ucharc,uchar*str);

externuchardatatime[7];

voidformat_datatime(uchard,uchar*a)

{

*a=(d>>4)+'0';*(a+1)=(d&0x0f)+'0';

}

voidmain()

{

init_lcd();

//ds1302_init();

while

(1)

{

ds1302_readtime();

format_datatime(datatime[6],lcd_buf_1+5);

format_datatime(datatime[4],lcd_buf_1+8);

format_datatime(datatime[3],lcd_buf_1+11);

strcpy(lcd_buf_1+13,week[datatime[5]-1]);

//

format_datatime(datatime[2],lcd_buf_2+5);

format_datatime(datatime[1],lcd_buf_2+8);

format_datatime(datatime[0],lcd_buf_2+11);

lcd_showstring(0,0,lcd_buf_1);

lcd_showstring(1,0,lcd_buf_2);

}

}

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2