蓝桥杯单片机第12届省赛程序题参考答案

答案仅供参考,有错误欢迎指正,非常感谢!!!

文件较多,代码较长,感谢查看!!!

目录

main.h

main.c

Init.h

Init.c

SMG.h

SMG.c

JZKey.h

JZKey.c

DSQ.h

DSQ.c

YanShi.h

YanShi.c

iic.h

iic.c

onewire.h

onewire.c

main.h

#ifndef __MAIN_H_
#define __MAIN_H_

#define uchar unsigned char
#define uint unsigned int

#include 
#include "Init.h"
#include "SMG.h"
#include "JZKey.h"
#include "DSQ.h"
#include "YanShi.h"
#include "iic.h"
#include "onewire.h"

void JieMian_Show();
void LED_Show();

#endif

main.c

#include "main.h"

uchar key_num=0;
uchar jie_mian=0;
char WenDu_CanShu=25;
uchar mo_shi=0;
uchar flag0=0;
uint wen_du;
uint dian_ya;

void main(){
	Init_BanZi();
	SMG_Init();
	DSQ_0_Init();
	JZKey_Init();
	while(1){
		key_num=JZKey_GetKeynum();
		if(key_num==4){
			jie_mian++;
			jie_mian=jie_mian%3;
		}
		else if(key_num==8&&jie_mian==1){
			WenDu_CanShu--;
			if(WenDu_CanShu<=0){
				WenDu_CanShu=0;
			}
		}
		else if(key_num==9&&jie_mian==1){
			WenDu_CanShu++;
			if(WenDu_CanShu>=99){
				WenDu_CanShu=99;
			}
		}
		else if(key_num==5){
			mo_shi++;
			mo_shi=mo_shi%2; 
		}
		wen_du=DS18B20_GetWenDu()*100;
		if(mo_shi==0){//DAC输出电压与温度相关
			if(wen_du<(WenDu_CanShu*100)){//实时温度小于温度参数,输出0V
				dian_ya=0;
				IIC_DAC_XieDate(0);
			}
			else{//否则输出5V
				dian_ya=500;
				IIC_DAC_XieDate(255);
			}
		}
		else if(mo_shi==1){//DAC输出电压与函数相关
			if(wen_du<2000){
				dian_ya=100;
				IIC_DAC_XieDate(51);
			}
			else if(wen_du>=2000&&wen_du<=4000){
				dian_ya=(wen_du*1.0)*(3.0/20.0)-200;
				IIC_DAC_XieDate((dian_ya*51.0)/100.0);
			}
			else if(wen_du>4000){
				dian_ya=400;
				IIC_DAC_XieDate(204);
			}
		}
	}
}

void DSQ_0_ZD() interrupt 1{
	flag0++;
	if(flag0>=1){
		flag0=0;
		JieMian_Show();
		LED_Show();
	}
}

void JieMian_Show(){//界面显示
	switch(jie_mian){
		case 0:{//温度显示界面
			if(wen_du>=1000){
				SMG_Show(12,16,16,16,wen_du/1000,((wen_du/100)%10)+32,(wen_du/10)%10,wen_du%10);
			}
			else if(wen_du>=0){
				SMG_Show(12,16,16,16,16,(wen_du/100)+32,(wen_du/10)%10,wen_du%10);
			}
			break;
		}
		case 1:{//参数设置界面
			if(WenDu_CanShu>=10){
				SMG_Show(18,16,16,16,16,16,WenDu_CanShu/10,WenDu_CanShu%10);
			}
			else if(WenDu_CanShu>=0){
				SMG_Show(18,16,16,16,16,16,16,WenDu_CanShu);
			}
			break;
		}
		case 2:{//DAC输出界面
			SMG_Show(12,16,16,16,16,(dian_ya/100)+32,(dian_ya/10)%10,dian_ya%10);
			break;
		}
	}
}

void LED_Show(){
	uchar L1=0xff;
	uchar L2_4=0xff;
	if(mo_shi==0){//处于模式1状态,L1亮
		L1=0xfe;
	}
	else if(mo_shi==1){//不处于模式1状态,L1灭
		L1=0xff;
	}
	switch(jie_mian){
		case 0:{
			L2_4=0xfd;
			break;
		}
		case 1:{
			L2_4=0xfb;
			break;
		}
		case 2:{
			L2_4=0xf7;
			break;
		}
	}
	P0=L1&L2_4;
	P2=(P2&0x1f)|0x80;
	P2=(P2&0x1f)|0x00;
}

Init.h

#ifndef __INIT_H_
#define __INIT_H_

#include 

void Init_BanZi();

#endif

Init.c

#include "Init.h"

void Init_BanZi(){
	P2=(P2&0x1f)|0xa0;
	P0=0x00;
	P2=(P2&0x1f)|0x80;
	P0=0xff;
}

SMG.h

#ifndef __SMG_H_
#define __SMG_H_

#include 

void SMG_Init();
void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8);

#endif

SMG.c

#include "SMG.h"

unsigned char code SMG_ZK[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

void SMG_Init(){
	P2=(P2&0x1f)|0xc0;
	P0=0xff;
	P2=(P2&0x1f)|0xe0;
	P0=0xff;
}

void SMG_Show(unsigned char n1,n2,n3,n4,n5,n6,n7,n8){
	static unsigned char i=0;
	i++;
	i=i%8;
	P2=(P2&0x1f)|0xc0;
	switch(i){
		case 1:{
			P0=0x01;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n1];
			break;
		}
		case 2:{
			P0=0x02;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n2];
			break;
		}
		case 3:{
			P0=0x04;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n3];
			break;
		}
		case 4:{
			P0=0x08;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n4];
			break;
		}
		case 5:{
			P0=0x10;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n5];
			break;
		}
		case 6:{
			P0=0x20;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n6];
			break;
		}
		case 7:{
			P0=0x40;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n7];
			break;
		}
		case 0:{
			P0=0x80;
			P2=(P2&0x1f)|0xe0;
			P0=~SMG_ZK[n8];
			break;
		}
	}
	P2=(P2&0x1f)|0x00;
}

JZKey.h

#ifndef __JZKEY_H_
#define __JZKEY_H_

#include "YanShi.h"

void JZKey_Init();
unsigned char JZKey_GetKeynum();

#endif

JZKey.c

#include "JZKey.h"

void JZKey_Init(){
	P30=1;
	P31=1;
	P32=1;
	P33=1;
	P34=0;
	P35=0;
	P42=0;
	P44=0;
}

unsigned char JZKey_GetKeynum(){
	unsigned char key_num=0;
	if(P30==0){
		P30=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=19;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=15;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=11;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=7;
			}
			while(P44==0);
		}
	}
	else if(P31==0){
		P31=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=18;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=14;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=10;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=6;
			}
			while(P44==0);
		}
	}
	else if(P32==0){
		P32=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){        
			Delay10ms();
			if(P34==0){
				key_num=17;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=13;
			}   
			while(P35==0);
		}   
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=9;
			}
			while(P42==0); 
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=5;
			}
			while(P44==0);
		}     
	}
	else if(P33==0){
		P33=0;
		P34=1;
		P35=1;
		P42=1;
		P44=1;
		if(P34==0){
			Delay10ms();
			if(P34==0){
				key_num=16;
			}
			while(P34==0);
		}
		else if(P35==0){
			Delay10ms();
			if(P35==0){
				key_num=12;
			}
			while(P35==0);
		}
		else if(P42==0){
			Delay10ms();
			if(P42==0){
				key_num=8;
			}
			while(P42==0);
		}
		else if(P44==0){
			Delay10ms();
			if(P44==0){
				key_num=4;
			}
			while(P44==0);
		}
	}
	JZKey_Init();
	return key_num;
}

DSQ.h

#ifndef __DSQ_H_
#define __DSQ_H_

#include 

void DSQ_0_Init();

#endif

DSQ.c

#include "DSQ.h"

void DSQ_0_Init(){
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x20;		//设置定时初值
	TH0 = 0xD1;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	EA=1;
	ET0=1;
}

YanShi.h

#ifndef __YANSHI_H_
#define __YANSHI_H_

#include 
#include "intrins.h"

void Delay10ms();

#endif

YanShi.c

#include "YanShi.h"

void Delay10ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 117;
	j = 184;
	do
	{
		while (--j);
	} while (--i);
}

iic.h

#ifndef _IIC_H
#define _IIC_H

#include 

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); 

unsigned char IIC_ADC_GetDate(unsigned char di_zhi);
void IIC_DAC_XieDate(unsigned char date);
void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char date);
unsigned char IIC_EEPROM_GetDate(unsigned char di_zhi);

#endif

iic.c

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

#include "iic.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 IIC_ADC_GetDate(unsigned char di_zhi){
	unsigned char date;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(di_zhi);
	IIC_WaitAck();
	IIC_Stop();

	IIC_Start();
    IIC_SendByte(0x91);
	IIC_WaitAck();
	date=IIC_RecByte();
	IIC_SendAck(1);
	IIC_WaitAck();
	IIC_Stop();
	return date;
}

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

void IIC_EEPROM_XieDate(unsigned char di_zhi,unsigned char date){
	IIC_Start();
    IIC_SendByte(0xa0);
    IIC_WaitAck();
    IIC_SendByte(di_zhi);
    IIC_WaitAck();
    IIC_SendByte(date);
    IIC_WaitAck();
    IIC_Stop();
}

unsigned char IIC_EEPROM_GetDate(unsigned char di_zhi){
	unsigned char date;
	IIC_Start();
    IIC_SendByte(0xa0);
    IIC_WaitAck();
    IIC_SendByte(di_zhi);
    IIC_WaitAck();
    IIC_Stop();

    IIC_Start();
    IIC_SendByte(0xa1);
    IIC_WaitAck();
    date=IIC_RecByte();
    IIC_SendAck(1);
    IIC_WaitAck();
    IIC_Stop();
    return date;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include 


unsigned char rd_temperature(void);  //; ;

double DS18B20_GetWenDu();

#endif

onewire.c

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

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

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

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

double DS18B20_GetWenDu(){
	double wen_du,zheng_shu,xiao_shu;
	unsigned char di,gao;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);

	init_ds18b20();
	EA=0;
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	EA=1;
	EA=0;
	di=Read_DS18B20();
	gao=Read_DS18B20();
	EA=1;
	if(gao>=248){
		di=~di;
		gao=~gao;
		if(di==0xff){
			di=0;
			gao++;
		}
		else{
			di++;
		}
		zheng_shu=((gao<<4)|(di>>4))*1.0;
		xiao_shu=(di&0x0f)*0.0625;
		wen_du=-(zheng_shu+xiao_shu);
	}
	else{
		zheng_shu=((gao<<4)|(di>>4))*1.0;
        xiao_shu=(di&0x0f)*0.0625;
        wen_du=(zheng_shu+xiao_shu);
	}
	return wen_du;
}

你可能感兴趣的:(蓝桥杯单片机省赛,单片机,51单片机,嵌入式硬件,物联网)