資源簡介
代碼經過驗證可以使用
直流電機觸發溫度以及轉速都可自行調控
代碼片段和文件信息
#include????
#include?
unsigned?char?code?digit[10]={“0123456789“};?????
unsigned?char?code?Str[]={“Test?by?DS18B20“};????
unsigned?char?code?Error[]={“Error!Check!“};?????//說明沒有檢測到DS18B20
unsigned?char?code?Temp[]={“Temp:“};?????????????
unsigned?char?code?Cent[]={“Cent“};????????????//溫度單位
sbit?RS=P2^5;???????????//寄存器選擇位將RS位定義為P2.5引腳
sbit?RW=P2^6;??????????//讀寫選擇位將RW位定義為P2.6引腳
sbit?E=P2^7;??????????//使能信號位,
sbit?BF=P0^7;??????????//忙碌標志位,,將BF位定義為P0.7引腳
sbit?DQ=P2^3;
sbit?PWM?=?P1^0;
unsigned?char?CYCLEPWM_ONcount;?
unsigned?char?PWM_Num;
void?delay1ms()
{
????unsigned?char?ij;?
for(i=0;i<4;i++)
????for(j=0;j<33;j++)?;???
}
void?delaynms(unsigned?int?n)
{
????unsigned?int?i;
for(i=0;i ????delay1ms();
}
bit?BusyTest(void)
{
????bit?result;
RS=0;???????
????RW=1;
????E=1;????????
????_nop_();???
????_nop_();
????_nop_();?
????_nop_();???
????result=BF;?
????E=0;?????????
????return?result;
}
void?WriteInstruction?(unsigned?char?dictate)
{???
????while(BusyTest()==1);???
RS=0;??????????????????
RW=0;???
E=0;??????????????????
??????????????????????????
_nop_();
_nop_();??????????????
P0=dictate;???????????
_nop_();
_nop_();
_nop_();
_nop_();??????????????
E=1;???????????????????
_nop_();
_nop_();
_nop_();
_nop_();???????????????
???E=0;?????????????????
}
void?WriteAddress(unsigned?char?x)
{
?????WriteInstruction(x|0x80);?
}
void?WriteData(unsigned?char?y)
{
????while(BusyTest()==1);?
????RS=1;???????????
????RW=0;
????E=0;????????????
????P0=y;???????????
????_nop_();
????_nop_();
????_nop_();
????_nop_();???????
????E=1;???????????
????_nop_();
????_nop_();
????_nop_();
_nop_();????????
E=0;????????
}
void?LcdInitiate(void)
{?????????????
?????????????
WriteInstruction(0x38);????
?????????????
WriteInstruction(0x0c);?????
?????????????
WriteInstruction(0x06);?????
?????????????
WriteInstruction(0x01);????????????????
}?
unsigned?char?time;???
bit?Init_DS18B20(void)?
{
bit?flag;?????????
DQ?=?1;??????????
for(time=0;time<2;time++);
DQ?=?0;???????????
for(time=0;time<200;time++);????????
DQ?=?1;??????????
for(time=0;time<10;time++);
flag=DQ;????????????
for(time=0;time<200;time++);
return?(flag);????
}
unsigned?char?ReadOneChar(void)
{
???unsigned?char?i=0;?
???unsigned?char?dat;?
???for?(i=0;i<8;i++)
???{
?????
?????DQ?=1;??????
?????_nop_();?????
?????DQ?=?0;?????
????dat>>=1;
?????_nop_();?????
?????DQ?=?1;?????
?????for(time=0;time<2;time++);??????
?????if(DQ==1)
?????dat|=0x80;?
?????else
?????dat|=0x00;??
?????for(time=0;time<8;time++);???????????
?????}?????????????????????
?return(dat);????
}
WriteOneChar(unsigned?char?dat)
{
unsigned?char?i=0;
for?(i=0;?i<8;?i++)
????{
????DQ?=1;???????
????_nop_();??????
????DQ=0;??????????
????DQ=dat&0x01;???
?? for(time=0;time<10;time++)?;
????DQ=1;????
評論
共有 條評論