1637831448.png (250.55 KB, 下載次數: 39)
下載附件
2021-11-25 17:11 上傳
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit RS = P2^0;
- sbit RW = P2^1;
- sbit EN = P2^2;
- sbit k1=P3^2;
- sbit k2=P3^3;
- sbit k3=P3^4;
- sbit k4=P3^5;
- sbit k5=P3^6;//加速
- //sbit k6=P3^7;//減速
- sbit DQ = P3^7;
- sbit motor1_run = P2^3;
- sbit motor2_r = P2^4;
- sbit motor2_l = P2^5;
- sbit IN1 = P2^6;
- sbit IN2 = P2^7;
- unsigned int temp;
- unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- unsigned char code table1[] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
- #define st_mo P1
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- #define DataPort P0
- #define KeyPort P3
- uchar code start[]="open";
- uchar code close[]="stop";
- uchar ccw[]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //
- uchar cw[]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; //
- uchar i;
- uint value_h=320;
- uint value_l=200;
- uchar PWM=180;
- uint temp_data,temp_data2;
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void DelayUs2x(uchar t)
- {
- while(--t);
- }
- /*------------------------------------------------
- ------------------------------------------------*/
- void delay(unsigned char t)
- {
- while(t--) ;
- }
- void delayms(unsigned char ms)
- {
- unsigned char i ;
- while(ms--)
- {
- for(i = 0 ; i < 120 ; i++) ;
- }
- }
- void ccw_motor() //逆時針旋轉
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- for(i=0;i<8;i++)
- {
- st_mo=ccw[i];
- delay(500);
- }
- }
- }
- void cw_motor() //順時針旋轉
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- for(i=0;i<8;i++)
- {
- st_mo=cw[i];
- delay(500);
- }
- }
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- bit LCD_Check_Busy(void) //檢查忙標志位函數(LCD)
- {
- DataPort= 0xFF; //DataPort為單位變量,初始值為0xFF
- RS_CLR;
- RW_SET;
- EN_CLR;
- _nop_(); //空操作,給硬件反應時間
- EN_SET;
- return (bit)(DataPort & 0x80); //如果忙標志位BF=1,回到bit
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Write_Com(uchar com)
- {
- while(LCD_Check_Busy());
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort= com;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Write_Data(uchar Data) //寫數據函數
- {
- while(LCD_Check_Busy());
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort= Data;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Clear(void) //清屏
- {
- LCD_Write_Com(0x01);
- delayms(5);
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Write_String(uchar x,uchar y,uchar *s) //顯示字符串
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- while (*s)
- {
- LCD_Write_Data( *s);
- s ++;
- }
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Write_Char(uchar x,uchar y,uchar Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38);
- delayms(5);
- LCD_Write_Com(0x38);
- delayms(5);
- LCD_Write_Com(0x38);
- delayms(5);
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x08);
- LCD_Write_Com(0x01);
- LCD_Write_Com(0x06);
- delayms(5);
- LCD_Write_Com(0x0C);
- }
- unsigned char KeyScan(void)
- {
- unsigned char keyvalue;
- if(KeyPort!=0xff)
- {
- delayms(10);
- if(KeyPort!=0xff)
- {
- keyvalue=KeyPort;
- while(KeyPort!=0xff);
- switch(keyvalue)
- {
- case 0xfe:return 1;break;
- case 0xfd:return 2;break;
- case 0xfb:return 3;break;
- case 0xf7:return 4;break;
- case 0xef:return 5;break;
- case 0xdf:return 6;break;
- case 0xbf:return 7;break;
- case 0x7f:return 8;break;
- default:return 0;break;
- }
- }
- }
- return 0;
- }
- /*****************DS18B20******************/
- void Init_Ds18b20(void)
- {
- DQ = 1;
- delay(1);
- DQ = 0;
- delay(250);
- DQ = 1;
- delay(100);
- }
- uchar Read_One_Byte()
-
- {
- uchar i = 0;
- uchar dat = 0;
- for(i=8;i>0;i--)
- {
- DQ = 0;
-
- _nop_();
- dat >>= 1;
- DQ = 1;
- delay(1);
- if(DQ)
- {
- dat |= 0x80;
- }
- delay(10);
- }
- return (dat);
- }
- void Write_One_Byte(uchar dat)
- {
- uchar i = 0;
- for(i=8;i>0;i--)
- {
- DQ = 0;
- _nop_();
- DQ = dat&0x01;
-
- delay(10);
- DQ = 1;
- dat >>= 1;
- delay(1);
- }
- }
- uint Get_Tmp()
- {
- float tt;
- uchar a,b;
- Init_Ds18b20();
- Write_One_Byte(0xcc);
- Write_One_Byte(0x44);
- Init_Ds18b20();
- Write_One_Byte(0xcc);
- Write_One_Byte(0xbe);
- a = Read_One_Byte();
- b = Read_One_Byte();
- temp = b;
- temp <<= 8;
- temp = temp|a;
- tt = temp*0.0625;
-
- temp = tt*10+0.5;
-
- return temp;
- }
- void timer0() interrupt 1
- {
- TR1=0 ;
- TH0=0x9f ;
- TL0=0xce ;
- TH1=PWM ;
- TR1=1 ;
- IN1=0 ;
- IN2=0 ;
- }
- void timer1() interrupt 3
- {
- TR1=0 ;
- IN1=1 ;
- IN2=0 ;
-
- }
- /*------------------------------------------------
-
- ------------------------------------------------*/
- void main(void)
- {
- uchar num;
- LCD_Init();
- LCD_Clear();
- //temperature
- LCD_Write_Char(0,0,'T');LCD_Write_Char(1,0,':');LCD_Write_Char(4,0,0x2e);LCD_Write_Char(6,0,0xdf);
- LCD_Write_Char(7,0,'W');LCD_Write_Char(8,0,':');
- LCD_Write_Char(0,2,'H');LCD_Write_Char(1,2,':');LCD_Write_Char(4,2,0x2e);LCD_Write_Char(6,2,0xdf);
- LCD_Write_Char(7,2,'D');LCD_Write_Char(8,2,':');LCD_Write_Char(11,2,0x2e);LCD_Write_Char(13,2,0xdf);
-
-
-
- IN1=0;
- IN2=0;
- //TMOD=0x21 ;
- TMOD=0x11 ;
- //TH0=0xff ;
- //TL0=0xce ;
- TH0=0x9f ;
- TL0=0xce ;
- TH1=PWM ;
- TL1=0 ;
-
- while(1)
- {
- //delayms(200);
- temp_data=Get_Tmp(); //PWM溫度調速
- if(temp_data>value_h){temp_data2=value_h;}
- else if(temp_data<value_l){temp_data2=value_l;}
- else{temp_data2=temp_data;}
-
- if((temp_data2>220) && (temp_data2<230)){PWM=165;}
- if((temp_data2>230) && (temp_data2<240)){PWM=170;}
- if((temp_data2>240) && (temp_data2<250)){PWM=175;}
- if((temp_data2>250) && (temp_data2<260)){PWM=180;}
- if((temp_data2>260) && (temp_data2<270)){PWM=185;}
- if((temp_data2<280) && (temp_data2>270)){PWM=190;}
- if((temp_data2<290) && (temp_data2>280)){PWM=195;}
- if((temp_data2<300) && (temp_data2>290)){PWM=200;}
- if((temp_data2<310) && (temp_data2>300)){PWM=210;}
- if((temp_data2<320) && (temp_data2>310)){PWM=220;}
- LCD_Write_Com(0xc2);
- LCD_Write_Data(value_h/100+0x30);
- LCD_Write_Com(0xc3);
- LCD_Write_Data(((value_h/10)%10)+0x30);
- LCD_Write_Com(0xc5);
- LCD_Write_Data(value_h%10+0x30);
-
- LCD_Write_Com(0xc9);
- LCD_Write_Data(value_l/100+0x30);
- LCD_Write_Com(0xca);
- LCD_Write_Data(((value_l/10)%10)+0x30);
- LCD_Write_Com(0xcc);
- LCD_Write_Data(value_l%10+0x30);
-
- LCD_Write_Com(0x8d);
- LCD_Write_Data(PWM/100+0x30);
- LCD_Write_Com(0x8e);
- LCD_Write_Data((PWM/10)%10+0x30);
- LCD_Write_Com(0x8f);
- LCD_Write_Data(PWM%10+0x30);
-
- LCD_Write_Com(0x82);
- LCD_Write_Data(temp_data2/100+0x30);
- LCD_Write_Com(0x83);
- LCD_Write_Data(((temp_data2/10)%10)+0x30);
- LCD_Write_Com(0x85);
- LCD_Write_Data((temp_data2%10)+0x30);
- if(motor1_run==0)
- {
- EA=1;
- ET0=1;
- ET1=1;
- TR0=1 ;
- LCD_Write_String(9,0,start);
- }
- else
- {
- EA=0;
- ET0=0;
- ET1=0;
- TR0=0 ;
- IN1=0 ;
- IN2=0 ;
- LCD_Write_String(9,0,close);
- }
- if((motor2_l==0)&&(motor2_r==1)) //順時針程序
- {
- for(num=0;num<10;num++)
- {
- cw_motor();
- }
- }
- if((motor2_l==1)&&(motor2_r==0)) //逆時針程序
- {
- for(num=0;num<10;num++)
- {
- ccw_motor();
- }
- }
- if(k1==0)
- {
- delayms(50);
- if(k1==0)
- {
- value_h++;
- if(value_h>600){value_h=0;}
- }
- }
- if(k2==0)
- {
- delayms(50);
- if(k2==0)
- {
- value_h--;
- if(value_h<0){value_h=600;}
- }
- }
- if(k3==0)
- {
- delayms(50);
- if(k3==0)
- {
- value_l++;
- if(value_l>500){value_l=0;}
- }
- }
- if(k4==0)
- {
- delayms(50);
- if(k4==0)
- {
-
- value_l--;
- if(value_l<0){value_l=500;}
- }
- }
- if(k5==0)
- {
- delayms(50);
- if(k5==0)
- {
- PWM=PWM+10 ;
- if(PWM>255){PWM=150;}
- }
- }
- // if(k6==0)
- // {
- // delayms(20);
- // if(k6==0)
- // {
- // PWM=PWM-10 ;
- // if(PWM<150){PWM=150;}
- // }
- // }
- }
- }
復制代碼
|