- #include < reg52.h >
- #include < intrins.h >
- #define uchar unsigned char
- #define uint unsigned int
- sbit LEDB=P1^0; //---LED藍燈
- sbit LEDR=P1^1; //LED紅燈
- sbit LEDG=P1^2; //LED綠色
- sbit LEDC=P1^3;//白的燈泡
- bit ledjc=0;//led檢測 ,
- sbit k1=P1^4; //---按鍵1
- sbit k2=P1^5; //---按鍵2
- sbit k3=P1^6; //---按鍵3
- sbit stop=P1^7; //---按鍵4
- sbit DQ1 = P2^0; //溫度顯示
- sbit E=P2^7; //1602使能引腳
- sbit RW=P2^6; //1602讀寫引腳
- sbit RS=P2^5; //1602數據/命令選擇引腳
- ucharrcount,bCount,yCount,rZKB,bZKB,yZKB,temp;
- uchar TempBuffer[5],temp_value,play,wendu;
- uchar ge,shi,bai,num,wendu,play,mode;
- uint PWM_LOW=0;//定義周期并賦值
- uint bCYCLE=900,rCYCLE=700,gCYCLE=600;
- void inti();
- void keyscan();
- void display();
- void display1();
- void display2();
- void fuction();
- void delayms(uint z);
- void Delay(unsigned int t); //函數聲明
- void show_temperture();
- uchar key();
- void Delay(uint t);
- /********************************************************************
- * 名稱 :delay()
- * 功能 : 延時,延時時間大概為5US。
- * 輸入 : 無
- * 輸出 : 無
- ***********************************************************************/
- void delay()
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- void delayms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /********************************************************************
- * 名稱 : bitBusy(void)
- * 功能 : 這個是一個讀狀態函數,讀出函數是否處在忙狀態
- * 輸入 : 輸入的命令值
- * 輸出 : 無
- ***********************************************************************/
- bit Busy(void)
- {
- bitbusy_flag = 0;
- RS= 0;
- RW= 1;
- E= 1;
- delay();
- busy_flag= (bit)(P0 & 0x80);
- E= 0;
- returnbusy_flag;
- }
- /********************************************************************
- * 名稱 :wcmd(uchar del)
- * 功能 : 1602命令函數
- * 輸入 : 輸入的命令值
- * 輸出 : 無
- ***********************************************************************/
- void wcmd(uchar del)
- {
- while(Busy());
- RS= 0;
- RW= 0;
- E= 0;
- delay();
- P0= del;
- delay();
- E= 1;
- delay();
- E= 0;
- }
- /********************************************************************
- * 名稱 :wdata(uchar del)
- * 功能 : 1602寫數據函數
- * 輸入 : 需要寫入1602的數據
- * 輸出 : 無
- ***********************************************************************/
- void wdata(uchar del)
- { while(Busy());
- RS= 1;
- RW= 0;
- E= 0;
- delay();
- P0= del;
- delay();
- E= 1;
- delay();
- E= 0;
- }
- /********************************************************************
- * 名稱 :L1602_init()
- * 功能 : 1602初始化,請參考1602的資料
- * 輸入 : 無
- * 輸出 : 無
- ***********************************************************************/
- void L1602_init(void)
- {
- wcmd(0x38);
- delay();
- wcmd(0x38);
- delay();
- wcmd(0x38);
- delay();
- wcmd(0x38);
- delay();
- wcmd(0x0c);
- wcmd(0x06);
- delay();
- wcmd(0x01);
- }
- /********************************************************************
- * 名稱 :L1602_char(uchar hang,uchar lie,char sign)
- * 功能 : 改變液晶中某位的值,如果要讓第一行,第五個字符顯示"b" ,調用該函數如下
- L1602_char(1,5,'b')
- * 輸入 : 行,列,需要輸入1602的數據
- * 輸出 : 無
- ***********************************************************************/
- void L1602_char(uchar hang,ucharlie,uchar sign)
- {
- uchar a;
- if(hang== 1) a = 0x80;
- if(hang== 2) a = 0xc0;
- a= a + lie - 1;
- wcmd(a);
- wdata(0x30+sign);
- }
- /********************************************************************
- * 名稱 :L1602_string(uchar hang,uchar lie,uchar *p)
- * 功能 : 改變液晶中某位的值,如果要讓第一行,第五個字符開始顯示"ab cd ef" ,調用該函數如下
- L1602_string(1,5,"ab cd ef;")
- * 輸入 : 行,列,需要輸入1602的數據
- * 輸出 : 無
- ***********************************************************************/
- void L1602_string(uchar hang,ucharlie,uchar *p)
- {
- uchara,b=0;
- if(hang== 1) a = 0x80;
- if(hang== 2) a = 0xc0;
- a= a + lie - 1;
- while(1)
- {
- wcmd(a++);
- b++;
- if((*p == '\0')||(b==16)) break;
- wdata(*p);
- p++;
- }
- }
- /**********************************************************/
- // 定時器 0中斷服務程序.
- /*********************************************************/
- void timer0() interrupt 1
- {
- TH0=(65536-10)/256;//裝入初值,定時50ms;
- TL0=(65536-10)%256;
- rcount++;
- bCount++;
- yCount++;
- if(rcount>599) rcount=0;
- if(bCount>599)bCount=0;
- if(yCount>599)yCount=0;
- if (rcount>=rZKB)
- {LEDG=1;}
- else
- {LEDG=0;}
- if (yCount>=yZKB)
- {LEDR=1;}
- else
- {LEDR=0;}
- if (bCount>=bZKB)
- {LEDB=1;}
- else
- {LEDB=0;}
- }
- void w1()
- { rZKB++;
- if(rZKB>=600&&rZKB<600)
- rZKB=599;
- if(rZKB>=600)
- rZKB = 0;
- //num=0;
- }
- void w2()
- {
- bZKB ++;
- if(bZKB>=600&&bZKB<600)
- bZKB=599;
- if(bZKB>=600)
- bZKB = 0;
- //num=0;
- }
- void w3()
- { delay();
- yZKB ++;
- if(yZKB>=600&&yZKB<600)
- yZKB=599;
- if(yZKB>=600)
- yZKB = 0;
- //num=0;
- }
- //啟動輸出
- void w4()
- {
- if((rZKB<=600)&&(0==ledjc))
- {
- rZKB+=8;
- if(rZKB>600)
- {
- ledjc=1;
- rZKB=600;
- }
- }
- if((rZKB>=0)&&(1==ledjc))
- {
- rZKB-=8;
- if(rZKB<0)
- {
- ledjc=0;
- rZKB=0;
- }
- num=0;
- }
- }
- void w5()
- {
- bZKB +=8;
- if(bZKB>=600&&bZKB<600)
- bZKB=599;
- if(bZKB>=600)
- bZKB = 0;
- num=0;
- }
- void w6()
- {
- //delay();
- yZKB +=8;
- if(yZKB>=600&&yZKB<600)
- yZKB=599;
- if(yZKB>=600)
- yZKB = 0;
- num=0;
- }
- /*********************************************************/
- // 定時器初始化
- /*********************************************************/
- void inti()
- {
- TMOD=0x1;//T0方式1工作
- IE=0x82;//EA=1;允許所有中斷,ET0==1;允許T0中斷
- TH0=(65536-8)/256;//裝入初值,定時50ms;
- TL0=(65536-8)%256;
- TR0=1;//啟動定時器
- }
- /*******************************************************
- //LCD1602占空比顯示程序
- *******************************************************/
- void display()
- {
- L1602_string(1,1,"B");
- bai=(uchar)rZKB/100;
- shi=(uchar)rZKB%100/10;
- ge=(uchar)rZKB%10;
- delay();
- L1602_char(1,2,bai);
- L1602_char(1,3,shi);
- L1602_string(1,4,".");
- L1602_char(1,5,ge);
- L1602_string(1,6,"%");
- }
- void display1()
- {
- L1602_string(1,8,"R");
- bai=(uchar)bZKB/100;
- shi=(uchar)bZKB%100/10;
- ge=(uchar)bZKB%10;
- delay();
- L1602_char(1,9,bai);
- L1602_char(1,10,shi);
- L1602_string(1,11,".");
- L1602_char(1,12,ge);
- L1602_string(1,13,"%");
- }
- void display2()
- {
-
- L1602_string(2,1,"G");
- bai=(uchar)yZKB/100;
- shi=(uchar)yZKB%100/10;
- ge=(uchar)yZKB%10;
- delay();
- L1602_char(2,2,bai);
- L1602_char(2,3,shi);
- L1602_string(2,4,".");
- L1602_char(2,5,ge);
- L1602_string(2,6,"%");
- }
- /*------------------------------------------------
- 延時函數,含有輸入參數 unsigned int t,無返回值
- unsigned int 是定義無符號整形變量,其值的范圍是
- 0~65535
- ------------------------------------------------*/
- void Delay(uint t)
- {
- while(--t);
- }
- /*------------------------------------------------
- 自動播放
- ------------------------------------------------*/
- void display3 ()
- {
- while (1) //主循環
- {
- show_temperture();
- LEDB=1;
- Delay(60000); //特意加延時,可以看到熄滅的過程
- for(PWM_LOW=1;PWM_LOW<bCYCLE;PWM_LOW++)
- {//PWM_LOW表示低
- //電平時間,這個循環中低電平時長從1累加到CYCLE(周期)的值,即600次
- LEDB=0; //點亮LED
- Delay(PWM_LOW);//延時長度,600次循環中從1加至599
- LEDB=1; //熄滅LED
- Delay(bCYCLE-PWM_LOW);//延時長度,600次循環中從599減至1
- }
- LEDB=0;
- for(PWM_LOW=bCYCLE-1;PWM_LOW>0;PWM_LOW--)
- {//與逐漸變亮相反的過程
- LEDB=0;
- Delay(PWM_LOW);
- LEDB=1;
- Delay(bCYCLE-PWM_LOW);
- }
- /********************************************
- //紅燈
- *****************************************/
- LEDR=1;
- Delay(60000); //特意加延時,可以看到熄滅的過程
- for(PWM_LOW=1;PWM_LOW<rCYCLE;PWM_LOW++)
- {//PWM_LOW表示低
- //電平時間,這個循環中低電平時長從1累加到CYCLE(周期)的值,即600次
- LEDR=0; //點亮LED
- Delay(PWM_LOW);//延時長度,600次循環中從1加至599
- LEDR=1; //熄滅LED
- Delay(rCYCLE-PWM_LOW);//延時長度,600次循環中從599減至1
- }
- LEDR=0;
- for(PWM_LOW=rCYCLE-1;PWM_LOW>0;PWM_LOW--)
- {//與逐漸變亮相反的過程
- LEDR=0;
- Delay(PWM_LOW);
- LEDR=1;
- Delay(rCYCLE-PWM_LOW);
- }
- /********************************************
- //綠燈燈
- *****************************************/
- LEDG=1;
- Delay(60000); //特意加延時,可以看到熄滅的過程
- for(PWM_LOW=1;PWM_LOW<bCYCLE;PWM_LOW++)
- {//PWM_LOW表示低
- //電平時間,這個循環中低電平時長從1累加到CYCLE(周期)的值,即600次
- LEDG=0; //點亮LED
- Delay(PWM_LOW);//延時長度,600次循環中從1加至599
- LEDG=1; //熄滅LED
- Delay(bCYCLE-PWM_LOW);//延時長度,600次循環中從599減至1
- }
- LEDB=0;
- for(PWM_LOW=bCYCLE-1;PWM_LOW>0;PWM_LOW--)
- {//與逐漸變亮相反的過程
- LEDG=0;
- Delay(PWM_LOW);
- LEDG=1;
- Delay(bCYCLE-PWM_LOW);
- }
- if(stop==1);
- break; //主循環中添加其他需要一直工作的程序
- }
- }
-
- /*********************************************************/
- // 鍵盤掃描
- /*********************************************************/
- void keyscan()
- {
- inti();
- if(k1==0)
- w1();
- if(k2==0)
- w2();
- if(k3==0)
- w3();
- }
- /****************************************************
- //矩陣鍵盤調用
- *****************************************************/
- uchar key()
- {
- P3=0xfe;
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- { temp=P3;
- switch(temp)
- {
- case 0xee:num=1;
- break;
- case 0xde:num=2;
- break;
- case 0xbe:num=3;
- break;
- case 0x7e:num=4;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- }
- }
- P3=0xfd;
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- { temp=P3;
- switch(temp)
- {
- case 0xed:num=5;
- break;
- case 0xdd:num=6;
- break;
- case 0xbd:num=7;
- break;
- case 0x7d:num=8;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- }
- }
- P3=0xfb;
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- { temp=P3;
- switch(temp)
- {
- case 0xeb:num=9;
- break;
- case 0xdb:num=10;
- break;
- case 0xbb:num=11;
- break;
- case 0x7b:num=12;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- }
- }
- P3=0xf7;
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- {
- delayms(5);
- temp=P3;
- temp=temp&0xf0;
- while(temp!=0xf0)
- { temp=P3;
- switch(temp)
- {
- case 0xe7:num=13;
- break;
- case 0xd7:num=14;
- break;
- case 0xb7:num=15;
- break;
- case 0x77:num=16;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- }
- }
- }
- return num;
- }
- /***********ds18b20子程序*************************/
- /***********ds18b20延遲子函數(晶振12MHz )*******/
- void delay_18B20(unsigned int i)
- {
- while(i--);
- }
- /**********ds18b20初始化函數**********************/
- void Init_DS18B20(void)
- {
- unsigned char x=0;
- DQ1 = 1; //DQ復位
- delay_18B20(8); //稍做延時
- DQ1 = 0; //單片機將DQ拉低
- delay_18B20(80); //精確延時 大于 480us
- DQ1 = 1; //拉高總線
- delay_18B20(14);
- x=DQ1; //稍做延時后 如果x=0則初始化成功 x=1則初始
- delay_18B20(20);
- }
- /***********ds18b20讀一個字節**************/
- unsigned char ReadOneChar(void)
- {
- uchari=0;
- uchardat = 0;
- for(i=8;i>0;i--)
- {
- DQ1= 0; // 給脈沖信號
- dat>>=1;
- DQ1= 1; // 給脈沖信號
- if(DQ1)
- dat|=0x80;
- delay_18B20(4);
- }
- return(dat);
- }
- /*************ds18b20寫一個字節****************/
- void WriteOneChar(uchar dat)
- {
- unsigned char i=0;
- for (i=8; i>0; i--)
- {
- DQ1 =0;
- DQ1= dat&0x01;
- delay_18B20(5);
- DQ1= 1;
- dat>>=1;
- }
- }
- /**************讀取ds18b20當前溫度************/
- void ReadTemp(void)
- { unsignedchar a=0;
- unsignedchar b=0;
- unsignedchar t=0;
- Init_DS18B20();
- WriteOneChar(0xCC); // 跳過讀序號列號的操作
- WriteOneChar(0x44); // 啟動溫度轉換
- delay_18B20(100); // this message is Very important
- Init_DS18B20();
- WriteOneChar(0xCC); //跳過讀序號列號的操作
- WriteOneChar(0xBE); //讀取溫度寄存器等(共可讀9個寄存器)
- delay_18B20(100);
- a=ReadOneChar(); //讀取溫度值低位
- b=ReadOneChar(); //讀取溫度值高位
- temp_value=b<<4;
- temp_value+=(a&0xf0)>>4;
- }
- void temp_to_str() //溫度數據轉換成液晶字符顯示
- {
- TempBuffer[0]=temp_value/10+'0'; //十位
- TempBuffer[1]=temp_value%10+'0'; //個位
- TempBuffer[2]=0xdf; //溫度符號
- TempBuffer[3]='C';
- TempBuffer[4]='\0';
- }
- void show_temperture() //液晶顯示程序
- {
- ReadTemp(); //開啟溫度采集程序
- temp_to_str(); //溫度數據轉換成液晶字符
- L1602_string(2,7,"temp1:");
- L1602_string(2,13,TempBuffer); //顯示溫度
- }
- voidTEM_INIT()
- {
- Init_DS18B20( ); //DS18B20初始化
- }
- void fuction()
- { if(key()==1)
- w4();
- if(key()==2)
- w5();
- if(key()==3)
- w6();
- }
- /*************************************************
- //主函數
- ***************************************************/
- voidmain()
- {
- //inti();
- LEDC=0;
- L1602_string(1,1,"Instructor ZHF");
- L1602_string(2,1,"Produced by MZM");
- delayms(500);
- L1602_init();
- TEM_INIT();
- display3 ();
- while(1)
- { //display3 ();
- //keyscan ();
- keyscan ();
- fuction ();
- display ();
- display1 ();
- display2 ();
- //display3 ();
- }
- }
-
-
復制代碼
|