12届蓝桥杯单片机模拟题(包括客观题)

程序题

12届蓝桥杯单片机模拟题(包括客观题)_第1张图片

main.c

#include

#define uchar unsigned char

#define s0 0
#define s1 1
#define s2 2

int read_vol(uchar add);
	
code uchar semg[13] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90,
										   0xc1, 0x8c, 0xff};
code uchar semg_bit[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
uchar semg_temp[8] = {12, 12, 12, 12, 12, 12, 12, 12};

uchar state = s0, state_dat = s0, state_para = s0;
int vol[2][2] = {{0, 0}, {250, 300}};


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

	i = 59;
	j = 90;
	do
	{
		while (--j);
	} while (--i);
}


void allinit()
{
	P2 = 0x80;	P0 = 0xff;
	P2 = 0xa0;	P0 = 0x00;
	P2 = 0xc0;	P0 = 0xff;
	P2 = 0xe0;	P0 = 0xff;
}


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


void led()
{
	if(vol[0][0] > vol[1][0])
		P00 = 0;
	else
		P00 = 1;
	
	if(vol[0][1] > vol[1][1])
		P01 = 0;
	else
		P01 = 1;
	
	if(state_dat == 0 || state_para == 0)
		P02 = 0;
	else
		P02 = 1;
	
	if(state == 0)
		P03 = 0;
	else
		P03 = 1;
	
	if(vol[0][0] > vol[0][1])
		P04 = 0;
	else
		P04 = 1;
	
	P0 = P0 | 0xe0;
	P2 = 0x80;
	P2 = 0x00;	P0 = 0xff;
}


void display()
{
	uchar i;
	
	if(i == 5)
	{
		P2 = 0xe0; 	P0 = semg[semg_temp[i]] & 0x7f;
	}
	else
	{
		P2 = 0xe0; 	P0 = semg[semg_temp[i]];
	}
	P2 = 0xc0; 	P0 = semg_bit[i];
	
	P2 = 0x00;	P0 = 0xff;
	
	i++;
	if(i == 8)
		i = 0;
}


void fun() interrupt 1
{
	uchar i;
	
	display();
	led();

	i++;
	if(i == 50)
	{
		i = 0;
		vol[0][0] = read_vol(0x01);
		vol[0][1] = read_vol(0x03);		
	}
}


char keyscan()
{
	static uchar keybefore = 0;
	uchar keycode = 0, trg = 0;
	char keyvalue = -1;
	
	P3 = P3 | 0x0f;	P44 = 0; P42 = 0;	P35 = 0;
	keycode = (P3 ^ 0xff) & 0x0f;
	trg = keycode & (keycode ^ keybefore);
	keybefore = keycode;
	
	if(trg == 0x01)						keyvalue = 7;
	else if(trg == 0x02)			keyvalue = 6;
	else if(trg == 0x04)			keyvalue = 5;
	else if(trg == 0x08)			keyvalue = 4;
	else											keyvalue = -1;
	
	return keyvalue;
}


void menu(char keyvalue)
{
	static int temp_vp1 = 250, temp_vp3 = 300;
	
	switch(state)
	{
		case s0:
		{
			semg_temp[0] = 10;
			semg_temp[1] = state_dat * 2 + 1;
			semg_temp[2] = 12;
			semg_temp[3] = 12;
			semg_temp[4] = 12;
			semg_temp[5] = vol[state][state_dat] / 100;
			semg_temp[6] = (vol[state][state_dat] - semg_temp[5] * 100) / 10;
			semg_temp[7] = vol[state][state_dat] % 10;
			switch(state_dat)
			{
				case s0:
				{
					if(keyvalue == 4)
					{
						state_dat = s1;
					}
					else if(keyvalue == 5)
					{
						state = s1;
						state_para = s0;
					}
					else	;
				}	break;
				
				case s1:
				{
					if(keyvalue == 4)
					{
						state_dat = s0;
					}
					else if(keyvalue == 5)
					{
						state = s1;
						state_para = s1;
					}
					else	;
				}	break;
				
				default:	state_dat = s0;	break;
			}
		}	break;
		
		case s1:
		{
			semg_temp[0] = 11;
			semg_temp[1] = state_para * 2 + 1;
			semg_temp[2] = 12;
			semg_temp[3] = 12;
			semg_temp[4] = 12;
			switch(state_para)
			{
				case s0:
				{
					semg_temp[5] = temp_vp1 / 100;
					semg_temp[6] = (temp_vp1 - semg_temp[5] * 100) / 10;
					semg_temp[7] = temp_vp1 % 10;
					if(keyvalue == 4)
					{
						state_para = s1;
					}
					else if(keyvalue == 5)
					{
						vol[state][state_para] = temp_vp1;
						state = s0;
						state_dat = s0;
					}
					else if(keyvalue == 6)
					{
						temp_vp1 = temp_vp1 + 20;
						if(temp_vp1 > 500)
						{
							temp_vp1 = 0;
						}
					}
					else if(keyvalue == 7)
					{
						temp_vp1 = temp_vp1 - 20;
						if(temp_vp1 < 0)
						{
							temp_vp1 = 500;
						}
					}
					else	;
				}	break;
				
				case s1:
				{
					semg_temp[5] = temp_vp3 / 100;
					semg_temp[6] = (temp_vp3 - semg_temp[5] * 100) / 10;
					semg_temp[7] = temp_vp3 % 10;
					if(keyvalue == 4)
					{
						state_para = s0;
					}
					else if(keyvalue == 5)
					{
						vol[state][state_para] = temp_vp3;						
						state = s0;
						state_dat = s1;
					}
					else if(keyvalue == 6)
					{
						temp_vp3 = temp_vp3 + 20;
						if(temp_vp3 > 500)
						{
							temp_vp3 = 0;
						}
					}
					else if(keyvalue == 7)
					{
						temp_vp3 = temp_vp3 - 20;
						if(temp_vp3 < 0)
						{
							temp_vp3 = 500;
						}
					}
					else	;
				}	break;
				
				default:	state_para = s0;	break;
			}
		}	break;
		
		default:	state = s0;	break;
	}
}


void main()
{
	char keyvalue = 0;
	
	allinit();
	Timer0Init();
	
	while(1)
	{
		keyvalue = keyscan();
		menu(keyvalue);
		Delay5ms();
	}
}

iic.c

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

#include "reg52.h"
#include "intrins.h"

#define uchar unsigned char

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

//等待应答
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;    
}

int read_vol(uchar add)
{
	int dat = 0;
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();	
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_Stop();
	
	return dat * 1.96078;
}

客观题(答案不保证正确率)

12届蓝桥杯单片机模拟题(包括客观题)_第2张图片

你可能感兴趣的:(蓝桥杯单片机,单片机,嵌入式)