蓝桥杯温度记录器

2023-10-21 07:38
文章标签 蓝桥 温度 记录器

本文主要是介绍蓝桥杯温度记录器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

系列文章目录

蓝桥杯温度记录器(示例)
蓝桥杯单片机模板1
蓝桥杯单片机模板2
蓝桥杯单片机各功能模块的使用


文章目录

  • 系列文章目录
  • 前言
  • 一、温度记录器(间隔两秒)
  • 二、实现代码
    • 1.主程序
    • 2.其他
  • 总结


前言

第一次写博客,主要是为了记录一下自己一段时间的蓝桥杯单片机的学习,主要是学习的51单片机。By the way我使用的是keil5如果用别的版本,可能有点小问题,但应该还是能解决的。


提示:以下是本篇文章正文内容,下面案例可供参考

一、温度记录器(间隔两秒)

该题为第七届模拟题,稍微做了一点点修改,还是很简单的。下面是原题下载链接: link.

二、实现代码

1.主程序

main.c

#include <STC15.H>
#include "onewire.h"
#include "ds1302.h"
#include "key.h"
#include "intrins.h"
#define uchar unsigned charuchar code smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};
uchar code smg_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar key_value;
uchar mode=0;//模式
uchar shi,fen,miao;
uchar Table[8];
uchar temp_range[10];    //使用数组,温度[10]
uchar length=2;
bit smg_flag;
bit led_flag;
bit light;
bit t_flag = 0;//读取温度标志位
uchar index=0;//索引void ShowTable()
{if(mode==0)//模式0显示界面{Table[0]=10;Table[1]=10;Table[2]=10;Table[3]=10;Table[4]=10;Table[5]=11;Table[6]=length/10;Table[7]=length%10;}else if(mode==1)//模式1显示界面{EA=0;miao = Read_Ds1302_Byte(0x81);fen = Read_Ds1302_Byte(0x83);shi = Read_Ds1302_Byte(0x85);EA=1;Table[0] = shi/16;Table[1] = shi%16;Table[3] = fen/16;Table[4] = fen%16;Table[6] = miao/16;Table[7] = miao%16;if(smg_flag == 1){Table[2] = 11;Table[5] = 11;}else if(smg_flag == 0){Table[2] = 10;Table[5] = 10;}}else if(mode==2){Table[0] = 11;Table[1] = index/10;Table[2] = index%10;Table[3] = 10;Table[4] = 10;Table[5] = 11;Table[6] = temp_range[index]/10;Table[7] = temp_range[index]%10;}
}
void ShowLed(uchar led_dat)
{P2=0x80;P0=led_dat;P2=0x00;
}void Timer0Init()		
{AUXR |= 0x80;		TMOD &= 0xF0;		TL0 = 0x40;		TH0 = 0xA2;TF0 = 0;		TR0 = 1;		ET0=1;EA=1;
}	void main(void)
{static uchar i=0;P2=0xa0;P0=0x00;P2=0x00; // close buzzer and relayP2=0x80;P0=0xff;P2=0x00;Timer0Init();while(1){		key_value=read_key();if(mode==0){if(key_value==5)//按下S5,进入模式1{set_sfm(23,59,50);index=0;mode=1;}}if(mode==2){	if(key_value==6){light=0;//L1灭index++;if(index>=10) index=0;}if(key_value==7)//按下S7,返回模式1{mode=0;index=0;i=0;}}if(mode == 1&&t_flag == 1)      //读取温度{t_flag = 0;temp_range[index]=ReadTemp();       //一个一个赋值index++;if(index >= 10)          //采集到10个温度index复位,进入模式2{mode = 2;	light = 1;          //L1闪烁index = 0;														}}ShowTable();              //显示数码管}	
}void timer0() interrupt 1 using 1
{static int smg_count=0,i=0,t_count,count=0;TL0 = 0x40;		TH0 = 0xA2;smg_count++;if(smg_count>=1){smg_count=0;P2=0xc0;P0=0x00;P2=0x00;P2=0xe0;P0=~smg_du[Table[i]];P2=0x00;P2=0xc0;P0=smg_wei[i];P2=0x00;i++;if(i>=8) i=0;}if(++count>=500)//1s{smg_flag = ~smg_flag;led_flag = ~led_flag;		count = 0;t_count++;	}if(mode==1&&t_count==length)//经过length秒扫描一次温度{t_count=0;t_flag=1;}else if(mode!=1){t_count=0;t_flag=0;}if(mode==2&&light==1){if(led_flag==1){ShowLed(0xfe);}else if(led_flag==0){ShowLed(0xff);}}else if(light==0){ShowLed(0xff);}
}

