基于Proteus的STM32F401RE温度检测控制仿真系统–西安电子科技大学大二微控制器个人项目
温度检测控制仿真系统
注意:两个平台芯片种类是不一样的,一样的话会仿真不了(我的电脑是这样,也有看别人的用的芯片型号一样可以仿真,见仁见智吧,提供一种方向)
可参考7.4任务实践STM32F401RE :中断方式检测按键
文章目录
系统要求
建立proteus工程
这个芯片不需要配置供电网
驱动LM061L
proteus原理图
cubemx操作
详情可参考CubeMx+Keil+Proteus仿真STM32-LCD1602
LCD1602的c和h文件
LCD1602.c
#include "LCD1602.h"
//D0-D7设定方向:I-输入;O-输出
void DataDir(char dir)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
HAL_GPIO_WritePin(GPIOC, D0_Pin|D1_Pin|D2_Pin|D3_Pin|D4_Pin|D5_Pin|D6_Pin|D7_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = D0_Pin|D1_Pin|D2_Pin|D3_Pin|D4_Pin|D5_Pin|D6_Pin|D7_Pin;
GPIO_InitStruct.Pull = GPIO_PULLUP;
if(dir == 'I')
{
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
}
else if(dir == 'O')
{
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
}
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
//D0-D7读数据
uint8_t ReadData()
{
uint8_t dat=0;
//DataDir('I');
if(HAL_GPIO_ReadPin(GPIOC, D0_Pin)==GPIO_PIN_SET) dat|=0x01;
if(HAL_GPIO_ReadPin(GPIOC, D1_Pin)==GPIO_PIN_SET) dat|=0x02;
if(HAL_GPIO_ReadPin(GPIOC, D2_Pin)==GPIO_PIN_SET) dat|=0x04;
if(HAL_GPIO_ReadPin(GPIOC, D3_Pin)==GPIO_PIN_SET) dat|=0x08;
if(HAL_GPIO_ReadPin(GPIOC, D4_Pin)==GPIO_PIN_SET) dat|=0x10;
if(HAL_GPIO_ReadPin(GPIOC, D5_Pin)==GPIO_PIN_SET) dat|=0x20;
if(HAL_GPIO_ReadPin(GPIOC, D6_Pin)==GPIO_PIN_SET) dat|=0x40;
if(HAL_GPIO_ReadPin(GPIOC, D7_Pin)==GPIO_PIN_SET) dat|=0x80;
return dat;
}
//D0-D7写数据
void WriteData(uint8_t dat)
{
uint16_t Set_Pins = 0, Rst_Pins = 0;
//DataDir('O');
if(dat & 0x01) Set_Pins |= D0_Pin;
else Rst_Pins |= D0_Pin;
if(dat & 0x02) Set_Pins |= D1_Pin;
else Rst_Pins |= D1_Pin;
if(dat & 0x04) Set_Pins |= D2_Pin;
else Rst_Pins |= D2_Pin;
if(dat & 0x08) Set_Pins |= D3_Pin;
else Rst_Pins |= D3_Pin;
if(dat & 0x10) Set_Pins |= D4_Pin;
else Rst_Pins |= D4_Pin;
if(dat & 0x20) Set_Pins |= D5_Pin;
else Rst_Pins |= D5_Pin;
if(dat & 0x40) Set_Pins |= D6_Pin;
else Rst_Pins |= D6_Pin;
if(dat & 0x80) Set_Pins |= D7_Pin;
else Rst_Pins |= D7_Pin;
HAL_GPIO_WritePin(GPIOC, Set_Pins, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, Rst_Pins, GPIO_PIN_RESET);
}
//LCD忙等待
void LCD_Busy_Wait()
{
uint8_t status;
DataDir('I');
RS_InstructionR();
RW_Read();
do
{
E_Set();
__NOP();
status = ReadData();
E_Rst();
}
while(status & 0x80);
}
//写LCD指令
void LCD_Write_Cmd(uint8_t cmd)
{
DataDir('O');
WriteData(cmd);
RS_InstructionR();
RW_Write();
E_Rst();
RS_InstructionR();
RW_Write();
E_Set();
__NOP();
E_Rst();
LCD_Busy_Wait();
}
//写LCD数据寄存器
void LCD_Write_Data(uint8_t dat)
{
DataDir('O');
WriteData(dat);
RS_DataR();
RW_Write();
E_Set();
__NOP();
E_Rst();
LCD_Busy_Wait();
}
//LCD初始化
void LCD_Init()
{
LCD_Write_Cmd(0x38);
HAL_Delay(2);
LCD_Write_Cmd(0x01);
HAL_Delay(2);
LCD_Write_Cmd(0x06);
HAL_Delay(2);
LCD_Write_Cmd(0x0c);
HAL_Delay(2);
}
//在x行(0-1),y列(0-15)显示字符串
void LCD_ShowString(uint8_t x, uint8_t y, char *str)
{
uint8_t i=0;
//设置显示起始位置
if(x == 0)
LCD_Write_Cmd(0x80|y);
else if(x == 1)
LCD_Write_Cmd(0xc0|y);
//输出字符串
for(i=0; i<16 && str[i]!='\0'; i++)
{
LCD_Write_Data(str[i]);
HAL_Delay(2);
}
}
LCD1602.h
#ifndef INC_LCD1602_H_
#define INC_LCD1602_H_
#include "main.h"
//选择数据寄存器
#define RS_DataR() HAL_GPIO_WritePin(GPIOA, RS_Pin, GPIO_PIN_SET)
#define RS_InstructionR() HAL_GPIO_WritePin(GPIOA, RS_Pin, GPIO_PIN_RESET)
//选择指令寄存器
//读操作
#define RW_Read() HAL_GPIO_WritePin(GPIOA, RW_Pin, GPIO_PIN_SET)
//写操作
#define RW_Write() HAL_GPIO_WritePin(GPIOA, RW_Pin, GPIO_PIN_RESET)
//Enable操作:高电平-读取信息;下降沿-执行指令
#define E_Set() HAL_GPIO_WritePin(GPIOA, E_Pin, GPIO_PIN_SET)
#define E_Rst() HAL_GPIO_WritePin(GPIOA, E_Pin, GPIO_PIN_RESET)
void LCD_Init(void);
void LCD_ShowString(uint8_t x, uint8_t y, char *str);
#endif //INC_LCD1602_H_
注意#endif之后要空一行,否则会报错
若要修改管脚
修改这几处即可。
验证效果
Keil生成hex文件
添加hex文件到芯片里
双击芯片后:
开始仿真
效果
在main函数里添加:
char str1[]="TEMPRATURE:";
LCD_Init(); //初始化LCD1602
LCD_ShowString(0,0,str1); //LCD 显示设定字符串
仿真得到:
驱动LM35
proteus原理图
cubemx操作
详细见HAL LM35单通道温度采集
注意!
这里应该是500而不是330,因为lm35的驱动电压是5v:
驱动串口
下载虚拟串口软件
详细见串口模拟器VSPD(附VSPD安装包)
proteus原理图
cubemx操作
详细见STM32学习:串口通讯(proteus仿真)
注意文章给的代码里回调函数名字写错了,修改为正确的即可
晶振设置
如果乱码的话,尽量cubemx里也设置成32m(看具体情况吧,我的没有乱码),即:
若串口初始化后LCD不显示了
配置完串口后,记得打开微库!
驱动l298
理论知识
proteus原理图
cubemx操作
motor的c和h文件
motor.c
#include "motor.h"
#include "main.h"
void SZhun()
{
HAL_GPIO_WritePin(IN1_GPIO_Port,IN1_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(IN2_GPIO_Port,IN2_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(ENA_GPIO_Port,ENA_Pin,GPIO_PIN_SET);
}
void NZhun()
{
HAL_GPIO_WritePin(IN1_GPIO_Port,IN1_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(IN2_GPIO_Port,IN2_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(ENA_GPIO_Port,ENA_Pin,GPIO_PIN_SET);
}
void Stop()
{
HAL_GPIO_WritePin(IN1_GPIO_Port,IN1_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(IN2_GPIO_Port,IN2_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(ENA_GPIO_Port,ENA_Pin,GPIO_PIN_SET);
}
motor.h
#ifndef __motor_H_
#define __motor_H_
void SZhun(void);//顺时针转动
void NZhun(void);//逆时针转动
void Stop(void);//停止转动
#endif
最终整理
原理图
main函数
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "LCD1602.h"
#include <stdio.h>
#include "motor.h"
#include <stdlib.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint32_t AD_value;
int temp;
int last_temp=0;
char str[20];
uint8_t Rx_data = 0;
int Flag=0;
char str1[]="TEMPRATURE:";
uint8_t str2[] = "OK!\r\n";
char str3[] = "";
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART2)
{
if(Rx_data == '!')
{
HAL_UART_Transmit(&huart2,str2,sizeof(str2),1000);
Flag = 1;
HAL_UART_Receive_IT(&huart2,&Rx_data,1);
}
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
LCD_Init(); //初始化LCD1602
LCD_ShowString(0,0,str1); //LCD 显示设定字符串
HAL_UART_Receive_IT(&huart2,&Rx_data,1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,50);
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1),HAL_ADC_STATE_REG_EOC))
{
AD_value=HAL_ADC_GetValue(&hadc1);
temp=(int)AD_value*500/4095.0;
}
if(Flag == 1)
{
if(temp<25)
{
Stop();
sprintf(str3, "%d", temp);
LCD_ShowString(1,0,str3);
}
else if(temp>=25 && temp != last_temp)
{
SZhun();
sprintf(str3, "%d℃\r\n", temp);
sprintf(str1, "%d", temp);
LCD_ShowString(1,0,str1);
HAL_UART_Transmit(&huart2, (uint8_t *)str3, strlen(str3), HAL_MAX_DELAY);
last_temp = temp;
}
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
几处设计
整型数转字符串
显示在LCD1602上
char str3[] = "";
temp=(int)AD_value*500/4095.0;
sprintf(str3, "%d", temp);//把temp转化成字符串,存储在str3里
LCD_ShowString(1,0,str3);
从串口打印
char str3[] = "";
sprintf(str3, "%d℃\r\n", temp);
HAL_UART_Transmit(&huart2, (uint8_t *)str3, strlen(str3), HAL_MAX_DELAY);
这种方式可以减少显示数字的困扰,字符串显示和打印都非常方便。
设置标志位
为了发送运行命令后整个流程再运行,选择了设置标志位。
避免串口重复发送
举个例子,当温度一直是26℃时,很容易让串口一直发送26℃,稍显混乱,那能不能只发一次,等到升温了或者降温了,再发下一个呢?
这样可以很好的达到效果:
保证了每次相同的温度只发送一次。
作者:愚蠢小熊猫