单片机——ds1302代码
2016-09-24 19:00
267 查看
#include #include "./lcd/lcd.h" #define ds1302_sec_add 0x80 #define ds1302_min_add 0x82 #define ds1302_hr_add 0x84 #define ds1302_date_add 0x86 #define ds1302_month_add 0x88 #define ds1302_day_add 0x8a #define ds1302_year_add 0x8c #define ds1302_control_add 0x8e #define ds1302_charger_add 0x90 #define ds1302_clkburst_add 0xbe sbit RST = P1^0; sbit SCK = P1^1; sbit IO = P1^2; unsigned char timebuf[7] = {20,30,30,16,8,17,3}; unsigned char tempbuf[7]; unsigned char writebuf[7]; unsigned char readbuf[7]; unsigned char disbuf[7]; void ds1302_write_byte(unsigned char addr,unsigned char byte) { unsigned char i; addr = addr & 0xfe; SCK = 0; RST = 0; RST = 1; for(i = 0; i < 8; i++) { IO = addr & 0x01; SCK = 0; SCK = 1; addr >>= 1; } for(i = 0; i < 8; i++) { IO = byte & 0x01; SCK = 0; SCK = 1; byte >>= 1; } SCK = 0; RST = 0; } unsigned char ds1302_read_byte(unsigned char addr) { unsigned char i; unsigned char temp; addr = addr & 0xfe; SCK = 0; RST = 0; RST = 1; addr = addr + 1; for(i = 0; i < 8; i++) { IO = addr & 0x01; SCK = 0; SCK = 1; addr >>= 1; } for(i = 0; i < 8; i++) { SCK = 1; SCK = 0; temp >>= 1; if(IO) { temp += 0x80; } } RST = 0; return temp; } void ds1302_write_time() { unsigned char temp; unsigned char temp1; unsigned char i; for(i = 0; i < 7; i++) { temp = timebuf[i] / 10; temp1 = timebuf[i] % 10; writebuf[i] = (temp << 4) | temp1; } ds1302_write_byte(ds1302_control_add,0x00); ds1302_write_byte(ds1302_hr_add,writebuf[0]); ds1302_write_byte(ds1302_min_add,writebuf[1]); ds1302_write_byte(ds1302_sec_add,writebuf[2]); ds1302_write_byte(ds1302_year_add,writebuf[3]); ds1302_write_byte(ds1302_month_add,writebuf[4]); ds1302_write_byte(ds1302_date_add,writebuf[5]); ds1302_write_byte(ds1302_day_add,writebuf[6]); ds1302_write_byte(ds1302_control_add,0x80); } void ds1302_read_time() { unsigned char i; unsigned char temp; unsigned char temp1; readbuf[0] = ds1302_read_byte(ds1302_hr_add); readbuf[1] = ds1302_read_byte(ds1302_min_add); readbuf[2] = ds1302_read_byte(ds1302_sec_add); readbuf[3] = ds1302_read_byte(ds1302_year_add); readbuf[4] = ds1302_read_byte(ds1302_month_add); readbuf[5] = ds1302_read_byte(ds1302_date_add); readbuf[6] = ds1302_read_byte(ds1302_day_add); for(i = 0; i < 7; i++) { temp = (readbuf[i] >> 4); temp1 = (readbuf[i] & 0x0f); disbuf[i] = temp * 10 + temp1; } } void lcd_dis_time() { unsigned char lcddisbuf[9] = {0}; unsigned char lcddisdata[9] = {0}; lcddisbuf[0] = (disbuf[0] / 10) + 0x30; lcddisbuf[1] = (disbuf[0] % 10) + 0x30; lcddisbuf[2] = ':'; lcddisbuf[3] = (disbuf[1] / 10) + 0x30; lcddisbuf[4] = (disbuf[1] % 10) + 0x30; lcddisbuf[5] = ':'; lcddisbuf[6] = (disbuf[2] / 10) + 0x30; lcddisbuf[7] = (disbuf[2] % 10) + 0x30; lcddisdata[0] = (disbuf[3] / 10) + 0x30; lcddisdata[1] = (disbuf[3] % 10) + 0x30; lcddisdata[2] = '.'; lcddisdata[3] = (disbuf[4] / 10) + 0x30; lcddisdata[4] = (disbuf[4] % 10) + 0x30; lcddisdata[5] = '.'; lcddisdata[6] = (disbuf[5] / 10) + 0x30; lcddisdata[7] = (disbuf[5] % 10) + 0x30; lcd_dis_str(1,1,"20"); lcd_dis_str(1,3,lcddisdata); lcd_dis_str(2,1,"TIME:"); lcd_dis_str(2,7,lcddisbuf); switch(readbuf[6]) { case 1: { lcd_dis_str(1,13,"MON"); break; } case 2: { lcd_dis_str(1,13,"TUE"); break; } case 3: { lcd_dis_str(1,13,"WED"); break; } case 4: { lcd_dis_str(1,13,"THU"); break; } case 5: { lcd_dis_str(1,13,"FRI"); break; } case 6: { lcd_dis_str(1,13,"SAT"); break; } case 7: { lcd_dis_str(1,13,"SUN"); break; } } } void main() { lcd_init(); ds1302_write_time(); while(1) { ds1302_read_time(); lcd_dis_time(); } }
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- 浅谈C语言转义字符和格式控制符
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C 语言简单加减乘除运算
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中进制知识汇总
- C语言判断一个数是否是2的幂次方或4的幂次方
- C语言二进制思想以及数据的存储