資源簡(jiǎn)介
51單片機(jī)每隔1s讀取一次DS18B20溫度傳感器數(shù)據(jù),將溫度數(shù)據(jù)不斷存儲(chǔ)到SD存儲(chǔ)卡中,同時(shí)有串口不斷輸出溫度數(shù)據(jù)。帶proteus仿真。
代碼片段和文件信息
#include?
#include
#include
//#include
//#include
#include
#define uchar?unsigned?char
#define?uint?unsigned?int
#define?delay4us()?{_nop_();_nop_();_nop_();_nop_();}
sbit?DQ=P3^4;
uchar?code?df_Table[]={0112334456678899};?//溫度小數(shù)位對(duì)照表
uchar?CurrentT=0;//當(dāng)前讀取的溫度整數(shù)部分
uchar?Temp_Value[]={0x000x00}; ???//從DS18B20讀取的溫度值
uchar?Display_Digit[12]={00000000000‘\r‘};???//待顯示的各溫度數(shù)位
bit?DS18B20_IS_OK=1;??//DS18B20正常標(biāo)志
void?DelayXus(uint?x)??//延時(shí)1
{
??uchar?i;
??while(x--)
??{
??for(i=0;i<200;i++);
??}
}
void?Delay(uint?x)??//延時(shí)2
{
??while(x--);
}
uchar?Init_DS18B20()??//初始化18B20
{
??uchar?status;
??DQ=1;
??Delay(8);
??DQ=0;
??Delay(90);
??DQ=1;
??Delay(8);
??status=DQ;
??Delay(100);
??DQ=1;
??return?status;
}
uchar?ReadOneByte() ?//從DS18B20讀一字節(jié)數(shù)據(jù)
{
??uchar?idat=0;
??DQ=1;
??_nop_();
???for(i=0;i<8;i++)
????{
DQ=0;
dat>>=1;
DQ=1;
_nop_();
_nop_();
if(DQ)
dat|=0x80;
Delay(30);
DQ=1;
}
return?dat;
}
void?WriteOneByte(uchar?dat)???//從DS18B20寫一字節(jié)數(shù)據(jù)
{
?uchar?i;
?for(i=0;i<8;i++)
??{
???DQ=0;
???DQ=dat&0x01;
???Delay(5);
???DQ=1;
???dat>>=1;
??}
}
void?Read_Temperature()//??????從DS18B20讀取溫度值
{
?if(Init_DS18B20()==1)???//DS18B20故障
???????DS18B20_IS_OK=0;
else
{
??WriteOneByte(0xcc);??//跳過序列號(hào)命令
??WriteOneByte(0x44);???//啟動(dòng)溫度轉(zhuǎn)換命令
??Init_DS18B20();//復(fù)位DS18B20
??WriteOneByte(0xcc);//跳過序列號(hào)命令
??WriteOneByte(0xbe);//讀取溫度寄存器
??Temp_Value[0]=ReadOneByte();?//讀取溫度低字節(jié)
??Temp_Value[1]=ReadOneByte(); //讀取溫度高字節(jié)
??DS18B20_IS_OK=1;????//DS18B20正常
}
}
void?Display_Temperature() //將08B20溫度數(shù)據(jù)讀取到Display_Digit[]里面
{
//uchar?i;
uchar?t=150ng=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[5]=df_Table[Temp_Value[0]&0x0f]+‘0‘;?//由低四位確定小數(shù)值
??CurrentT=(((Temp_Value[0]&0xf0)>>4)|((Temp_Value[1]&0x07)<<4));
??if(CurrentT/100==0)
??Display_Digit[1]=‘?‘;
??else
??Display_Digit[1]=CurrentT/100+‘0‘;
??Display_Digit[2]=(CurrentT%100)/10+‘0‘;
??Display_Digit[3]=CurrentT%10+‘0‘;
??Display_Digit[4]=‘.‘;
??Display_Digit[6]=‘0‘;
??Display_Digit[7]=‘0‘;
??Display_Digit[8]=‘?‘;
??Display_Digit[9]=‘c‘;
??if(ng==1)
??Display_Digit[0]=‘-‘;
??else
??Display_Digit[0]=‘+‘;
//??LCD_Write_String(61Display_Digit);
//??LCD_Write_String(20“thermometry“);
//??LCD_Write_String(11“Temp“);
}
void?temp_18b20() //實(shí)現(xiàn)溫度的讀
{
uchar?i=1;
//LCD_Init();
Read_Temperature();
Delay(5000);
Delay(5000);
while(i--)
?{
??Read_Temperature();
???if(DS18B20_IS_OK)
??????Display_Temperature();
??DelayXus(100);
??}?
}
main()
{
?unsigned?long?addr_sd=0x00000000; ?//SD卡地址
?UART(); //單片機(jī)串口初始化
?SdInit(); ?//初始化SD卡
while(1)
?{
?temp_18b20(); //讀出溫度數(shù)據(jù)
?SdWriteBlock(Display_Digitaddr_sd16); ???//將溫度數(shù)據(jù)寫入addr_sd指向內(nèi)存
?Sen_Stri
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件?????422850??2015-08-13?13:25??截圖02_副本.jpg
?????文件????????802??2015-08-13?11:38??temp_show_p\chuankou.h
?????文件???????2787??2015-08-13?10:45??temp_show_p\DS18B20.h
?????文件?????117326??2015-08-13?10:58??temp_show_p\Last?Loaded?show.DBK
?????文件???????3305??2015-08-13?13:16??temp_show_p\main.c
?????文件???????7126??2015-08-13?13:16??temp_show_p\main.LST
?????文件??????18373??2015-08-13?13:16??temp_show_p\main.OBJ
?????文件???????5051??2015-08-13?12:46??temp_show_p\sd.h
?????文件?????128280??2015-08-13?13:18??temp_show_p\SD_card.mmc
?????文件?????123286??2015-08-13?13:06??temp_show_p\show.DSN
?????文件???????1107??2015-08-13?13:28??temp_show_p\show.PWI
?????文件???????6376??2005-03-22?15:22??temp_show_p\STARTUP.A51
?????文件??????14049??2015-08-13?13:05??temp_show_p\STARTUP.LST
?????文件????????749??2015-08-13?13:05??temp_show_p\STARTUP.OBJ
?????文件??????15889??2015-08-13?13:16??temp_show_p\temp_s
?????文件???????3696??2015-08-13?13:16??temp_show_p\temp_s.hex
?????文件?????????58??2015-08-13?13:16??temp_show_p\temp_s.lnp
?????文件??????23280??2015-08-13?13:16??temp_show_p\temp_s.M51
?????文件????????469??2015-08-13?13:28??temp_show_p\temp_s.plg
????.......?????57605??2015-08-13?13:28??temp_show_p\temp_s.uvopt
????.......?????13401??2015-08-12?23:15??temp_show_p\temp_s.uvproj
????.......?????57601??2015-08-13?13:06??temp_show_p\temp_s_uvopt.bak
????.......?????????0??2015-08-12?22:31??temp_show_p\temp_s_uvproj.bak
?????文件?????187540??2015-08-13?13:22??截圖01_副本.jpg
?????目錄??????????0??2015-08-13?13:28??temp_show_p
-----------?---------??----------?-----??----
??????????????1211006????????????????????25
- 上一篇:分治法快速排序算法QuickSort C
- 下一篇:GUI銀行管理系統(tǒng)
評(píng)論
共有 條評(píng)論