当前位置:网站首页>蓝桥杯省一之路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
边栏推荐
- Solution of garbled code on idea console
- logback的配置文件加载顺序
- 英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
- 计组 | 【七 输入/输出系统】知识点与例题
- Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
- 昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
- DDT + Excel for interface test
- Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
- Change the icon size of PLSQL toolbar
- Dlib of face recognition framework
猜你喜欢

G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration

Summary according to classification in sail software

Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme

漫画:什么是IaaS、PaaS、SaaS?

Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand

The first line and the last two lines are frozen when paging

Force buckle - 198 raid homes and plunder houses

Disk management and file system

Selenium IDE and XPath installation of chrome plug-in

How to upgrade openstack across versions
随机推荐
File system read and write performance test practice
关于局域网如何组建介绍
伪分布安装spark
Day 10 abnormal mechanism
Modify the test case name generated by DDT
Redis "8" implements distributed current limiting and delay queues
Day 9 static abstract class interface
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
MySql主从复制
Public variables of robotframework
Loading order of logback configuration file
Pytorch: the pit between train mode and eval mode
Postman batch production body information (realize batch modification of data)
RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
New project of OMNeT learning
Use case execution of robot framework
各大框架都在使用的Unsafe类,到底有多神奇?
Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
Nacos 详解,有点东西