当前位置:网站首页>Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
2022-07-03 07:30:00 【start field】
Today, I finally have time to write the topic analysis of the eighth provincial competition , The eighth (2017) It's not difficult on the whole , Let's have a look .
subject



This topic is the same, using three modules ( Key 、LED、 Nixie tube ), It also uses a real-time clock (DS1302) and DS18B20. among DS18B20 I also passed the last exam . No single module is difficult , The topic is also easy to understand , The most important thing in writing a topic is whether your thinking and logic are correct .
1 The nixie tube shows
There are only two modes of nixie tube display , One is clock display , The other is alarm clock display , Press the key s7,s6 To control .
2 LED Show
When the value of the clock and the alarm clock are the same ,L1 With 0.2 Blink at intervals of seconds for 5 Seconds and then close , Or press any key to close during flashing .
3 Key module
The title uses a separate key ,s7 You can adjust the time of the clock 、 branch 、 second , You can define a variable mode1, When mode1 by 0 Show clock , by 1 The hour of the clock starts blinking at one second intervals , by 2 Minutes begin to flash at intervals of one second , by 3 Seconds starts blinking at one second intervals .s6 Define a variable mode2, Function and s7 equally .s5、s4 Is to add and subtract , But it is only useful when the hours, minutes and seconds of the clock or alarm clock flash . in addition s4 There is another function in the clock display interface ( That is to say mode1==0) Press to display the temperature , Release and return to the clock display interface .
4 DS18B20
Is to rewrite the underlying driver code (onewire), Then put it in the timer , Measure every once in a while .
5 DS1302
It is also the part of rewriting the underlying driver code (onewire), Then put it in the timer , Measure every once in a while .
6 Code
onewire.c
#include"onewire.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t)
{
t*=12;
while(t--);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int get_temp()
{
unsigned char high,low;
unsigned int result;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
result=(high<<8)|low;
result*=6.25;
return result;
}onewire.h
#ifndef _ONEWIRE_H_
#define _ONEWIRE_H_
#include<stc15f2k60s2.h>
#include"intrins.h"
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned int get_temp();
#endifds1302.h
It is recommended that the data read from the clock be placed in the array initialized by the clock , Otherwise, if you create an array to put the data of reading time , Every time you add 、 After subtracting the hours, minutes and seconds of the clock , The clock will start from initialization .
#include"ds1302.h"
unsigned char Time_Init[]={50,59,23,1,1,1,1};
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302((dat/10<<4)|(dat%10));
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
unsigned char dat1,dat2;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
dat1=temp/16;
dat2=temp%16;
temp=dat1*10+dat2;
return (temp);
}
void ds1302_Init(void)
{
unsigned char add=0x80,i=0;
Write_Ds1302_Byte( 0x8e,0x00 );
for(i=0;i<7;i++)
{
Write_Ds1302_Byte( add,Time_Init[i] );
add+=2;
}
Write_Ds1302_Byte( 0x8e,0x80 );
}
void ds1302_Read(void)
{
unsigned char add=0x81,i=0;
Write_Ds1302_Byte( 0x8e,0x80 );
for(i=0;i<7;i++)
{
Time_Init[i]=Read_Ds1302_Byte( add );
add+=2;
}
Write_Ds1302_Byte( 0x8e,0x80 );
}ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.h>
#include <intrins.h>
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void ds1302_Init(void);
void ds1302_Read(void);
#endif
init.c
#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
u8 tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
u8 seg[]={11,11,11,11,11,11,11,11};
static u8 segadder=0,key_state=0;
extern u8 mode1,mode2,c,d; // extern Indicates that this variable is defined elsewhere , To quote... Here
u8 key_num,num1,key_press;
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 Timer0Init(void) //1ms Timer for 0
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xCD;
TH0 = 0xD4;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void display() // Nixie tube display function
{
P2=(P2&0x1f)|0xe0;P0=0xff;P2&=0x1f; // Blanking
P2=(P2&0x1f)|0xc0;P0=1<<segadder;P2&=0x1f; // Biting
P2=(P2&0x1f)|0xe0;P0=tab[seg[segadder]];P2&=0x1f; // Segment selection
if(++segadder==8) segadder=0;
}
u8 read_key() // Independent keyboard
{
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&&c==0&&d==1) // Press to display the temperature on the clock display interface
{
jm3();
d=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&&c==0&&d==0)d=1; // Release and return to the clock display interface
key_state=state_0;
}
break;
}
num1=key_num;
key_num=0;
return num1;
}init.h
#ifndef _INIT_H_
#define _INIT_H_
#include<stc15f2k60s2.h>
#include"intrins.h"
void close_init();
void Timer0Init(void);
void display();
unsigned char read_key();
#endifjm.c
#include"jm.h"
#include"init.h"
#include"onewire.h"
#define u8 unsigned char
#define u16 unsigned int
extern u8 seg[],mode1,mode2,Time_Init[]; // extern Indicates that this variable is defined elsewhere , To quote... Here
extern u8 mode1_flag,temp_flag,mode2_flag;
u8 ac[]={0,0,0}; // The initial value of the alarm clock
u16 temp;
void jm3() // Show the temperature
{
if(temp_flag==1)
{
temp_flag=0;
temp=get_temp();
}
seg[0]=11;
seg[1]=11;
seg[2]=11;
seg[3]=11;
seg[4]=11;
seg[5]=temp/1000;
seg[6]=temp/100%10;
seg[7]=12;
}
void jm4()
{
if(mode1==1)
{
if(Time_Init[2]==0) Time_Init[2]=23;
else Time_Init[2]--;
}
if(mode1==2)
{
if(Time_Init[1]==0) Time_Init[1]=59;
else Time_Init[1]--;
}
if(mode1==3)
{
if(Time_Init[0]==0) Time_Init[0]=59;
else Time_Init[0]--;
}
if(mode2==1)
{
if(ac[2]==0) ac[2]=23;
else ac[2]--;
}
if(mode2==2)
{
if(ac[1]==0) ac[1]=59;
else ac[1]--;
}
if(mode2==3)
{
if(ac[0]==0) ac[0]=59;
else ac[0]--;
}
}
void jm5()
{
if(mode1==1)
{
if(Time_Init[2]==23) Time_Init[2]=0;
else Time_Init[2]++;
}
if(mode1==2)
{
if(Time_Init[1]==59) Time_Init[1]=0;
else Time_Init[1]++;
}
if(mode1==3)
{
if(Time_Init[0]==0) Time_Init[0]=0;
else Time_Init[0]++;
}
if(mode2==1)
{
if(ac[2]==23) ac[2]=0;
else ac[2]++;
}
if(mode2==2)
{
if(ac[1]==59) ac[1]=0;
else ac[1]++;
}
if(mode2==3)
{
if(ac[0]==59) ac[0]=0;
else ac[0]++;
}
}
void jm6()
{
if(mode2==0) // Alarm clock display interface
{
seg[0]=ac[2]/10;
seg[1]=ac[2]%10;
seg[2]=10;
seg[3]=ac[1]/10;
seg[4]=ac[1]%10;
seg[5]=10;
seg[6]=ac[0]/10;
seg[7]=ac[0]%10;
}
if(mode2==1) // Flashes at one second intervals
{
if(mode2_flag==1)
{
seg[0]=11;
seg[1]=11;
}
else
{
seg[0]=ac[2]/10;
seg[1]=ac[2]%10;
}
seg[2]=10;
seg[3]=ac[1]/10;
seg[4]=ac[1]%10;
seg[5]=10;
seg[6]=ac[0]/10;
seg[7]=ac[0]%10;
}
if(mode2==2) // Minutes flash at intervals of one second
{
if(mode2_flag==1)
{
seg[3]=11;
seg[4]=11;
}
else
{
seg[3]=ac[1]/10;
seg[4]=ac[1]%10;
}
seg[0]=ac[2]/10;
seg[1]=ac[2]%10;;
seg[2]=10;
seg[5]=10;
seg[6]=ac[0]/10;
seg[7]=ac[0]%10;
}
if(mode2==3) // Seconds flashes at one second intervals
{
if(mode2_flag==1)
{
seg[6]=11;
seg[7]=11;
}
else
{
seg[6]=ac[0]/10;
seg[7]=ac[0]%10;
}
seg[0]=ac[2]/10;
seg[1]=ac[2]%10;
seg[2]=10;
seg[3]=ac[1]/10;
seg[4]=ac[1]%10;
seg[5]=10;
}
}
void jm7()
{
if(mode1==0) // Clock display interface
{
seg[0]=Time_Init[2]/10;
seg[1]=Time_Init[2]%10;
seg[2]=10;
seg[3]=Time_Init[1]/10;
seg[4]=Time_Init[1]%10;
seg[5]=10;
seg[6]=Time_Init[0]/10;
seg[7]=Time_Init[0]%10;
}
if(mode1==1) // Flashes at one second intervals
{
if(mode1_flag==1)
{
seg[0]=11;
seg[1]=11;
}
else
{
seg[0]=Time_Init[2]/10;
seg[1]=Time_Init[2]%10;
}
seg[2]=10;
seg[3]=Time_Init[1]/10;
seg[4]=Time_Init[1]%10;
seg[5]=10;
seg[6]=Time_Init[0]/10;
seg[7]=Time_Init[0]%10;
}
if(mode1==2) // Minutes flash at intervals of one second
{
if(mode1_flag==1)
{
seg[3]=11;
seg[4]=11;
}
else
{
seg[3]=Time_Init[1]/10;
seg[4]=Time_Init[1]%10;
}
seg[0]=Time_Init[2]/10;
seg[1]=Time_Init[2]%10;
seg[2]=10;
seg[5]=10;
seg[6]=Time_Init[0]/10;
seg[7]=Time_Init[0]%10;
}
if(mode1==3) // Seconds flashes at one second intervals
{
if(mode1_flag==1)
{
seg[6]=11;
seg[7]=11;
}
else
{
seg[6]=Time_Init[0]/10;
seg[7]=Time_Init[0]%10;
}
seg[0]=Time_Init[2]/10;
seg[1]=Time_Init[2]%10;
seg[2]=10;
seg[3]=Time_Init[1]/10;
seg[4]=Time_Init[1]%10;
seg[5]=10;
}
}init.h
#ifndef _INIT_H_
#define _INIT_H_
#include<stc15f2k60s2.h>
#include"intrins.h"
void close_init();
void Timer0Init(void);
void display();
unsigned char read_key();
#endifmain.c
#include"init.h"
#include"onewire.h"
#include"ds1302.h"
#include"jm.h"
#define u8 unsigned char
#define u16 unsigned int
extern u8 ac[],seg[],Time_Init[]; // extern Indicates that this variable is defined elsewhere , To quote... Here
u8 num,mode1=0,mode2=0;
u8 temp_count=0,ac_count=0;
u8 c,d;
u8 cd_flag=0,ac_flag=0,mode1_flag=0,mode2_flag=0,temp_flag=0;
u16 cd_count=0,mode1_count=0,mode2_count=0,time_count=0;
void main()
{
close_init();
Timer0Init();
ds1302_Init();
while(1)
{
num=read_key();
switch(num)
{
case 4:
if(c==1||d==1) // In the clock interface or alarm interface
{
if((mode1!=0)||(mode2!=0)) // In the hours, minutes and seconds of the clock or alarm clock
{
jm4();
ds1302_Init();
}
}
cd_flag=0; // When L1 When flashing, press any key to stop flashing
break;
case 5:
if(c==1||d==1)
{
if((mode1!=0)||(mode2!=0))
{
jm5();
ds1302_Init();
}
}
cd_flag=0; // When L1 When flashing, press any key to stop flashing
break;
case 6:
if(++mode2==4) mode2=0;
c=1,d=0;
cd_flag=0; // When L1 When flashing, press any key to stop flashing
break;
case 7:
if(++mode1==4) mode1=0;
c=0,d=1;
cd_flag=0; // When L1 When flashing, press any key to stop flashing
break;
}
num=0;
if(c==1&&d==0)jm6();
if(d==1&&c==0)jm7();
if((Time_Init[2]==ac[2])&&(Time_Init[1]==ac[1])&&(Time_Init[0]==ac[0]))
cd_flag=1; // When cd_flag by 1 when L1 Start blinking
if(ac_flag==1&&cd_flag==1)
{
P2=(P2&0x1f)|0x80;P0=0xfe;P2&=0x1f; //L1 bright
}
else
{
P2=(P2&0x1f)|0x80;P0=0xff;P2&=0x1f; //L1 destroy
}
}
}
void Timer0() interrupt 1
{
display();
if(++temp_count==200) // Every time 200ms Get the value of primary temperature
{
temp_count=0;
temp_flag=1;
}
if(cd_flag==1)
{
if(++ac_count==200) //L1 With 0.2s Start blinking
{
ac_count=0;
ac_flag^=1;
}
if(++cd_count>=5000) //5s after L1 destroy
{
cd_count=0;
cd_flag=0;
}
}
if(mode1!=0) // Not in the clock display interface , Hours, minutes and seconds 1s Start blinking
{
if(++mode1_count==1000)
{
mode1_count=0;
mode1_flag^=1;
}
}
if(mode2!=0) // Not in the alarm display interface , Hours, minutes and seconds 1s Start blinking
{
if(++mode2_count==1000)
{
mode2_count=0;
mode2_flag^=1;
}
}
if(++time_count==1000) // Read the value of time every second
{
time_count=0;
ds1302_Read();
}
}Last , There's something wrong with it , I hope you will correct me , Or better opinions and ideas can be discussed .
边栏推荐
- IndexSort
- Vertx's responsive MySQL template
- Interview questions about producers and consumers (important)
- Chapter VI - Containers
- TCP cumulative acknowledgement and window value update
- URL programming
- Circuit, packet and message exchange
- TypeScript let与var的区别
- Introduction of buffer flow
- Raspberry pie update tool chain
猜你喜欢

Discussion on some problems of array

不出网上线CS的各种姿势
![[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)](/img/d8/b4f39d9637c9886a8c81ca125d6944.jpg)
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)

Summary of abnormal mechanism of interview

Application of pigeon nest principle in Lucene minshouldmatchsumscorer
![[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211](/img/55/fea5fe315932b92993d21f861befbe.png)
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211

Common APIs

VMWare网络模式-桥接,Host-Only,NAT网络

《指环王:力量之戒》新剧照 力量之戒铸造者亮相

Arduino Serial系列函数 有关print read 的总结
随机推荐
Vertx multi vertical shared data
URL programming
Read config configuration file of vertx
[solved] win10 cannot find a solution to the local group policy editor
Deep learning parameter initialization (I) Xavier initialization with code
Download address collection of various versions of devaexpress
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
高并发内存池
最全SQL与NoSQL优缺点对比
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
带你全流程,全方位的了解属于测试的软件事故
为什么说数据服务化是下一代数据中台的方向?
Jeecg menu path display problem
Rabbit MQ message sending of vertx
Use of file class
JUnit unit test of vertx
Some experiences of Arduino soft serial port communication
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Store WordPress media content on 4everland to complete decentralized storage
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake