当前位置:网站首页>蓝桥杯省一之路06——第十二届省赛真题第二场
蓝桥杯省一之路06——第十二届省赛真题第二场
2022-04-23 16:41:00 【c语言的神】
一,代码部分
1,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;
}
//从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;
}
2,smgshow.c
#include "reg52.h"
void Delay(int t)
{
while(t--);
}
void HC573(unsigned char channel, unsigned char dat)
{
P0=dat;
switch(channel)
{
case 4:
P2=(P2 & 0x1f) | 0x80;//选通Y4C,LED
break;
case 5:
P2=(P2 & 0x1f) | 0xa0;//选通Y5C,蜂鸣器,继电器
break;
case 6:
P2=(P2 & 0x1f) | 0xc0;//选通Y6C,数码管位选
break;
case 7:
P2=(P2 & 0x1f) | 0xe0;//选通Y7C,数码管段码
break;
}
P2=(P2 & 0x1f) | 0x00;
}
void Display_SMG(unsigned char pos,unsigned char val)
{
HC573(6,0x01<<pos);
HC573(7,val);
Delay(500);
// HC573(6,0x01<<pos);
HC573(7,0xff);//消隐
}
void Display_ALL(unsigned char dat)
{
HC573(6,0xff);
HC573(7,dat);
}
3,main.c
#include "reg52.h"
#include "smgshow.h"
#include "iic.h"
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
unsigned char sta_smg=0;
unsigned char u_sta=0;
unsigned char switch_LED=0;
void Init_Timer()
{
TR1=1;
TR0=1;
TMOD=0x16;
TH0=0xff;
TL0=0xff;
TH1=(0-10000)/256;
TL1=(0-10000)%256;
EA=1;
ET0=1;
ET1=1;
}
unsigned int t;
void Service_T0() interrupt 1
{
t=t+1;
}
unsigned int count=0;
unsigned int count_key=0;
unsigned F_key=0;
unsigned int count_t=0;
unsigned int count_f=0;
unsigned int t_mem=65535;
float f;
void Service_T1() interrupt 3
{
TH1=(0-10000)/256;
TL1=(0-10000)%256;
count+=1;
if(count==100)
{
count=0;
count_t=t;
f=1.0/t;
count_f=f*1000000;
f=0;
t=0;
}
if(F_key==1)
{
count_key+=1;
}
}
float u_guanming=0;
unsigned int u_guanming_smg;
void Read_guanming()
{
unsigned char temp1;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp1=IIC_RecByte();//0~255
IIC_SendAck(1);
IIC_Stop();
u_guanming=temp1*5/255.0;
u_guanming_smg=u_guanming*100;
}
float u_dianweiqi=0;
unsigned int u_dianweiqi_smg;
float u_mem=99;
void Read_dianweiqi()
{
unsigned char temp2;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp2=IIC_RecByte();//0~255
IIC_SendAck(1);
IIC_Stop();
u_dianweiqi=temp2*5/255.0;
u_dianweiqi_smg=u_dianweiqi*100;
}
void SMGWork()
{
switch(sta_smg)
{
case 0:
Display_SMG(0,0x8e);
if(count_t>999999)
{
Display_SMG(1,SMGnotdotduanma[count_t/1000000%10]);
}
if(count_t>99999)
{
Display_SMG(2,SMGnotdotduanma[count_t/100000%10]);
}
if(count_t>9999)
{
Display_SMG(3,SMGnotdotduanma[count_t/10000%10]);
}
if(count_t>999)
{
Display_SMG(4,SMGnotdotduanma[count_t/1000%10]);
}
if(count_t>99)
{
Display_SMG(5,SMGnotdotduanma[count_t/100%10]);
}
if(count_t>9)
{
Display_SMG(6,SMGnotdotduanma[count_t/10%10]);
}
Display_SMG(7,SMGnotdotduanma[count_t%10]);
break;
case 1:
Display_SMG(0,0xc8);
if(count_f>999999)
{
Display_SMG(1,SMGnotdotduanma[count_f/1000000]);
}
if(count_f>99999)
{
Display_SMG(2,SMGnotdotduanma[count_f/100000%10]);
}
if(count_f>9999)
{
Display_SMG(3,SMGnotdotduanma[count_f/10000%10]);
}
if(count_f>999)
{
Display_SMG(4,SMGnotdotduanma[count_f/1000%10]);
}
if(count_f>99)
{
Display_SMG(5,SMGnotdotduanma[count_f/100%10]);
}
if(count_f>9)
{
Display_SMG(6,SMGnotdotduanma[count_f/10%10]);
}
Display_SMG(7,SMGnotdotduanma[count_f%10]);
break;
case 2:
Display_SMG(0,0xc1);
Display_SMG(1,~0x40);
if(u_sta==0)
{
Display_SMG(2,SMGnotdotduanma[1]);
Display_SMG(5,SMGdotduanma[u_guanming_smg/100%10]);
Display_SMG(6,SMGnotdotduanma[u_guanming_smg/10%10]);
Display_SMG(7,SMGnotdotduanma[u_guanming_smg%10]);
}
else
{
Display_SMG(2,SMGnotdotduanma[3]);
Display_SMG(5,SMGdotduanma[u_dianweiqi_smg/100%10]);
Display_SMG(6,SMGnotdotduanma[u_dianweiqi_smg/10%10]);
Display_SMG(7,SMGnotdotduanma[u_dianweiqi_smg%10]);
}
break;
}
}
void Scan_key()
{
if(S4==0)
{
Delay(500);
if(S4==0)
{
if(sta_smg==0)
{
sta_smg=1;
}
else if(sta_smg==1)
{
sta_smg=2;
u_sta=0;
}
else
{
sta_smg=0;
}
while(S4==0)
{
SMGWork();
}
}
}
if(S5==0)
{
Delay(500);
if(S5==0)
{
if(sta_smg==2 && u_sta==0)
{
u_sta=1;
}
else if(sta_smg==2 && u_sta==1)
{
u_sta=0;
}
while(S5==0)
{
SMGWork();
}
}
}
if(S6==0)
{
Delay(500);
if(S6==0)
{
Read_dianweiqi();
u_mem=u_dianweiqi;
}
while(S6==0)
{
SMGWork();
}
}
if(S7==0)
{
Delay(500);
if(S7==0)
{
F_key=1;
count_key=0;
while(S7==0)
{
SMGWork();
}
F_key=0;
if(count_key>100)
{
switch_LED =! switch_LED;
}
else
{
t_mem=count_t;
}
}
}
}
unsigned char sta_LED=0xff;
void LED()
{
if(switch_LED==0)
{
if(sta_smg != 2)
Read_dianweiqi();
if(u_dianweiqi>u_mem)
{
sta_LED &= ~0x01;
HC573(4,sta_LED);
}
else
{
sta_LED |= 0x01;
HC573(4,sta_LED);
}
if(count_t>t_mem)
{
sta_LED &= ~0x02;
HC573(4,sta_LED);
}
else
{
sta_LED |= 0x02;
HC573(4,sta_LED);
}
if(sta_smg==0)
{
sta_LED &= ~0x04;
HC573(4,sta_LED);
}
else
{
sta_LED |= 0x04;
HC573(4,sta_LED);
}
if(sta_smg==1)
{
sta_LED &= ~0x08;
HC573(4,sta_LED);
}
else
{
sta_LED |= 0x08;
HC573(4,sta_LED);
}
if(sta_smg==2)
{
sta_LED &= ~0x10;
HC573(4,sta_LED);
}
else
{
sta_LED |= 0x10;
HC573(4,sta_LED);
}
}
else
{
sta_LED=0xff;
HC573(4,sta_LED);
}
}
void Init_system()
{
HC573(4,0xff);
HC573(5,0x00);
Display_ALL(0xff);
}
void main()
{
Init_system();
Init_Timer();
while(1)
{
if(u_sta==0)
{
Read_guanming();
Delay(100);
}
else
{
Read_dianweiqi();
Delay(100);
}
SMGWork();
Scan_key();
LED();
}
}
第十二届第二场除了通道三实时电压大于缓存电压时L1亮起,只能在电压采集U-3界面实现(题目并没有说的很清楚,个人理解时在全界面有效)。这个问题的出现的原因是,我把读通道一和通道三的2个函数同时放在循环中读取,我发现读取的数值会混乱,本该显示通道一的电压结果显示出通道三,读取通道三会显示出通道一,有大佬路过可以帮忙答疑下。主要觉得自己把电压读取跟显示函数捆绑在一起了,只有在显示该通道时才读取该通道的电压,这几天尝试过很多新写法,不太理想,后续在进行优化。
在这道题目还学习到了如何判断按键的长按与短按,思路为在按下开关时,标志位置1(记住要让计数为清0,便于下一次计数),要设置while()循环,判断开关按下,便于定时器开始计时,每10ms计数位+1,判断计数位的时间来决定执行的内容。
二,总结
看了前面代码的朋友,应该会发现引用的头文件少了,因为最近写代码发现模块化太多的时候要定义很多的变量去接收,修改这些数值,太过繁琐,而且自身的编程水平太低无法做到对应的模块化有很强的移植性。
这几天才完成了这道题,刷题最重要的是能在一道题目学习到什么,而不是一味的一天一道,能学习到新知识才是最重要的,共勉。
版权声明
本文为[c语言的神]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_58931449/article/details/124346337
边栏推荐
- Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
- RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
- G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
- Knowledge points and examples of [seven input / output systems]
- 阿里研发三面,面试官一套组合拳让我当场懵逼
- MySQL的btree索引和hash索引区别
- Creation of RAID disk array and RAID5
- 深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
- How magical is the unsafe class used by all major frameworks?
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
猜你喜欢
文件操作详解(2)
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
ByteVCharts可视化图表库,你想要的我都有
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
欣旺达:HEV和BEV超快充拳头产品大规模出货
Real time operation of vim editor
Force buckle-746 Climb stairs with minimum cost
Selenium IDE and XPath installation of chrome plug-in
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
Installation and management procedures
随机推荐
The font of the soft cell changes color
LVM and disk quota
Construction of promtail + Loki + grafana log monitoring system
Postman batch production body information (realize batch modification of data)
How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
VIM uses vundle to install the code completion plug-in (youcompleteme)
深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
04 Lua operator
JMeter installation tutorial and solutions to the problems I encountered
UWA Pipeline 功能详解|可视化配置自动测试
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
loggie 源码分析 source file 模块主干分析
Encapsulating the logging module
05 Lua control structure
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
Server log analysis tool (identify, extract, merge, and count exception information)
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out