2.其他

key.c

#include <STC15.H>
#include "key.h"
#define key_input P3
#define key_state_0 0 //判断按键是否按下
#define key_state_1 1	//判断按键是否是抖动
#define key_state_2 2	//判断按键是否弹起
#define key_mask 0x0f //屏蔽不需要的IOchar read_key(void)
{static char key_state=0;char key_press,key_return=0;key_press=key_input&key_mask;switch(key_state){case key_state_0:	if(key_press!=key_mask) key_state=key_state_1;break;case key_state_1: if(key_press==(key_input&key_mask)){if(key_press==0x0e) key_return=7;if(key_press==0x0d) key_return=6;if(key_press==0x0b) key_return=5;key_state=key_state_2;}else key_state=key_state_0;break;case key_state_2: if(key_press==0x0f) key_state=key_state_0;break;}return key_return;
}

key.h

#ifndef __key_H
#define __key_Hchar read_key(void);
#endif

ds1302.c

/*程序说明: DS1302驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台 8051,12MHz日    期: 2011-8-9
*/#include <STC15.H>
#include "ds1302.h"
#include "intrins.h"
#define uchar unsigned char
sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位												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);			
}//设置时分秒
void set_sfm(uchar shi,uchar fen,uchar miao)
{Write_Ds1302_Byte(0x8e,0);Write_Ds1302_Byte(0x80,(miao/10)*16+miao%10);Write_Ds1302_Byte(0x82,(fen/10)*16+fen%10);Write_Ds1302_Byte(0x84,(shi/10)*16+shi%10);Write_Ds1302_Byte(0x8e,0x80);
}

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H#define uchar unsigned char
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 set_sfm(uchar shi,uchar fen,uchar miao);
#endif

onewire.c

