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

main.c

#include
#include
#include

unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc6,0x8c,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,DAtt;
unsigned int wendutt;
unsigned int temp;
unsigned char wendu;
bit flag,mode,Tmode;       //mode:0显示 1设置   Tmode:0下限  1上限
unsigned int Tmin=20,Tmax=30,Tmin_0,Tmax_0;

void display();
void keyscan();

void Timer1Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x20;		//设置定时初值
	TH1 = 0xD1;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	EA=1;
	ET1=1;
}

void Timer1() interrupt 3
{
	unsigned char P2_kepper=P2,P0_kepper=P0;
	P2=P2&0x1f;
	display();
	keytt++; if(keytt==10) {keytt=0; keyscan();}
	wendutt++;
	P2=P2_kepper,P0=P0_kepper  ;
	DAtt++;
}

void display()
{
	P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	if(smgcom==8)	smgcom=0;
}

void keyscan()
{
	unsigned char dat,trg,cnt;
	dat=P3^0xff;
	trg=dat&(dat^cnt);
	cnt=dat;
	
	if(trg&0x01)
	{
		if(mode==1)
		{
		  if(Tmode==0){Tmin--; if(Tmin==-1)Tmin=99;}
			else if(Tmode==1){Tmax--; if(Tmax==-1)Tmax=99;}
		}
	}			
	else if(trg&0x02)
	{
		if(mode==1)
		{
		  if(Tmode==0){Tmin++; if(Tmin==100)Tmin=0;}
			else if(Tmode==1){Tmax++; if(Tmax==100)Tmax=0;}
		}
	}	
	else if(trg&0x04) Tmode=~Tmode;
	else if(trg&0x08)
	{
		if(mode==0){Tmin_0=Tmin; Tmax_0=Tmax; mode=1;}
		else if(mode==1){if(Tmax>=Tmin){flag=0;mode=0;} else if(Tmax<Tmin){flag=1; mode=0; Tmax=Tmax_0; Tmin=Tmin_0;}}
	}
}

void xianshi()
{
	if(mode==0)
	{
		if(wendutt==750)
		{
			wendutt=0;
			temp=tempget()*100+0.5;
		}
		smgbuffer[0]=10;
		smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=12;
		smgbuffer[6]=temp/1000; smgbuffer[7]=temp%1000/100;
	}
	else if(mode==1)
	{
		smgbuffer[0]=11;
		smgbuffer[1]=smgbuffer[2]=smgbuffer[5]=12;
		smgbuffer[3]=Tmax/10; smgbuffer[4]=Tmax%10;
		smgbuffer[6]=Tmin/10; smgbuffer[7]=Tmin%10;		
	}
}

void LED()
{
	if(mode==0&&flag==0)
	{
		if(wendu>Tmax){P0=0xfe; P2=P2&0x1f|0x80; P2=P2&0x1f;}
		else if(wendu>=Tmin&&wendu<=Tmax){P0=0xfd; P2=P2&0x1f|0x80; P2=P2&0x1f;}
		else if(wendu<Tmin){P0=0xfb; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	}
	else if(mode==0&&flag==1)
	{
	  if(wendu>Tmax){P0=0xfe&0xf7; P2=P2&0x1f|0x80; P2=P2&0x1f;}
		else if(wendu>=Tmin&&wendu<=Tmax){P0=0xfd&0xf7; P2=P2&0x1f|0x80; P2=P2&0x1f;}
		else if(wendu<Tmin){P0=0xfb&0xf7; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	}
}

void DA()
{
	if(DAtt==200)
	{
		DAtt=0;
    if(wendu>Tmax) AD_write(0x40,204);
		else if(wendu>=Tmin&&wendu<=Tmax) AD_write(0x40,151);
		else if(wendu<Tmin) AD_write(0x40,102);
  }
}

void main()
{
	unsigned char TT;
	P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	for(TT=0;TT<70;TT++) temp=tempget()*100+0.5;
	Timer1Init();
	while(1)
	{
		wendu=temp/100;
		xianshi();
		LED();
		DA();
	}
}

onwire.c

/*
  程序说明: 单总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
  日    期: 2011-8-9
*/
#include "stc15f2k60s2.h"

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

//单总线延时函数
void Delay_OneWire(unsigned int t);  //STC89C52RC


//通过单总线向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 Delay_OneWire(unsigned int t)  //STC89C52RC
{
	t=t*12;
	while(t--);
}

float tempget()
{
	float temp;
	unsigned char low,high;
	
	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|low)*0.0625;
	return temp;
}








iic.c

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

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

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}

//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
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;  					// 0:应答,1:非应答
//    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;    
}

unsigned char AD_read(unsigned char add)
{
	unsigned char AD_temp;
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	AD_temp=IIC_RecByte();
	IIC_Stop();
	
	return AD_temp;
}

void AD_write(unsigned char add,unsigned char dat)  //注意没有返回值 是void!!!!!
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
                    
	IIC_Stop();
}




你可能感兴趣的:(蓝桥杯,蓝桥杯,单片机,职场和发展)