蓝桥杯基础18——第13届省赛真题与代码详解

目录

0.心得体会

1.题目如下

2.代码实现的思路

键值扫描

数码管窗口切换

数码管的动态扫描

继电器工作时L3闪烁,整点时刻L1灯光亮5秒

3.变量列表

定义的常量和数组

功能控制和状态变量

定时器和计数变量

4.代码参考

4.1 头文件

onewire.h

ds1302.h

4.2 驱动文件

onewire.c

ds1302.c

4.3 主函数代码


0.心得体会

这套题,刷新了我的认知:

        1.对于led的操作,不要定义单位的led,在io编程下总是会冲突,造成效果不好。然后我尝试了用与或进行位运算,但还是会造成引脚冲突。最后,我又尝试了定义一个led状态的变量,每次我都去改变这个变量,然后再将变量赋给P0端口,有大佬这样写出来了,但我还是失败了。于是,我想到了一个损招:本题只有3个led灯,因此建立了一个数组,将3个led的9种状态直接写出来。

        2.对于继电器的操作,也不要定义继电器的一个引脚,直接对P0口进行操作。

        3.主函数的循环中,最好的状态是只出现键值扫描函数、ds1302刷新函数、ds18b20刷新函数。然后外设的刷新采用中断一定时间产生一个标志位,外设读取标志位从而决定是否刷新。

        4.led、继电器、数码管的刷新可以放进中断程序中,对应刷新时间为:20*50us,20*50us,50*50us。

        5.对于io编程模式,要注意对锁存器常关闭。同时采用先赋值P0端口,再打开锁存器方式,这样有效防止数据冲突。

        6.要注意,ds18b20读出来的温度是三位数,因此在进行比较时需要除以10再进行

1.题目如下

2.代码实现的思路

键值扫描

键值扫描通过keyrunning函数实现。该函数检测外部按键的按下,并根据按键对key13_stateSMG_flag变量进行相应的修改。具体实现方式如下:

  • 使用C3C4作为控制列,H3H4作为行读取信号。
  • 通过设置C3C4的电平状态,可以检测到与它们相对应行H3H4的按键是否被按下。
  • 按键S13用于切换继电器工作状态(key13_state变量)。
  • 按键S12用于切换显示模式(SMG_flag变量)。
  • 按键S17S16分别用于调整时钟模式和设置温度(在SMG_flag为2和3时具有不同的功能)。

数码管窗口切换

数码管窗口切换是通过修改SMG_flag变量实现的,该变量影响flash_SMG函数中数码管显示的内容。

  • SMG_flag为1时,显示温度;
  • SMG_flag为2时,显示时间;
  • SMG_flag为3时,显示设置的温度。

数码管的动态扫描

数码管的动态扫描通过flash_SMG函数实现。该函数基于SMG_flagflash_count变量,动态调整数码管显示的内容,实现动态扫描效果。通过在定时器中断服务程序中调用flash_SMG函数,并且周期性地更改flash_count的值来实现数码管的动态扫描。

继电器工作时L3闪烁,整点时刻L1灯光亮5秒

  • 继电器的控制和LED(包括L3)的状态变化在relay_ledrunning函数中实现。
  • flag_5s用于判断是否在整点时刻,影响L1的亮灯逻辑。
  • count_100mstimer1_service中断服务程序中翻转,用于实现L3的闪烁效果。
  • 当继电器工作时,根据温度与设定温度的关系以及flag_5s的值,通过调用state_relaystate_led函数,控制继电器的开关和L3、L1的亮灯逻辑。

3.变量列表

定义的常量和数组

  • duanmaduanma_dot:定义了数码管的显示码(不含/含小数点)。用于控制数码管显示数字0-9及特殊符号。
  • led_state:定义了LED灯的状态,对应L1至L3的开关状态。其中1点亮,0熄灭。

功能控制和状态变量

  • SMG_flag:数码管显示模式标志,取值1到3,分别代表显示温度、时间和设置温度。
  • key13_state:切换工作模式,是一个位变量,0和1分别代表温度控制模式,和时间控制模式。
  • ds1302_mode2:用于控制数码管时间显示模式,是一个位变量,0和1分别代表正常显示“时分”和特殊显示“分秒”。
  • flash_ds1302flash_temperature:这两个位变量用于控制DS1302时间和DS18B20温度的刷新,1表示需要刷新,0表示不需要。
  • set_temperature:设定的温度值,用于温度控制逻辑,取值范围为10到99(摄氏度)。
  • temperature:DS18B20测量的实际温度值,用于显示和控制逻辑。

