单片机·实时时钟DS1302
dat>> dat>> 23,15,8,7,10} ; # define ds1302_w_addr 0x80 # define ds1302_r_addr 0x81 # endif main.c # include "main.h" void write_ds1302_byte(uint8 dat) { uint8 i ; for(i=0; i<8; i++) { SDA = dat&0x01 ; dat>>=1 ; SCK = 1 ; SCK = 0 ; } } uint8 read_ds1302_byte(void) { uint8 i, dat = 0 ; for(i=0; i<8; i++) { dat>>=1 ; if(SDA) dat |= 0x80 ; SCK = 1 ; SCK = 0 ; } return dat ; } void reset_ds1302(void) { RST = 0 ; SDA = 0 ; RST = 1 ; } void clear_ds1302_wp(void) { reset_ds1302() ; RST = 1 ; write_ds1302_byte(0x8E) ; write_ds1302_byte(0) ; SDA = 0 ; RST = 0 ; } void set_ds1302_wp(void) { reset_ds1302() ; RST = 1 ; write_ds1302_byte(0x8E) ; write_ds1302_byte(0x80) ; SDA = 0 ; RST = 0 ; } void write_ds1302(uint8 addr,uint8 dat) { reset_ds1302() ; RST = 1 ; write_ds1302_byte(addr) ; write_ds1302_byte(dat) ; SDA = 0 ; RST = 0 ; } uint8 read_ds1302(uint8 addr) { uint8 temp = 0 ; reset_ds1302() ; RST = 1 ; write_ds1302_byte(addr) ; temp =read_ds1302_byte() ; SDA = 0 ; RST = 0 ; return temp ; } void set_time(uint8 *timedata) { uint8 i, tmp ; for(i=0; i<7; i++) { tmp = timedata[i]/10 ; timedata[i] = timedata[i] ; timedata[i] = tmp*16 + timedata[i] ; } tmp = ds1302_w_addr ; clear_ds1302_wp() ; for(i=0; i<7; i++) { write_ds1302(tmp,timedata[i]) ; tmp+=2 ; } set_ds1302_wp() ; } void read_time(uint8 *timedata) { uint8 temp, i ; temp = ds1302_r_addr ; for(i=0; i<7; i++) { timedata[i] = read_ds1302(temp) ; temp+= 2 ; } } void wait(void) { P0 = 0xFF ; while(BUSY) { RS = 0 ; RW = 1 ; EN = 0 ; EN = 1 ; } EN = 0 ; } void w_dat(uint8 dat) { wait() ; EN = 0 ; P0 = dat ; RS = 1 ; RW = 0 ; EN = 1 ; EN = 0 ; } void w_cmd(uint8 cmd) { wait() ; EN = 0 ; P0 = cmd ; RS = 0 ; RW = 0 ; EN = 1 ; EN = 0 ; } void w_hexnum(uint8 dat) { w_dat(dat/16 + 0) ; //这里注意/16,输出为8421BCD码,需要转换 w_dat(dat + 0) ; } void Init_LCD1602(void) { w_cmd(0x38) ; w_cmd(0x0c) ; w_cmd(0x06) ; w_cmd(0x01) ; } void main(void) { Init_LCD1602() ; set_time(&time) ; while(1) { read_time(&time) ; //指针的概念 w_cmd(0x80) ; w_hexnum(time[6]) ; w_dat(-) ; w_hexnum(time[4]) ; w_dat(-) ; w_hexnum(time[3]) ; w_cmd(0xC0) ; w_hexnum(time[2]) ; w_dat(:) ; w_hexnum(time[1]) ; w_dat(:) ; w_hexnum(time[0]) ; } }
单片机实时时钟DS130 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)