蓝桥杯 模拟风扇控制系统
题目要求
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);
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)
{
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)
{
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;
case 2:InitHC138(4);P0=0XFD;break;
case 3:InitHC138(4);P0=0XFB;break;
}
if(miao==0)
{
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)
{
unsigned char a;
while(t--)
{
for(a=0;a<12;a++);
}
}
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);
}
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;
}
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;
}
完整的驱动函数及工程文件请摸我