蓝桥杯单片机组第九届国赛

main.c

#include"stc15f2k60s2.h"
#include"onewire.h"
#include"iic.h"

unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc0&0x7f,0xf9&0x7f,0xa4&0x7f,0xb0&0x7f,0x99&0x7f,0x92&0x7f,0x82&0x7f,0xf8&0x7f,0x80&0x7f,0x90&0x7f,~0x3E,~0x71,~0x39,~0x76,~0x5E,0xff};
unsigned char smgbuffer[8];	 //U F C H D
unsigned char smgcom,keytt,wendutt,ADtt;
unsigned char Rb2;

unsigned char mode=0;
unsigned char moshi=0;

unsigned int flag,NE555tt,zheng,fan,zheng_1,fan_1,T,f;


void display();
void keyscan();

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

void Timer0() interrupt 1
{
	if(P34==0)
	{
		fan++;
		if(flag==0){flag=1; zheng_1=zheng; zheng=0;}
	}	
	if(P34==1)
	{
		zheng++;
		if(flag==1){flag=0; fan_1=fan; fan=0;}
	}
}

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

void Timer1() interrupt 3
{
	unsigned char P0_keeper=0,P2_keeper=0;
	P0_keeper=P0; P2_keeper=P2; P2=P2&0x1f;

	wendutt++;
	ADtt++;
	keytt++; if(keytt==10){keyscan(); keytt=0;}
	display();

	NE555tt++;
	{
		if(NE555tt==969){TR0=1; ET0=1;}
		if(NE555tt==999){NE555tt=0; TR0=0; ET0=0; T=(zheng_1+fan_1)*5; f=1000000/T;}
	}

	P0=P0_keeper; P2=P2_keeper;
}

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){P0=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	else if(trg&0x08){if(mode==0||mode==1) moshi++; if(moshi==3)moshi=0;}
}

void wenduwengding()
{
	unsigned char Ti;
	for(Ti=0;Ti<70;Ti++)
	{
		temp=tempget()*100+0.5;
	}
}

void wenduhuoqu()
{
	if(wendutt==200)
	{
		temp=tempget()*100+0.5;
		wendutt=0;
	}	
}

void ADC()
{
	if(ADtt==200)
	{
		Rb2=AD_read(0x03);
		ADtt=0;
	}
}

void xianshi()
{
	if(mode==0)
	{
		if(moshi==0)
		{
			smgbuffer[0]=22;
			smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=25;
			smgbuffer[4]=temp/1000;
			smgbuffer[5]=temp%1000/100+10;
			smgbuffer[6]=temp%1000%100/10;
			smgbuffer[7]=temp%10;
		}
		else if(moshi==1)
		{
			smgbuffer[0]=20;
			smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=25;
			smgbuffer[6]=(unsigned int)(Rb2*1.96+0.5)/100;
			smgbuffer[7]=(unsigned int)(Rb2*1.96+0.5)%100/10;
		}
		else if(moshi==2)
		{
			unsigned char f1,f2,f3,f4,f5;
			smgbuffer[0]=21;
			smgbuffer[1]=smgbuffer[2]=25;
			f1=f/10000; f2=f%10000/1000; f3=f%10000%1000/100; f4=f%10000%1000%100/10; f5=f%10;
			if(f1==0){smgbuffer[3]=25; smgbuffer[4]=f2; smgbuffer[5]=f3; smgbuffer[6]=f4; smgbuffer[7]=f5;}
			else if(f1==0 && f2==0){smgbuffer[3]=smgbuffer[4]=25; smgbuffer[5]=f3; smgbuffer[6]=f4; smgbuffer[7]=f5;}
			else if(f1==0 && f2==0 && f3==0){smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=25; smgbuffer[6]=f4; smgbuffer[7]=f5;}
		}
	}
//	else if(mode==1)
//	{
//	
//	}
}

void main()
{
	P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	config(0x3f);
	wenduwengding();
	Timer0Init();
	Timer1Init();
	while(1)
	{
		ADC();
		wenduhuoqu();
		xianshi();
	}
}

onwire.c

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

void config(unsigned char jingdu)
{
	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0x4e);
	 Write_DS18B20(0x5f);
	 Write_DS18B20(0x00);
	 Write_DS18B20(jingdu);	 	
}



iic.c

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

#include "reg52.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;  
		IIC_WaitAck();
}

//从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);
    }
    IIC_WaitAck();
		return da;    
}

unsigned char AD_read(unsigned char add)
{
	unsigned char AD_temp;

	IIC_Start();
	IIC_SendByte(0x90);
	IIC_SendByte(add);

	IIC_Start();
	IIC_SendByte(0x91);
	AD_temp=IIC_RecByte();
	IIC_Stop();

	return AD_temp;
}

void EEPROM_write(unsigned char add,dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_SendByte(add);
	IIC_SendByte(dat);
	IIC_Stop();
}

unsigned char EEPROM_read(unsigned char add)
{
	unsigned char EEPROM_temp;

	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_SendByte(add);

	IIC_Start();
	IIC_SendByte(0xa1);
	EEPROM_temp=IIC_RecByte();
	IIC_Stop();

	return EEPROM_temp;	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值