您的位置:首页 > 其它

单片机==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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  单片机