温度传感器18b20toe2rom3读系列号C程序在52单片机上测试通过Word文件下载.docx
- 文档编号:7075364
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:19
- 大小:17.98KB
温度传感器18b20toe2rom3读系列号C程序在52单片机上测试通过Word文件下载.docx
《温度传感器18b20toe2rom3读系列号C程序在52单片机上测试通过Word文件下载.docx》由会员分享,可在线阅读,更多相关《温度传感器18b20toe2rom3读系列号C程序在52单片机上测试通过Word文件下载.docx(19页珍藏版)》请在冰点文库上搜索。
/************************************************************************/
main()
{
///unsignedcharNumber2[8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
//WrToROM_P(Number2,0,8);
//测试用从E2OM显示数据OK
ReadRom2EERom(0);
display(0,0xf0);
//mDelay(200);
display(4,0xf0);
}
voidtim(void)interrupt1using1//中断,用于数码管扫描和温度检测间隔
TH0=0xef;
//定时器重装值
TL0=0xf0;
num++;
if(num==50)
{num=0;
flag_get=1;
//标志位有效
second++;
if(second>
=60)
{second=0;
minute++;
}
count++;
if(count==1)
{P2=0;
dataled=str[0];
}//数码管扫描
if(count==2)
{P2=1;
dataled=str[1];
if(count==3)
{P2=2;
dataled=str[2];
count=0;
/*************************************************************************************/
voiddelay(unsignedinti)//延时函数
while(i--);
//char?
/***************************************************************************************/
//18b20初始化函数
voidInit_DS18B20(void)
unsignedcharx=0;
DQ=1;
//DQ复位
delay(8);
//稍做延时
DQ=0;
//单片机将DQ拉低
delay(80);
//精确延时大于480us;
9*(r7+1)+(256*9+1)*r6+1个指令周期;
[9*(80+1)+1]*1us=730us
//拉高总线
delay(10);
//100us
x=DQ;
//稍做延时后如果x=0则初始化成功x=1则初始化失败
delay(5);
//读一个字节
unsignedcharReadOneChar(void)
unsignedchari=0;
unsignedchardat=0;
for(i=8;
i>
0;
i--)
{
//给脉冲信号
dat>
>
=1;
if(DQ)
dat|=0x80;
return(dat);
//写一个字节
voidWriteOneChar(unsignedchardat)
unsignedchari=0;
for(i=8;
i>
i--)
DQ=dat&
0x01;
delay(5);
//读取温度
unsignedcharReadTemperature(void)
unsignedchara=0;
unsignedcharb=0;
unsignedchart=0;
unsignedcharadr=0;
//floattt=0;
Init_DS18B20();
WriteOneChar(0xCC);
//跳过读序号列号的操作
WriteOneChar(0x44);
//启动温度转换
delay(200);
//跳过读序号列号的操作
WriteOneChar(0xBE);
//读取温度寄存器等(共可读9个寄存器)前两个就是温度
a=ReadOneChar();
//LSB
b=ReadOneChar();
//MSB
b<
<
=4;
b+=(a&
0xf0)>
4;
//舍弃a的低4位,16*0.0625=1度,最小单位度;
读得数据带符号位若温度为负显示肯度错
t=b;
//tt=t*0.0625;
//t=tt*10+0.5;
//放大10倍输出并四舍五入
return(t);
voidReadRom2EERom(unsignedcharaddr)//该段中dat[]改为指针类型,将会出错,以后二者不要混用。
unsignedchari;
unsignedchardat[1];
dat[0]=1;
WriteOneChar(0x33);
//delay(200);
for(i=0;
i<
8;
i++)
dat[0]=ReadOneChar();
WrToROM_B(dat,addr+i,1);
*******************************************************************************
/*copyright2007,ShangHaiHaoTunElectronicTechnologyco.,Ltd
*
*ThissampleisusedforEEPROM24C02ReadandWrite.
*writebyShifang2007-4-29
*V1.1
*/
#include<
reg52.h>
intrins.h>
#defineAddWr0xae//WriteAddress
#defineAddRd0xaf//ReadAddress
//全局变量
sbitSda=P1^2;
//定义总线连接端口;
sda数据线;
SCL时钟线;
sbitScl=P1^1;
sbitWP=P1^0;
//Noconnecthere
externunsignedcharcodetab[];
voidmDelay(unsignedcharj)//Anormaldelay
unsignedinti;
for(;
j>
j--)
for(i=0;
125;
{;
//启动IIC总线//边沿触发
voidStart(void)
Sda=1;
_nop_();
_nop_();
Scl=1;
Sda=0;
Scl=0;
//时钟线的恢复
//停止IIC总线
voidStop(void)
//应答IIC总线
voidAck(void)
//非应答IIC总线
voidNoAck(void)
//发送一个字节
voidSend(unsignedcharData)
{
unsignedcharBitCounter=8;
unsignedchartemp;
do
temp=Data;
Scl=0;
//IIC协议
_nop_();
if((temp&
0x80)==0x80)
Sda=1;
//发送数据;
以这样的方式取位
else
Sda=0;
Scl=1;
temp=Data<
1;
Data=temp;
BitCounter--;
}
while(BitCounter);
//归零是必须的否则STOP()会出错
//读一个字节并返回变量
unsignedcharRead(void)
unsignedchartemp=0;
unsignedchartemp1=0;
Scl=1;
if(Sda)
temp=temp|0x01;
temp=temp&
0xfe;
if(BitCounter-1)
{
temp1=temp<
temp=temp1;
}
while(BitCounter);
return(temp);
//bytewritetoROM
voidWrToROM_B(unsignedcharData[],unsignedcharAddress,unsignedcharNum)
unsignedchari;
unsignedchar*PData;
PData=Data;
WP=0;
Num;
Start();
Send(AddWr);
//WriteAddress控制字1010,A2,A1,A0,R/W
Ack();
Send(Address+i);
//WritesubAddress
Send(*(PData+i));
//WriteData
Stop();
mDelay(20);
WP=1;
//pagewritetoROM(错误原因在哪?
)
voidWrToROM_P(unsignedcharData[],unsignedcharAddress,unsignedcharNum)
Start();
Send(Address);
Stop();
WP=1;
//readfromROM
voidRdFromROM_B(unsignedcharData[],unsignedcharAddress,unsignedcharNum)//RANDOMREAD
//WriteAddress
Send(Address+i);
Send(AddRd);
//ReadAddress
*(PData+i)=Read();
//ReadData
NoAck();
voidRdFromROM_P(unsignedcharData[],unsignedcharAddress,unsignedcharNum)
//WritesubAddress伪写
voiddisplay(unsignedcharAdrE2rom,unsignedchardelytime)//读数E2rom内容在数码管上显示OK
{
while(delytime)
{
unsignedchari;
//unsignedchartemp;
for(i=0;
{
unsignedcharDate[]={0};
unsignedchartemp;
unsignedcharDate2[2]={0,0};
//unsignedchar*p;
//p=Date;
RdFromROM_B(Date,AdrE2rom+i,1);
temp=Date[0];
temp=temp&
0xf;
Date2[0]=temp;
//低位
temp=(temp&
Date2[1]=temp;
//高位
P2=i*2;
P0=tab[Date2[1]];
mDelay
(1);
P2=i*2+1;
P0=tab[Date2[0]];
delytime--;
}
/*
voidmain()
#defineNUM_09
unsignedcharNumber[4]={0x06,0x5b,0x4f,0x66};
//显示码值1234
//unsignedcharNumber2[9]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};
//unsignedchar*Number2=0x5b;
//用指针不行?
?
unsignedcharNumber3[9];
unsignedcharNumber4[1]={0x3f};
unsignedcharj;
//写保护关掉
P2=0x00;
//WrToROM_B(Number2,0,9);
for(j=0;
j<
NUM_0;
j++)
WrToROM_B(Number4,j,1);
//清除数据
//mDelay(200);
WrToROM_P(Number,4,4);
//写入24c02
mDelay(200);
Number[0]=0;
//清除当前数据
Number[1]=0;
Number[2]=0;
Number[3]=0;
//*Number2=0;
//Number2[0]=0;
RdFromROM_P(Number3,0,NUM_0);
//RdFromROM(Number3,8,NUM_0);
//调用存储数据
while
(1)
P0=Number3[i];
//显示存储数据
mDelay(200);
i++;
if(i==NUM_0)
i=0;
/*while
(1)
P0=*Number2;
}*/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 温度传感器 18 b20toe2rom3 系列 程序 52 单片机 测试 通过