DS18B20温度传感器设计原理图及c程序代码

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

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

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

资源描述

/*******************代码部分**********************//***************writer:shopping.w******************/#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedchar#definedelayNOP(){_nop_();_nop_();_nop_();_nop_();}sbitDQ=P3^3;sbitLCD_RS=P2^0;sbitLCD_RW=P2^1;sbitLCD_EN=P2^2;ucharcodeTemp_Disp_Title[]={CurrentTemp:};ucharCurrent_Temp_Display_Buffer[]={TEMP:};ucharcodeTemperature_Char[8]={0x0c,0x12,0x12,0x0c,0x00,0x00,0x00,0x00};ucharcodedf_Table[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};ucharCurrentT=0;ucharTemp_Value[]={0x00,0x00};ucharDisplay_Digit[]={0,0,0,0};bitDS18B20_IS_OK=1;voidDelayXus(uintx){uchari;while(x--){for(i=0;i200;i++);}}bitLCD_Busy_Check(){bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOP();result=(bit)(P0&0x80);LCD_EN=0;returnresult;}voidWrite_LCD_Command(ucharcmd){while(LCD_Busy_Check());LCD_RS=0;LCD_RW=0;LCD_EN=0;_nop_();_nop_();P0=cmd;delayNOP();LCD_EN=1;delayNOP();LCD_EN=0;}voidWrite_LCD_Data(uchardat){while(LCD_Busy_Check());LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delayNOP();LCD_EN=1;delayNOP();LCD_EN=0;}voidLCD_Initialise(){Write_LCD_Command(0x01);DelayXus(5);Write_LCD_Command(0x38);DelayXus(5);Write_LCD_Command(0x0c);DelayXus(5);Write_LCD_Command(0x06);DelayXus(5);}voidSet_LCD_POS(ucharpos){Write_LCD_Command(pos|0x80);}voidDelay(uintx){while(--x);}ucharInit_DS18B20(){ucharstatus;DQ=1;Delay(8);DQ=0;Delay(90);DQ=1;Delay(8);DQ=1;returnstatus;}ucharReadOneByte(){uchari,dat=0;DQ=1;_nop_();for(i=0;i8;i++){DQ=0;dat=1;DQ=1;_nop_();_nop_();if(DQ)dat|=0X80;Delay(30);DQ=1;}returndat;}voidWriteOneByte(uchardat){uchari;for(i=0;i8;i++){DQ=0;DQ=dat&0x01;Delay(5);DQ=1;dat=1;}}voidRead_Temperature(){if(Init_DS18B20()==1)DS18B20_IS_OK=0;else{WriteOneByte(0xcc);WriteOneByte(0x44);Init_DS18B20();WriteOneByte(0xcc);WriteOneByte(0xbe);Temp_Value[0]=ReadOneByte();Temp_Value[1]=ReadOneByte();DS18B20_IS_OK=1;}}voidDisplay_Temperature(){uchari;uchart=150,ng=0;if((Temp_Value[1]&0xf8)==0xf8){Temp_Value[1]=~Temp_Value[1];Temp_Value[0]=~Temp_Value[0]+1;if(Temp_Value[0]==0x00)Temp_Value[1]++;ng=1;}Display_Digit[0]=df_Table[Temp_Value[0]&0x0f];CurrentT=((Temp_Value[0]&0xf0)4)|((Temp_Value[1]&0x07)4);Display_Digit[3]=CurrentT/100;Display_Digit[2]=CurrentT%100/10;Display_Digit[1]=CurrentT%10;Current_Temp_Display_Buffer[11]=Display_Digit[0]+'0';Current_Temp_Display_Buffer[10]='.';Current_Temp_Display_Buffer[9]=Display_Digit[1]+'0';Current_Temp_Display_Buffer[8]=Display_Digit[2]+'0';Current_Temp_Display_Buffer[7]=Display_Digit[3]+'0';if(Display_Digit[3]==0)Current_Temp_Display_Buffer[7]='';if(Display_Digit[2]==0&&Display_Digit[3]==0)Current_Temp_Display_Buffer[8]='';if(ng){if(Current_Temp_Display_Buffer[8]=='')Current_Temp_Display_Buffer[8]='-';elseif(Current_Temp_Display_Buffer[7]=='')Current_Temp_Display_Buffer[7]='-';elseCurrent_Temp_Display_Buffer[6]='-';}Set_LCD_POS(0x00);for(i=0;i16;i++){Write_LCD_Data(Temp_Disp_Title[i]);}Set_LCD_POS(0x40);for(i=0;i16;i++){Write_LCD_Data(Current_Temp_Display_Buffer[i]);}Set_LCD_POS(0x4d);Write_LCD_Data(0x00);Set_LCD_POS(0x4e);Write_LCD_Data('C');}voidmain(){LCD_Initialise();Read_Temperature();Delay(50000);Delay(50000);while(1){Read_Temperature();if(DS18B20_IS_OK)Display_Temperature();DelayXus(100);}}

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

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

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

×
保存成功