/*程序说明: 单总线驱动程序软件环境: Keil uVision 4.10 硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机日    期: 2011-8-9
*/
#include <STC15.H>
#include "onewire.h"
#define uchar unsigned charsbit DQ = P1^4;  //单总线接口//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{t*=12;while(t--);
}//通过单总线向DS18B20写一个字节
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);
}//从DS18B20读取一个字节
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设备初始化
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;
}//读温度
uchar ReadTemp()
{uchar low,high,temp;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();//高字节low=low/16;high=high*16;temp=low+high;return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#define uchar unsigned char
uchar ReadTemp();
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif

总结

第一次发文,这只是一个尝试。以后可能会学点别的东西,总结总结经验再记录下来。接下来我会发点模板,这样再写的时候也就省去了很多重复的操作,专心思考核心代码即可。当然,最好还是深入了解一下各个功能模块,这样就可以随心所欲的写代码了。有点嘴碎了哈哈,希望能和大家一起学习吧。(记得点赞哟)

这篇关于蓝桥杯温度记录器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/252906

相关文章

C语言蓝桥杯

一、语言基础 竞赛常用库函数 最值查询 min_element和max_element在vector(迭代器的使用) nth_element函数的使用 例题lanqiao OJ 497成绩分析 第一种用min_element和max_element函数的写法 第二种用min和max的写法 二分查找 二分查找只能对数组操作 binary_s

力扣 739. 每日温度【经典单调栈题目】

1. 题目 理解题意: 1.1. 给一个温度集合, 要返回一个对应长度的结果集合, 这个结果集合里面的元素 i 是 当前 i 位置的元素的下一个更高温度的元素的位置和当前 i 位置的距离之差, 若是当前元素不存在下一个更高温度的元素, 则这个位置用0代替; 2. 思路 本题用单调栈来求解;单调栈就适用于来求当前元素左边或者右边第一个比当前元素大或者小的元素;【单调栈:让栈中的元素保持单调

找不同-第15届蓝桥省赛Scratch初级组真题第4题

[导读]:超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成,后续会不定期解读蓝桥杯真题,这是Scratch蓝桥杯真题解析第183讲。 如果想持续关注Scratch蓝桥真题解读,可以点击《Scratch蓝桥杯历年真题》并订阅合集,查阅教程更方便。 第15届蓝桥杯省赛已于2024年8月24日落下帷幕,编程题一共有5题,分别如下: 猪八戒落地 游乐场 画西瓜 找不同 消

【蓝桥杯嵌入式(一)程序框架和调度器】

蓝桥杯嵌入式(一)程序框架和调度器 序、代码命名规则零、STM32和8051⼀、软件及环境安装⼆、⼯程框架搭建1.时钟配置2、SYS配置3、⼯程配置4、NVIC配置5.、Keil配置 三、系统初始化四、任务调度器 链接: 视频出处 序、代码命名规则 以下是一些常见的举例 零、STM32和8051 链接: 8位和32位单片机最本质区别 ⼀、软件及环境安装

【蓝桥杯嵌入式(二)Led、Key、Lcd】

蓝桥杯嵌入式(二)Led、Key、Lcd 五、Led模块1.原理图配置2. 知识点3.底层代码 六、Key模块1.原理图配置2.知识点3.底层代码底层代码(四⾏代码版本)底层代码(状态机版本) 七、LCD模块1.原理图配置2.知识点底层代码 五、Led模块 1.原理图配置 2. 知识点 链接: 上拉电阻的通俗解释 链接: 单⽚机怎么输出⾼电平!推挽输出和开

优化采样参数提升大语言模型响应质量:深入分析温度、top_p、top_k和min_p的随机解码策略

当向大语言模型(LLM)提出查询时,模型会为其词汇表中的每个可能标记输出概率值。从这个概率分布中采样一个标记后,我们可以将该标记附加到输入提示中,使LLM能够继续输出下一个标记的概率。这个采样过程可以通过诸如 temperature 和 top_p 等参数进行精确控制。但是你是否曾深入思考过temperature和top_p参数的具体作用? 本文将详细解析并可视化定义LLM输出行为的

蓝桥杯:整数删除

// 蓝桥杯整数删除.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<stdio.h>#define MAX 100void findmin(int a[],int n,int& pos){int min=a[0];pos=0;//pos=0我开始忘了,特别注意

第十五届蓝桥杯图形化省赛题目及解析

第十五届蓝桥杯图形化省赛题目及解析 一. 单选题 1. 运行以下程序,角色会说( )? A、29     B、31     C、33     D、35 正确答案:C 答案解析: 重复执行直到m>n不成立,即重复执行直到m<=n。所有当m小于或者 等于n时,循环结束。循环过程中变量m与变量n的变化如下表: 通过上述表格可知,循环到第五次循环结束。m的值为14,n的值为19

第八届蓝桥杯 最大公共子串(动态规划)

标题:最大公共子串 最大公共子串长度问题就是: 求两个串的所有子串中能够匹配上的最大长度是多少。 比如:"abcdkkk" 和 "baabcdadabc", 可以找到的最长的公共子串是"abcd",所以最大公共子串长度为4。 下面的程序是采用矩阵法进行求解的,这对串的规模不大的情况还是比较有效的解法。 请分析该解法的思路,并补全划线部分缺失的代码。 #include <stdio.h

蓝桥杯第八届 方格分割(dfs)

标题:方格分割6x6的方格,沿着格子的边线剪开成两部分。要求这两部分的形状完全相同。如图:p1.png, p2.png, p3.png 就是可行的分割法。试计算:包括这3种分法在内,一共有多少种不同的分割方法。注意:旋转对称的属于同一种分割法。请提交该整数,不要填写任何多余的内容或说明文字。   观察可得他是一个中心对称图形,我们只需要搜索它的对称线即可。我们可以把对称线抽象为从(