#include <avr/io.h>
#include<avr/interrupt.h>
#include <util/delay.h>
#include "LCD.h"
#define uchar unsigned char
#define uint unsigned int
#define DQ_IN DDRD&= ~(1 << PD3)
#define DQ_OUT DDRD|=(1 << PD3)
#define DQ_SET PORTD|=(1 << PD3)
#define DQ_CLR PORTD&=~(1 << PD3)
#define DQ_R PIND&(1 << PD3)
uchar table[3]={0,0,0};
uchar ds18b20_reset(void)
{
uchar i;
DQ_OUT;
DQ_CLR;
_delay_us(500);
DQ_SET; //release bus
_delay_us(100);
DQ_IN;
i = DQ_R;
_delay_us(500);
DQ_SET;
return i;
}
/***************************************/
void ds18b20_write_byte(uchar value)
{
uchar i;
for(i=0; i<8; i++)
{
DQ_OUT;
DQ_CLR;
_delay_us(10);
if(value&0x01)
{
DQ_SET;
}
else
DQ_CLR;
_delay_us(100);
value = value >> 1;
DQ_SET;//总线释放
_delay_us(2);
}
}
/******************************/
uchar ds18b20_read_byte(void)
{
uchar i;
uchar value;
for(i=0; i<8; i++)
{
value = value>>1;
DQ_OUT;
DQ_CLR;
_delay_us(10);
DQ_SET;
DQ_IN;//采样开始
if(DQ_R)
{
value |= 0x80;
}
_delay_us(50);
}
return value;
}
/**************************************/
void data_pro(uint temp)
{
uint a;
table[2] = temp/100;
a = temp%100;
table[1] = a/10;
table[0] = a%10;
}
/****************************************/
void main(void)
{
uchar k;
uchar i,j;
uint temp;
system_init(); //系统初始化,设
_delay_ms(100); //延时100ms
LCD_init(); //液晶参数初始化
LCD_clear();
while(1)
{
ds18b20_reset();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0x44);
_delay_us(2000);
ds18b20_reset();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0xbe);
i = ds18b20_read_byte();//lsb
j = ds18b20_read_byte();//msb
temp = j*256 + i; //26.8
temp = temp*0.625;//268
data_pro(temp);
for(k=0; k<20; k++)
{
LCD_write_com(0xc6);
LCD_write_data(table[2]+0x30);
LCD_write_data(table[1]+0x30);
LCD_write_data(table[0]+0x30);
}
}
}
|