您的位置 首页 方案

AVR128的程序——DS18B20

includeiom128vh>includemacrosh>defineucharunsignedchardefineuintunsignedintdefineDQ_INDDRE=~(1

#include

#include
#define uchar unsigned char
#define uint unsigned int
#define DQ_IN DDRE &= ~(1 << PE6)
#define DQ_OUT DDRE |= (1 << PE6)
#define DQ_CLR PORTE &= ~(1 << PE6)
#define DQ_SET PORTE |= (1 << PE6)
#define DQ_R PINE & (1 << PE6)
uchar TempData[8];
uchar WeiMa[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar led_discode[11]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
void Delayms(uint MS)
{
uint i,j;
for( i=0;i
for(j=0;j<1141;j++);
}
void Delayus(uint US)
{
uint i;
US=US*5/4;
for( i=0;i
}
void smg_init(void)
{
DDRA=0XFF;
PORTA=0XFF;
DDRC=0XFF;
PORTC=0XFF;
DDRF|=0X0E;
PORTF|=0X0E;
PORTF&=0xF7;
DDRB |=0X10;
PORTB|=0X10;
}
unsigned char ds1820_reset(void)
{
unsigned char i;
DQ_OUT;
DQ_CLR;
Delayus(500);
DQ_SET;
DQ_IN;
Delayus(100);
i = DQ_R;
Delayus(500);
return i;
}
unsigned char ds1820_read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i = 0; i < 8; i++)
{
value >>= 1;
DQ_OUT;
DQ_CLR;
Delayus(10);
DQ_SET;
DQ_IN;
if (DQ_R) value|=0x80;
Delayus(60);
}
return value;
}
void ds1820_write_byte(unsigned char value)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
DQ_OUT;
DQ_CLR;
Delayus(10);
if (value & 0x01) DQ_SET;
Delayus(100);
DQ_SET;
value >>= 1;
}
}
void data_pro(uchar data)
{
TempData[2] = led_discode[data0];
TempData[1] = led_discode[data0/10];
TempData[0] = led_discode[data/100];
}
void display(uchar FirstBit,uchar Num)
{
unsigned char i;
for(i=0;i
{
PORTC=led_discode[10];
PORTA=WeiMa[i+FirstBit];
PORTC=TempData[i];
Delayms(2);
}
}
void display1(uchar FirstBit,uchar Num)
{
unsigned char i;
for(i=0;i
{
PORTC=led_discode[10];
PORTA=WeiMa[i+FirstBit];
PORTC=0X7F;
Delayms(2);
}
}
void main(void)
{
uchar i,j;
uint temp;
smg_init();
while (1)
{
ds1820_reset();
ds1820_write_byte(0xCC);
ds1820_write_byte(0x44);
Delayms(2);
ds1820_reset();
ds1820_write_byte(0xCC);
ds1820_write_byte(0xbe);
i=ds1820_read_byte();
j=ds1820_read_byte();
temp=j*256+i;
temp=temp*0.625;
data_pro(temp);
display(5,3);
display1(6,1);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/262343.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部