《蓝桥杯真题》:2021单片机省赛第二场暨第十三届蓝桥杯赛前模拟试题
Posted 是七喜呀!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《蓝桥杯真题》:2021单片机省赛第二场暨第十三届蓝桥杯赛前模拟试题相关的知识,希望对你有一定的参考价值。
2021单片机省赛第二场
有关题目
实现代码
注意:
频率测量功能
:单片机 P34 引脚需与J3 排针上的 SIGNAL 引脚短接
限制条件方面
:①切换通道生效仅在电压界面。
②定时器T0作为计数器时,除了中断溢出标志TF0可以不用外,TR0也得正常开启
③长按键判断完之后,要清零。
④周期的单位us,不是秒!
数据准确度问题
:在C语言中,1000 / 33 和 1000.0 / 33不是同样的结果!这点我们在处理电压值和频率值时会用到
处理数据
:由于pcf8591读取电压读取的为上一次的值,读取光敏电阻和电位器两端电压时可能会读取不正确,为此解决方法有两个:①可以读两次,就如我们在下面main.c中所写rd_pcf8591
②可以将读取的光敏电阻和电位器两端电压值进行交换即可!
底层代码
:rd_pcf8591()函数读取最后需调用iic.c中IIC_SendAck()发送非应答信号,即SDA发送一个高电平
main.c
#include "STC15F2K60S2.h"
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
uchar jm = 0;//界面初始化频率界面
code uchar tab[] = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x8e,0xc8,0xc1,0xbf;
//f、下U,U, - 11 12 13 14
uint cnt_freq;//计数1s多少个脉冲
uint curr_freq;//实时频率值
uint curr_light, curr_ain3;//分别对应通道1和3的值
uint vol, freq;//缓存电压和频率值
uchar cnt_press;//计算s7按压下去多少秒,超过20,则为长按键
bit flag_channel;//0为初始话进入通道1,1就进入通道3
bit press_s7_flag;//s7按下标志位, 1为s7按下
bit led_flag = 1;//0为熄灭,1为打开
void sys_init();
uchar rd_pcf8591(uchar addr);
void key_handle();
void dsp_smg_bit(uchar pos, val);
void display();//显示功能,分三个大块
void dsp_freq();
void dsp_period();
void dsp_vol();
void delay_k(uchar t);//延时t * 10us
void Delay1ms(); //1ms@12.000MHz,延时1ms用于给足数码管足够显示时间
void led();
void main()
sys_init();
while(1)
curr_light = (uint)(rd_pcf8591(0x01) * 100.0 / 51);
curr_ain3 = (uint)(rd_pcf8591(0x03) * 100.0 / 51);
key_handle();
display();
led();
void led()
if (led_flag)//led此时正常点亮
if (curr_ain3 > vol)
P2 = (P2 & 0x1f) | 0x80;
L1 = 0;
if (curr_freq > freq)
P2 = (P2 & 0x1f) | 0x80;
L2 = 0;
if (0 == jm)
P2 = (P2 & 0x1f) | 0x80;
L3 = 0;
else if (1 == jm)
P2 = (P2 & 0x1f) | 0x80;
L4 = 0;
else if (2 == jm)
P2 = (P2 & 0x1f) | 0x80;
L5 = 0;
else
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
void key_handle()
if(!S4)//切换模式
delay_k(20);
if (!S4)
while(!S4)
display();
if (++jm >= 3)
jm = 0;
if (1 == jm)
flag_channel = 0;//从周期界面进入电压界面后, 均为通道 1 电压显示界面
if(!S5)//切换通道
delay_k(20);
if (!S5)
while(!S5)
display();
if (2 == jm)//注意限制条件
flag_channel = !flag_channel;
if(!S6)//保存当前采集到的通道 3 电压数据
delay_k(20);
if (!S6)
while(!S6)
display();
vol = curr_ain3;
if(!S7)
delay_k(20);
if (!S7)
press_s7_flag = 1;
while(!S7)
display();
press_s7_flag = 0;
if (cnt_press > 20)
led_flag = !led_flag;
else
freq = curr_freq;
cnt_press = 0;//注意清零
void display()
if (0 == jm)
dsp_freq();
else if (1 == jm)
dsp_period();
else if (2 == jm)
dsp_vol();
void dsp_vol()
dsp_smg_bit(1, 13);//U
dsp_smg_bit(2, 14);//-
if(!flag_channel)
dsp_smg_bit(3, 1);//通道1
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (6 - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[curr_light / 100] & 0x7f;//注意添加小数点
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
dsp_smg_bit(7, curr_light / 10 % 10);
dsp_smg_bit(8, curr_light % 10);
else
dsp_smg_bit(3, 3);//通道3
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (6 - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[curr_ain3 / 100] & 0x7f;
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
dsp_smg_bit(7, curr_ain3 / 10 % 10);
dsp_smg_bit(8, curr_ain3 % 10);
void dsp_period()
float p = 1000000.0 / curr_freq;
uint x = (uint)p;
dsp_smg_bit(1, 12);//n
if (x > 9999)
dsp_smg_bit(4, x / 10000);
if (x > 999)
dsp_smg_bit(5, x / 1000 % 10);
if (x > 99)
dsp_smg_bit(6, x / 100 % 10);
if (x > 9)
dsp_smg_bit(7, x / 10 % 10);
if (x >= 0)
dsp_smg_bit(8, x % 10);
void dsp_freq()
dsp_smg_bit(1, 11);//F
if (curr_freq > 9999)
dsp_smg_bit(4, curr_freq / 10000);
if (curr_freq > 999)
dsp_smg_bit(5, curr_freq / 1000 % 10);
if (curr_freq > 99)
dsp_smg_bit(6, curr_freq / 100 % 10);
if (curr_freq > 9)
dsp_smg_bit(7, curr_freq / 10 % 10);
if (curr_freq >= 0)
dsp_smg_bit(8, curr_freq % 10);
void dsp_smg_bit(uchar pos, val)
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (pos - 1);
P2 = (P2 & 0x1f) | 0xe0;
P0 = tab[val];
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
void delay_k(uchar t)
while(t--)
display();
void Delay1ms() //@12.000MHz
unsigned char i, j;
i = 12;
j = 169;
do
while (--j);
while (--i);
uchar rd_pcf8591(uchar addr)
uchar da;
uchar i;
for (i = 0; i < 2; i++)//由于读一次读取的是上一次数据的值,故我们读取两次,拿到现在的值
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
da = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return da;
void timer1() interrupt 3
static uchar i1 = 0;
if (++i1 == 20)
i1 = 0;
curr_freq = cnt_freq;
cnt_freq = 0;
if (press_s7_flag)
cnt_press++;
void timer0() interrupt 1
cnt_freq++;
void sys_init()
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0xaf;
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
TMOD = 0x04; //设置定时器模式,定时器0--计数器模式,定时器1--定时器模式
TL0 = 0xff;
TH0 = 0xff;//来一个脉冲就溢出计数一次
TR0 = 1;
//50ms
TL1 = 0xB0; //设置定时初值
TH1 = 0x3C; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET0 = 1;
ET1 = 1;
EA = 1;
iic.c
#include "iic.h"
#define DELAY_TIME 5
//I2C总线内部延时函数
void IIC_Delay(unsigned char i)
do_nop_();
while(i--);
//I2C总线启动信号
void IIC_Start(void)
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
//I2C总线停止信号
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;
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;
iic.h
#ifndef _IIC_H
#define _IIC_H
#include "reg52.h"
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
《蓝桥杯真题》:2022单片机省赛第二场(第十三 / 13届第二场)
《蓝桥杯真题》:2022单片机省赛第二场(第十三 / 13届第二场)
《蓝桥杯真题》:2022单片机省赛第二场(第十三 / 13届第二场)
《蓝桥杯真题》:2022单片机省赛第二场_客观题(第十三 / 13届第二场)