定时器和计数变量

  • count_50us:定时器0的计数变量,用于控制50微秒的计时和相应的事件触发。
  • flash_count:用于数码管动态扫描的计数变量,控制数码管的显示内容和顺序。
  • flag_5s:一个位变量,用于标识是否在整点时刻亮灯5秒的状态。

4.代码参考

4.1 头文件

onewire.h

#ifndef __ONEWIRE_H__
#define __ONEWIRE_H__void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
void Delay_OneWire(unsigned int t);#endif

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 );#endif

4.2 驱动文件

onewire.c

/*	# 	单总线代码片段说明1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题中对单片机时钟频率的要求,进行代码调试和修改。
*/#include <reg52.h>sbit DQ = P1^4;//
void Delay_OneWire(unsigned int t)  
{unsigned char i;while(t--){for(i=0;i<12;i++);}
}//
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;
}

ds1302.c

/*	# 	DS1302代码片段说明1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题中对单片机时钟频率的要求,进行代码调试和修改。
*/								//
#include <reg52.h>
#include <intrins.h>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);		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);			
}

4.3 主函数代码

#include <reg52.h>
#include <intrins.h>
#include "ds1302.h"
#include "onewire.h"sbit AUXR = 0x8e;sbit C3 = P3^5;
sbit C4 = P3^4;
sbit H3 = P3^2;
sbit H4 = P3^3;unsigned char code duanma[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1,0xbf};
unsigned char code duanma_dot[10] = { 0x40 , 0x79 , 0x24 , 0x30 , 0x10 , 0x12 , 0x02 , 0x78 , 0x00 , 0x10 };
unsigned char code write_ds1302_addr[7] = { 0x80 , 0x82 , 0x84 , 0x86 , 0x88 , 0x8a , 0x8c };
unsigned char code read_ds1302_addr[7] = { 0x81 , 0x83 , 0x85 , 0x87 , 0x89 , 0x8b , 0x8d };
unsigned char ds1302_time[8] = { 0x45 , 0x59 , 0x7 , 0x11 , 0x04 , 0x04 , 0x24 };
unsigned char code led_state[8] = { 0xff , 0xfb , 0xfd , 0xf9 , 0xfe , 0xfa , 0xfc , 0xf8 }; //对应L1,L2,L3,1点亮,0熄灭
//									000		001		010	   011	 100 	101 	110 	111void flash_SMG ();
void keyrunning ();
void relay_ledrunning ();unsigned char SMG_flag = 1;
bit key13_state = 0;
bit ds1302_mode2 = 0;void select_HC573 ( unsigned char channal )
{switch ( channal ){case 4:P2 = ( P2 & 0x1f ) | 0x80;break;case 5:P2 = ( P2 & 0x1f ) | 0xa0;break;case 6:P2 = ( P2 & 0x1f ) | 0xc0;break;case 7:P2 = ( P2 & 0x1f ) | 0xe0;break;case 0:P2 = ( P2 & 0x1f ) | 0x00;break;}
}void state_SMG ( unsigned char pos_SMG , unsigned char value_SMG )
{select_HC573 ( 0 );P0 = 0x01 << pos_SMG;	select_HC573( 6 );select_HC573 ( 0 );P0 = value_SMG;select_HC573( 7 );select_HC573 ( 0 );
}void state_SMG_all ( unsigned char value_SMG_all )
{select_HC573 ( 0 );P0 = 0xff;	select_HC573( 6 );select_HC573 ( 0 );P0 = value_SMG_all;select_HC573( 7 );select_HC573 ( 0 );
}	void state_relay ( unsigned char value_relay )
{select_HC573 ( 0 );	if ( value_relay == 0 ){P0 =0x00;}else{P0 =0x10;}select_HC573 ( 5 );select_HC573 ( 0 );
}void state_led ( unsigned char value_led )
{select_HC573 ( 0 );P0 = 0xff;select_HC573 ( 4 );P0 = value_led;select_HC573 ( 4 );	select_HC573 ( 0 );
}void init_sys ()
{select_HC573 ( 0 );P0 = 0xff;	select_HC573 ( 4 );select_HC573 ( 0 );P0 = 0x00;select_HC573 ( 5 );select_HC573 ( 0 );}void init_ds1302()
{unsigned char i;Write_Ds1302_Byte( 0x8e , 0x00 );for ( i=0 ; i<8 ; i++ ){Write_Ds1302_Byte( write_ds1302_addr[i] , ds1302_time[i] );}Write_Ds1302_Byte( 0x8e , 0x80 );
}bit flash_ds1302 = 0;
void ds1302_flash ()
{if ( flash_ds1302 == 1 ){unsigned char i;for ( i=0 ; i<8 ; i++ ){ds1302_time[i] = Read_Ds1302_Byte ( read_ds1302_addr[i] );}flash_ds1302 = 0;}
}void Delay700ms()		//@12.000MHz
{unsigned char i, j, k;_nop_();_nop_();i = 32;//32j = 236;k = 16;do{do{while (--k);} while (--j);keyrunning ();	
//		relay_ledrunning ();		} while (--i);
}bit flash_temperature = 0;
unsigned int set_temperature = 23;
unsigned int temperature = 0;
void ds18b20_temperature ()
{if ( flash_temperature == 1 ){unsigned char LSB,MSB;init_ds18b20();Write_DS18B20(0xcc);	Write_DS18B20(0x44);Delay700ms();init_ds18b20();Write_DS18B20(0xcc);	Write_DS18B20(0xbe);LSB = Read_DS18B20();MSB = Read_DS18B20();temperature = MSB;temperature = ( temperature << 8 ) | LSB;temperature = (temperature >> 4)*10 + (LSB & 0x0f)*0.625;flash_temperature = 0;}}//===================================================================================
void init_timer0 (void)		//50微秒@12.000MHz
{AUXR &= 0x7F;		//定时器时钟12T模式TMOD &= 0xF0;		//设置定时器模式TMOD |= 0x02;		//设置定时器模式TL0 = 0xCE;		//设置定时初值TH0 = 0xCE;		//设置定时重载值TF0 = 0;		//清除TF0标志TR0 = 1;		//定时器0开始计时EA = 1;ET0 = 1;
}unsigned char count_50us = 0;
unsigned char flash_count = 0;
void timer0_service () interrupt 1
{count_50us++;if ( count_50us % 50 == 0 ){if ( SMG_flag == 1 ){if ( ++flash_count > 5 ){flash_count = 0;}}else if ( SMG_flag == 2 ){if ( ++flash_count == 8 ){flash_count = 0;}}else if ( SMG_flag == 3 ){if ( ++flash_count > 4 ){flash_count = 0;}}flash_SMG ();		}if ( count_50us == 200 ){count_50us = 0;flash_ds1302 = 1;if ( SMG_flag == 1 ){flash_temperature = 1;			}}if ( count_50us % 20 == 0 ){relay_ledrunning ();}}//=======================================================================
void init_timer1(void)		//50毫秒@12.000MHz
{AUXR &= 0xBF;		//定时器时钟12T模式TMOD &= 0x0F;		//设置定时器模式TL1 = 0xB0;		//设置定时初值TH1 = 0x3C;		//设置定时初值TF1 = 0;		//清除TF1标志TR1 = 1;		//定时器1开始计时EA = 1;ET1 = 1;
}unsigned char count_50ms = 0;
bit count_100ms = 0;
void timer1_service () interrupt 3
{if ( ++count_50ms % 2 == 0 ){count_100ms = ~count_100ms;}	
}bit flag_5s = 0;
void relay_ledrunning ()
{if ( ds1302_time[0] >= 0x00 && ds1302_time[0] < 0x06 && ds1302_time[1] == 0x00 ){flag_5s = 1;}else {flag_5s = 0;}if ( (key13_state == 0) && (temperature/10 >= set_temperature) ){state_relay( 1 );if ( flag_5s == 0 ){if ( count_100ms == 0 ){state_led ( led_state[2] );}else{state_led ( led_state[3] );}}else if ( flag_5s == 1 ){if ( count_100ms == 0 ){state_led ( led_state[6] );}else{state_led ( led_state[7] );}	}}else if ( (key13_state == 0) && (temperature/10 < set_temperature) ){state_relay( 0 );if ( flag_5s == 0 ){state_led ( led_state[2] );}else if ( flag_5s == 1 ){state_relay ( 1 );state_led ( led_state[6] );}}else if ( key13_state == 1 ){if ( flag_5s == 0 ){state_relay( 0 );state_led ( led_state[0] );}else if ( flag_5s == 1 ){state_relay( 1 );if ( count_100ms == 0 ){state_led ( led_state[4] );}else{state_led ( led_state[5] );}}}else{state_led ( 7 );state_relay ( 0 );}
}	void flash_SMG ()
{state_SMG_all ( 0xff );if ( SMG_flag == 1 ){switch ( flash_count ){case 0 :	state_SMG ( 0 , duanma[10] );break;case 1 :	state_SMG ( 1 , duanma[SMG_flag] );break;case 2 :	state_SMG ( 5 , duanma[temperature/100] );break;case 3 :	state_SMG ( 6 , duanma_dot[temperature/10%10] );break;case 4 :	state_SMG ( 7 , duanma[temperature%10] );break;case 5 :	state_SMG_all ( 0xff );break;}}else if ( SMG_flag == 2 ){switch ( flash_count ){case 0 :	state_SMG ( 0 , duanma[10] );break;case 1 :	state_SMG ( 1 , duanma[SMG_flag] );break;case 2 :	if ( ds1302_mode2 == 1 ){state_SMG ( 3 , duanma[ds1302_time[1]/16] );}else{state_SMG ( 3 , duanma[ds1302_time[2]/16] );}break;case 3 :if ( ds1302_mode2 == 1 ){state_SMG ( 4 , duanma[ds1302_time[1]%16] );}else{state_SMG ( 4 , duanma[ds1302_time[2]%16] );}				break;case 4 :	state_SMG ( 5 , duanma[11] );break;case 5 :if ( ds1302_mode2 == 1 ){state_SMG ( 6 , duanma[ds1302_time[0]/16] );}else{state_SMG ( 6 , duanma[ds1302_time[1]/16] );}				break;case 6 :	if ( ds1302_mode2 == 1 ){state_SMG ( 7 , duanma[ds1302_time[0]%16] );}else{state_SMG ( 7 , duanma[ds1302_time[1]%16] );}break;case 7 :	state_SMG_all ( 0xff );break;}}		else if ( SMG_flag == 3 ){switch ( flash_count ){case 0 :	state_SMG ( 0 , duanma[10] );break;case 1 :	state_SMG ( 1 , duanma[SMG_flag] );break;case 2 :	state_SMG ( 6 , duanma[set_temperature/10] );break;case 3 :	state_SMG ( 7 , duanma[set_temperature%10] );break;case 5 :	state_SMG_all ( 0xff );break;}}
}void Delay2ms()		//@12.000MHz
{unsigned char i, j;i = 24;j = 85;do{while (--j);} while (--i);
}void keyrunning ()
{C3 = 0;C4 = H3 = H4 = 1;if ( H3 == 0 ){Delay2ms();if ( H3 == 0 ){while ( H3 == 0 );//S13key13_state = ~key13_state;}}else if ( H4 == 0 ){Delay2ms();if ( H4 == 0 ){while ( H4 == 0 );//S12				if ( ++SMG_flag == 4 ){SMG_flag = 1;}}}C4 = 0;C3 = H3 = H4 = 1;	if ( H3 == 0 ){Delay2ms();if ( H3 == 0 ){while ( H3 == 0 )//S17{				if ( SMG_flag == 2 ){	ds1302_flash();ds1302_mode2 = 1;}		}ds1302_mode2 = 0;if ( SMG_flag == 3 ){if ( --set_temperature == 9 ){set_temperature = 10;}}}}else if ( H4 == 0 ){Delay2ms();if ( H4 == 0 ){while ( H4 == 0 );//S16if ( SMG_flag == 3 ){if ( ++set_temperature == 100 ){set_temperature = 99;}}}}
}	void main ()
{init_ds1302();init_timer0();init_timer1();init_sys();while ( 1 ){keyrunning ();				ds1302_flash ();ds18b20_temperature ();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/615382.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

react使用npm i @reduxjs/toolkit react-redux

npm i reduxjs/toolkit react-redux 创建一个 store文件夹&#xff0c;里面创建index.js文件和子模块文件夹 index,js文件写入以下代码 import {configureStore} from reduxjs/toolkit // 导入子模块 import counterReducer from ./modules/one import two from ./modules/tw…

Acwing.1375 奶牛回家(最短路朴素dijkstra)

题目 晚餐时间马上就到了&#xff0c;奶牛们还在各自的牧场中悠闲的散着步。 当农夫约翰摇动铃铛&#xff0c;这些牛就要赶回牛棚去吃晚餐。 在吃晚餐之前&#xff0c;所有奶牛都在自己的牧场之中&#xff0c;有些牧场中可能没有奶牛。 每个牧场都通过一条条道路连接到一个…

蓝桥杯【第15届省赛】Python B组

这题目难度对比历届是相当炸裂的简单了…… A&#xff1a;穿越时空之门 【问题描述】 随着 2024 年的钟声回荡&#xff0c;传说中的时空之门再次敞开。这扇门是一条神秘的通道&#xff0c;它连接着二进制和四进制两个不同的数码领域&#xff0c;等待着勇者们的探索。 在二进制…

一些知识点小细节

当遇到的问题有关逆序输出&#xff0c;可以转换一下思想&#xff0c;就是使用for循环的时候&#xff0c;i的初始化是从数组或者是字符串的最后一个&#xff0c;然后注意设置循环结束的条件&#xff0c;最重要的是不要忘记i--;而不是I&#xff1b; 注意&#xff1a;当要逆序输出…

Day 39:动态规划 LeedCode 62.不同路径 63. 不同路径 II

62. 不同路径 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish” &#xff09;。 问总共有多少条不同的路径&#…

Suno AI

Suno is the latest big name in AI, but what is it? Keep reading to learn everything you need to know about Suno AI, including what it is, what it can do, and how much it costs. Suno AI是一款由Anthropic公司开发的人工智能音乐生成器&#xff0c;它利用先进…

在 Vue 3 中如何利用 TypeScript 进行类型检查和类型推断?

在 Vue 3 中&#xff0c;TypeScript 的集成得到了显著的改进&#xff0c;使得开发者可以更加便捷地进行类型检查和类型推断。以下是一些在 Vue 3 中利用 TypeScript 进行类型检查和类型推断的方法&#xff1a; 使用 <script setup> 语法&#xff1a;<script setup>…

Android适配平板屏幕尺寸

一、划分手机和平板 人为判断方法: 大于6英寸的就是平板。小于6英寸的都是手机 平板尺寸&#xff1a; 6英寸、7英寸、10英寸、14英寸… Android系统支持多配置资源文件&#xff0c;我们可以追加新的资源目录到你的Android项目中。命名规范&#xff1a; 资源名字-限制符 l…

python比较两张图片是否一样并复制

python比较两张图片是否一样并复制 1、导入库 pip install imagehash2、流程 1、通过PIL.image读取两张图片 2、通过imagehash.average_hash计算两张图片的哈希值并计较 3、如果相同则使用shutil.copy复制3、示例 img文件夹下有以下三张图片 import os import shutilimpo…

【机器学习】深入剖析贝叶斯算法原理及其广泛应用

一、引言 在机器学习的广阔领域中&#xff0c;贝叶斯算法以其独特的概率推理方式占据了重要的地位。它不仅为分类问题提供了有效的解决方案&#xff0c;还在自然语言处理、信息检索、垃圾邮件过滤等诸多领域发挥着不可替代的作用。 贝叶斯算法的基本思想源于贝叶斯定理&#xf…

MySQL基础知识——MySQL架构

看一个事儿千万不要直接陷入细节里&#xff0c;你应该先鸟瞰其全貌&#xff0c;这样能够帮助你从高维度理解问题。 同样&#xff0c; 对于MySQL的学习也是这样。平时我们使用数据库&#xff0c;看到的通常都是一个整体。 比如&#xff0c;你有个最简单的表&#xff0c;表里只…

JS - BOM(浏览器对象模型)

BOM 浏览器对象模型 BOM可以使我们通过JS来操作浏览器 在BOM中为我们提供了一组对象&#xff0c;用来完成对浏览器的操作 BOM对象 BOM&#xff08;Browser Object Model&#xff09;是指浏览器对象模型&#xff0c;它提供了与浏览器窗口进行交互的对象和方法。BOM包括一些核…