当前位置:网站首页>Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
2022-07-03 07:31:00 【start field】
Don't say anything else , Let's take a look at the ninth (2018) Your topic .
subject


This topic still uses three modules ( Nixie tube 、LED、 Key ), Also used. EEPROM And analog inputs (ADC), They are also some common peripherals , As long as you have practiced , It's not difficult , The most important thing is programming logic .
1 The nixie tube shows
The nixie tube has two interfaces that need to be displayed. One is the interface of setting state , There is also a brightness level interface . The setting status interface consists of operation mode and flow interval .
2 LED Show
This time LED It's the point , There are not only four operating modes, but also four brightness levels . Set the array in four operation modes , And then cycle through . The four brightness levels are ADC 0 To 255 Divide into four , At which level ,LED Just how long it will be on , In this way, there will be a visual brightness level , See code for details .
3 Key module
Using separate keys ,s7 yes LED Start and stop of ,s6 Define a variable mode1, by 0 When the nixie tube is turned off ( Press s4 Display brightness level ), by 1 Display the operation mode 1 And circulation interval , by 2 Runtime mode 1 Flash press s5 or s4 Add and subtract , by 3 The time flow interval flashes and press s5 or s4 Add and subtract .
4 EEPROM
Is to rewrite the underlying driver code (IIC), Save the flow interval after adding and subtracting , In time delay 5ms.
5 ADC
Is to rewrite the underlying driver code (IIC).
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);
#endifinit.h
#include"init.h"
#include"jm.h"
#define u8 unsigned char
#define u16 unsigned int
#define state_0 0
#define state_1 1
#define state_2 2
static u8 segadder=0,key_state;
extern u8 mode1,a;
u8 seg[]={11,11,11,11,11,11,11,11};
u8 tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
u8 key_press,key_num,value;
void close_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<<segadder;P2&=0x1f;
P2=(P2&0x1f)|0xe0;P0=tab[seg[segadder]];P2&=0x1f;
if(++segadder==8) segadder=0;
}
u8 read_key() // Independent buttons
{
key_press=P3&0x0f;
switch(key_state)
{
case state_0:
if(key_press!=0x0f)
key_state=state_1;
break;
case state_1:
if(key_press!=0x0f)
{
if((key_press&0x08)==0)
{
if(mode1==0&&a==1)
{
jm3();
a=0;
}
key_num=4;
}
if((key_press&0x04)==0) key_num=5;
if((key_press&0x02)==0) key_num=6;
if((key_press&0x01)==0) key_num=7;
key_state=state_2;
}
else
key_state=state_0;
break;
case state_2:
if(key_press==0x0f)
{
if(mode1==0&&a==0)a=1;
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 close_init();
void display();
unsigned char read_key();
void Timer0Init(void);
#endifjm.h
#include"jm.h"
#include"IIC.h"
#define u8 unsigned char
#define u16 unsigned int
extern bit sp,mode_flag,val_flag; // extern Indicates that this variable is defined elsewhere , To quote... Here
extern u8 seg[],mode1,RB2_value;
extern u8 val1,val2,val3,val4,val_count;
u8 mode12[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; // Pattern 1,2 Array of
u8 mode34[]={0x7e,0xbd,0xdb,0xe7}; // Pattern 3,4 Array of
u8 mode=1,i=0,j=7,x=0,y=3,mode2=1;
void Delay5ms()
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void jm3() // Brightness level display function
{
seg[0]=11;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=11;
seg[6]=10;
seg[7]=RB2_value;
}
void jm4() //s4 The function of
{
if(mode1==2)
{
if(--mode==0)mode=4;
}
if(mode1==3)
{
if(mode==1)val1-=10;
if(val1<=40)val1=40;
write_eeprom(0x10,val1);
Delay5ms();
if(mode==2)val2-=10;
if(val2<=40)val2=40;
write_eeprom(0x20,val2);
Delay5ms();
if(mode==3)val3-=10;
if(val3<=40)val3=40;
write_eeprom(0x40,val3);
Delay5ms();
if(mode==4)val4-=10;
if(val4<=40)val4=40;
write_eeprom(0x80,val4);
Delay5ms();
}
}
void jm5() //s5 The function of
{
if(mode1==2)
{
if(++mode==5)mode=1;
}
if(mode1==3)
{
if(mode==1)val1+=10;
if(val1>=120)val1=120;
write_eeprom(0x10,val1);
Delay5ms();
if(mode==2)val2+=10;
if(val2>=120)val2=120;
write_eeprom(0x20,val2);
Delay5ms();
if(mode==3)val3+=10;
if(val3>=12)val3=120;
write_eeprom(0x40,val3);
Delay5ms();
if(mode==4)val4+=10;
if(val4>=120)val4=120;
write_eeprom(0x80,val4);
Delay5ms();
}
}
void jm6()
{
if(mode1==0) // Nixie tube off
{
seg[0]=11;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=11;
seg[6]=11;
seg[7]=11;
}
if(mode1==1) // Display the operation mode 1 And circulation interval
{
seg[0]=10;
seg[1]=1;
seg[2]=10;
seg[3]=11;
seg[4]=val1/100;
seg[5]=val1/10%10;
seg[6]=val1%10;
seg[7]=0;
}
if(mode1==2) // Operation mode 1 flashing
{
if(mode_flag==1)
{
seg[0]=10;
seg[1]=mode;
seg[2]=10;
seg[3]=11;
if(mode==1)
{
seg[4]=val1/100;
seg[5]=val1/10%10;
seg[6]=val1%10;
seg[7]=0;
}
if(mode==2)
{
seg[4]=val2/100;
seg[5]=val2/10%10;
seg[6]=val2%10;
seg[7]=0;
}
if(mode==3)
{
seg[4]=val3/100;
seg[5]=val3/10%10;
seg[6]=val3%10;
seg[7]=0;
}
if(mode==4)
{
seg[4]=val4/100;
seg[5]=val4/10%10;
seg[6]=val4%10;
seg[7]=0;
}
}
else
{
seg[0]=11;
seg[1]=11;
seg[2]=11;
seg[3]=11;
}
}
if(mode1==3) // The flow interval flashes
{
if(mode_flag==1)
{
if(mode==1)
{
seg[4]=val1/100;
seg[5]=val1/10%10;
seg[6]=val1%10;
seg[7]=0;
}
if(mode==2)
{
seg[4]=val2/100;
seg[5]=val2/10%10;
seg[6]=val2%10;
seg[7]=0;
}
if(mode==3)
{
seg[4]=val3/100;
seg[5]=val3/10%10;
seg[6]=val3%10;
seg[7]=0;
}
if(mode==4)
{
seg[4]=val4/100;
seg[5]=val4/10%10;
seg[6]=val4%10;
seg[7]=0;
}
}
else
{
seg[4]=11;
seg[5]=11;
seg[6]=11;
seg[7]=11;
}
}
}
void jm7()
{
if(sp==1) // open LED
{
if(mode2==1&&val_flag==0)
{
P2=(P2&0x1f)|0x80;
P0=mode12[i];
P2&=0x1f;
}
if(mode2==1&&val_flag==1)
{
val_flag=0;
if(++i==8)
{
i=0;
mode2=2;
val_count=0;
}
}
if(mode2==2&&val_flag==0)
{
P2=(P2&0x1f)|0x80;
P0=mode12[j];
P2&=0x1f;
}
if(mode2==2&&val_flag==1)
{
val_flag=0;
if(--j==-1)
{
j=7;
mode2=3;
val_count=0;
}
}
if(mode2==3&&val_flag==0)
{
P2=(P2&0x1f)|0x80;
P0=mode34[x];
P2&=0x1f;
}
if(mode2==3&&val_flag==1)
{
val_flag=0;
if(++x==4)
{
x=0;
mode2=4;
val_count=0;
}
}
if(mode2==4&&val_flag==0)
{
P2=(P2&0x1f)|0x80;
P0=mode34[y];
P2&=0x1f;
}
if(mode2==4&&val_flag==1)
{
val_flag=0;
if(--y==-1)
{
y=3;
mode2=1;
val_count=0;
}
}
}
else
{
P2=(P2&0x1f)|0x80;P0=0xff;P2&=0x1f;
}
}jm.h
#ifndef _JM_H_
#define _JM_H_
#include<stc15f2k60s2.h>
void jm3();
void jm4();
void jm5();
void jm6();
void jm7();
void Delay5ms();
#endifmain.c
#include"init.h"
#include"IIC.h"
#include"jm.h"
#define u8 unsigned char
#define u16 unsigned int
extern u8 mode2;
u8 a,b,num,mode1=0,RB2=0,RB2_value=0,RB2_count=0,e_count=0;
bit sp=0,mode_flag=0,val_flag=1,e_flag=0;
u16 mode_count=0,val_count=0;
u8 val1=40,val2=40,val3=40,val4=40;
void main()
{
close_init();
Timer0Init();
while(1)
{
RB2=Read_AD();
num=read_key();
switch(num)
{
case 4:
jm4();
break;
case 5:
jm5();
break;
case 6:
if(++mode1==4)mode1=0;
a=1;
break;
case 7:
sp^=1;
b=1;
break;
}
num=0;
if(a==1)jm6();
if(b==1)jm7();
if(RB2>=0&&RB2<64)RB2_value=1; // hold ADC The value of is divided into four levels
if(RB2>=64&&RB2<128)RB2_value=2;
if(RB2>=128&&RB2<192)RB2_value=3;
if(RB2>=192&&RB2<=255)RB2_value=4;
if(e_flag==1) // Read EEPROM The value in
{
e_flag=0;
val1=Read_eeprom(0x10);
val2=Read_eeprom(0x20);
val3=Read_eeprom(0x40);
val4=Read_eeprom(0x80);
}
}
}
void Timer0() interrupt 1
{
display();
val_count++;
if(mode1==2||mode1==3)
{
if(++mode_count==800)
{
mode_count=0;
mode_flag^=1;
}
}
if((val_count==val1*10)&&mode2==1)
{
val_count=0;
val_flag=1;
}
if((val_count==val2*10)&&mode2==2)
{
val_count=0;
val_flag=1;
}
if((val_count==val3*10)&&mode2==3)
{
val_count=0;
val_flag=1;
}
if((val_count==val4*10)&&mode2==4)
{
val_count=0;
val_flag=1;
}
if(++RB2_count<=RB2_value) jm7(); // The higher the brightness level ,LED The longer it shines
else if((RB2_count>RB2_value)&&(RB2_count<=4))
{
P2=(P2&0x1f)|0x80;P0=0xff;P2&=0x1f;
}
else if(RB2_count>4)RB2_count=0;
if(++e_count==200)
{
e_count=0;
e_flag=1;
}
} Last , There's something wrong with it , I hope you will correct me , Or better opinions and ideas can be discussed .
边栏推荐
猜你喜欢

Lucene skip table

Epoll related references

How long is the fastest time you can develop data API? One minute is enough for me

Leetcode 198: 打家劫舍

不出网上线CS的各种姿势

Take you through the whole process and comprehensively understand the software accidents that belong to testing

HCIA notes

最全SQL与NoSQL优缺点对比

1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log

论文学习——鄱阳湖星子站水位时间序列相似度研究
随机推荐
树莓派更新工具链
"Moss ma not found" solution
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Win 2008 R2 crashed at the final installation stage
Hello world of vertx
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
JS monitors empty objects and empty references
Lombok -- simplify code
Image recognition and detection -- Notes
GStreamer ffmpeg avdec decoded data flow analysis
Le Seigneur des anneaux: l'anneau du pouvoir
Leetcode 198: 打家劫舍
Inverted chain disk storage in Lucene (pfordelta)
Jeecg menu path display problem
[most detailed] latest and complete redis interview book (50)
Vertx multi vertical shared data
Use of file class
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
Interview questions about producers and consumers (important)
【最詳細】最新最全Redis面試大全(50道)