只需调用gettemp();就可以了!出口参数wmh是显示的高位,wml使显示的低位,然后调用你的显示程序就可以了!
/*************************************************************************
ds18b20头文件
M16 内部8M
*************************************************************************/
#define CLR_DIR_1WIRE DDRC&=~BIT(2) //只要修改这里的参数就可以了!呵呵!
#define SET_DIR_1WIRE DDRC|=BIT(2) //里面什么都不用该!
#define CLR_OP_1WIRE PORTC&=~BIT(2)
#define SET_OP_1WIRE PORTC|=BIT(2)
#define CHECK_IP_1WIRE (PINC & 0x04) //检测
unsigned char wmh,wml;
void init_1820()
{
SET_DIR_1WIRE; //设置PC2 为输出
SET_OP_1WIRE;
CLR_OP_1WIRE;
delay_nus(480); //480us以上
SET_OP_1WIRE;
CLR_DIR_1WIRE;
delay_nus(20); //15~60us
while(CHECK_IP_1WIRE);
SET_DIR_1WIRE;
SET_OP_1WIRE;
delay_nus(140); //60~240us
}
void write_1820(unsigned char x)
{
unsigned char m;
for(m=0;m<8;m++)
{
CLR_OP_1WIRE;
if(x&(1<
SET_OP_1WIRE;
else
{CLR_OP_1WIRE;}
delay_nus(40); //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<
else
temp&=~(1<
delay_nus(50); //60~120us
SET_DIR_1WIRE;
}
return (temp);
}
void gettemp() //读取温度值
{
unsigned char temh,teml,wm0,wm1,wm2,wm3;
init_1820(); //复位18b20
write_1820(0xcc); // 发出转换命令
write_1820(0x44);
// delay_nms(800); //不延时也好使,不知道怎么回事!
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;
}
//延时程序:
//delay.h
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i
delay_1ms();
}
关键字:AVR M16 DS18B20 头文件
引用地址:AVR M16 8M 的DS18B20头文件
/*************************************************************************
ds18b20头文件
M16 内部8M
*************************************************************************/
#define CLR_DIR_1WIRE DDRC&=~BIT(2) //只要修改这里的参数就可以了!呵呵!
#define SET_DIR_1WIRE DDRC|=BIT(2) //里面什么都不用该!
#define CLR_OP_1WIRE PORTC&=~BIT(2)
#define SET_OP_1WIRE PORTC|=BIT(2)
#define CHECK_IP_1WIRE (PINC & 0x04) //检测
unsigned char wmh,wml;
void init_1820()
{
SET_DIR_1WIRE; //设置PC2 为输出
SET_OP_1WIRE;
CLR_OP_1WIRE;
delay_nus(480); //480us以上
SET_OP_1WIRE;
CLR_DIR_1WIRE;
delay_nus(20); //15~60us
while(CHECK_IP_1WIRE);
SET_DIR_1WIRE;
SET_OP_1WIRE;
delay_nus(140); //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_nus(40); //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<
SET_DIR_1WIRE;
}
return (temp);
}
void gettemp() //读取温度值
{
unsigned char temh,teml,wm0,wm1,wm2,wm3;
init_1820(); //复位18b20
write_1820(0xcc); // 发出转换命令
write_1820(0x44);
// delay_nms(800); //不延时也好使,不知道怎么回事!
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;
}
//延时程序:
//delay.h
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i
}
小广播
热门活动
换一批
更多
设计资源 培训 开发板 精华推荐
最新单片机文章
更多精选电路图
更多热门文章
更多每日新闻
更多往期活动
11月13日历史上的今天
厂商技术中心