新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 基于ATMEGA16的DS18B20測溫程序

基于ATMEGA16的DS18B20測溫程序

作者: 時間:2012-08-04 來源:網(wǎng)絡(luò) 收藏

//讀取溫度值先讀取暫存器的值在進行溫度轉(zhuǎn)換否則會意外出錯
unsigned int readTemp(void)
{
unsigned char tempL,tempH;
unsigned int temp;
//開始讀取溫度
ds18b20_reset();//18B20復位
ds18b20_write_byte(0xcc);//跳過ROM
ds18b20_write_byte(0xbe);//命令讀取暫存器
tempL=ds18b20_read_byte();//從暫存器中讀取數(shù)據(jù)
tempH=ds18b20_read_byte();//從暫存器中讀取數(shù)據(jù)
temp=(tempH8)|tempL;//總值為高位*256+低位
temp=temp*0.625;//為了保留1位小數(shù),最小單位為0.0625
ds18b20_reset();//18B20初始化
ds18b20_write_byte(0xcc);//對ROM進行操作,因為只接了1個器件所以寫跳過指令
ds18b20_write_byte(0x44);//啟動溫度轉(zhuǎn)換
Delay_ms(1);//給硬件一點時間讓其進行轉(zhuǎn)換
return(temp);
}

--------------------------------------------------------


display.c:
#include iom16v.h>
#include macros.h>
#include"display.h"
#define uchar unsigned char
#define uint unsigned int

uchar table1[]="0123456789";
uchar table2[]="design:zhubo";
uchar table3[]="temperature:";
uchar table4[]=".";

extern int temperture;
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;ims;i++)
{
for(j=0;j1141;j++);
}
}

void write_com(uchar com)
{
PORTD=~BIT(4);
PORTD=~BIT(5);
PORTB=com;
PORTD|=BIT(6);
delay(1);
PORTD=~BIT(6);
}

void write_dat(uchar dat)
{
PORTD|=BIT(4);
PORTD=~BIT(5);
PORTB=dat;
PORTD|=BIT(6);
delay(1);
PORTD=~BIT(6);
}
void LCD_init()
{
DDRB=0XFF;
DDRD|=BIT(4)|BIT(5)|BIT(6);
PORTD=~BIT(6);

write_com(0X38);
delay(5);
write_com(0X01);
delay(5);
write_com(0X0C);
delay(5);
write_com(0X06);
delay(5);

}
void display()
{
uint i;

write_com(0X80+0);
delay(5);
for(i=0;i12;i++)
{
write_dat(table3[i]);
delay(5);
}

write_com(0X80+12);
delay(5);
write_dat(table1[temperture/100%10]);
delay(5);
write_com(0X80+13);
delay(5);
write_dat(table1[temperture/10%10]);
delay(5);
write_com(0X80+14);
delay(5);
write_dat(table4[0]);
delay(5);
write_com(0X80+15);
delay(5);
write_dat(table1[temperture%10]);
delay(5);

write_com(0X80+0X40);
delay(5);
for(i=0;i12;i++)
{
write_dat(table2[i]);
delay(5);
}
}


--------------------------------------------------------
接口函數(shù):
18B20.h:
#define uchar unsigned char
#define uint unsigned int

extern uchar ds18b20_reset();
extern void ds18b20_write_byte(uchar value);
extern uint ds18b20_read_byte(void);
extern uint readTemp(void);
display.h:
#define uchar unsigned char
#define uint unsigned int
#ifndef display_h
#define display_h
extern void write_com(uchar com);
extern void write_dat(uchar dat);
extern void LCD_init();
extern void display();
extern uchar table[];
#endif
delay.h:

/**************************************************************
**函數(shù)功能:延時1us
**輸入?yún)?shù):無
**返回值:無
**在本函數(shù)外定義變量:無
**調(diào)用的函數(shù):NOP()
**************************************************************/
void Delay_1us(void);

/**************************************************************
**函數(shù)功能:us級延時
**輸入?yún)?shù):xus:延時us數(shù)
**返回值:無
**在本函數(shù)外定義變量:無
**調(diào)用的函數(shù):Delay_1us()
**************************************************************/
void Delay_Us(unsigned int xus);

/**************************************************************
**函數(shù)功能:延時1ms
**輸入?yún)?shù):無
**返回值:無
**在本函數(shù)外定義變量:無
**調(diào)用的函數(shù):NOP()
**************************************************************/
void Delay_1ms(void);

/**************************************************************
**函數(shù)功能:ms級延時
**輸入?yún)?shù):xms:延時ms數(shù)
**返回值:無
**在本函數(shù)外定義變量:無
**調(diào)用的函數(shù):Delay_1ms()
**************************************************************/
void Delay_ms(unsigned int xms);

/**************************************************************
**函數(shù)功能:10us級延時
**輸入?yún)?shù):x10us:延時x10us數(shù)
**返回值:無
**在本函數(shù)外定義變量:無
**調(diào)用的函數(shù):無
**************************************************************/
void Delay10us(int x10us);


void Delay_1us()
{
unsigned char t=2;
t--;
}

void Delay_Us(unsigned int xus)
{
while (xus--)
Delay_1us();
}

void Delay_1ms(void)
{
unsigned int i;
for(i=0;i1142;i++)
;
}

void Delay_ms(unsigned int x)
{
int i,j; //variable:declare int
for(i=0; ix; i++)
for(j=0; j498;j++)
{
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
NOP();
}
}

void Delay10us(int x10us)//crystal=8MHz
{
int i;int j; //variable:declare int
for(i=0; ix10us; i++)
{
for(j=0;j2;j++)
{
NOP();
NOP();
NOP();
}
}
}


上一頁 1 2 下一頁

關(guān)鍵詞: ATMEGA16 DS18B20 測溫程序

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