51单片机多点温度采集系统程序清单

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeAT89X52.h#includeIntrins.h#defineDATAP1//1602驱动端口//ROM操作命令#defineREAD_ROM0x33//读ROM#defineSKIP_ROM0xCC//跳过ROM#defineMATCH_ROM0x55//匹配ROM#defineSEARCH_ROM0xF0//搜索ROM#defineALARM_SEARCH0xEC//告警搜索//存储器操作命令#defineANEW_MOVE0xB8//重新调出E^2数据#defineREAD_POWER0xB4//读电源#defineTEMP_SWITCH0x44//启动温度变换#defineREAD_MEMORY0xBE//读暂存存储器#defineCOPY_MEMORY0x48//复制暂存存储器#defineWRITE_MEMORY0x4E//写暂存存储器//数据存储结构typedefstructtagTempData{unsignedcharbtThird;//百位数据unsignedcharbtSecond;//十位数据unsignedcharbtFirst;//个位数据unsignedcharbtDecimal;//小数点后一位数据unsignedcharbtNegative;//是否为负数}TEMPDATA;TEMPDATAm_TempData;//引脚定义sbitDQ=P2^7;//数据线端口sbitRS=P2^0;sbitRW=P2^1;sbitE=P2^2;//DS18B20序列号,通过调用GetROMSequence()函数在P1口读出(读8次)constunsignedcharcodeROMData1[8]={0x28,0x33,0xC5,0xB8,0x00,0x00,0x00,0xD7};//U1constunsignedcharcodeROMData2[8]={0x28,0x30,0xC5,0xB8,0x00,0x00,0x00,0x8E};//U2constunsignedcharcodeROMData3[8]={0x28,0x31,0xC5,0xB8,0x00,0x00,0x00,0xB9};//U3constunsignedcharcodeROMData4[8]={0x28,0x32,0xC5,0xB8,0x00,0x00,0x00,0xE0};//U4constunsignedcharcodeROMData5[8]={0x28,0x34,0xC5,0xB8,0x00,0x00,0x00,0x52};//U5constunsignedcharcodeROMData6[8]={0x28,0x35,0xC5,0xB8,0x00,0x00,0x00,0x65};//U6constunsignedcharcodeROMData7[8]={0x28,0x36,0xC5,0xB8,0x00,0x00,0x00,0x3C};//U7constunsignedcharcodeROMData8[8]={0x28,0x37,0xC5,0xB8,0x00,0x00,0x00,0x0B};//U8//判断忙指令voidBusy(){DATA=0xff;RS=0;RW=1;while(DATA&0x80){E=0;E=1;}E=0;}//写指令程序voidWriteCommand(unsignedcharbtCommand){Busy();RS=0;RW=0;E=1;DATA=btCommand;E=0;}//写数据程序voidWriteData(unsignedcharbtData){Busy();RS=1;RW=0;E=1;DATA=btData;E=0;}//清屏显示voidClear(){WriteCommand(1);}//初始化voidInit(){WriteCommand(0x0c);//开显示,无光标显示WriteCommand(0x06);//文字不动,光标自动右移WriteCommand(0x38);//设置显示模式:8位2行5x7点阵}//显示单个字符voidDisplayOne(bitbRow,unsignedcharbtColumn,unsignedcharbtData,bitbIsNumber){if(bRow)WriteCommand(0xc0+btColumn);elseWriteCommand(0x80+btColumn);if(bIsNumber)WriteData(btData+0x30);elseWriteData(btData);}//显示字符串函数voidDisplayString(bitbRow,unsignedcharbtColumn,unsignedchar*pData){while(*pData!='\0'){if(bRow)WriteCommand(0xc0+btColumn);//显示在第1行elseWriteCommand(0x80+btColumn);//显示在第0行WriteData(*(pData++));//要显示的数据btColumn++;//列数加一}}//延时16us子函数voidDelay16us(){unsignedchara;for(a=0;a4;a++);}//延时60us子函数voidDelay60us(){unsignedchara;for(a=0;a18;a++);}//延时480us子函数voidDelay480us(){unsignedchara;for(a=0;a158;a++);}//延时240us子函数voidDelay240us(){unsignedchara;for(a=0;a78;a++);}//延时500ms子函数voidDelay500ms(){unsignedchara,b,c;for(a=0;a250;a++)for(b=0;b3;b++)for(c=0;c220;c++);}//芯片初始化voidInitialization(){while(1){DQ=0;Delay480us();//延时480usDQ=1;Delay60us();//延时60usif(!DQ)//收到ds18b20的应答信号{DQ=1;Delay240us();//延时240usbreak;}}}//写一个字节(从低位开始写)voidWriteByte(unsignedcharbtData){unsignedchari,btBuffer;for(i=0;i8;i++){btBuffer=btDatai;if(btBuffer&1){DQ=0;_nop_();_nop_();DQ=1;Delay60us();}else{DQ=0;Delay60us();DQ=1;}}}//读一个字节(从低位开始读)unsignedcharReadByte(){unsignedchari,btDest;for(i=0;i8;i++){btDest=1;DQ=0;_nop_();_nop_();DQ=1;Delay16us();if(DQ)btDest|=0x80;Delay60us();}returnbtDest;}//序列号匹配voidMatchROM(constunsignedchar*pMatchData){unsignedchari;Initialization();WriteByte(MATCH_ROM);for(i=0;i8;i++)WriteByte(*(pMatchData+i));}//得到64位ROM序列(在P1口显示,必须与Proteus联调且在单步调试下才能得到)/*voidGetROMSequence(){unsignedchari;Initialization();WriteByte(READ_ROM);for(i=0;i8;i++)P1=ReadByte();}*///读取温度值TEMPDATAReadTemperature(){TEMPDATATempData;unsignedintiTempDataH;unsignedcharbtDot,iTempDataL;staticunsignedchari=0;TempData.btNegative=0;//为0温度为正i++;if(i==9)i=1;Initialization();WriteByte(SKIP_ROM);//跳过ROM匹配WriteByte(TEMP_SWITCH);//启动转换Delay500ms();//调用一次就行Delay500ms();Initialization();//多个芯片的时候用MatchROM(ROMData)换掉WriteByte(SKIP_ROM)switch(i){case1:MatchROM(ROMData1);break;//匹配1case2:MatchROM(ROMData2);break;//匹配2case3:MatchROM(ROMData3);break;//匹配3case4:MatchROM(ROMData4);break;//匹配4case5:MatchROM(ROMData5);break;//匹配5case6:MatchROM(ROMData6);break;//匹配6case7:MatchROM(ROMData7);break;//匹配7case8:MatchROM(ROMData8);break;//匹配8}//WriteByte(SKIP_ROM);//跳过ROM匹配(单个芯片时用这句换掉上面的switch)WriteByte(READ_MEMORY);//读数据iTempDataL=ReadByte();iTempDataH=ReadByte();iTempDataH=8;iTempDataH|=iTempDataL;if(iTempDataH&0x8000){TempData.btNegative=1;iTempDataH=~iTempDataH+1;//负数求补}//为了省去浮点运算带来的开销,而采用整数和小数部分分开处理的方法(没有四舍五入)btDot=(unsignedchar)(iTempDataH&0x000F);//得到小数部分iTempDataH=4;//得到整数部分btDot*=5;//btDot*10/16得到转换后的小数数据btDot=3;//数据处理TempData.btThird=(unsignedchar)iTempDataH/100;TempData.btSecond=(unsignedchar)iTempDataH%100/10;TempData.btFirst=(unsignedchar)iTempDataH%10;TempData.btDecimal=btDot;returnTempData;}//数据处理子程序voidDataProcess(){m_TempData=ReadTemperature();if(m_TempData.btNegative)DisplayOne(1,6,'-',0);elseDisplayOne(1,6,m_TempData.btThird,1);DisplayOne(1,7,m_TempData.btSecond,1);DisplayOne(1,8,m_TempData.btFirst,1);DisplayOne(1,10,m_TempData.btDecimal,1);}voidmain(){//GetROMSequence();Clear();Init();DisplayString(0,0,Temperature);DisplayOne(1,9,'.',0);while(1)DataProcess();}

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功