当前位置:网站首页>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();
#endif
ds1302.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();
#endif
jm.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();
#endif
main.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 .
边栏推荐
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- Common architectures of IO streams
- The underlying mechanism of advertising on websites
- High concurrency memory pool
- C代码生产YUV420 planar格式文件
- 专题 | 同步 异步
- [HCAI] learning summary OSI model
- Leetcode 198: house raiding
- 【已解决】win10找不到本地组策略编辑器解决方法
- Sorting, dichotomy
猜你喜欢
The embodiment of generics in inheritance and wildcards
URL programming
Store WordPress media content on 4everland to complete decentralized storage
Sorting, dichotomy
docker建立mysql:5.7版本指定路径挂载不上。
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
Summary of Arduino serial functions related to print read
Leetcode 213: 打家劫舍 II
论文学习——鄱阳湖星子站水位时间序列相似度研究
【已解决】Unknown error 1146
随机推荐
[untitled]
OSI knowledge sorting
Topic | synchronous asynchronous
论文学习——鄱阳湖星子站水位时间序列相似度研究
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
Some basic operations of reflection
VMWare网络模式-桥接,Host-Only,NAT网络
Deep learning parameter initialization (I) Xavier initialization with code
Web router of vertx
Vertx metric Prometheus monitoring indicators
Hisat2 - stringtie - deseq2 pipeline for bulk RNA seq
2021-07-18
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
An overview of IfM Engage
Common operations of JSP
Mail sending of vertx
TypeScript let與var的區別
Qtip2 solves the problem of too many texts
docket
Grpc message sending of vertx