第十三届蓝桥杯单片机省赛真题

main.c

#include "key.h"
#include "iic.h"
#include "ds1302.h"
#include "led.h"
#include "sys.h"
#include "timer.h"
#include "smg.h"
#include "ult.h"
#include "onewire.h"
uchar SMG[] = {10,10,10,10,10,10,10,10};
uchar dot[] = {0,0,0,0,0,0,0,0};
uchar display_state;
uint temp;
uchar time_mode;
uchar mode;
uchar temp_canshu = 23;
uchar LED[] = {0,0,0,0,0,0,0,0};
void led_pro()
{
	if(led_dly<10) return;
	else led_dly = 0;
	if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 0)
	{
		LED[0] = 1;
	}
	else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 5)
	{
		LED[0] = 0;
	}
	LED[1] = (mode == 0)?1:0;
	LED[2] = (state_relay == 1)?led_state:0;
}


void smg_pro()
{
	if(smg_dly<100) return;
	else smg_dly = 0;

	if(display_state == 0)
	{
		dot[6] = 1;
		SMG[0] = 11;
		SMG[1] = 1;
		SMG[2] = 10;
		SMG[3] = 10;
		SMG[4] = 10;
		SMG[5] = temp/100;
		SMG[6] = temp/10%10;
		SMG[7] = temp%10;
	}
	else if(display_state == 1)
	{
		if(time_mode == 0)
		{
			dot[6] = 0;
			SMG[0] = 11;
			SMG[1] = 2;
			SMG[2] = 10;
			SMG[3] = Time[2] / 16;
			SMG[4] = Time[2] % 16;
			SMG[5] = 12;
			SMG[6] = Time[1] / 16;
			SMG[7] = Time[1] % 16;
		}
		else if(time_mode == 1)
		{
			dot[6] = 0;
			SMG[0] = 11;
			SMG[1] = 2;
			SMG[2] = 10;
			SMG[3] = Time[1] / 16;
			SMG[4] = Time[1] % 16;
			SMG[5] = 12;
			SMG[6] = Time[0] / 16;
			SMG[7] = Time[0] % 16;				
		}
	}	
	else if(display_state == 2)
	{
		SMG[0] = 11;
		SMG[1] = 3;
		SMG[2] = 10;
		SMG[3] = 10;
		SMG[4] = 10;
		SMG[5] = 10;
		SMG[6] = temp_canshu / 10;
		SMG[7] = temp_canshu % 10;
	}	
}
void collect()
{
	if(col_dly<10) return;
	else col_dly = 0;	
	ds1302_read();
	temp = temp_read(1);
	if(mode == 0)
	{
		if(temp > (temp_canshu*10))
		{
			state_relay = 1;
		}
		else if(temp <= (temp_canshu*10))
		{
			state_relay = 0;
		}
	}
	else if(mode == 1)
	{
		state_relay = 0;
		if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 0)
		{
			state_relay = 1;
		}
		else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 1)
		{
			state_relay = 1;
		}
		else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 2)
		{
			state_relay = 1;
		}
		else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 3)
		{
			state_relay = 1;
		}
		else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 4)
		{
			state_relay = 1;
		}

		else if(Time[1]/16 == 0 && Time[1]%16 == 0 && Time[0]/16 == 0 && Time[0]%16 == 5)
		{
			state_relay = 0;
		}
	}
}
void key_pro()
{
	uchar key;
	if(key_dly<10) return;
	else key_dly = 0;
	key = key_scan();	
	if(key == 15)
	{
		display_state ++;
		if(display_state == 3)
			display_state = 0;
	}
	else if(key == 11)
	{
		mode++;
		if(mode == 2)
			mode = 0;
	}
	else if(display_state == 1)
	{
		if(key == 12)
		{
			time_mode = 1;
		}
		else if(key == 17)
		{
			time_mode = 0;
		}
	}
	else if(display_state == 2)
	{
		if(key == 16)
		{
			temp_canshu ++;
			if(temp_canshu == 91)
				temp_canshu = 90;
		}
		else if(key == 12)
		{
			temp_canshu --;
			if(temp_canshu == 9)
				temp_canshu = 10;
		}			
	}

}

void main()
{
	sys_init();
	temp = temp_read(1);
	Delay_ms(750);
	Timer1Init();
	EA = 1;
	ds1302_write();
	while(1)
	{
		collect();
		led_pro();
		key_pro();
		smg_pro();
	}
}

timer.c

#include "timer.h"
unsigned long systick_ms;
uchar col_dly;
uchar key_dly;
uchar state_relay;
uchar smg_dly;
uchar led_dly;
uint fre;
uint count;
uint led_count;
uchar led_state;
uchar pos;
void Timer0Init(void)		//100微秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x9C;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
}

