#include <reg52.h> //調用單片機頭文件
#include <intrins.h>
#include "common.h"
#include "lcd1602.h"
#include "DS18B20_One.h"
#define uchar unsigned char //無符號字符型 宏定義 變量范圍0~255
#define uint unsigned int //無符號整型 宏定義 變量范圍0~65535
#define GEAR1 65 //1檔
#define GEAR2 50 //2檔
#define GEAR3 40 //3檔
#define GEAR4 30 //4檔
#define GEAR5 25 //5檔
#define GEAR6 20 //6檔
#define GEAR7 15 //7檔
#define GEAR8 10 //8檔
#define GEAR9 5 //9檔
sbit Motor = P3^4;//風扇
sbit RTHW = P1^4;//人體紅外
sbit key1 = P1^1;//加
sbit key2 = P1^2;//減
sbit key3 = P1^3;//切換
unsigned char IrValue[6];
unsigned char Time;
unsigned char T_max = 30;
uchar PWM_Val = 0xFF;
uchar count = 0;
unsigned char shoudong = 1;//手動調節檔位
bit Auto_flag = 1;
bit on_0ff = 1;
/*******************************************************************************
* 函 數 名 : delay
* 函數功能 : 延時函數,i=1時,大約延時10us
*******************************************************************************/
void delay(uint i)
{
while(i--);
}
void Timer0Init(void)//11.0592M,定時100us
{
TMOD |= 0x01;
TL0 = 0xA4;
TH0 = 0xFF;
ET0 = 0;
TR0 = 1;
EA = 1;
}
void display_Max()//顯示上限值
{
lcd_write_char(2,1,T_max/10+'0');
lcd_write_char(3,1,T_max%10+'0');
}
void keyscan()//按鍵掃描
{
if(key1 == 0)//加鍵按下
{
delay_ms(10);
if(key1 == 0)
{
if(Auto_flag==1)
{ while(!key1);
if(T_max < 99)//最大只能到99
{
T_max++;
}
display_Max();//顯示
}
else
{
if(shoudong < 10)
shoudong++;
}
}
}
else if(key2 == 0)//減鍵按下
{
delay_ms(10);
if(key2 == 0)
{
while(!key2);
if(Auto_flag==1)
{
if(T_max > 0)//最小只能到0
{
T_max--;
}
display_Max();//顯示
}
else
{
if(shoudong > 1)
shoudong--;
}
}
}
else if(key3 == 0)//切換鍵按下
{
delay_ms(10);
if(key3 == 0)
{
while(!key3);
Auto_flag = ~Auto_flag;
if(Auto_flag==0)
{
lcd_write_str(12, 0, "SD");
Motor = 1;
}
}
}
}
void PWM_Dispose(uchar x)//PWM檔位調節
{
switch(x)
{
case(1):PWM_Val = GEAR1;lcd_write_str(14,1,"1 ");break;
case(2):PWM_Val = GEAR2;lcd_write_str(14,1,"2 ");break;
case(3):PWM_Val = GEAR3;lcd_write_str(14,1,"3 ");break;
case(4):PWM_Val = GEAR4;lcd_write_str(14,1,"4 ");break;
case(5):PWM_Val = GEAR5;lcd_write_str(14,1,"5 ");break;
case(6):PWM_Val = GEAR6;lcd_write_str(14,1,"6 ");break;
case(7):PWM_Val = GEAR7;lcd_write_str(14,1,"7 ");break;
case(8):PWM_Val = GEAR8;lcd_write_str(14,1,"8 ");break;
case(9):PWM_Val = GEAR9;lcd_write_str(14,1,"9 ");break;
case(10):PWM_Val = 0;Motor = 1;lcd_write_str(14,1,"10");break;
default:PWM_Val = 0;Motor = 1;lcd_write_str(14,1,"10");break;
}
}
void temp_dispose(int temp)//溫度控制風扇處理,總共10檔
{
if(temp <= T_max)//如果實際溫度低于上限,關閉風扇
{
PWM_Val = 0xFF;
Motor = 0;
lcd_write_str(14,1,"--");
}
else
{
PWM_Dispose(temp - T_max);//PWM調節,溫度高于上限,每高于一度檔位增加一檔
}
}
/*******************************************************************************
* 函 數 名 : datapros()
* 函數功能 : 溫度讀取處理轉換函數
* 輸 入 : temp
* 輸 出 : 無
*******************************************************************************/
void datapros()
{
int temp=0;
ET0 = 0;
temp = DS18B20_TF();//讀取溫度值
ET0 = 1;
//顯示溫度
lcd_write_char(6,0,temp % 100 / 10+'0');
lcd_write_char(7,0,temp % 10+'0');
if(Auto_flag)//在自動模式下,感應到有人,并且溫度高于上限,開啟風扇
{
lcd_write_str(12, 0, "Auto");
if(RTHW == 1)
{
temp_dispose(temp);
}
else
{
PWM_Val = 0xFF;
Motor = 0;
lcd_write_str(14,1,"--");
}
}
else//手動模式下,Auto不顯示
{
lcd_write_str(12, 0, " ");
}
}
void main()
{
uint cnt = 0;
Motor = 0;//關閉風扇
lcd_init();//1602初始化
Timer0Init();//開啟定時器
DS18B20_TF();//溫度初始化
lcd_write_str(0, 0, " Ds18b20 Init...");
delay_ms(1000);
lcd_write_str(0, 0, "Temp: C ");
lcd_write_str(0, 1, "M: C Gear: ");
display_Max();
while(1)
{
keyscan();
if(Auto_flag == 0)
{
if(on_0ff)//不是在關閉狀態下,執行手動檔位
{
PWM_Dispose(shoudong);
}
else//關閉風扇
{
PWM_Val = 200;
Motor = 0;
lcd_write_str(14,1,"--");
}
}
if(cnt++ > 100)//延時一段時間
{
cnt = 0;
datapros();
}
delay_ms(1);
}
}
void Tim0_str()interrupt 1 using 2//定時器中斷1,用于PWM調節
{
TL0 = 0xA4;
TH0 = 0xFF;
count++;
if(count > 200)//20毫秒一個周期
count = 0;
if(count >= PWM_Val)
Motor = 1;
else
Motor = 0;
}
|