- //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;
- }
- }
复制代码
|