DS18B20+DS1302
萬年歷 + 溫度顯示
IMG_3033.JPG (1.15 MB, 下載次數: 35)
下載附件
BS18B20+DS1302
2018-9-26 19:13 上傳
- #include "config.h"
- #include "USART1.h"
- #include "DS1302.h"
- #include "delay.h"
- #include "timer.h"
- /***************************3õê¼ê±¼ä¶¨òå**********************************/
- u8 time_buf[8] = {0x20,0x18,0x01,0x21,0x20,0x30,0x30,0x07}; // 3õê¼ê±¼ä
- u8 readtime[20]; // μ±Ç°ê±¼ä
- u8 sec_buf = 0; // Ãë»o′æ
- u8 sec_flag = 0; // Ãë±ê־λ
- u8 buf[100];
- u16 nian = 0,yue = 0,ri = 0;
- u16 shi = 0,fen = 0,miao = 0;
- u16 xingqi = 0;
- void ds1302_init(void)
- {
- RST = 0; // RST½ÅÖÃμí
- SCK = 0; // SCK½ÅÖÃμí
- }
- void ds1302_write_byte(u8 addr, u8 d)
- {
- u8 i;
- RST=1; // Æô¶ˉDS1302×üÏß
- // D′èëÄ¿±êμØÖ·£oaddr
- addr = addr & 0xFE; // ×îμíλÖÃá㣬¼Ä′æÆ÷0λÎa0ê±D′£¬Îa1ê±¶á
- for (i = 0; i < 8; i ++)
- {
- if (addr & 0x01) {IO = 1;}
- else {IO = 0;}
- SCK = 1; // 2úéúê±Öó
- SCK = 0;
- addr = addr >> 1;
- }
- // D′èëêy¾Y£od
- for (i = 0; i < 8; i ++)
- {
- if (d & 0x01) {IO = 1;}
- else {IO = 0;}
- SCK = 1; // 2úéúê±Öó
- SCK = 0;
- d = d >> 1;
- }
- RST = 0; // í£Ö1DS1302×üÏß
- }
- u8 ds1302_read_byte(u8 addr)
- {
- u8 i,temp;
- RST=1; // Æô¶ˉDS1302×üÏß
- // D′èëÄ¿±êμØÖ·£oaddr
- addr = addr | 0x01; // ×îμíλָߣ¬¼Ä′æÆ÷0λÎa0ê±D′£¬Îa1ê±¶á
- for (i = 0; i < 8; i ++)
- {
- if (addr & 0x01) {IO = 1;}
- else {IO = 0;}
- SCK = 1;
- SCK = 0;
- addr = addr >> 1;
- }
- // êä3öêy¾Y£otemp
- for (i = 0; i < 8; i ++)
- {
- temp = temp >> 1;
- if (IO){temp |= 0x80;}
- else{temp &= 0x7F;}
- SCK = 1;
- SCK = 0;
- }
- RST = 0; // í£Ö1DS1302×üÏß
- return temp;
- }
- void ds1302_write_time(void)
- {
- ds1302_write_byte(ds1302_control_add,0x00); // 1رÕD′±£»¤
- ds1302_write_byte(ds1302_sec_add,0x80); // ÔYí£ê±Öó
- //ds1302_write_byte(ds1302_charger_add,0xa9); // ä¸á÷3äμç
- ds1302_write_byte(ds1302_year_add,time_buf[1]); // Äê
- ds1302_write_byte(ds1302_month_add,time_buf[2]); // ÔÂ
- ds1302_write_byte(ds1302_date_add,time_buf[3]); // èÕ
- ds1302_write_byte(ds1302_hr_add,time_buf[4]); // ê±
- ds1302_write_byte(ds1302_min_add,time_buf[5]); // ·Ö
- ds1302_write_byte(ds1302_sec_add,time_buf[6]); // Ãë
- ds1302_write_byte(ds1302_day_add,time_buf[7]); // Öü
- ds1302_write_byte(ds1302_control_add,0x80); // ′ò¿aD′±£»¤
- }
- void ds1302_read_time(void)
- {
- time_buf[1]=ds1302_read_byte(ds1302_year_add); // Äê
- time_buf[2]=ds1302_read_byte(ds1302_month_add); // ÔÂ
- time_buf[3]=ds1302_read_byte(ds1302_date_add); // èÕ
- time_buf[4]=ds1302_read_byte(ds1302_hr_add); // ê±
- time_buf[5]=ds1302_read_byte(ds1302_min_add); // ·Ö
- time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7f; // Ã룬Æá±ÎÃëμÄμú7룬±üÃa3¬3ö59
- time_buf[7]=ds1302_read_byte(ds1302_day_add); // Öü
- }
- void display()
- {
- sprintf(buf,"DS16(85,50,'%4d Äê %02d ÔÂ %02d èÕ',15);\r\n",(int)nian,(int)yue,(int)ri);PrintString1(buf);delay_ms(10);
- if (xingqi == 1){sprintf(buf,"DS16(130,100,' Monday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 2){sprintf(buf,"DS16(130,100,' Tuesday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 3){sprintf(buf,"DS16(130,100,' Wednesday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 4){sprintf(buf,"DS16(130,100,' Thursdy ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 5){sprintf(buf,"DS16(130,100,' Friday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 6){sprintf(buf,"DS16(130,100,' Saturday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- if (xingqi == 7){sprintf(buf,"DS16(130,100,' Sunday ',15);\r\n");PrintString1(buf);delay_ms(10);}
- sprintf(buf,"DS16(115,150,'%02d : %02d : %02d ',15);\r\n",(int)shi,(int)fen,(int)miao);PrintString1(buf);delay_ms(10);
- }
- void sys_init()
- {
- delay_ms(255);
- PrintString1("CLS(0);");
- PrintString1("CLS(0);");
- PrintString1("\r\n");
- delay_ms(255);
- }
復制代碼
|