单片机==ds18B20_ds1302_时日周温(31)
2016-10-20 08:08
246 查看
#include <reg52.h> #include "delay.h" #include <intrins.h> #include <stdio.h> #define ds1302_sec_add 0x80 //??¨ºy?Y¦Ì??¡¤ #define ds1302_min_add 0x82 //¡¤?¨ºy?Y¦Ì??¡¤ #define ds1302_hr_add 0x84 //¨º¡À¨ºy?Y¦Ì??¡¤ #define ds1302_date_add 0x86 //¨¨?¨ºy?Y¦Ì??¡¤ #define ds1302_month_add 0x88 //??¨ºy?Y¦Ì??¡¤ #define ds1302_day_add 0x8a //D??¨²¨ºy?Y¦Ì??¡¤ #define ds1302_year_add 0x8c //?¨º¨ºy?Y¦Ì??¡¤ #define ds1302_control_add 0x8e //????¨ºy?Y¦Ì??¡¤ #define ds1302_charger_add 0x90 #define ds1302_clkburst_add 0xbe sbit RST = P1^0; sbit SCK = P1^1; sbit IO = P1^2; sbit ds = P1^3; bit ack = 0; unsigned char w_tbuf = 3; unsigned char writeweek = 0; unsigned char readweek = 0; unsigned char disweek = 0; unsigned char tempbuf[3]; unsigned char readbuf[3]; unsigned char disbuf[3]; unsigned char writebuf[3]; unsigned char w_dbuf[3]; unsigned char r_dbuf[3]; unsigned char d_dbuf[3]; unsigned char datebuf[3] = {16,8,19}; unsigned char timebuf[3] = {9,59,10}; //unsigned char weekbuf[7] = {MON,TUE,WED,THU,FRI,SAT,SUN}; #define LCDPORT P0 sbit RS = P2^4; sbit RW = P2^5; sbit E = P2^6; #define LCD_WRITE_DATA 1 #define LCD_WRITE_COM 0 void lcd_write(unsigned char byte, unsigned char flag) { if(flag) { RS = 1; } else { RS = 0; } RW = 0; E = 1; LCDPORT = byte; delay_us(5); E = 0; } void lcd_init() { delay_ms(16); lcd_write(0x38, LCD_WRITE_COM); delay_ms(5); lcd_write(0x38, LCD_WRITE_COM); delay_ms(5); lcd_write(0x38, LCD_WRITE_COM); delay_ms(5); lcd_write(0x38, LCD_WRITE_COM); delay_ms(5); lcd_write(0x08, LCD_WRITE_COM); delay_ms(5); lcd_write(0x01, LCD_WRITE_COM); delay_ms(5); lcd_write(0x06, LCD_WRITE_COM); delay_ms(5); lcd_write(0x0C, LCD_WRITE_COM); delay_ms(5); } void display_str(unsigned char x, unsigned char y, unsigned char * string) { if((x > 15) || (y > 1)) { return; } if(0 == y) { lcd_write(0x80 + x, LCD_WRITE_COM); } if(1 == y) { lcd_write(0x80 + 0x40 + x , LCD_WRITE_COM); } while( (*string) != '\0' ) { lcd_write(*string, LCD_WRITE_DATA); string++; } } 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 i; unsigned char temp; unsigned char temp1; for(i = 0; i < 3; 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_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); for(i = 0; i < 3; i++) { temp = (readbuf[i] >> 4); temp1 = (readbuf[i] & 0x0f); disbuf[i] = temp * 10 + temp1; } } void lcd_dis_time() { unsigned char lcddisbuf[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; display_str(0,0,lcddisbuf); } void ds1302_write_date() { unsigned char i; unsigned char temp; unsigned char temp1; for(i = 0; i < 3; i++) { temp = datebuf[i] / 10; temp1 = datebuf[i] % 10; w_dbuf[i] = (temp << 4) | temp1; } ds1302_write_byte(ds1302_control_add,0x00); ds1302_write_byte(ds1302_year_add,w_dbuf[0]); ds1302_write_byte(ds1302_month_add,w_dbuf[1]); ds1302_write_byte(ds1302_date_add,w_dbuf[2]); ds1302_write_byte(ds1302_control_add,0x80); } void ds1302_read_date() { unsigned char i; unsigned char temp; unsigned char temp1; r_dbuf[0] = ds1302_read_byte(ds1302_year_add); r_dbuf[1] = ds1302_read_byte(ds1302_month_add); r_dbuf[2] = ds1302_read_byte(ds1302_date_add); for(i = 0; i < 3; i++) { temp = (r_dbuf[i] >> 4); temp1 = (r_dbuf[i] & 0x0f); d_dbuf[i] = temp * 10 + temp1; } } void lcd_dis_date() { unsigned char lcddisbuf[9] = {0}; lcddisbuf[0] = (d_dbuf[0] / 10) + 0x30; lcddisbuf[1] = (d_dbuf[0] % 10) + 0x30; lcddisbuf[2] = '-'; lcddisbuf[3] = (d_dbuf[1] / 10) + 0x30; lcddisbuf[4] = (d_dbuf[1] % 10) + 0x30; lcddisbuf[5] = '-'; lcddisbuf[6] = (d_dbuf[2] / 10) + 0x30; lcddisbuf[7] = (d_dbuf[2] % 10) + 0x30; display_str(0,1,lcddisbuf); } void ds1302_write_week() { writeweek = w_tbuf % 10; ds1302_write_byte(ds1302_control_add,0x00); ds1302_write_byte(ds1302_day_add,writeweek); ds1302_write_byte(ds1302_control_add,0x80); } void ds1302_read_week() { readweek = ds1302_read_byte(ds1302_day_add); disweek = readweek & 0x0f; } void lcd_dis_week() { switch(disweek) { case 0x01: { display_str(13,1,"Mon"); break; } case 0x02: { display_str(13,1,"Tue"); break; } case 0x03: { display_str(13,1,"Wed"); break; } case 0x04: { display_str(13,1,"Thu"); break; } case 0x05: { display_str(13,1,"Fri"); break; } case 0x06: { display_str(13,1,"Sat"); break; } case 0x07: { display_str(13,1,"Sun"); break; } } } void ds_reset() { ds = 1; ds = 0; delay_us(200); delay_us(200); //Òª±£Ö¤480us~960us ds = 1; //free ds delay_us(30); if(0 == ds) { ack = 1; } else { ack = 0; } delay_us(200); delay_us(100); //¹ýÁËÄǶεÍ×ÜÏß } void ds_send_byte(unsigned char byte) { unsigned char i; for(i = 0; i < 8; i++) { ds = 0; //ÀµÍ _nop_(); //1us _nop_(); //´óÓÚ1us ds = byte & 0x01; //ÏȲÙ×÷×îµÍλ byte >>= 1; delay_us(30); //×îÉÙ60us ds = 1; //ÔÙÀ¸ß } delay_us(30); } bit ds_read_bit() { bit tmp; ds = 1; ds = 0; _nop_(); _nop_(); ds = 1; tmp = ds; delay_us(30); //×îÉÙ60us return tmp; } unsigned char ds_read_byte() { unsigned char i,j,k; for(i = 0; i < 8; i++) { j = ds_read_bit(); k = (j << 7) | (k >> 1); } return k; } void main() { unsigned char a; unsigned int b,temp; float temperture; unsigned char disbuf[20]; lcd_init(); //ds1302_write_time(); //ds1302_write_date(); //ds1302_write_week(); while(1) { ds1302_read_time(); ds1302_read_date(); ds1302_read_week(); lcd_dis_time(); lcd_dis_date(); lcd_dis_week(); ds_reset(); ds_send_byte(0xcc); //Ìø¹ý¶ÁROMµÄÃüÁî ds_send_byte(0x44); //Æô¶¯Î¶Èת»» ds_reset(); ds_send_byte(0xcc); //Ìø¹ý¶ÁROMµÄÃüÁî ds_send_byte(0xbe); a = ds_read_byte(); //µÍ×Ö½Ú b = ds_read_byte(); //¸ß×Ö½Ú temp = (b << 8) | a; temperture = (float)temp * 0.0625; sprintf(disbuf, "%7.3f", temperture); display_str(9,0,disbuf); } }
相关文章推荐
- 单片机配合DS1302时钟芯片自制简易电子表(测试通过)
- 基于DS18B20温感和单片机 实现热水器功能
- 单片机温度控制系统DS18B20
- 51单片机——DS18B20
- 单片机DS18b20 温度检测液晶显示
- 单片机控制SPI协议时钟芯片DS1302之模块化编程及待解决的问题(持续更新中)
- 单片机温度控制系统DS18B20
- 单片机练习 - DS18B20温度转换与显示
- 单片机(七)__ds1302实时时钟
- 单片机--ds1302
- 单片机学习笔记--DS18B20
- 单片机复习笔记005 -- DS1302时钟芯片
- 51单片机的DS1302的驱动程序
- 单片机==ds18B20(32)
- 单片机控制DS18B20自制简易电子温度计(测试通过)(一)
- 单片机ds18b20测量温度
- 单片机控制DS18B20自制简易电子温度计(测试通过)(二)
- 15单片机温度DS18b20温度模块 Dream Cather 第五天
- LCD1602显示+ds18b20温度测试+ds1302时钟显示
- 51单片机以定时中断的方法实现DS18B20时序