蓝桥杯——第七届省赛_模拟风扇控制系统

一、使用到的模块

DS18B20,LED,按键,PWM输出,数码管

题目要求

蓝桥杯——第七届省赛_模拟风扇控制系统_第1张图片
蓝桥杯——第七届省赛_模拟风扇控制系统_第2张图片

二、代码部分

实验平台:CT107D
实验芯片:stc15f2k60s2
实验现象:完成题目要求
代码如下

onewire.c

#include "stc15f2k60s2.h"

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
     
	t = t*12;
	while(t--);
}

//通过单总线向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;
}

unsigned int duwendu()
{
     
	 
	 unsigned char high,low;
	 unsigned int temp;
	 //ET0 = 0;
	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0x44);
	 Delay_OneWire(200);

	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0xbe);

	 low =	Read_DS18B20();
	 high = Read_DS18B20();
	 temp = high<<8;
	 temp |= low;
	 temp = temp*0.0625;
	 //ET0 = 1;
	 return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char rd_temperature(void);  //; ;
unsigned int read_temp();

#endif

main.c

#include
#include

#define uchar unsigned char
#define uint unsigned int

sbit buzz = P0^6;
sbit LED1 = P0^0;
sbit LED2 = P0^1;
sbit LED3 = P0^2;
sbit LED8 = P0^7;

bit open_flag = 0;
bit pwm_flag = 0;
bit S7_flag = 0;
bit temp_flag = 0;

sbit key_in1 = P3^0;
sbit key_in2 = P3^1;
sbit key_in3 = P3^2;
sbit key_in4 = P3^3;


uchar code duan[]={
     0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00,0x40};
uchar keysta[]={
     1,1,1,1};
uchar keymap[]={
     1,2,3,4};
uchar disbuff[8];
uchar mode=1;
uchar pwm;
uchar time;
uchar temp = 10;
uchar pwm_count;

void LED()
{
     	P0 = ~0x00;

	if(pwm_flag == 1)
	{
     		
		P2 = P2&0x1f|0x80;
		LED8 = 0;
		P2 = P2&0x1f;	
	}

	if(pwm_flag == 0)
	{
     		
		P2 = P2&0x1f|0x80;
		LED8 = 1;
		P2 = P2&0x1f;	
	}
	
	if(mode == 1)
	{
     
		P2 = P2&0x1f|0x80;
		LED1 = 0;
		LED2 = 1;
		LED3 = 1;
		P2 = P2&0x1f;	
	}
	
	if(mode == 2)
	{
     
		P2 = P2&0x1f|0x80;
		LED1 = 1;
		LED2 = 0;
		LED3 = 1;
		P2 = P2&0x1f;	
	}
	if(mode == 3)
	{
     
		P2 = P2&0x1f|0x80;
		LED1 = 1;
		LED2 = 1;
		LED3 = 0;
		P2 = P2&0x1f;	
	}	
}

void close_buzz()
{
     
	buzz = 0;
	P2 = P2&0x1f|0xa0;
	P2 = P2&0x1f;
}

void display()
{
     
	static uchar index;
	
	P2 = P2&0x1f|0xe0;
	P0 = ~0x00;
	P2 = P2&0x1f;


	P2 = P2&0x1f|0xc0;
	P0 = 1<<index;
	P2 = P2&0x1f;

	
	P2 = P2&0x1f|0xe0;
	P0 = ~duan[disbuff[index]];
	P2 = P2&0x1f;

	index++;
	index &= 0x07;	
}

void show_num()
{
     
	if(S7_flag == 1)
	{
     
	 disbuff[0]=17;
	 disbuff[1]=4;
	 disbuff[2]=17;
	 disbuff[3]=16;
	 disbuff[4]=16;
	 disbuff[5]=temp/10;
	 disbuff[6]=temp%10;
	 disbuff[7]=12;	
	}

	else
	{
     
	 disbuff[0]=17;
	 disbuff[1]=mode;
	 disbuff[2]=17;
	 disbuff[3]=16;
	 disbuff[4]=0;
	 disbuff[5]=time/100;
	 disbuff[6]=time/10%10;
	 disbuff[7]=time%10;	
	}
}

void keyscan()
{
     
	static uchar keybuff[]={
     0xff,0xff,0xff,0xff};
	uchar j;

	keybuff[0] = keybuff[0]<<1|key_in1;	
	keybuff[1] = keybuff[1]<<1|key_in2;
	keybuff[2] = keybuff[2]<<1|key_in3;
	keybuff[3] = keybuff[3]<<1|key_in4;

	for(j=0;j<4;j++)
	{
     
		if((keybuff[j]&0x0f) == 0x00)
		{
     
			 keysta[j]=0;
		}
		
		else if((keybuff[j]&0x0f) == 0x0f)
		{
     
			 keysta[j]=1;
		}	
	}
}

void keyfun(uchar key)
{
     
	static uchar S5_count = 0;
	 switch(key)
	 {
     
	 	case 1:
		S7_flag = ~S7_flag;break;

		case 2:	
		open_flag = 0;
		time = 0;
		pwm_count = 0;
		break;

		case 3:
		
		if(++S5_count>=3)
		{
     
			S5_count = 0;
		}

		if(S5_count == 0)
		{
     
			time = 0;	
		}

		if(S5_count == 1)
		{
     
			time = 60;	
		}

		if(S5_count == 2)
		{
     
			time = 120;	
		}
		open_flag = 1;
		break;

		case 4:
		if(++mode == 4) mode = 1; break;
	 }
}

void keydrive()
{
     
	static uchar keyback[]={
     1,1,1,1};
	uchar i;
	for(i=0;i<4;i++)
	{
     
		if(keysta[i]!=keyback[i])
		{
     
			if(keysta[i] != 0)
			{
     
				keyfun(keymap[i]);
			}
			keyback[i]=	keysta[i];
		}		
	}
}

void pwm_set()
{
     
	if(mode == 1)
	{
     
		pwm = 2;	
	}
	

	if(mode == 2)
	{
     
		pwm = 3;	
	}
	

	if(mode == 3)
	{
     
		pwm = 7;	
	}

}
void Timer0Init(void)		//100微秒@11.0592MHz
{
     
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x50;		//设置定时初值
	TH0 = 0xFB;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
	EA = 1;
}

void time1() interrupt 1 
{
     
	//static count;
	static uint sec_count;
	 keyscan();
	 display();
	 
	 if(++pwm_count == 10)
	 {
     
	 	pwm_count = 0;
		pwm_flag = 0;				
	 }

	 if(open_flag == 1)
	 {
     
		 if(pwm_count == pwm)
		 {
     
		 	pwm_flag = 1;
		 }	 	
	 }

	 if(++sec_count == 10000)
	 {
     
		temp_flag = 1;
		sec_count = 0;
		if(time!=0)
		{
     
			time--;	
		}
		
		if(time == 0)
		{
     
			open_flag = 0;
			pwm_count = 0;
		}
	 }
	  LED();
}

void main()
{
     
	close_buzz();
	Timer0Init();
	while(1)
	{
     
	show_num();
	keydrive();
	LED();
	pwm_set();

	if(temp_flag == 1)
	{
     
		temp = duwendu();
		temp_flag = 0;
	}
	}
}

你可能感兴趣的:(蓝桥杯,单片机)