当前位置:网站首页>智能温度控制系统
智能温度控制系统
2022-07-29 05:22:00 【半生烟火一世迷离】
单片机读取DS18B20温度显示在LCD1602上,当温度大于30℃小于35℃蜂鸣器发声,当温度大于35℃时电机转动
DS18b20单总线数字温度计,蜂鸣器模块低电平有效,电机用(L298N)驱动,不懂的可以看我之前写的电机驱动(L298)的用法它们相同
主函数
#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B20.h"
#include "MOTOR.h"
#include "Delay.h"
sbit BEEP=P1^5;
int i=0;
float T;
void main()
{
DS18B20_ConvertT();
Delay(1000);
LCD_Init();
LCD_ShowString(1,1,"Temperature:");
while(1)
{
DS18B20_ConvertT();
T=DS18B20_ReadT();
if(T<0)
{
LCD_ShowChar(2,1,'-');
T=-T;
}
else
{
LCD_ShowChar(2,1,'+');
}
LCD_ShowNum(2,2,T,3);
LCD_ShowChar(2,5,'.');
LCD_ShowNum(2,6,(unsigned long)(T*10000)%10000,4);
if(T<30)
{
i=0;
}
while(T>30&&T<35)
{
BEEP=i;
i=1;
Delay(1000);
BEEP=1;
break;
}
while(T>35)
{
BEEP=0;
MOTOR();
break;
}
if(T<35)
{
MOTOR_step();
}
}
}
单总线收发数据函数(由某站up主参考而来其实可以和温度转换写在一起)
#include <REGX52.H>
//引脚定义
sbit OneWire_DQ=P3^7;
// 单总线初始化
unsigned char OneWire_Init(void)
{
unsigned char i;
unsigned char AckBit;
OneWire_DQ=1;
OneWire_DQ=0;
i = 247;while (--i); //Delay 500us
OneWire_DQ=1;
i = 32;while (--i); //Delay 70us
AckBit=OneWire_DQ;
i = 247;while (--i); //Delay 500us
return AckBit;
}
//单总线发送一位
void OneWire_SendBit(unsigned char Bit)
{
unsigned char i;
OneWire_DQ=0;
i = 4;while (--i); //Delay 10us
OneWire_DQ=Bit;
i = 24;while (--i); //Delay 50us
OneWire_DQ=1;
}
//单总线接收一位
unsigned char OneWire_ReceiveBit(void)
{
unsigned char i;
unsigned char Bit;
OneWire_DQ=0;
i = 2;while (--i); //Delay 5us
OneWire_DQ=1;
i = 2;while (--i); //Delay 5us
Bit=OneWire_DQ;
i = 24;while (--i); //Delay 50us
return Bit;
}
// 单总线发送一个字节
void OneWire_SendByte(unsigned char Byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
OneWire_SendBit(Byte&(0x01<<i));
}
}
// 接收的一个字节
unsigned char OneWire_ReceiveByte(void)
{
unsigned char i;
unsigned char Byte=0x00;
for(i=0;i<8;i++)
{
if(OneWire_ReceiveBit()){Byte|=(0x01<<i);}
}
return Byte;
}
DS18b20温度转换
#include <REGX52.H>
#include "OneWire.h"
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE
//温度转换
void DS18B20_ConvertT(void)
{
OneWire_Init();
OneWire_SendByte(DS18B20_SKIP_ROM);
OneWire_SendByte(DS18B20_CONVERT_T);
}
//提取温度
float DS18B20_ReadT(void)
{
unsigned char TLSB,TMSB;
int Temp;
float T;
OneWire_Init();
OneWire_SendByte(DS18B20_SKIP_ROM);
OneWire_SendByte(DS18B20_READ_SCRATCHPAD);
TLSB=OneWire_ReceiveByte();
TMSB=OneWire_ReceiveByte();
Temp=(TMSB<<8)|TLSB;
T=Temp/16.0;//相当于温度*0.0625
return T;
}
LCD显示驱动函数
#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^4;
sbit LCD_EN=P3^6;
#define LCD_DataPort P2
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData(SingleNumber+'0');
}
else
{
LCD_WriteData(SingleNumber-10+'A');
}
}
}
/**
* @brief 在LCD1602指定位置开始以二进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
}
}
电机驱动函数(比较简单的用法)可以自己加定时器pwm调数功能
#include <REGX52.H>
sbit EB=P1^0;
sbit L1=P1^1;
sbit L2=P1^2;
void MOTOR_step()
{
EB=0;
}
void MOTOR()
{
EB=1;
L1=1;
L2=0;
}
不得不说这个up主真的挺厉害的,之前做的实物后来拆了,现在没有图片了
边栏推荐
- 基于FPGA:多目标运动检测(手把手教学①)
- 电力电子:单项逆变器设计(MATLAB程序+AD原理图)
- PyTorch中的模型构建
- ABSA1: Attentional Encoder Network for Targeted Sentiment Classification
- 2022春招——芯动科技FPGA开发岗笔试题(原题以及心得)
- 【Transformer】ACMix:On the Integration of Self-Attention and Convolution
- 6、 Pointer meter recognition based on deep learning key points
- How to use the pre training language model
- 2、 How to save the images of train and test in MNIST dataset?
- [target detection] generalized focal loss v1
猜你喜欢

Jianzhi core taocloud full flash SDS helps build high-performance cloud services

基于FPGA:运动目标检测(补充仿真结果,可用毕设)

华为云14天鸿蒙设备开发-Day5驱动子系统开发

1、 Focal loss theory and code implementation

Continue the new journey and control smart storage together

【Transformer】SOFT: Softmax-free Transformer with Linear Complexity

基于STC51:四轴飞控开源项目原理图与源码(入门级DIY)

ML16-神经网络(2)

入门到入魂:单片机如何利用TB6600高精度控制步进电机(42/57)

二、OCR训练时,将txt文件和图片数据转为lmdb文件格式
随机推荐
2022春招——芯动科技FPGA岗技术面(一面心得)
ML7自学笔记
2022春招——上海安路FPGA岗面经(以及乐鑫SOC面试)
iSCSI vs iSER vs NVMe-TCP vs NVMe-RDMA
ReportingService WebService form authentication
1、 Combine multiple txt files into one TXT file
2、 Summary of deep learning data enhancement methods
How to perform POC in depth with full flash distribution?
Configuration and use of Nacos external database
ML10自学笔记-SVM
2、 During OCR training, txt files and picture data are converted to LMDB file format
电脑视频暂停再继续,声音突然变大
五、图像像素统计
1、 Pytorch Cookbook (common code Collection)
PyTorch基础知识(可入门)
2022春招——禾赛科技FPGA技术岗(一、二面,收集于:数字IC打工人及FPGA探索者)
STM32:麦克纳姆轮进行循迹任务(库函数程序代码)
第一周任务 深度学习和pytorch基础
Tf.get in tensorflow_ Detailed explanation of variable() function
ML8自学笔记