蓝桥杯-模拟风扇控制系统

 模拟风扇控制系统

蓝桥杯-模拟风扇控制系统_第1张图片

蓝桥杯-模拟风扇控制系统_第2张图片

 

蓝桥杯-模拟风扇控制系统_第3张图片

 


/***********

模拟风扇控制系统

************/

#include 
#include 
#define uint  unsigned int
#define uchar unsigned char

uint  wendu,count;
uchar set,model,time,num,aa;
uchar yi,er,san,si,wu,liu,qi,ba;
uchar shuma[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};   //1100 0110
void keyscan();
void display();
//
void allinit()
{
	P2=0x80;P0=0xff;
	P2=0xa0;P0=0x00;
	P2=0xc0;P0=0xff;
	P2=0xff;P0=0xff;
}
//
void Delay_Ms(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
	   for(y=845;y>0;y--);
}
//
void timer0Init(void)		//100微秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xAE;		//设置定时初值
	TH0 = 0xFB;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

//
void main()
{
	set = 0;
	time = 0;
	model = 1;
	allinit();
	timer0Init();
	while(1)
	{
		keyscan();
		display();
	}
}
//
void exter0() interrupt 1   //100us进入一次   100us * 10000 = 1s
{
	num++;count++;
	
	if(count>=10000)
	{
		if(time>0) {time--;count=0;}
		else {time=0;count=0;}
	}
	if(num>=10) {num=0;}
	
	if(model==1)
	{
		if(num<=2) {P34 = 1;}
		else {P34 = 0;}
	}
	else if(model==2)
	{
		if(num<=3) {P34 = 1;}
		else {P34 = 0;}
	}
	else if(model==3)
	{
		if(num<=7) {P34 = 1;}
		else {P34 = 0;}
	}
}
//
void keyscan()
{
	if(set==0)
	{
		if(time==0 || P31==0) {EA=0;ET0=0;time=0;while(P31==0);}
		else {EA=1;ET0=1;}
		if(P30==0) {EA=0;ET0=0;set=1;while(P30==0);}
		
		if(P33==0)
		{
					 if(model==1) {model=2;}
			else if(model==2) {model=3;}
			else if(model==3) {model=1;}
			while(P33==0);
		}
		//
		if(time>0)
		{
		       if(model==1) {P2=0x80;P0=0xfe;}
			else if(model==2) {P2=0x80;P0=0xfd;}
			else if(model==3) {P2=0x80;P0=0xfb;}
		}
		else {P2=0x80;P0=0xff;} 
		//
		if(P32==0)
		{
					 if(time==0) {time=60;}
			else if(time>0 && time<=60) {time=120;}
			else if(time>60) {time=0;}
			while(P32==0);
		}
		yi=10;er=model;san=10;si=11;
		wu=time/1000;liu=time%1000/100;
		qi=time%100/10;ba=time%10;
	}
	//
	else if(set==1)
	{
		P2=0x80;P0=0xff;
		if(P30==0) {EA=1;ET0=1;set=0;while(P30==0);}
		
		wendu = get_wendu();
		
		yi=10;er=4;san=10;si=11;wu=11;
		liu=wendu/10;qi=wendu%10;ba=12;
	}
}
//
void display()
{
	P2=0xc0;
	P0=0x01;
	P2=0xff;
	P0=shuma[yi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x02;
	P2=0xff;
	P0=shuma[er];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x04;
	P2=0xff;
	P0=shuma[san];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x08;
	P2=0xff;
	P0=shuma[si];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x10;
	P2=0xff;
	P0=shuma[wu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x20;
	P2=0xff;
	P0=shuma[liu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x40;
	P2=0xff;
	P0=shuma[qi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x80;
	P2=0xff;
	P0=shuma[ba];
	Delay_Ms(1);
}

onewire.h

#ifndef _ONEWIRE_H
#define _ONEWIRE_H

#include "stc15f2k60s2.h"
#include "intrins.h"

#define OW_SKIP_ROM 0xcc
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xbe

//IC引脚定义
sbit DQ = P1^4;

//函数声明
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
bit Init_DS18B20(void);
unsigned char Read_DS18B20(void);

unsigned int get_wendu();
#endif

onewire.c

/*
  程序说明: 单总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台
  日    期: 2011-8-9
*/

#include "onewire.h"

//单总线延时函数
void Delay_OneWire(unsigned int t)
{
	unsigned char i;
  while(t--)
		for(i=0;i<8;i++);
}

//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;
}

//通过单总线向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;
}
//
unsigned int get_wendu()
{
	unsigned int  date;
	unsigned char high,low;
	Init_DS18B20();
  Write_DS18B20(0xcc);
  Write_DS18B20(0x44);
	
	Init_DS18B20();
  Write_DS18B20(0xcc);
  Write_DS18B20(0xbe);
	
	low  = Read_DS18B20();
	high = Read_DS18B20();
	
	date = high;
	date <<= 8;
	date = date | low;
	date = date*0.0625;
	
	return date;
}

个人见解,感谢阅读。

你可能感兴趣的:(蓝桥杯)