- //DS18B20溫度傳感器函數
- #include<reg52.h>
- #include<math.h>
- #include<intrins.h>
- #include<string.h>
- #defineuchar unsigned char
- unsignedchar high_time,low_time,count=0;
- sbitoutput=P3^4;
- sbitSPEAKER=P3^0;
- sbitDATA= P3^7;
- voiddelay(unsigned int i)
- {
- while(i--);
- }
- Init_DS18B20(void) //傳感器初始化
- {
- ucharx=0;
- DATA=1; //DQ復位
- delay(10); //稍作延時
- DATA=0; //單片機將DQ拉低
- delay(80); //延時80s
- DATA=1; //拉高總線
- delay(20);
- x=DATA; //稍作延時后,如果x=0則初始化成功,x=1則失敗
- delay(30);
- return0;
- }
- //讀1字節
- ReadOneChar(void)
- {
- uchari=0;
- uchardat=0;
- for(i=8;i>0;i--)
- {
- DATA=0; //給脈沖信號
- dat>>=1;
- DATA=1; //給脈沖信號
- if(DATA)
- dat|=0x80;
- delay(8);
- }
- return(dat);
- }
- //寫1字節
- WriteOneChar(uchardat)
- {
- uchari=0;
- for(i=8;i>0;i--)
- {
- DATA=0;
- DATA=dat&0x01;
- delay(10);
- DATA=1;
- dat>>=1;
- }
- delay(8);
- return 0;
- }
- //讀取溫度
- intReadTemperature(void)
- {
- uchara=0;
- ucharb=0;
- int t=0;
- floattt=0;
- ucharflag_Negative_number;
- Init_DS18B20();
- WriteOneChar(0xCC); //跳過讀序號列號的操作
- WriteOneChar(0x44); //啟動溫度轉換
- Init_DS18B20();
- WriteOneChar(0xCC); //跳過讀序號列號的操作
- WriteOneChar(0xBE); //讀取溫度寄存器等(供可讀9個寄存器),前兩個就是溫度
- a=ReadOneChar(); //低位
- b=ReadOneChar(); //高位
- t=b;
- t<<=8;
- t=t|a;
- if(b&0x80)
- {
- t=~t+1;
- flag_Negative_number=1;
- }
- else
- {
- flag_Negative_number=0;
- }
- tt=t*0.0625; //DS18B20溫度傳感器的分辨率
- t=tt*10+0.5;
- tt=t+0.05;
- return(tt);
- }
- //LCD1602液晶顯示函數
- #include<reg52.h>
- #defineuchar unsigned char
- sbitEN=P2^2;
- sbitRS=P2^0;
- sbitRW=P2^1;
- voidDelayms_1602(unsigned int x) //延時函數
- {unsignedint i;
- while(x--)
- for(i=0;i<200;i++);
- }
- ucharBusy_Check_1602() //忙檢測
- {
- ucharLCD_Status;
- RS=0;
- RW=1;
- EN=1;
- Delayms_1602(1);
- LCD_Status=P0;
- EN=0;
- returnLCD_Status;
- }
- voidWrite_LCD_Command_1602(uchar cmd) //LCD1602寫命令
- {
- while((Busy_Check_1602()&0x80)==0x80);
- RS=0;
- RW=0;
- EN=0;
- P0=cmd;
- EN=1;
- Delayms_1602(1);
- EN=0;
- }
- voidWrite_LCD_Data_1602(uchar dat) //LCD1602寫數據
- {
- while((Busy_Check_1602()&0x80)==0x80);
- RS=1;
- RW=0;
- EN=0;
- P0=dat;
- EN=1;
- Delayms_1602(1);
- EN=0;
- }
- voidInitialize_LCD_1602() //初始化LCD1602
- {
- Write_LCD_Command_1602(0x38); //顯示模式設置
- Delayms_1602(1);
- Write_LCD_Command_1602(0x01); //顯示清屏
- Delayms_1602(1);
- Write_LCD_Command_1602(0x06); //顯示光標移動設置
- Delayms_1602(1);
- Write_LCD_Command_1602(0x0c); //顯示開/關及光標設置
- Delayms_1602(1);
- }
- voidShowString_1602(uchar x,uchar y,uchar * str)
- {
- uchari=0;
- if(y==0)
- Write_LCD_Command_1602(0x80|x);
- if(y==1)
- Write_LCD_Command_1602(0xc0|x);
- for(i=0;i<16;i++)
- {
- Write_LCD_Data_1602(str[i]);
- }
- }
- //溫控系統主函數
- sbitK1=P1^7;
- sbitK2=P1^6;
- sbitK3=P1^5;
- #defineuchar unsigned char
- #defineuint unsigned int
- #defineTime_5ms (0x10000-100)
- uchartable [] ="temp= . C" ;
- uchartable1 [] ="set temp= . C" ;
- uchar qian, bai, shi, ge; //定義變量
- uchar qian1, bai1, shi1, ge1; //定義變量
- uchar qian2, bai2, shi2, ge2; //定義變量
- ucharh1, h2, h3, h4;
- int temp;
- ucharm=0;
- ucharset_temper=50;
- voidkeyscan() //按鍵處理函數
- {
- if(K1==0)
- m++;
- if(m==1)
- {if(K2==0)
- set_temper++;
- elseif(K3==0)
- set_temper--;
- }
- if(m==2)
- m=0;
- }
- voiddisplay()
- { int h;
- if(m==1)
- {
- qian1=set_temper%10000/1000;
- bai1=set_temper%1000/100; //顯示百位
- shi1=set_temper%100/10; //顯示十位
- ge1=set_temper%10; //顯示個位
- table1[9]=qian1+'0';
- table1[10]=bai1+'0';
- table1[11]=shi1+'0';
- table1[12]=ge1+'0';
- }
- //if(m==0)
- //{
- //}
- h=high_time;
- h1=h/1000;
- temp=ReadTemperature(); //讀溫度
- qian=temp%10000/1000;
- bai=temp%1000/100; //顯示百位
- shi=temp%100/10; //顯示十位
- ge=temp%10; //顯示個位
- table[5]=qian+'0';
- table[6]=bai+'0';
- table[7]=shi+'0';
- table[9]=ge+'0';
- }
- //intredtemper(void)
- //{ inth=0;
- //intw=0;
- //h=ReadTemperature();
- //qian2=h%10000/1000;
- //bai2=h%1000/100; //顯示百位
- //shi2=h%100/10; //顯示十位
- //ge2=h%10; //顯示個位
- //w=qian2*1000+bai2*100+shi2*10+ge2;
- //return(w);
- //}
- voidwarm()
- {
- uintt,a;
- t=set_temper;
- t=t*10;
- a=bai*100+shi*10+ge;
- SPEAKER=1;
- if(a>750)
- { SPEAKER=0;
- }
- else
- {SPEAKER=1;
- }
- }
- voidmain()
- {
- TMOD=TMOD|0x01;
- TMOD=TMOD&0xF1;
- ET0=1; //定時器0的中斷控制位
- EX0=1; //外部的中斷0控制位
- IT0=1; //外部中斷0為下降沿觸發方式
- EA=1;
- high_time=50;
- low_time=50;
- Init_DS18B20(); //溫度傳感器初始化
- Initialize_LCD_1602(); //LCD1602初始化
- while(1)
- {
- keyscan();
- display();
- warm();
- ShowString_1602(0,0,table);
- ShowString_1602(0,1,table1);
- }
- }
- #include<reg52.h> //52芯片管腳定義頭文件
- #include<intrins.h> //內部包含延時函數 _nop_();
- #defineuchar unsigned char
- #defineuint unsigned int
- ucharcode FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //四相八拍正轉編碼
- ucharcode REV[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; ////四相八拍反轉編碼
- sbit Y1 =P1^0; //正轉
- sbit Y2 =P1^1; //反轉
- sbit Y3 =P1^2; //停止
- sbit BEEP = P1^4;
- /********************************************************/
- /*
- /* 延時t毫秒
- /*11.0592MHz時鐘,延時約1ms
- /*
- /********************************************************/
- voiddeloy(uint t)
- {
- uint y;
- while(t--)
- {
- for(y=0; y<125; y++)
- { }
- }
- }
- /**********************************************************/
- voiddeloyB(uchar x) //x*0.14MS
- {
- uchar i;
- while(x--)
- {
- for (i=0; i<13; i++)
- { }
- }
- }
- /**********************************************************/
- voidbeep()
- {
- uchar i;
- for (i=0;i<100;i++)
- {
- deloyB(4);
- BEEP=!BEEP; //BEEP取反
- }
- BEEP=1;
- }
- /********************************************************/
- /*
- /*步進電機正轉
- /*
- /********************************************************/
- void motor_ffw()
- {
- uchar i;
- uint j;
- for (j=0; j<8; j++) //轉1*n圈
- {
- if(K3==0)
- {break;} //退出此循環程序
- for (i=0; i<8; i++) //一個周期轉45度
- {
- P1 = FFW; //取數據
- deloy(2); //調節轉速
- }
- }
- }
- /********************************************************/
- /*
- /*步進電機反轉
- /*
- /********************************************************/
- void motor_rev()
- {
- uchar i;
- uint j;
- for (j=0; j<8; j++) //轉1×n圈
- {
- if(K3==0)
- {break;} //退出此循環程序
- for (i=0; i<8; i++) //一個周期轉45度
- {
- P1 = REV; //取數據
- deloy(2); //調節轉速
- }
- }
- }
- /********************************************************
- *
- * 主程序
- *********************************************************/
- void zhengzhuang()
- {
- uchar r,N=64; //N 步進電機運轉圈數
- while(1)
- {
- if(K1==0)
- {
- beep();
- for(r=0;r<N;r++)
- {
- motor_ffw(); //電機正轉
- if(K3==0)
- {beep();break;} //退出此循環程序
- }
- }
- else if(K2==0)
- {
- beep();
- for(r=0;r<N;r++)
- {
- motor_rev(); //電機反轉
- if(K3==0)
- {beep();break;} //退出此循環程序
- }
- }
- else
- P1 = 0xf0;
- }
- }
復制代碼
|