天天看點

藍橋杯單片機 模拟風扇控制系統

藍橋杯 模拟風扇控制系統

題目要求

藍橋杯單片機 模拟風扇控制系統

main.c

#include <STC15F2K60S2.H>
#include <onewire.h>
#include <delay.h>
sbit s4=P3^3;
sbit s5=P3^2;
sbit s6=P3^1;
sbit s7=P3^0;
unsigned char code SMG_duanma[13]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
unsigned char code SMG_position[9]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char flag=1,SMGflag=0,s5_flag=1;
unsigned char count,pwm=2,miao=0;
int time=0;
void InitHC138(unsigned char channel)
{
	switch(channel)
	{
		case 4 :P2=(P2&0X1F)|0X80;break;
		case 5 :P2=(P2&0X1F)|0XA0;break;
		case 6 :P2=(P2&0X1F)|0XC0;break;
		case 7 :P2=(P2&0X1F)|0XE0;break;
	}
}
/*****************系統初始化函數**********************/
void InitSystem()
{
	InitHC138(4);       //關閉LED燈
	P0=0xff;
	InitHC138(5);				//關閉繼電器和蜂鳴器
	P0=0x00;
	InitHC138(6);				//關閉數位管
	P0=0xff;
	InitHC138(7);
	P0=0xff;
}
/*****************數位管顯示函數**********************/
void SMG_Select(unsigned char pos,unsigned char dat)
{
	InitHC138(6);
	P0=SMG_position[pos];
	InitHC138(7);
	P0=SMG_duanma[dat];
}
void SMG_control()
{
	if(SMGflag==0)
	{
		yi=san=10,er=flag,si=11;
		wu=0;liu=miao/100,qi=miao%100/10,ba=miao%10;
	}
	if(SMGflag==1)
	{
		yi=san=10,er=4,si=wu=11;
		liu=temp/10;qi=temp%10;ba=12;
	} 
}
void SMG_Display()
{
	SMG_control();       //狀态選擇
	SMG_Select(1,yi); Delay2ms();
	SMG_Select(2,er); Delay2ms();
	SMG_Select(3,san);Delay2ms();
	SMG_Select(4,si); Delay2ms();
	SMG_Select(5,wu); Delay2ms();
	SMG_Select(6,liu);Delay2ms();
	SMG_Select(7,qi); Delay2ms();
	SMG_Select(8,ba); Delay2ms();	
}
void Timer0Init(void)		//0.1毫秒@11.0592MHz
{
	TMOD=0x00;		//設定定時器模式
	TH0=(8192-100)/32;
	TL0=(8192-100)%32;
	TR0=1;
	ET0=1;
	EA=1;
}
void Timer0(void)interrupt 1
{
	TH0=(8192-100)/32;
	TL0=(8192-100)%32;
	count++;time++;
	if(time==1000)//1s時間到
	{
		time=0;
		if(miao>0)
		{
			miao--;
		}
	}
	if(miao>0)
	{
		if(count==pwm)
			{
				P34=0;
			}
			if(count==10)
			{
				count=0;
				P34=1;
			}
	}
}
void LED()
{
	switch(flag)
	{
		case 1:InitHC138(4);P0=0XFE;break;//L1點亮
		case 2:InitHC138(4);P0=0XFD;break;//L2點亮
		case 3:InitHC138(4);P0=0XFB;break;//L3點亮
	}
	if(miao==0) //剩餘工作時間為0,關閉LED燈
	{
		InitHC138(4);
		P0=0XFF;
	}
}
void scan()
{
	if(s4==0)//工作模式切換
	{
		Delay10ms();
		if(s4==0)
		{
			switch(flag)
			{
				case 1:flag=2;pwm=2;break;//睡眠風
				case 2:flag=3;pwm=7;break;//自然風
				case 3:flag=1;pwm=2;break;//常風
			}
		}
		while(s4==0) SMG_Display();		
	}
	if(s5==0)
	{
		Delay10ms();
		if(s5==0)
		{
			switch(s5_flag) //定時時間切換
			{
				case 1:miao=60;s5_flag=2;break;
				case 2:miao=120;s5_flag=3;break;
				case 3:miao=0;s5_flag=1;break;
			}
		}
		while(s5==0) SMG_Display();
	}
	if(s6==0)//清除或停止
	{
		Delay10ms();
		if(s6==0) miao=0;
		while(s6==0) SMG_Display();
	}
	if(s7==0)//顯示溫度
	{
		Delay10ms();
		if(s7==0)
		{
			if(SMGflag==1) SMGflag=0;//切換為工作模式界面
			else if(SMGflag==0) SMGflag=1;//切換為溫度界面
		}
	}
	while(s7==0) SMG_Display();
}
void main()
{
	InitSystem();
	Timer0Init();
	while(1)
	{
		SMG_Display();
		LED();
		scan();
		Read_temperature();
	}
}
           

onewire.c

#include "stc15f2k60s2.h"

sbit DQ = P1^4;  //單總線接口
unsigned int temp;
//單總線延時函數
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	unsigned char a;
	while(t--)
	{
		for(a=0;a<12;a++);
	}
}

//通過單總線向DS18B20寫一個位元組
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//從DS18B20讀取一個位元組
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20裝置初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}
void Read_temperature()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xBE);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	init_ds18b20();
	temp=(MSB<<8)|LSB;
	temp=temp>>4;
}
           

完整的驅動函數及工程檔案請摸我

繼續閱讀