蓝桥杯单片机省赛第十二届第一场

 

 

 

main.c

#include <STC15F2K60S2.H>
#include <IIC.H>
#include <ONEWIRE.H>

#define NO_KEY 0XFF
#define KEY_STATE0 0
#define KEY_STATE1 1
#define KEY_STATE2 2

unsigned int temp_count;
float temp;
unsigned int temp_now=0;
unsigned int temp_set=25;
unsigned int dianya;
bit dianya_mode=1;
void Init_temp()
{
		init_ds18b20();
	  Write_DS18B20(0xcc);
		Write_DS18B20(0x44);
}

float read_temp()
{
		float date;
	  unsigned char low,high;
	  init_ds18b20();
	  Write_DS18B20(0xcc);
		Write_DS18B20(0xbe);
	
	  low=Read_DS18B20();
	  high=Read_DS18B20();
	  date=(high<<8)|low;
	  date=date*0.0625;
	 
	  return date;
	 
}

void temp_process()
{
		if(temp_count>100)
		{
			 temp_count=0;
			 temp=read_temp();
			 temp=temp*100;
			 temp_now=temp;
			 Init_temp();
		}
}
void Timer2Init()		//1毫秒@12.000MHz
{
	AUXR &= 0xFB;		//定时器时钟12T模式
	T2L = 0x18;		//设置定时初值
	T2H = 0xFC;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	IE2|=0x04;
	EA=1;
}

void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
		P0=p0date;
	  P2=P2&0X1F|p2date;
	  P2&=0X1F;
}
unsigned int key_count;
unsigned char display_mode;
unsigned char key_scan()
{
		static unsigned char key_state=KEY_STATE0;
	  unsigned char key_value=0,key_temp;
	  unsigned char key1,key2;
	 
	  P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;
	  if(P44==0){key1=0X70;}
		if(P42==0){key1=0XB0;}
		if(P35==0){key1=0XD0;}
		if(P34==0){key1=0XE0;}
		if((P44==1)&&(P42==1)&&(P35==1)&&(P34==1)){key1=0XF0;}
		
		P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;
		if(P30==0){key2=0X0E;}
		if(P31==0){key2=0X0D;}
		if(P32==0){key2=0X0B;}
		if(P33==0){key2=0X07;}
		if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1)){key2=0X0F;}
		
		key_temp=key1|key2;
		
		switch(key_state)
		{
			case KEY_STATE0:
				              if(key_temp!=NO_KEY)
											{
													key_state=KEY_STATE1;
											}
											break;
			case KEY_STATE1:if(key_temp==NO_KEY)
			                {
												 key_state=KEY_STATE0;
											}
											else
											{
												switch(key_temp)
												{
													case 0x77:key_value=4;break;
													case 0x7b:key_value=5;break;
													case 0x7d:key_value=6;break;
													case 0x7e:key_value=7;break;
													
													case 0xb7:key_value=8;break;
													case 0xbb:key_value=9;break;
													case 0xbd:key_value=10;break;
													case 0xbe:key_value=11;break;
													
													case 0xd7:key_value=12;break;
													case 0xdb:key_value=13;break;
													case 0xdd:key_value=14;break;
													case 0xde:key_value=15;break;
													
													case 0xe7:key_value=16;break;
													case 0xeb:key_value=17;break;
													case 0xed:key_value=18;break;
													case 0xee:key_value=19;break;
												}
												key_state=KEY_STATE2;
											}
				              break;
			case KEY_STATE2:
				             if(key_temp==NO_KEY)
										 {
												key_state=KEY_STATE0;
										 }break;
		}
		
		return key_value;
}

void key_process()
{
	  unsigned char key_val;
		if(key_count>10)
		{
		   key_val=key_scan();
			 switch(key_val)
			 {
				 case 4:
					      display_mode++;
				        if(display_mode==3)
								{
									display_mode=0;
								}
								break;
				 case 5:
					      dianya_mode=~dianya_mode;
				        break;
				 case 6:break;
				 case 7:break;
				 
				 case 8:
					      if(display_mode==1)
								{
									if(temp_set>0)
									{
										temp_set--;
									}
								}
								break;
				 case 9:
					      if(display_mode==1)
								{
									if(temp_set<99)
									{
										temp_set++;
									}
								}
								break;
				 case 10:break;
				 case 11:break;
				 
				 case 12:break;
				 case 13:break;
				 case 14:break;
				 case 15:break;
				 
				 case 16:break;
				 case 17:break;
				 case 18:break;
				 case 19:break;
			 }
		}
}

unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int smg_count;
	
void smg_show()
{
		unsigned char i;
	  Device_ctrl(0xc0,0);
	  Device_ctrl(0xe0,~smg_display[i]);
	  Device_ctrl(0xc0,0x01<<i);
	  i=(i+1)%8;
}

void smg_process()
{
		if(smg_count>3)
		{
			smg_count=0;
			if(display_mode==0)
			{
					smg_display[0]=0x39;
				  smg_display[1]=0x00;
				  smg_display[2]=0x00;
				  smg_display[3]=0x00;
				  
					smg_display[4]=smg_du[temp_now/1000];
				  smg_display[5]=smg_du[temp_now/100%10]|0x80;
				  smg_display[6]=smg_du[temp_now/10%10];
				  smg_display[7]=smg_du[temp_now%10];
			}
			if(display_mode==1)
			{
				  smg_display[0]=0x73;
				  smg_display[1]=0x00;
				  smg_display[2]=0x00;
				  smg_display[3]=0x00;
				
				  smg_display[4]=0x00;
				  smg_display[5]=0x00;
				  smg_display[6]=smg_du[temp_set/10];
				  smg_display[7]=smg_du[temp_set%10];
			}
			if(display_mode==2)
			{
				  smg_display[0]=0x77;
				  smg_display[1]=0x00;
				  smg_display[2]=0x00;
				  smg_display[3]=0x00;
				
				  smg_display[4]=0x00;
				  smg_display[5]=smg_du[dianya/100]|0x80;
				  smg_display[6]=smg_du[dianya/10%10];
				  smg_display[7]=smg_du[dianya%10];
			}
		}
}

unsigned int da_count;
void da(unsigned char date)
{
		IIC_Start();
    IIC_SendByte(0x90);
    IIC_WaitAck();
    IIC_SendByte(0x40);
    IIC_WaitAck(); 
    IIC_SendByte(date);
    IIC_WaitAck(); 
    IIC_Stop(); 	
}

void da_process()
{
		if(da_count>5)
		{
			da_count=0;
			
			if(dianya_mode)
			{
					if(temp_now<temp_set*100)
					{
							da(0);
						  dianya=0;
					}
					else
					{
							da(255);
						  dianya=500;
					}
			}
			else
			{
				if(temp_now<2000)
				{
					 da(51);
					 dianya=100;
				}
				else if(temp_now<4000)
				{
					  dianya=temp_now*3/20-200;
						da(dianya/2);
					  
				}
				else  
				{
					da(51*4);
					dianya=400;
				}
			}
		}
}

unsigned char led_ctrl=0xff;
unsigned int led_count;

void led_process()
{
		if(led_count>5)
		{
				led_count=0;
			 
			  if(dianya_mode==1)
				{
					led_ctrl&=~0x01;
				}
				else
				{
					led_ctrl|=0x01;
				}
				if(display_mode==0)
				{
					led_ctrl&=~0x02;
				}
				else
				{
					led_ctrl|=0x02;
				}
				
				if(display_mode==1)
				{
					led_ctrl&=~0x04;
				}
				else
				{
					led_ctrl|=0x04;
				}
				
				if(display_mode==2)
				{
					led_ctrl&=~0x08;
				}
				else
				{
					led_ctrl|=0x08;
				}
				Device_ctrl(0x80,led_ctrl);
		}
}
void main()
{
	  Timer2Init();
	  Device_ctrl(0xa0,0x00);
	  Device_ctrl(0x80,0xff);
	  Init_temp();
		while(1)
		{
			key_process();
			temp_process();
			smg_process();
			da_process();
			led_process();
		}
}

void timer2service() interrupt 12
{
	key_count++;
	smg_count++;
	temp_count++;
	da_count++;
	led_count++;
	smg_show();
}

onewire.c

#include "onewire.h"

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

//单总线写操作
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;
}

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

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include <STC15F2K60S2.H>

sbit DQ = P1^4;  

void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);

#endif

iic.c

#include "iic.h"

#define DELAY_TIME 5

//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}

//I2C总线启动信号
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//I2C总线停止信号
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答或非应答信号
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//I2C总线发送一个字节数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//I2C总线接收一个字节数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

iic.h

#ifndef __IIC_H
#define __IIC_H

#include <STC15F2K60S2.H>

#include "intrins.h"

sbit SDA = P2^1;
sbit SCL = P2^0;

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值