当前位置:网站首页>The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer
The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer
2022-07-28 00:44:00 【*Black heart radish three bars*】
Since you want to know about the 11th Blue Bridge Cup SCM national tournament , That little editor doesn't say much , Let's take a look at the program design questions of the 11th single chip microcomputer .

I believe you have a general idea after reading the test questions , There are three basic digital tubes in the test questions of the 11th National Championships 、 Key 、LED, Plus DS1302、i2c、 And DS18B20. Now Xiaobian is presenting his personal code here .
The main function main.c
#include "config.h"
extern unsigned char mod4 ,mod5,segbuff[],hour,minute,second,flag_v;//S4 Interface control display
extern unsigned int temp,flag_L31;// Temperature access Voltage access
unsigned char light = 0;// Light reading
unsigned char count_temp = 0,count_v = 0;// Time reading temperature Time reading voltage
unsigned int cc = 0,old_liht = 0,count_L3 = 0;// Time reading temperature Save the last photosensitivity Dark state timing
extern unsigned int k_temp1,k_hour1,k_led1,k_temp,k_hour,k_led,date_v;
float temp232 = 0;
void main(){
unsigned char key_num = 20;
sysinit();
init_ds1302(0x16,0x59,0x50);
Timer1Init();
while(1){
key_num = scankbd();
switch(key_num){
case 0:// Switch between data interface and parameter interface
if(++mod4 == 2) mod4 = 0;
break;
case 1:// Toggle display
if(++mod5 == 3) mod5 = 0;
break;
case 4:// Parameter plus
if(mod4 == 1) S8();
break;
case 5:// Parameter minus
if(mod4 == 1) S9();
break;
default:
break;
}
if(cc >= 200){// Reading temperature
temp = rd_temperature();
cc = 0;
}
if(count_v == 50){// Get light
light = adc(1);
temp232 = (float)(light/51.0);
date_v = temp232*100;
if(light>130){
flag_v = 1;// On state
}else{
flag_v = 0;// Dark state
}
count_v = 0;
}
if(mod4 == 0){k_hour = k_hour1;k_temp = k_temp1;k_led = k_led1;}
read_ds1302();// Acquisition time
S4();// Judgment interface
}
}
void tiem1() interrupt 3{
cc++;
segs();
count_v++;
if(flag_v == 0){
count_L3++;
}else{
count_L3 = 0;
}
ledpaly();
}config.h
#ifndef _CONFIG_H
#define _CONFIG_H
#include <STC15F2K60S2.h>
#include "intrins.h"
#include "iic.h"
#include "ds1302.h"
#include "onewire.h"
void sysinit();
void segs();
unsigned char scankbd();
void Timer1Init(void) ;
void S4();
void S51();
void S52();
void S8();
void S9();
void ledpaly();
#endifconfig.c
#include "config.h"
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
sbit L7 = P0^6;
sbit L8 = P0^7;
sbit relay = P0^4;
sbit buzzer = P0^6;
#define kbd_io P3
#define kbd_maskrow 0x0f
#define state_0 0
#define state_1 1
#define state_2 2
unsigned char segbuff[] = {10,10,10,10,10,10,10,10};
unsigned char segtab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,
0xc0&0x7f,0xf9&0x7f,0xa4&0x7f,0xb0&0x7f,0x99&0x7f,0x92&0x7f,0x82&0x7f,0xf8&0x7f,0x90&0x7f,0x90&0x7f,0xbf,0xff,
0xc6,0x86,0x8c};// C E P
unsigned char mod5 = 0;//S5 Display control
unsigned int temp = 0,k_temp = 25,k_hour = 0x17,k_led = 4,date_v = 0;// Storage temperature Store partial pressure value Temperature parameters Hour parameter LED Parameters
unsigned char flag_v = 0,flag_L31 = 0,hour1 = 0;// Display light and dark (1 dark 0 bright )
unsigned char mod4 = 0,hour_temp = 0;//S4 Interface control
unsigned int k_temp1 = 25,k_hour1 = 17,k_led1 = 4;
extern unsigned char hour,minute,second;// when branch second
extern unsigned int count_L3;
void sysinit(){
P2 = P2&0x1f|0x80;P0 = 0xff; P2 &= 0x1f;
P2 = P2&0x1f|0xa0;relay = 0;buzzer = 0;P2 &= 0x1f;
P2 = P2&0x1f|0xe0;P0 = 0xff;P2 &= 0x1f;
P2 = P2&0x1f|0xc0;P0 = 0x00;P2 &= 0x1f;
}
void segs(){
static unsigned char segaddr = 0;
P2 = P2&0x1f|0xe0;P0 = 0xff;P2 &= 0x1f;
P2 = P2&0x1f|0xc0;P0 = 1<<segaddr;P2 &= 0x1f;
P2 = P2&0x1f|0xe0;P0 = segtab[segbuff[segaddr]];P2 &= 0x1f;
if(++segaddr == 8) segaddr = 0;
}
unsigned char scankbd(){
unsigned char key_num = 20;
static unsigned char kbd_state = 0;
unsigned char kbd_press;
switch(kbd_state){
case state_0:
kbd_io = 0x0f;P42 = 0;P44 = 0;kbd_press = kbd_io;
if(kbd_press != kbd_maskrow){
kbd_state = state_1;
}
break;
case state_1:
kbd_press = kbd_io;
if(kbd_press != kbd_maskrow){
if((kbd_io&0x08)==0) key_num = 0;
if((kbd_io&0x04)==0) key_num = 1;
if((kbd_io&0x02)==0) key_num = 2;
if((kbd_io&0x01)==0) key_num = 3;
kbd_io = 0xf0;P44 = 1;P42 = 1;
if(P44 == 0) key_num += 0;
if(P42 == 0) key_num += 4;
if((kbd_io&0x20)==0) key_num += 8;
if((kbd_io&0x10)==0) key_num += 12;
kbd_state = state_2;
}else{
kbd_state = state_0;
}
break;
case state_2:
kbd_io = 0x0f;P42 = 0;P44 = 0;kbd_press = kbd_io;
if(kbd_press == kbd_maskrow){
kbd_state = state_0;
}
break;
default :
break;
}
return key_num;
}
void S4(){// Interface control Data interface Parameter interface
switch(mod4){
case 0:// Data interface
S51();
break;
case 1:// Parameter interface
S52();
break;
}
}
void S51(){// Display interface switching Time temperature Brightness state
switch(mod5){// Display time
case 0:
segbuff[0] = hour/16;
segbuff[1] = hour%16;
segbuff[2] = 10;
segbuff[3] = minute/16;
segbuff[4] = minute%16;
segbuff[5] = 10;
segbuff[6] = second/16;
segbuff[7] = second%16;
break;
case 1:// Show the temperature
segbuff[0] = 24;
segbuff[1] = 10;
segbuff[2] = 10;
segbuff[3] = 10;
segbuff[4] = 10;
segbuff[5] = temp/1000;
segbuff[6] = temp%1000/100+12;
segbuff[7] = temp%1000%100/10;
break;
case 2:// Display partial pressure brightness
segbuff[0] = 25;
segbuff[1] = 10;
segbuff[2] = date_v/100+12;
segbuff[3] = date_v/10%10;
segbuff[4] = date_v%10;
segbuff[5] = 10;
segbuff[6] = 10;
segbuff[7] = flag_v;
break;
}
}
void S52(){// Display parameters
switch(mod5){// Display time parameters
case 0:
segbuff[0] = 26;
segbuff[1] = 1;
segbuff[2] = 10;
segbuff[3] = hour_temp/10;
segbuff[4] = hour_temp%10;
segbuff[5] = 10;
segbuff[6] = k_hour/10;
segbuff[7] = k_hour%10;
break;
case 1:// Display temperature parameters
segbuff[0] = 26;
segbuff[1] = 2;
segbuff[2] = 10;
segbuff[3] = 10;
segbuff[4] = 10;
segbuff[5] = 10;
segbuff[6] = k_temp/10;
segbuff[7] = k_temp%10;
break;
case 2:// Display indicator parameters
segbuff[0] = 26;
segbuff[1] = 3;
segbuff[2] = 10;
segbuff[3] = 10;
segbuff[4] = 10;
segbuff[5] = 10;
segbuff[6] = 10;
segbuff[7] = k_led%10;
break;
}
}
//mod5 0 Represents the hour parameter 1 Indicates the temperature parameter 2 Express led Parameters
void S8(){// Current parameter minus 1
switch(mod5){
case 0:
if(--k_hour1 >= 24) k_hour1 = 23;
break;
case 1:
if(--k_temp1 >=100) k_temp1 = 99;
break;
case 2:
if(--k_led1 <= 3) k_led1 = 8;
break;
}
}
void S9(){// Current parameter plus 1
switch(mod5){
case 0:
if(++k_hour1 >= 24) k_hour1 = 0;
break;
case 1:
if(++k_temp1 >=100) k_temp1 = 0;
break;
case 2:
if(++k_led1 >=9) k_led1 = 4;
break;
}
}
void ledpaly(){
P2 = P2&0x1f|0x80;P0 = 0xff;P2 &= 0x1f;
hour_temp = hour/16*10+hour%16;
if(hour_temp>=k_hour || hour_temp<=8){// The conditions are not enough
P2 = P2&0x1f|0x80;L1 = 0;P2 &= 0x1f;//L1 bright
}else{
P2 = P2&0x1f|0x80;L1 = 1;P2 &= 0x1f;//L1 destroy
}
if(temp <= k_temp*100){
P2 = P2&0x1f|0x80;L2 = 0;P2 &= 0x1f;//L2 bright
}else{
P2 = P2&0x1f|0x80;L2 = 1;P2 &= 0x1f;//L2 destroy
}
if(count_L3 >= 3000){
P2 = P2&0x1f|0x80;L3 = 0;P2 &= 0x1f;//L3 bright
}else{
P2 = P2&0x1f|0x80;L3 = 1;P2 &= 0x1f;//L3 destroy
}
if(flag_v == 0){
switch(k_led){// Select light L4-L8
case 4: P2 = P2&0x1f|0x80;L4 = 0;P2 &= 0x1f;
break;
case 5: P2 = P2&0x1f|0x80;L5 = 0;P2 &= 0x1f;
break;
case 6: P2 = P2&0x1f|0x80;L6 = 0;P2 &= 0x1f;
break;
case 7: P2 = P2&0x1f|0x80;L7 = 0;P2 &= 0x1f;
break;
case 8: P2 = P2&0x1f|0x80;L8 = 0;P2 &= 0x1f;
break;
default: break;
}
}
}
void Timer1Init(void) //1 millisecond @12.000MHz
{
AUXR &= 0xBF; // Timer clock 12T Pattern
TMOD &= 0x0F; // Set timer mode
TL1 = 0x18; // Set the initial timing value
TH1 = 0xFC; // Set the initial timing value
TF1 = 0; // eliminate TF1 sign
TR1 = 1; // Timer 1 Start timing
EA = 1;
ET1 = 1;
}iic.c
/*
Program description : IIC Bus driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz
Japan period : 2011-8-9
*/
#include <STC15F2K60S2.h>
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
// Bus pin definition
sbit SDA = P2^1; /* cable */
sbit SCL = P2^0; /* Clock line */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
// Bus start condition
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
// Bus stop condition
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Send reply
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0: The reply ,1: Non response
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Waiting for an answer
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
// adopt I2C The bus sends data
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;
}
// from I2C Data is received on the bus
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 adc(unsigned char add){
unsigned char dat = 0;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return dat;
}iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char adc(unsigned char add);
#endifds1302.c
/*
Program description : DS1302 The driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz
Japan period : 2011-8-9
*/
#include <STC15F2K60S2.h>
#include "intrins.h"
#include "ds1302.h"
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302 Reset
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);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
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_();
return (temp);
}
unsigned char hour = 0,minute = 0,second = 0;
void init_ds1302(unsigned char h,unsigned char m,unsigned char s){
Write_Ds1302_Byte(0x8e,0);
Write_Ds1302_Byte(0x84,h);
Write_Ds1302_Byte(0x82,m);
Write_Ds1302_Byte(0x80,s);
Write_Ds1302_Byte(0x8e,0x80);
}
void read_ds1302(){
hour = Read_Ds1302_Byte(0x85);
minute = Read_Ds1302_Byte(0x83);
second = Read_Ds1302_Byte(0x81);
}ds1302.h
#ifndef __DS1302_H
#define __DS1302_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 init_ds1302(unsigned char h,unsigned char m,unsigned char s);
void read_ds1302();
#endif
onewire.c
/*
Program description : Single bus driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform ( External crystal oscillator 12MHz) STC89C52RC Single chip microcomputer
Japan period : 2011-8-9
*/
#include "STC15F2K60S2.h"
#include "onewire.h"
sbit DQ = P1^4; // Single bus interface
// Single bus delay function
void Delay_OneWire(unsigned int t) //STC89C52RC
{
unsigned char i;
while(t--){
for(i =0;i<12;i++)
;
}
}
// Through a single bus to DS18B20 Write a byte
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);
}
// from DS18B20 Read a byte
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;
}
//DS18B20 Device initialization
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 rd_temperature(void){
unsigned char low,high;
unsigned int result;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low = Read_DS18B20();
high = Read_DS18B20();
result = high&0x0f;
result <<= 8;
result |= low;
result = result*6.25;//0.0625*100;
return result;
}onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned int rd_temperature(void);
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif
边栏推荐
- Ali Er Mian: why do we need to separate databases and tables?
- 半导体测试设备市场现状:国产化率仍不足10%!
- Y79. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (10)
- Application scenario Display of metauniverse
- mysql数据库的基本操作(一)-——基于数据库
- code review 工具
- Yongzhou water quality testing laboratory construction: Furniture description
- [leetcode] 547. Number of provinces (medium)
- 智能便利店带你解锁未来科技购物体验
- MATLAB | MATLAB地形生成:矩形迭代法 · 傅里叶逆变换法 · 分形柏林噪声法
猜你喜欢