void Timer0Init_Count(void)		//1毫秒@12.000MHz
{
	TMOD &= 0xF0;	
	TMOD |= 0x05;		//设置定时器模式
	TL0 = 0;		//设置定时初始值
	TH0 = 0;		//设置定时初始值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}

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

void timer0() interrupt 1
{
	
}

void timer1() interrupt 3  
{
	if(led_count++ == 100)
	{
		led_state = ~led_state;
		led_count = 0;
	}
	systick_ms++;
	key_dly++;
	smg_dly++;
	col_dly++;
    led_dly++;	
	smg(SMG,dot,pos);
	led(LED,pos);
	if(++pos == 8) pos = 0;
	relay(state_relay);
}

key.c

#include "key.h"

bit key_up = 1;


uchar key_scan()
{
	static bit S17_flag;
	uchar key;
	P35 = P34 = 1;
	P32 = P33 = 0;
	if(key_up == 1 && (P35 == 0 || P34 == 0))
	{
		if(P35 == 0)         key = 3;
		else if(P34 == 0)   key = 4;
		else 				return 0;
		key_up = 0;
		P35 = P34 = 0;
		P33 = P32 = 1;
		if(P32  == 0)		key = key + 8;
		else if(P33 == 0)   key = key + 12;
		if(key == 12)
			S17_flag = 1;
		return key;
	}
	else if(P35 == 1 && P34 == 1)
	{
		key_up = 1;
		if(S17_flag == 1)
		{
			S17_flag = 0;
			return 17;
		}
	}
	return 0;	
}

led.c

#include "led.h"

void led(uchar * LED,uchar pos)
{
	static uchar temp = 0xff;
	if(LED[pos])
		temp &= ~(0x01<<pos);
	else
		temp |= 0x01<<pos;
	P0 = temp;
	hc573(4);
}

void relay(state_relay)
{
	uchar temp;
	if(state_relay)
		temp |= 0x10;
	else 
		temp &= ~0x10;
	P0 = temp;
	hc573(5);
}

void beep(state_beep)
{
	uchar temp;
	if(state_beep)
		temp |= 0x40;
	else 
		temp &= ~0x40;
	P0 = temp;
	hc573(5);
}

smg.c

#include "smg.h"

code uchar segment[] = {
  //   0    1    2    3    4    5    6    7    8    9 	熄灭   U    -
	  0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x3E,0x40
};

void smg(uchar *temp,uchar * dot,uchar pos)
{
	P0 = 0xff;
	hc573(7);
	P0 = 0x01 << pos;
	hc573(6);
	if(dot[pos] == 0)
		P0 = ~segment[temp[pos]];
	else
		P0 = (~segment[temp[pos]]) & 0x7f;
	hc573(7);
}

ds1302.c

#include "ds1302.h"
//
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;

code uchar write_addr[] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code uchar read_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};

uchar Time[] = {0x25,0x59,0x13};
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}
void ds1302_write()
{
	uchar i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<3;i++)
	{
		Write_Ds1302_Byte(write_addr[i],Time[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}
void ds1302_read()
{
	uchar i;
	for(i=0;i<3;i++)
	{
		Time[i] = Read_Ds1302_Byte(read_addr[i]);
	}
}

    onewire.c

#include "onewire.h"
sbit DQ = P1^4;
//
void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);
	}
}

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

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

uint temp_read(uchar dot)
{
	uchar tml,tmh;
	uint temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	tml = Read_DS18B20();
	tmh = Read_DS18B20();
	temp = (tmh<<8)|tml;
	switch(dot)
	{
		case 0:temp = temp *  0.0625;break;
		case 1:temp = temp *  0.625;break;
		case 2:temp = temp  *  6.25;break;
	}
	return temp;
}

iic.c

#include "iic.h"
#define DELAY_TIME	10
sbit sda = P2^1;
sbit scl = P2^0;
//
static void I2C_Delay(unsigned char n)
{
    do
    {
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();		
    }
    while(n--);      	
}

//
void I2CStart(void)
{
    sda = 1;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 0;
	I2C_Delay(DELAY_TIME);
    scl = 0;    
}

//
void I2CStop(void)
{
    sda = 0;
    scl = 1;
	I2C_Delay(DELAY_TIME);
    sda = 1;
	I2C_Delay(DELAY_TIME);
}

//
void I2CSendByte(unsigned char byt)
{
    unsigned char i;
	
    for(i=0; i<8; i++){
        scl = 0;
		I2C_Delay(DELAY_TIME);
        if(byt & 0x80){
            sda = 1;
        }
        else{
            sda = 0;
        }
		I2C_Delay(DELAY_TIME);
        scl = 1;
        byt <<= 1;
		I2C_Delay(DELAY_TIME);
    }
	
    scl = 0;  
}

//
unsigned char I2CReceiveByte(void)
{
	unsigned char da;
	unsigned char i;
	for(i=0;i<8;i++){   
		scl = 1;
		I2C_Delay(DELAY_TIME);
		da <<= 1;
		if(sda) 
			da |= 0x01;
		scl = 0;
		I2C_Delay(DELAY_TIME);
	}
	return da;    
}

//
unsigned char I2CWaitAck(void)
{
	unsigned char ackbit;
	
    scl = 1;
	I2C_Delay(DELAY_TIME);
    ackbit = sda; 
    scl = 0;
	I2C_Delay(DELAY_TIME);
	
	return ackbit;
}

//
void I2CSendAck(unsigned char ackbit)
{
    scl = 0;
    sda = ackbit; 
	I2C_Delay(DELAY_TIME);
    scl = 1;
	I2C_Delay(DELAY_TIME);
    scl = 0; 
	sda = 1;
	I2C_Delay(DELAY_TIME);
}

void PCF8591_Write(uchar control,uchar dat)
{
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(control);
	I2CWaitAck();
	I2CSendByte(dat);
	I2CWaitAck();
	I2CStop();
}

uchar PCF8591_Read(uchar control)
{
	uchar dat;
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(control);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0x91);	
	I2CWaitAck();
	dat = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	return dat;
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值