非常好的18B20驱动程序
/****************************************************************************** *
* 标题: 试验数码管上显示温度*
*
*
******************************************************************************* **
* 1.通过本例程了解DLASS18b20的基本原理和使用,理解并掌握18B20驱动程序的编写*
* 2.了解掌握I2C总线接口的工作原理及一般编程方法。
*
* 插上18B20 观察数码管的实际温度显示
* 用排线将JP8(P1口) 与J12 连接在数码管上可以看温度显示
*
* * * * 请学员认真消化本例程,懂DLASS18b20在C语言中的操作*
******************************************************************************* */
#include <reg52.H>
extern GetTemp(); //声明引用外部函数
extern unsigned int idata Temperature; // 声明引用外部变量
void delay(unsigned int i);
//else IO
sbit LS138A=P2^2; //管脚定义
sbit LS138B=P2^3;
sbit LS138C=P2^4;
// 此表为LED 的字模// 0 1 2 3 4 5 6 7 8 9 A b c d E - L P U Hidden _ (20)
unsigned char code Disp_Tab[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0xbf,0xc7,0x8 c,0xc1, 0xff, 0xf7 };
unsigned long LedOut[5],LedNumVal;
void system_Ini()
{
TMOD|= 0x11;
TH1=0xDC; //11.0592M
TL1=0x00;
IE = 0x8A;
TR1 = 1;
}
main()
{ unsigned char i;
system_Ini();
while(1)
{
GetTemp();
/********以下将读18b20的数据送到LED数码管显示*************/
LedNumVal=Temperature;
LedOut[0]=Disp_Tab[LedNumVal%10000/1000];
LedOut[1]=Disp_Tab[LedNumVal%1000/100];
LedOut[2]=Disp_Tab[LedNumVal%100/10]; //十位
LedOut[3]=Disp_Tab[LedNumVal%10]; //个位
for(i=0; i<4; i++)
{
P1 = LedOut[i] ;
switch(i)
{ //138译码
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
//case 4:LS138A=0; LS138B=0; LS138C=1; break;
}
delay(150);
}
P1 = 0XFF;
}
}
//延时程序
void delay(unsigned int i) {
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--); }。
温度传感器18B20驱动程序(C语言)
温度传感器18B20驱动程序(C语言)我精心整理的一个温度传感器18B20的程序包,可直接调用这个程序适用于12MHz晶振的51单片机,默认为89C52系列,如果是89C51的话,把头文件改为#include "reg51.h"就可以了,下面是头文件和C文件,大家在工程里加入C文件,在主程序中加入头文件,直接调用read_temp();函数,就可以返回浮点型的温度值,为10进制,可以处理后直接用LCD或数码管显示,很方便,是我做温控的子程序。
/*************头文件******************/#include "reg52.h"#include "intrins.h" //_nop_();延时函数用#define uchar unsigned char#define uint unsigned intsbit DQ="P3"^4; //温度输入口ow_reset(void);//18B20复位void write_byte(uchar val);//写命令uchar read_byte(void);//读数据/***********读出温度函数**********输入:无输出:zs+xs,格式为123.4即为浮点型数的温度格式===================================*/float read_temp();/***************C文件**********************/#include "reg52.h"#include "18B20.h" //_nop_();延时函数用//*******温度小数部分用查表法**********//uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09}; /***********11微秒延时函数**********///void delay(uint t){for(;t>0;t--);}///***********18B20复位函数**********/ow_reset(void){char presence="1";while(presence){while(presence){DQ=1;_nop_();_nop_();DQ=0; //delay(50); // 550usDQ=1; //delay(6); // 66uspresence=DQ; // presence="0继续下一步"}delay(45); //延时500uspresence = ~DQ;}DQ=1;}/////**********18B20写命令函数*********///向 1-WIRE 总线上写一个字节void write_byte(uchar val){uchar i;for (i=8; i>0; i--) //{DQ=1;_nop_();_nop_();DQ = 0;_nop_();_nop_();_nop_();_nop_();_nop_();//5us DQ = val&0x01; //最低位移出delay(6); //66usval=val/2; //右移一位}DQ = 1;delay(1);}///*********18B20读1个字节函数********///从总线上读取一个字节uchar read_byte(void){uchar i;uchar value = 0;for (i=8;i>0;i--){DQ=1;_nop_();_nop_();value>>=1;DQ = 0; //_nop_();_nop_();_nop_();_nop_(); //4usDQ = 1;_nop_();_nop_();_nop_();_nop_(); //4usif(DQ)value|=0x80;delay(6); //66us}DQ=1;return(value);}///***********读出温度函数**********输入:无输出:zs+xs,格式为123.4即为浮点型数的温度格式===================================*/ //float read_temp(){float zs,xs;//存放整数和小数uchar temp_data[2];//读出温度暂存EA=0;//禁止中断,防止读错ow_reset(); //总线复位write_byte(0xCC); // 发Skip ROM命令write_byte(0xBE); // 发读命令temp_data[0]=read_byte(); //温度低8位temp_data[1]=read_byte(); //温度高8位ow_reset();write_byte(0xCC); // Skip ROMwrite_byte(0x44); // 发转换命令xs=ditab[temp_data[0]&0x0f];//小数位xs=xs/10;zs=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);//整数位return (zs+xs);EA=1;//开中断}//结束当然,大家做数字温度计的话直接使用这个程序就可以了,但是如果要做多点温控的话,大家还要自己写查找序列号的程序!加油!/************************************************************ *18B20驱动程序,DQ为数据口,接于P2.1*11.0592M晶振,上拉4.7k电阻*Author:fyb*2005-3-25 11:23,OK!*************************************************************/ #i nclude<reg51.h>#i nclude<intrins.h>#define uchar unsigned char#define uint unsigned intsbit dq = P1^7;bit flag;uint Temperature;uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节uchar id_buff[8];uchar *p;uchar crc_data;uchar code CrcTable [256]={0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62 , 96, 130, 220,35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 12 8, 222, 60, 98,190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29 , 67, 161, 255,70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210,48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 2 05,17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 2 38,50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 1 45, 207, 45, 115,202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 2 44, 170, 72, 22,233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 2 15, 137, 107, 53};///************************************************************ *Function:延时处理*parameter:*Return:*Modify:*************************************************************/ void TempDelay (uchar us){while(us--);}/************************************************************ *Function:18B20初始化*parameter:*Return:*Modify:*************************************************************/ void Init18b20 (void){dq=1;_nop_();dq=0;TempDelay(86); //delay 530 uS//80_nop_();dq=1;TempDelay(14); //delay 100 uS//14_nop_();_nop_();_nop_();if(dq==0)flag = 1; //detect 1820 success!elseflag = 0; //detect 1820 fail!TempDelay(20); //20_nop_();_nop_();dq = 1;}/************************************************************ *Function:向18B20写入一个字节*parameter:*Return:*************************************************************/ void WriteByte (uchar wr) //单字节写入{uchar i;for (i=0;i<8;i++){dq = 0;_nop_();dq=wr&0x01;TempDelay(5); //delay 45 uS //5_nop_();_nop_();dq=1;wr >>= 1;}}/************************************************************ *Function:读18B20的一个字节*parameter:*Return:*Modify:*************************************************************/ uchar ReadByte (void) //读取单字节{uchar i,u=0;for(i=0;i<8;i++){dq = 0;u >>= 1;if(dq==1)u |= 0x80;TempDelay (4);_nop_();}return(u);}/************************************************************ *Function:读18B20*parameter:*Return:*Modify:*************************************************************/ void read_bytes (uchar j){uchar i;for(i=0;i<j;i++){*p = ReadByte();p++;}}/************************************************************ *Function:CRC校验*parameter:*Return:*Modify:*************************************************************/ uchar CRC (uchar j)uchar i,crc_data=0;for(i=0;i<j;i++) //查表校验crc_data = CrcTable[crc_data^temp_buff[i]];return (crc_data);}/************************************************************ *Function:读取温度*parameter:*Return:*Modify:*************************************************************/ void GemTemp (void){read_bytes (9);if (CRC(9)==0) //校验正确{Temperature = temp_buff[1]*0x100 + temp_buff[0];// Temperature *= 0.625;Temperature /= 16;TempDelay(1);}}/************************************************************ *Function:内部配置*parameter:*Return:*Modify:*************************************************************/ void Config18b20 (void) //重新配置报警限定值和分辨率Init18b20();WriteByte(0xcc); //skip romWriteByte(0x4e); //write scratchpadWriteByte(0x19); //上限WriteByte(0x1a); //下限WriteByte(0x7f); //set 11 bit (0.125)Init18b20();WriteByte(0xcc); //skip romWriteByte(0x48); //保存设定值Init18b20();WriteByte(0xcc); //skip romWriteByte(0xb8); //回调设定值}/************************************************************ *Function:读18B20ID*parameter:*Return:*Modify:*************************************************************/ void ReadID (void)//读取器件 id{Init18b20();WriteByte(0x33); //read romread_bytes(8);}/************************************************************ *Function:18B20ID全处理*parameter:*Return:*Modify:*************************************************************/ void TemperatuerResult(void){p = id_buff;ReadID();Config18b20();Init18b20 ();WriteByte(0xcc); //skip romWriteByte(0x44); //Temperature convertInit18b20 ();WriteByte(0xcc); //skip romWriteByte(0xbe); //read Temperaturep = temp_buff;GemTemp();}。
STM32下DS18B20驱动程序,适用于ucos系统
#include "bsp.h"
HAL_DS18B20_T HAL_DS18B20;
/*********************************
硬件配置DATA:PC1
*********************************/
#define DS18B20GPIO GPIOA
#define DS18B20PIN GPIO_Pin_7
*形参:无
*返回值:无
*********************************************************************************************************
*/
int DS18B20_read_bit(void)
{
int ret;
return ret;
}
/*
*********************************************************************************************************
*函数名: DS18B20_write_byte
*功能说明: DS18B20写1个字节,低字节先写;
{
int i;
for(i=0; i<8; i++)
{
DS18B20_write_bit((data>>i)&0x01);
}
}
/*
*********************************************************************************************************
12864 18b20好使的C程序
void LCDSendWord(unsigned char *p) //向LCD发送一个字符串,
{
while(*p>0) //比如void LCDSendWord('你好');就是让LCD显示"你好"
{
WriteDataLCD(*p);
uchar TX=0; //转换后的温度值小数部分 //DS18B20的数据线
void delay(uint x);
void reset (void);
void write(uchar val);
uchar read(void);
Delay(5);
}
/******************给LCD写入指令**********************/
void WriteCommandLCD(unsigned char WCLCD) //BuysC为0时忽略忙检测
{
CheckBusy(); //检测忙
void LCDTestWord(bit i,unsigned char word);
void Delay(unsigned char num);
/**********定义温度传感器DS18B20的数据线和相关函数*************/
sbit DQ=P2^7;
char TZ=0; //转换后的温度值整数部分
val=val>>1;
}
}
uchar read(void) //18b20读字节
{
uchar i,d=0;
uchar value=0; //读t;0;i--)
DS18B20 arm驱动程序
dat=(j>1);
}
returndat;
}
voidDS18B20_writebyte(unsignedchardat)
{
unsignedinti;
unsignedcharj;
for(i=1;i{
j=dat&0x01;
dat=dat>>1;
if(j)
{
#include
#include
#include
#include
#defineDEVICE_NAMEDS18B20
#defineDS18B20_MAJOR231
#defineDQS3C2410_GPG0
#defineCLKS3C2410_GPB0
#defineW1S3C2410_GPH9
#defineW2S3C2410_GPE10
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(10);
s3c2410_gpio_setpin(DQ,HIGH);
udelay(45);
}
else
{
s3c2410_gpio_cfgpin(DQ,OUT);
s3c2410_gpio_setpin(DQ,LOW);
udelay(15);
s3c2410_gpio_cfgpin(DQ,IN);
bit=s3c2410_gpio_getpin(DQ);
udelay(45);
returnbit;
}
unsignedcharDS18B20_readbyte(void)
51单片机写DS18B20的驱动
51单片机写DS18B20的驱动初始化操作方法:单片机先给总线一个高电平,略微延时,然后拉低总线,至少延时480us(我们取600us),然后拉高总线,等待15~60us 的时间(我们取80us),此后DS18B20 如果响应,则会发出0,拉低总线,否则为1,单片机检测是否响应的时间在60~240us 之内,之后释放总线,程序如下:bitDS18B20_init()//初始化{uchar i;bit flag_response=0;DQ=1;_nop_();_nop_();DQ=0;delay_us(90); //延时600usDQ=1;delay_us(10); //延时80usfor(i=0;i{_nop_();_nop_();_nop_();_nop_(); _nop_();if(!DQ){ flag_response=1;break;}}delay_us(63);//延时420usDQ=1;//释放总线return(flag_response);}写操作方法:由于是单总线,读写操作都分为写0 和写1,从一个字节的最低位逐次往DS18B20 里写,先使总线产生一个由1 到0 的跳变,等待15us(我们取13us),然后判断要写的那位是1 还是0,如果是1,则拉高总线,0 则不去操作总线(因为总线开始产生跳变时已被拉低),等待45us(我们取64us)以后释放总线(这段时间DS18B20 会采样,如上图所示),程序如下:void DS18B20_write(uchar dat)//写1 个字节{uchar i;for(i=0;i{DQ=1;_nop_(); _nop_();DQ=0;delay_us(1);//延时13usif(dat&0x01)DQ=1;delay_us(8);//延时64usDQ=1;//释放总线dat>>=1;}}读操作方法:使总线产生一个由1 到0 的跳变,等待1us,马上拉高总线,等待大约12 个us(datasheet 推荐的采样时间是快接近15us 的时候,我们略微提前),单片机采样,如果总线被拉低,则读出的是0,否则就读出1,然后延。
DS18B20驱动程序
DS18B20驱动程序作者: 嵌入式应用软件园目录第1章程序功能 (3)第2章程序代码 (4)第3章DS18B20简介 (11)3.1 DS18B20概述 (11)3.2 DS18B20的内部结构 (11)3.3 DS18B20温度传感器的存储器 (12)3.4 DS18B20使用中注意事项 (13)3.4 DS18B20常用命令详述 (14)DS18B20驱动程序DS18B20_MAIN,您可以直接在你的应用当中使用,直接返回温度的BCD码值。
BCD码值可以直接在数码管或LCD 上显示出来。
驱动程序中包含DS18B20温度传感器用到的全部函数。
如,DS18B20的复位函数(RESET_18B20)、写函数(WRITE_18B20)、读函数(READ_18B20),读出ROM中64的ID函数(READ_18B20ID),搜索DS18B20的ID函数(CHECK_18B20ID),警报搜索(ALARM_18B20ID)。
直接调用函数RE_TEMP,会返回当前的温度值。
再经过函数CONV_TEMP转换后,可以返回温度值的BCD码值。
我们的辛勤付出,需要您的肯定,请访问: 嵌入式应用软件园。
如需源代码/此代码技术支持,请访问: 嵌入式应用软件园。
第3章DS18B20简介3.1 DS18B20概述DS18B20 “一线总线”数字化温度传感器是DALLAS最新单线数字温度传感器,测量温度范围为-55°C~+125°C,在-10~+85°C范围内,精度为±0.5°C。
现场温度直接以“一线总线”的数字方式传输,大大提高了系统的抗干扰性。
适合于恶劣环境的现场温度测量,支持3V~5.5V的电压范围,使系统设计更灵活、方便。
而且新一代产品更便宜,体积更小。
DS18B20可以程序设定9~12位的分辨率,精度为±0.5°C。
可选更小的封装方式,更宽的电压适用范围。
简洁的DS18B20驱动程序
DS18B20_SET_OUT(); DS18B20_DelayUs(90); if( DS18B20_READ_IN() ) rBit = 1; else rBit = 0; if( gIE ) // 恢复当前全局中断的状态 __enable_interrupt(); DS18B20_DelayUs(200); DS18B20_DelayUs(200); $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ ** DS18B20 读位函数 ** 注意: (1)时序的准确性 (2)开关全局中断 */ static BOOLEAN DS18B20_ReadBit(void) { BOOLEAN rBit; BOOLEAN gIE = 0; if( __get_SR_register() & GIE ) // 保存当前全局中断的状态 {
#define COM_MEM_COPY_SCR 0X48 #define COM_MEM_RECALL_E 0XB8 #define COM_MEM_READ_POWER 0XB4 /*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ ** US 延迟函数 ** 注意:必须根据不同的 MCU、晶体频率而做更改 */ static void DS18B20_DelayUs(INT8U us) // @8MHZ { for(; us>0; us--) {
gIE = 1; __disable_interrupt(); } DS18B20_CLR_OUT(); DS18B20_DelayUs(4); DS18B20_SET_OUT(); DS18B20_DelayUs(4); if( DS18B20_READ_IN() ) rBit = 1; else rBit = 0; DS18B20_DelayUs(60); if( gIE ) // 恢复当前全局中断的状态
ds18b20驱动程序
/*包含了这么多的头文件,也不知道有的有没有用*/#include<linux/platform_device.h>#include<linux/delay.h>#include<linux/fs.h>#include<linux/module.h>#include<linux/kernel.h>#include<linux/uaccess.h>#include<linux/clk.h>#include<asm-arm/arch-s3c2410/regs-gpio.h>#include<asm/io.h>#include<asm/arch/map.h>#include<asm-arm/plat-s3c24xx/clock.h>#include<asm/arch/regs-clock.h>/*和引脚相关的宏定义*/#define DQ S3C2410_GPH9#define CFG_IN S3C2410_GPH9_INP#define CFG_OUT S3C2410_GPH9_OUTPstatic int opencount = 0;unsigned char init_ds(void)//ds18b20复位,返回0成功,返回1失败{unsigned char ret = 0;s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_pullup(DQ, 0);s3c2410_gpio_setpin(DQ, 1);s3c2410_gpio_setpin(DQ, 0);udelay(500);s3c2410_gpio_setpin(DQ, 1);udelay(50);s3c2410_gpio_cfgpin(DQ, CFG_IN);udelay(200);ret = s3c2410_gpio_getpin(DQ);s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_pullup(DQ, 0);s3c2410_gpio_setpin(DQ, 1);return ret;}void write_byte(char data)//向18b20写一个字节{char i = 0;s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_pullup(DQ, 1);for(i=0; i<8; i++){s3c2410_gpio_setpin(DQ, 0);udelay(10);s3c2410_gpio_setpin(DQ, 1);s3c2410_gpio_setpin(DQ, data&0x01);udelay(60);s3c2410_gpio_setpin(DQ, 1);udelay(2);data >>= 1;}}unsigned char read_byte(void)//从18b20读一个字节{unsigned char i;unsigned char data=0;s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_pullup(DQ, 0);for(i=0; i<8; i++){s3c2410_gpio_setpin(DQ, 0);udelay(1);s3c2410_gpio_setpin(DQ, 1);s3c2410_gpio_cfgpin(DQ, CFG_IN);udelay(10);data >>= 1;if(s3c2410_gpio_getpin(DQ))data |= 0x80;udelay(50);s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_pullup(DQ, 0);s3c2410_gpio_setpin(DQ, 1);udelay(2);}return data;}/*文件的写函数,没有实际意义,用户不能写文件*/static ssize_t ds18b20_write(struct file*filp,const char*buffer, size_t count, loff_t *ppos){return 10;//随便返回一个正整数}/*18b20的读函数,读出的温度只保留了整数*/static ssize_t ds18b20_read(struct file*filp,char*buffer,size_t count, loff_t *ppos){unsigned char a=0, b=0;unsigned int t=0;unsigned char temp=0;unsigned char flag;flag = init_ds();if(flag&0x01)return-1;write_byte(0x0cc);write_byte(0x44);udelay(500);flag = init_ds();if(flag&0x01)return-1;write_byte(0x0cc);write_byte(0x0be);a = read_byte();b = read_byte();flag = init_ds();temp = ((b & 0x7) << 4)) | (a >> 4);copy_to_user(buffer,&temp, 1);return 1;}static int ds18b20_open(struct inode *node,struct file*file) {unsigned char flag;if(opencount == 1)return-EBUSY;flag = init_ds();if(flag&0x01){printk("uable to open device!/n");return-1;}else{opencount++;printk("device opened!/n");return 0;}}static int ds18b20_release(struct inode *node,struct file*file) {opencount--;printk("device released!/n");return 0;}static struct file_operations ds18b20_fops ={.owner = THIS_MODULE,.write= ds18b20_write,.read= ds18b20_read,.release = ds18b20_release,.open= ds18b20_open,};static int ds18b20_probe(struct platform_device *pdev){int ret;s3c2410_gpio_cfgpin(DQ, CFG_OUT);s3c2410_gpio_setpin(DQ, 0);ret = register_chrdev(0,"ds18b20",&ds18b20_fops);if(!ret)printk("--------------Uable to register driver!-------------------/n");printk("Probe for ds18b20 finished!/n");return 0;}static struct platform_driver ds18b20 ={.probe = ds18b20_probe,.driver ={.name ="s3c2410-ds",.owner = THIS_MODULE,},};int __init ds18b20_init(void){printk("Initial driver for ds18b20....................../n");return platform_driver_register(&ds18b20);}void __exit ds18b20_exit(void){return platform_driver_unregister(&ds18b20);}module_init(ds18b20_init);module_exit(ds18b20_exit);MODULE_AUTHOR("********************"); MODULE_DESCRIPTION("DS18B20 driver for s3c2410"); MODULE_LICENSE("GPL");。
51单片机驱动DS18B20温度传感器程序及心得
51单片机驱动DS18B20温度传感器程序及心得关于DS18B20温度传感器,在没有硬件设备的辅助下,写内部程序有些困难,因为看不到实际信号波形。
对于单片机,我。
渐渐的有些心灰意冷。
虽然掌握了1_WIRE总线,却少了很多喜悦,下雨了。
它是我的爱好,我付出了很多,可是我看不到实际的前景。
以我个人之力,要步入尖端芯片领域,很困难,在这里,采棉花是个普遍性的大问题,大型机械设备缺陷很多,如果以微控制芯片提高精度,我想效益会相当可观,可是技术瓶颈难以逾越。
硬件研发,失败了,所有投入赴之东流,成功了,回报丰厚。
现在,各行各业都处于饱和,没有成熟先进的技术,很难有立足之地,,,,,我开始重新审视我的选择。
艰难。
/*建立时间: 2013年5月2日;前言: 我用软件仿真,测算延时时间,效果不错,但是根据教程,复位时,先释放总线(wd高电平),然后主机拉低wd,持续时间为400--960微妙.后主机拉高wd,持续15-60微妙,后从机,会拉低电平持续时间是60--240微妙(此时表示复位成功),如果从机没有将总线拉低,则复位失败.然后,主机拉高电平60--240微妙.复位结束;可,实验证明,在,从机,拉低总线电平后,持续一定时间,从机还会将总线拉高!这是教程中的一个重大错误!temperature sensor reset module finishing time: 23:08:00(温度传感器复位模块完成时间 )temperature sensor 操作过程:1. reset DS18B20;2. 发出Skip ROM 命令(CCH); (跳跃ROM命令)3. 发出Convert T命令(44H); (温度转换命令)4. reset DS18B20;5. 发出Skip ROM命令(CCH);(跳跃ROM命令)6. 发出读取命令(BEH);7. 读出两个字节的温度;8. 温度格式转换;2013年5月8日22:56:44DS18B20 Temperature sensor read data module accomplish;(温度传感器读数据模块完成)现在还有温度显示模块没有完成,硬件是1602液晶屏......2013年5月9日19:32:31今天,温度传感器的程序主体结构全部完成!!!!! 不容易啊,值得庆祝一下!!!!!!一共写了7天程序!{陆陆续续};喝个品酸乳果汁.......*/#include ;typedef unsigned char uint8 ;typedef unsigned int uint16;sbit wd = P3^2; //定义数据单总线;sbit e =P1^5; // 定义1602液晶显示器数据使能端口; sbit rs=P1^0; // 定义数据/指令选择端口;sbit rw=P1^1; // 定义读/写选择端口;sbit BF=P0^7; // 定义繁忙位;bit w=0; //定义一个全局一位变量;//===========1602液晶显示器模块;=============== busy() //液晶屏繁忙检测函数;{ e=0;rs=0;rw=0;P0=0xff;do{ e=0;//使能位清零;rs=0;//指令;rw=1;//读;e=1;//数据传输启动;}while(BF); //如果BF==0;则液晶处于空闲状态; e=0;}play_data(uint8 wr) //液晶写入数据;{busy();//繁忙检测;P0=wr;//装载数据;rs=1;//数据;rw=0;//写入;e=1;//传输开始;e=0;//传输结束;}play_cmd(uint8 cmd) //液晶写入指令; {busy(); //繁忙检测;P0=cmd; //装载数据;rs=0;//指令;rw=0;//写入;e=1;//传输开始;e=0;//传输结束;}reset_1602()//1602液晶显示器初始化函数;play_cmd(0x38);play_cmd(0x0c);play_cmd(0x06);play_cmd(0x01);}//=========温度传感器延时模块============================// sbit led= P1^0;delay(uint8 num )//如果unm等于1;延时16.28微妙; {while(num--); //如果num大于一,则16.28+(num-1)*6.51.}delay2()//此函数延时3.26微秒;{uint8 j=0;j=9;}delay3()uint16 s=60000;while(s--);}reset_1820() //========复位温度传感器; ======== {while(wd){wd=1;delay(140);//拉高总线,延时大概921微妙左右;(延时值自定); wd=0;//总线由单片机拉低,下为延时函数,大概800微秒左右;delay(61); //1个此函数会延时400微妙左右;delay(61); //两个是800微妙左右;wd=1;//主机拉高总线,68微秒左右;delay(9);//延时68微妙左右if(wd==0)//如果wd是0就终止复位;(代表复位成功);{while(wd==0);//总线一旦为低,那么就等待从机再将总线拉高. break;//终止while循环;(reset function end)}else{wd=1;delay(20);//延时140微妙;}}delay(30); //此时总线为高电平并延时205微妙,复位成功!;// if(wd)led=0;//此语句为检验是否复位成功;P1^0外接9012三极管接led小灯;}write_byte(uint8 dat){uint8 i=0;for(i=0;i;>;=1;delay(6); //A点到此处用时65.11微秒;wd=1;//总线释放;delay2(); //延时3.26微秒;}}//========此函数执行完成之后总线为高电平; uint8 read_byte() //=====读8位数据;==================={uint8 j=0, dat =0;for(j=0;j;>;=1;wd=0; // A点 mcu拉低电平3.26微秒;delay2();// 延时3.26微秒;wd=1;if(wd){dat|=0x80;}//读完数据后A点到此处是11.93微秒,保持在15微秒之内;delay(9);// A点到此处80.29微秒; 理想时间范围是60--120微秒;wd=1; //释放总线;delay2(); //延时3.26微秒;}return dat;}start_sensor()//启动传感器;{reset_1820();write_byte(0xcc); //跳跃命令;write_byte(0x44); //转换temperature(温度)命令; }uint8 read_temp()//从温度传感器度温度数据过程;{uint8 ak[2];uint16 dat=0 , j=0;reset_1820();//复位温度传感器write_byte(0xcc); //跳跃rom命令;write_byte(0xbe) ; // 发出读数据命令;ak[0]=read_byte(); //读取第一个字节数据;ak[1]=read_byte(); //读取第二个字节数据;dat=ak[1];//要把两个八位数据载入1个16位变量里;dat;>;11)==0x1f) //此语句是负温度进入.0x1f是二进制5个全1;{dat=(~dat)+1;//负温度要取反加一操作;dat/=16;//传感器给的温度系数要除以16后,得到的数才是常规温度系数;w=0;//此语句是在主函数中用来判断是正温度还是负温度;return dat; //向主函数返回数据,并终止函数; }j=dat;if((j>;>;11)==0)//如果是零则是正温度;{dat/=16;//数据直接除以16,就得到了常规温度系数;w=1;//1代表正;return dat; //向主函数返回数据,并终止函数;}return 130; //向主函数返回数据,并终止函数;}delay_ms() //延时1秒;{uint8 i=250;uint16 j=608;while(j--){ while(i--);i=250;}}error() //测温出错;{uint8 i=5, j=0 ,ak[]="Error!"; while(i--)play_cmd(0x82);while(ak[j]!='\0'){play_data(ak[j++]);} j=0;delay3();play_cmd(0x01);delay3();}}main(){uint8dat=0 ,j=3,len=0 ,num[]="start.....";reset_1602();while(j--)//这是启动电源时,液晶显示:start....(并闪烁3次){play_cmd(0x82);while(num[len]!='\0'){play_data(num[len++]);}len=0;delay3();play_cmd(0x01);delay3();}while(1){start_sensor();//启动温度传感器;delay_ms();//等待1秒;dat = read_temp(); //读取温度数据并赋给dat变量;if(dat==130)//如果返回来的数据是130,表明采集温度出错;{error();}else if(w==1) //如果w是1,就代表正温度.输出;{play_cmd(0x83);play_data(dat/10+'0');play_data(dat%10+'0');}else//否则,就是负温度,输出时前面加一个负号;{play_cmd(0x82);play_data('-');play_data(dat/10+'0');play_data(dat%10+'0');}} //我的亲娘四舅奶奶啊!!!!!!终于完成了!!!!2013年5月9日19:32:10}。
温度传感器18B20驱动程序(C语言)
//1楼主:温度传感器18B20驱动程序(C语言)/*************************************************************18B20驱动程序,DQ为数据口,接于P2.1*11.0592M晶振,上拉4.7k电阻*************************************************************/#define uchar unsigned char#define uint unsigned intsbit dq = P1^7;bit flag;uint Temperature;uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节uchar id_buff[8];uchar *p;uchar crc_data;uchar code CrcTable [256]={0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176,238,50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};///*************************************************************Function:延时处理*parameter:*Return:*Modify:*************************************************************/void TempDelay (uchar us){while(us--);}/*************************************************************Function:18B20初始化*parameter:*Return:*Modify:*************************************************************/void Init18b20 (void){dq=1;_nop_();dq=0;TempDelay(86); //delay 530 uS//80_nop_();dq=1;TempDelay(14); //delay 100 uS//14_nop_();_nop_();_nop_();if(dq==0)flag = 1; //detect 1820 success!elseflag = 0; //detect 1820 fail!TempDelay(20); //20_nop_();_nop_();dq = 1;}/************************************************************ *Function:向18B20写入一个字节*parameter:*Return:*Modify:*************************************************************/ void WriteByte (uchar wr) //单字节写入{uchar i;for (i=0;i<8;i++){dq = 0;_nop_();dq=wr&0x01;TempDelay(5); //delay 45 uS //5_nop_();_nop_();dq=1;wr >>= 1;}}/************************************************************ *Function:读18B20的一个字节*parameter:*Return:*Modify:*************************************************************/ uchar ReadByte (void) //读取单字节{uchar i,u=0;for(i=0;i<8;i++){dq = 0;u >>= 1;dq = 1;if(dq==1)u |= 0x80;TempDelay (4);_nop_();}return(u);}/************************************************************ *Function:读18B20*parameter:*Return:*Modify:*************************************************************/ void read_bytes (uchar j){uchar i;for(i=0;i {*p = ReadByte();p++;}}/************************************************************ *Function:CRC校验*parameter:*Return:*Modify:*************************************************************/ uchar CRC (uchar j){uchar i,crc_data=0;for(i=0;i crc_data = CrcTable[crc_data^temp_buff[i]]; return (crc_data);}/************************************************************ *Function:读取温度*parameter:*Return:*Modify:*************************************************************/ void GemTemp (void){read_bytes (9);if (CRC(9)==0) //校验正确{Temperature = temp_buff[1]*0x100 + temp_buff[0];// Temperature *= 0.625;Temperature /= 16;TempDelay(1);}}/************************************************************ *Function:内部配置*parameter:*Return:*Modify:*************************************************************/ void Config18b20 (void) //重新配置报警限定值和分辨率{Init18b20();WriteByte(0xcc); //skip romWriteByte(0x4e); //write scratchpadWriteByte(0x19); //上限WriteByte(0x1a); //下限WriteByte(0x7f); //set 11 bit (0.125)Init18b20();WriteByte(0xcc); //skip romWriteByte(0x48); //保存设定值Init18b20();WriteByte(0xcc); //skip romWriteByte(0xb8); //回调设定值}/************************************************************ *Function:读18B20ID*parameter:*Return:*Modify:*************************************************************/ void ReadID (void)//读取器件 id{Init18b20();WriteByte(0x33); //read romread_bytes(8);}/************************************************************ *Function:18B20ID全处理*parameter:*Return:*Modify:*************************************************************/ void TemperatuerResult(void){p = id_buff;ReadID();Config18b20();Init18b20 ();WriteByte(0xcc); //skip romWriteByte(0x44); //Temperature convertInit18b20 ();WriteByte(0xcc); //skip romWriteByte(0xbe); //read Temperaturep = temp_buff;GemTemp();}/*急什么?急着买吗?还是要程序?我给你提供一段吧!也是网友写的。
