#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define INT8U unsigned char
/*18B20*/
#define CLR_DIR_1WIRE DDRD&=~(0x80) //只要修改这里的参数就可以了!呵呵!
#define SET_DIR_1WIRE DDRD|=(0x80) //里面什么都不用该!
#define CLR_OP_1WIRE PORTD&=~(0x80)
#define SET_OP_1WIRE PORTD|=(0x80)
#define CHECK_IP_1WIRE (PIND & 0x80) //检测
unsigned char tem;
unsigned char H_tem,L_tem;
unsigned char wmh,wml;
uchar s[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
/*延时函数 */
void delayus(unsigned int i)
{
while(i--);
}
void delayms(unsigned int ms) //10us延时函数
{
while(ms--)
{
delayus(1000);
}
}
/* 18B20 函数*/
unsigned char wmh,wml;
//-------------延时程序------------------------
void delay_3us(void) //3us延时函数
{
;
}
void delay_10us(void) //10us延时函数
{
unsigned int i;
for (i=0;i<2;i++);
}
void delay_30us(void) //30us延时函数
{
unsigned int n;
for(n=10;n>0;n--)
asm("nop");
}
void delay_600us(void) //600us延时函数
{
unsigned.
int i;
for(i = 0;i < 49;i ++)
delay_10us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<407;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i
}
///////////18b20初始化/////////////////////
void init_1820()
{
SET_DIR_1WIRE; //设置PD0 为输出
SET_OP_1WIRE;
CLR_OP_1WIRE;
delay_600us(); //480us以上
SET_OP_1WIRE;
CLR_DIR_1WIRE;
delay_30us(); //15~60us
while(CHECK_IP_1WIRE);
SET_DIR_1WIRE;
SET_OP_1WIRE;
delay_30us();
delay_30us();
delay_30us();
delay_30us(); //60~240us
}
/////////写入///////////////////
void write_1820(unsigned char x)
{
unsigned char m;
for(m=0;m<8;m++)
{
CLR_OP_1WIRE;
if (x&(1<
else
{CLR_OP_1WIRE;}
delay_30us(); //15~60us
SET_OP_1WIRE;
}
SET_OP_1WIRE;
}
////////////读出//////////////////////
unsigned char read_1820()
{
unsigned char temp,k,n;
temp=0;
for(n=0;n<8;n++)
{
CLR_OP_1WIRE;
SET_OP_1WIRE;
CLR_DIR_1WIRE;
k=(CHECK_IP_1WIRE); //读数据,从低位开始
if(k)
temp|=(1<
temp&=~(1<
delay_30us(); //60~120us
SET_DIR_1WIRE;
}
return (temp);
}
///////////////读取温度值////////////////////////////
void gettemp(void)
{
unsigned char temh,teml,wm0,wm1,wm2,wm3;
init_1820(); //复位18b20
write_1820(0xcc); // 发出转换命令
write_1820(0x44);
init_1820();
write_1820(0xcc); //发出读命令
write_1820(0xbe);
teml=read_1820(); //读数据
temh=read_1820();
wm0=teml>>4; //只要高8位的低四位和低8位的高四位,温度范围0~99啦!
wm1=temh<<4;
wm2=wm1+wm0; //16进制转10进制
wm3=wm2/100;
wmh=(wm2%100)/10; //出口参数了!wmh是显示的高位,wml使显示的低位
wml=(wm2%100)%10;
}
这个是我做过的 一个关于18B20的 驱动程序 是AVR 的 楼主可以参考一下
先看程序有问题没,如果程序没问题的话,那就是硬件电路处问题了,估计你是DS18B20接反了,要不就是DS18B20温度传感器是个坏的,用万用表检查下就知道
呵呵 最近我呀在研究这个问题