想用ds1302和1602液晶做做时钟的程序,在proteus仿真,但怎么也达不到要求。编译无错,运行结果出乎意料,只显示0,而且时钟没工作。
各位大侠帮忙着分析分析,谢谢了。
/*************程序ds1302.c*****************/
#include <reg52.h> #include "ds1302.h" #include "lcd1602.h"
//uchar th[]="i am a teacher !"; void main() { uchar i; struct _time time; // struct _time *t=time; lcd_init(); ds1302_init(); set_time(ds1302_sec,5); set_time(ds1302_min,15); set_time(ds1302_hr,12); set_time(ds1302_date,21); set_time(ds1302_mon,12); set_time(ds1302_day,6); set_time(ds1302_year,12); while(1) { get_time(&time); date_to_format(&time); time_to_format(&time); lcd_write_cmd(0x80+0); for(i=0;i<9;i++) { lcd_write_dat(time.DateFormat); //lcd_write_dat(th); } lcd_write_cmd(0x80+0x40); for(i=0;i<9;i++) { lcd_write_dat(time.TimeFormat); //lcd_write_dat(th); } //delay_ms(100); } }
/*********************程序ds1302.h************************/
#include <reg52.h> #define uchar unsigned char #define uint unsigned int
#define ds1302_sec 0x80 #define ds1302_min 0x82 #define ds1302_hr 0x84 #define ds1302_date 0x86 #define ds1302_mon 0x88 #define ds1302_day 0x8a #define ds1302_year 0x8c
sbit ds1302_rst=P1^5; sbit ds1302_sclk=P1^6; sbit ds1302_io=P1^7; sbit ACC7=ACC^7; sbit ACC0=ACC^0;
struct _time { unsigned char sec; unsigned char min; unsigned char hr; unsigned char date; unsigned char mon; unsigned char day; unsigned char year; unsigned char DateFormat[9]; unsigned char TimeFormat[9]; }; //定义的时间类型
void ds1302_write(uchar cmd,uchar dat) //往指定地址写入数据 { uchar i; ds1302_sclk=0; ds1302_rst=1; ACC=cmd; for(i=0;i<8;i++) { ds1302_io=ACC0; ds1302_sclk=1; ds1302_sclk=0; ACC=ACC>>1; } ACC=dat; for(i=0;i<8;i++) { ds1302_io=ACC0; ds1302_sclk=1; ds1302_sclk=0; ACC=ACC>>1; } ds1302_sclk=1; ds1302_rst=0; }
uchar ds1302_read(uchar cmd) //读出指定地址的数据 { uchar i; ds1302_sclk=0; ds1302_rst=1; ACC=cmd; for(i=0;i<8;i++) { ds1302_sclk=0; ds1302_io=ACC0; ds1302_sclk=1; // ds1302_sclk=0; ACC=ACC>>1; } for(i=0;i<8;i++) { // ACC7=ds1302_io; ACC=ACC>>1; ds1302_sclk=1; ds1302_sclk=0; ACC7=ds1302_io; } ds1302_sclk=1; ds1302_rst=0; return ACC; }
void ds1302_SetProtect(bit flag) //设置写保护 { if(flag) ds1302_write(0x8E,0x80); else ds1302_write(0x8E,0x00); }
void set_time(uchar add,uchar var) //设置时间 { ds1302_SetProtect(0); ds1302_write(add,((var/10)<<4|(var%10))); //转成BCD码 }
void get_time(struct _time *t) //读取时钟寄存器放入time结构体 { uchar var; var=ds1302_read(ds1302_sec); t->sec=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_min); t->min=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_hr); t->hr=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_date); t->date=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_mon); t->mon=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_day); t->day=(var&0x70>>4)*10+(var&0x0f); var=ds1302_read(ds1302_year); t->year=(var&0x70>>4)*10+(var&0x0f); }
void date_to_format(struct _time *t) //将日期数据转化为日期格式并将数字转为字符 { t->DateFormat[0] = t->year/10+'0'; t->DateFormat[1] = t->year%10+'0'; t->DateFormat[2] ='-'; t->DateFormat[3] = t->mon/10+'0'; t->DateFormat[4] = t->mon%10+'0'; t->DateFormat[5] = '-'; t->DateFormat[6] = t->date/10+'0'; t->DateFormat[7] = t->date%10+'0'; t->DateFormat[8] = '\0'; }
void time_to_format(struct _time *t) //将时间数据转化为时间格式并将数字转为字符 { t->TimeFormat[0] = t->hr/10+'0'; t->TimeFormat[1] = t->hr%10+'0'; t->TimeFormat[2] =':'; t->TimeFormat[3] = t->min/10+'0'; t->TimeFormat[4] = t->min%10+'0'; t->TimeFormat[5] = ':'; t->TimeFormat[6] = t->sec/10+'0'; t->TimeFormat[7] = t->sec%10+'0'; t->TimeFormat[8] = '\0'; } //初始化启动时钟 void ds1302_init() { uchar second=ds1302_read(ds1302_sec); if(second&0x80) set_time(ds1302_sec,0); }
/****************程序lcd1602.h*************************/
#include <reg52.h> #define uchar unsigned char #define uint unsigned int
sbit lcd_rs=P2^0; sbit lcd_rw=P2^1; sbit lcd_e=P2^2;
sfr lcd_d=0x80;
void delay_ms(uint ms) { uint x,y; for(x=ms;x>0;x--) for(y=120;y>0;y--); }
void lcd_write_cmd(uchar cmd) { lcd_rs=0; lcd_rw=0; lcd_e=0; lcd_d=cmd; lcd_e=1; delay_ms(1); lcd_e=0; }
void lcd_write_dat(uchar dat) { lcd_rs=1; lcd_rw=0; lcd_e=0; lcd_d=dat; lcd_e=1; delay_ms(1); lcd_e=0; }
void lcd_init() { lcd_write_cmd(0x38); delay_ms(1); lcd_write_cmd(0x01); delay_ms(1); lcd_write_cmd(0x0c); delay_ms(1); lcd_write_cmd(0x06); delay_ms(1); } |