本帖最后由 渴死的鱼丶 于 2015-11-27 09:55 编辑
#include"reg51.h"#include"1302.h"#include"1602.h"#define uchar unsigned char #define uint unsigned intuchar shi,fen,miao,ri,yue,nian,zhou;void write_sfm(uchar addr,uchar date) //时分秒分离个时位{ uchar shi,ge; shi=date/10; ge=date%10; write_cmd(0xc0+addr); write_data(0x30+shi); write_data(0x30+ge);}void write_nyr(uchar addr,uchar date) //年月日分离个时位{ uchar shi,ge; shi=date/10; ge=date%10; write_cmd(0x80+addr); write_data(0x30+shi); write_data(0x30+ge);}void display() //往1602液晶显示数据{ uchar d; write_DS1302(0x8e,0x00);
d=read_DS1302(0x81); miao=(d/16)*10+d%16; write_sfm(10,miao);
d=read_DS1302(0x83); fen=(d/16)*10+d%16; write_sfm(7,fen);
d=read_DS1302(0x85); shi=(d/16)*10+d%16; write_sfm(4,shi);
d=read_DS1302(0x87); ri=(d/16)*10+d%16; write_nyr(10,ri);
d=read_DS1302(0x89); yue=(d/16)*10+d%16; write_nyr(7,yue);
d=read_DS1302(0x8b); zhou=(d/16)*10+d%16; write_nyr(14,zhou);
d=read_DS1302(0x8d); nian=(d/16)*10+d%16; write_nyr(4,nian);
write_DS1302(0x8e,0x80);}void main(){
lcd_init(); init_DS1302(); while(1) { display(); }}
1302驱动#ifndef _1302_H_#define _1302_H_#define uchar unsigned char#define uint unsigned intsbit SCK=P1^7;sbit IO=P2^3;sbit RST=P1^3;sbit ACC0=ACC^0;sbit ACC7=ACC^7;
void write_bety(uchar a) //写入一个字节{ uchar i; ACC=a; SCK=0; for(i=8;i>0;i--) { IO=ACC0; SCK=1; SCK=0; ACC=ACC>>1; }}uchar read_bety() //读出一个字节{ uchar i; SCK=0; for(i=8;i>0;i--) { ACC=ACC>>1; ACC7=IO; SCK=1; SCK=0; } return (ACC);} uchar write_DS1302(uchar d1,uchar d2){ RST=0; SCK=0; RST=1; write_bety(d1); write_bety(d2); SCK=1; RST=0;}uchar read_DS1302(uchar cg){ uchar f; RST=0; SCK=0; RST=1; write_bety(cg); f=read_bety(); SCK=1; RST=0; return(f);}
void init_DS1302(){ write_DS1302(0x8e,0x00); //wp=0; 去除写保护
write_DS1302(0x80,0x00); write_DS1302(0x82,0x00); write_DS1302(0x84,0x19); write_DS1302(0x86,0x25); write_DS1302(0x88,0x11); write_DS1302(0x8a,0x03); write_DS1302(0x8c,0x15);
write_DS1302(0x8e,0x80); //wp=1 加写保护}#endif
1602驱动
#ifndef _1602_H_#define _1602_H_#define uchar unsigned char#define uint unsigned int#define DATA P0sbit rs=P2^0;sbit rw=P2^1;sbit en=P1^2;uchar code table[]=" 20 - - ";uchar code table1[]=" : : ";void delay(uint num) //延迟函数 num毫秒{ uint x,y; for(x=num;x>0;x--) for(y=110;y>0;y--);}void write_cmd(uchar cmd) //写指令函数{ rs=0; rw=0; DATA=cmd; delay(5); en=1; delay(5); en=0;}
void write_data(uchar dat) //写数据函数{ rs=1; rw=0; DATA=dat; delay(5); en=1; delay(5); en=0;}void lcd_init(void) //初始化液晶{ uchar i; write_cmd(0x02); // 归位 write_cmd(0x06); // 设置输入方式 write_cmd(0x0c); //控制显示开关 write_cmd(0x38); //功能设置 write_cmd(0x01); //清屏
write_cmd(0x80); for(i=0;i<16;i++) { write_data(table
); delay(5); } write_cmd(0xc0); for(i=0;i<16;i++) { write_data(table1); delay(5); } }#endif
-
开发板
-
-
测试程序.rar
下载积分: 积分 -1 分
33.02 KB, 下载次数: 11, 下载积分: 积分 -1 分
一周热门 更多>