基于51单片机的DS1302+LCD12864+DS18B20小综合

#include #include

#define uint unsigned int #define uchar unsigned char

#define first_line 0x80 #define second_line 0x90 #define third_line 0x88 #define forth_line 0x98

#define sec_addr_pos 0x04 #define min_addr_pos 0x02 #define hor_addr_pos 0x00 #define day_addr_pos 0x05 #define mon_addr_pos 0x03 #define yer_addr_pos 0x01 #define wek_addr_pos 0x02 #define sbl_addr_pos 0x04 #define Tep_addr_pos 0x05 #define Tmp_addr_pos 0x06

uchar code line_one[]=\年10月31日 %uchar code line_two[]=\:22:45 %uchar code line_three[]=\星期五 - 23℃ %uchar str_wek[]=\一二三四五六日℃\

char second,minute,hour,day,week,month,year; uchar key_flag,mode_cnt;

uint temperature,symbol;//用于存储DS18B20采集的整个数据 uchar DS18B20_H;//用于存储DS18B20数据采集的整数部分 uchar DS18B20_L;//用于存储DS18B20数据采集的小数部分 uchar init_18B20_fail;

sfr LCD_DATA=0x80; sbit LCD_EN=P2^7; sbit LCD_RS=P2^6; sbit LCD_RW=P2^5; sbit DS_CLK=P3^6; sbit DS_RST=P3^5; sbit DS_IO=P3^4; sbit ACC0=ACC^0;

sbit ACC7=ACC^7; sbit key_1=P2^4; sbit key_2=P2^3; sbit key_3=P2^2; sbit DQ=P3^7;

void delay_ms(uint ms); void delay_us(uint us); bit busy(void);

void write_command(uchar command); void write_data(uchar dat); void lcd_pos(uchar x,uchar y); void LCD_init(void);

void Input_1byte(uchar TD); uchar Output_1byte(void);

uchar Read_DS1302(uchar DS_ADD);

void write_DS1302(uchar DS_ADD,uchar DS_DATE); void write_time(uchar addr,uchar num); uchar BCD_to_Dec(uchar num); void write_week(uchar num);

void write_date(uchar addr,uchar num); uchar readsecond(void); uchar readminute(void); uchar readhour(void); uchar readday(void); uchar readmonth(void); uchar readweek(void); uchar readyear(void); void readtime(void); void DS1302_init(void); void key_scan(void);

uchar DS18B20_read_byte(void);

void DS18B20_write_byte(uchar byte); uchar DS18B20_Init(void); uint read_temperature(void); void write_temp(uchar temp_h);

void write_symbol(uchar addr,uchar symbol); void display_18B20_err(void);

void main(void)

{

key_flag=0; second=20; minute=30; hour=10; day=31; week=3; month=12; year=16;

LCD_init();

/*while(1){

write_date(day_addr_pos,day); write_date(mon_addr_pos,month); write_date(yer_addr_pos,year); write_time(sec_addr_pos,second); write_time(min_addr_pos,minute); write_time(hor_addr_pos,hour); write_week(week); }*/

if(!(Read_DS1302(0x8E)&0x80)){ DS1302_init(); }

while(1){

key_scan();

if(key_flag==0){

second=Read_DS1302(0x81);

write_time(sec_addr_pos,BCD_to_Dec(second));

minute=Read_DS1302(0x83);

write_time(min_addr_pos,BCD_to_Dec(minute));

hour=Read_DS1302(0x85);

温度

write_time(hor_addr_pos,BCD_to_Dec(hour)); week=Read_DS1302(0x8B);

write_week(week); day=Read_DS1302(0x87);

write_date(day_addr_pos,BCD_to_Dec(day)); month=Read_DS1302(0x89);

write_date(mon_addr_pos,BCD_to_Dec(month)); year=Read_DS1302(0x8D);

write_date(yer_addr_pos,BCD_to_Dec(year));

readtime();//读出十进制数,提供给按键进行数据的+/-的处理

temperature=read_temperature(); init_18B20_fail=DS18B20_Init(); if(init_18B20_fail==0){

//temperature=read_temperature();

if((temperature&0x8000)==0x8000)//判断读取的温度是正温度还是负 {

symbol=0x2D;

//write_symbol(0x0A,0x2C);//显示负号

temperature=~temperature+1;//负温度时处理,原数取反再加1 }else{

symbol=0xfe;

//write_symbol(0x0A,0xfe);//不显示正号 }

//write_symbol(0x0A,symbol);

DS18B20_H=(temperature>>4)&0x7f;//取温度读数的整数部分 DS18B20_L=temperature&0x0f;//取温度读数的小数部分 write_symbol(sbl_addr_pos,symbol); write_temp(DS18B20_H); }else{

display_18B20_err();

联系客服:779662525#qq.com(#替换为@)