当前位置:网站首页>Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
2022-07-03 07:35:00 【start field】
Recently, I have just finished writing the title of the 11th session , Let's see 2020 The title of the provincial competition in .
subject
The topic is still LED、 Nixie tube 、 Key three modules , And analog voltage input (ADC)、AT24C02(EEPROM), Are common peripherals , And the question appeared before , So it's not hard , All the tests are basic skills .
1 The nixie tube shows
The nixie tube mainly displays the obtained voltage 、 There are three interfaces for counting and parameter setting , However, pay attention to the voltage data interface after power on .
2 LED
When the voltage is less than the set parameter 5s after L1 bright , This is still very easy to do . When the count is odd L2 bright , If you count , I first judge whether the voltage is larger or smaller than the parameter, and then judge whether the voltage is larger or smaller than the parameter after a period of time , If the results of the two comparisons are different, there is an intersection between the voltage and the parameters . Define a variable count, Every time the wrong key is pressed count++, When count>=3 when L3 bright , The right key makes count=0.
3 Key module
This time I use a matrix keyboard, which is different from the previous one .s12 It's the voltage 、 Switch between the three interfaces of counting and parameter setting ,s13 It is a key that can reset the count ,s16,s17 It's an addition and subtraction button, but please note that the range is [0,5].
4 ADC
Is to rewrite the underlying driver code (IIC).
5 EEPROM
Is to rewrite the underlying driver code (IIC), Pay attention to adding and subtracting parameters *10 Save to after EEPROM in , So after adding and subtracting, you can directly /10.0 such *10 It is also the original value , In addition, the specified storage address is 0.
IIC.c
#include"IIC.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);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
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;
}
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;
}
unsigned char Read_AD()
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void write_eeprom(unsigned char add,unsigned char da)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(da);
IIC_WaitAck();
IIC_Stop();
}
unsigned char Read_eeprom(unsigned char add)
{
unsigned char da;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
da=IIC_RecByte();
IIC_Stop();
return da;
}
IIC.h
#ifndef _IIC_H_
#define _IIC_H_
#include<stc15f2k60s2.h>
#include<intrins.h>
void IIC_Delay(unsigned char i);
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char Read_AD();
void write_eeprom(unsigned char add,unsigned char da);
unsigned char Read_eeprom(unsigned char add);
#endif
init.c
#include"init.h"
#define u8 unsigned char
#define u16 unsigned int
#define state_0 0
#define state_1 1
#define state_2 2
#define key_io P3
extern u8 mode;
static u8 segaddr=0,key_press,key_state,key_num=0,row;
u8 value=0;
u8 code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc1,0x8c,0xc8};
u8 seg[]={11,11,11,11,11,11,11,11};
void all_init() // Turn off irrelevant peripherals
{
P2=(P2&0x1f)|0x80;P0=0xff;P2&=0x1f;
P2=(P2&0x1f)|0xa0;P04=0;P06=0;P2&=0x1f;
P2=(P2&0x1f)|0xc0;P0=0x00;P2&=0x1f;
P2=(P2&0x1f)|0xe0;P0=0xff;P2&=0x1f;
}
void display() // Nixie tube display function
{
P2=(P2&0x1f)|0xe0;P0=0xff;P2&=0x1f;
P2=(P2&0x1f)|0xc0;P0=1<<segaddr;P2&=0x1f;
if((mode==1||mode==2)&&segaddr==5) // Judge whether to use decimal point
{
P2=(P2&0x1f)|0xe0;P0=tab[seg[segaddr]]&0x7f;P2&=0x1f;
}
else
{
P2=(P2&0x1f)|0xe0;P0=tab[seg[segaddr]];P2&=0x1f;
}
if(++segaddr==8)segaddr=0;
}
u8 Read_key() // Matrix keyboard ( State machine )
{
switch(key_state)
{
case state_0:
key_io=0x0f; P42=0; P44=0;
key_press =key_io;
if(key_press != 0x0f)
key_state = state_1;
break;
case state_1:
key_press =key_io;
if(key_press != 0x0f)
{
if((key_io&0x08)==0) row=4;
if((key_io&0x04)==0) row=5;
if((key_io&0x02)==0) row=6;
if((key_io&0x01)==0) row=7;
key_io=0xf0; P42=1;P44=1;
if(P44==0) key_num=row;
if(P42==0) key_num=row+4;
if((key_io&0x20)==0) key_num=row+8;
if((key_io&0x10)==0) key_num=row+12;
key_state = state_2;
}
else
key_state = state_0;
break;
case state_2:
key_io=0x0f; P42=0; P44=0;
key_press =key_io;
if(key_press == 0x0f) key_state = state_0;
break;
}
value=key_num;
key_num=0;
return value;
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
EA = 1;
ET0 = 1;
}
init.h
#ifndef _INIT_H_
#define _INIT_H_
#include<stc15f2k60s2.h>
void all_init();
void display();
unsigned char Read_key();
void Timer0Init(void);
#endif
jm.c
#include"jm.h"
#include"IIC.h"
#define u8 unsigned char
#define u16 unsigned int
extern u8 mode,seg[],N,Y; // extern Indicates that this variable is defined elsewhere , To quote... Here
extern u16 RB2;
extern float V;
void Delay5ms()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void jm12()
{
if(mode==1) // Voltage display interface
{
seg[0]=12;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=RB2/100;
seg[6]=RB2/10%10;
seg[7]=RB2%10;
}
if(mode==2) // Parameter display interface
{
seg[0]=13;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=Y/10;
seg[6]=Y%10;
seg[7]=0;
}
if(mode==3) // Counting display interface
{
seg[0]=14;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=11;
seg[6]=N/10;
seg[7]=N%10;
}
}
void jm13() // Count reset interface
{
if(mode==3)
N=0;
}
void jm16()
{
if(mode==2)
{
Y+=5;
if(Y>=50)Y=0;
V=Y/10.0;
}
write_eeprom(0x00,V*10);
Delay5ms();
}
void jm17()
{
if(mode==2)
{
Y-=5;
if(Y<=0)Y=50;
V=Y/10.0;
}
write_eeprom(0x00,V*10);
Delay5ms();
}
jm.h
#ifndef _JM_H_
#define _JM_H_
#include<stc15f2k60s2.h>
void jm12();
void jm13();
void jm16();
void jm17();
void Delay5ms();
#endif
main.c
#include"init.h"
#include"IIC.h"
#include"jm.h"
#define u8 unsigned char
#define u16 unsigned int
u8 num=0,mode=1,N=0,Y=30,count=0,e_count=0,high=0,low=0;
u16 RB2_count=0,RB2=0,L1_count=0,cp_count=0;
bit RB2_flag=0,L1_flag=0,e_flag=0,cp_flag=0,L1=0;
extern u8 seg[];
float V;
void LED();
void main()
{
all_init();
Timer0Init();
while(1)
{
num=Read_key();
switch(num)
{
case 12:
if(++mode==4)mode=1;
break;
case 13:
if(mode==3) // Determine whether the key is correct
{
jm13();
count=0;
}
else count++;
break;
case 16:
if(mode==2) // Determine whether the key is correct
{
jm16();
count=0;
}
else count++;
break;
case 17:
if(mode==2) // Determine whether the key is correct
{
jm17();
count=0;
}
else count++;
break;
}
jm12();
LED();
if(Y*10>=RB2) // Compare the voltage and parameters for the first time
high=1;
else if(Y*10<=RB2)
low=1;
if(cp_flag==1) //500ms Judge the voltage and parameters for the second time
{
cp_flag=0;
if(high+low==2)
{
N++;
high=0,low=0;
}
else
high=0,low=0;
}
if(RB2_flag==1)
{
RB2_flag=0;
RB2=Read_AD();
RB2=(RB2*100)/51.0+0.5;
}
if(RB2<Y*10)L1_flag=1; // Judge L1 Whether it is on
else
{
L1_flag=0;
L1=0;
}
if(e_flag==1)
{
e_flag=0;
V=Read_eeprom(0x00);
Y=V;
}
}
}
void LED()
{
u8 i=0xff;
if(L1==1)
{
P2=(P2&0x1f)|0x80;P0=i&0xfe;P2&=0x1f;
}
else
{
P2=(P2&0x1f)|0x80;P0=i|0x01;P2&=0x1f;
}
if(N%2!=0)
{
P2=(P2&0x1f)|0x80;P0=i&0xfd;P2&=0x1f;
}
else
{
P2=(P2&0x1f)|0x80;P0=i|0x02;P2&=0x1f;
}
if(count>=3)
{
P2=(P2&0x1f)|0x80;P0=i&0xfb;P2&=0x1f;
}
else
{
P2=(P2&0x1f)|0x80;P0=i|0x04;P2&=0x1f;
}
}
void Timer0() interrupt 1
{
display();
if(++RB2_count==100)
{
RB2_count=0;
RB2_flag=1;
}
if(L1_flag==1)
{
L1_count++;
if(L1_count==5000)
{
L1_count=0;
L1=1;
}
}
if(++e_count==200)
{
e_count=0;
e_flag=1;
}
if(high==1||low==1)
{
if(++cp_count==500)
{
cp_count=0;
cp_flag=1;
}
}
}
边栏推荐
- 技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
- Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
- Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
- Lucene introduces NFA
- Some basic operations of reflection
- Read config configuration file of vertx
- Download address collection of various versions of devaexpress
- Beginners use Minio
- Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
- 技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
猜你喜欢
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
Leetcode 213: 打家劫舍 II
技术干货|AI框架动静态图统一的思考
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Comparison of advantages and disadvantages between most complete SQL and NoSQL
技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
Introduction of transformation flow
Leetcode 198: 打家劫舍
带你全流程,全方位的了解属于测试的软件事故
最全SQL与NoSQL优缺点对比
随机推荐
Common architectures of IO streams
docket
Image recognition and detection -- Notes
IO stream system and FileReader, filewriter
带你全流程,全方位的了解属于测试的软件事故
Jeecg data button permission settings
Spa single page application
Custom generic structure
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
Es writing fragment process
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
Rabbit MQ message sending of vertx
The difference between typescript let and VaR
The babbage industrial policy forum
Leetcode 198: house raiding
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
JUnit unit test of vertx
Chapter VI - Containers
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq