单片机==ds1302_时间日期周几(30)

来源:互联网 发布:多功能圣经软件下载 编辑:程序博客网 时间:2024/05/28 22:08
#include <reg52.h>#include "delay.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     0xbesbit RST = P1^0;sbit SCK = P1^1;sbit IO = P1^2;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,17};unsigned char timebuf[3] = {16,01,35};//unsigned char weekbuf[7] = {MON,TUE,WED,THU,FRI,SAT,SUN};#define LCDPORT P0sbit RS = P2^4;sbit RW = P2^5;sbit E = P2^6;#define LCD_WRITE_DATA 1#define LCD_WRITE_COM 0void 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(4,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 main(){      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();        }}
0 0
原创粉丝点击