微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR128的程序——DS18B20

AVR128的程序——DS18B20

时间:11-22 来源:互联网 点击:
#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);

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top