Build Release Blogs

LeetCode 415. 字符串相加 和 43. 字符串相乘

程序员工作中的理性与感性活动及所需的技能素养

Rational and perceptual activities and required skills in programmers' work

MATLAB | 那些你不得不知道的MATLAB小技巧(二)

服务器中毒了——菜是原罪

A few lines of code can easily realize the real-time reasoning of paddleocr. Come and get!

Read cmake in one article

Ali Er Mian: why do we need to separate databases and tables?

Openvino integrates tensorflow to accelerate reasoning
随机推荐
What are the namespaces and function overloads of + and @ in front of MATLAB folder
require、loadfile、dofile、load、loadstring
Jmeter 如何解决乱码问题?
基本初等函数
递归求解迷宫问题
Current situation of semiconductor testing equipment Market: the localization rate is still less than 10%!
自动推理的逻辑07–谓词演算
Server open sensitive port
How to realize fast recognition of oversized images
Is it amazing to extract text from pictures? Try three steps to realize OCR!
In the third week of July, the list of feigua data station B up main ranking list was released!
"Digital economy, science and technology for the good" talk about dry goods
See how well-known enterprises use Web3 to reshape their industries
Application scenario Display of metauniverse
Jin's thinking
Set data constructor
Code review tool
The second uncle cured my spiritual internal friction and made me angry out of station B
Yongzhou water quality testing laboratory construction: Furniture description
ASML推出第一代HMI多光束检测机:速度提升600%,适用于5nm及更先进工艺