目录
STM32作业设计
STM32作业实现(一)串口通信
STM32作业实现(二)串口控制led
STM32作业实现(三)串口控制有源蜂鸣器
STM32作业实现(四)光敏传感器
STM32作业实现(五)温湿度传感器dht11
STM32作业实现(六)闪存保存数据
STM32作业实现(七)OLED显示数据
STM32作业实现(八)触摸按键TPAD
STM32作业实现(九)驱动舵机
源码位置
开启舵机引脚
引线说明
直接匹配舵机三根线方便接入的引脚只有PB0,所以项目中使用的引脚是PB0
舵机sg90 pwm周期为20ms 占空比 正向2.5%~7.5% 反向 7.5%~12.5%
将占空比初始值设置正转
完整main
main.c
/* 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 "dma.h"
#include "i2c.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "dht11.h"
#include "w25q128.h"
#include "oled.h"
#include "tpad.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MAXSIZE 256 // 数组长度
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t uart_data = 0; // 处理不定长参数
char uart_buf[MAXSIZE] = ""; // 保存不定长命令
uint16_t uart_flag = 0; // 高位 字节用做标志位 低位 字节记录数组使用空间
uint16_t light_adc_dma_buf[2] = {0}; // 接收光敏数据
char msg[MAXSIZE] = ""; // 测试用
char write_data[MAXSIZE] = ""; // 写入w25q128闪存数据, 4字节数据位 + 数据
char read_data[MAXSIZE] = ""; // 读取w25q128闪存数据, 4字节数据位 + 数据
extern uint8_t dht11_data[5];
int dj_flag = 0; // 启动舵机标识 0未启动 1启动
char oled_humidity[5] = ""; // 湿度
char oled_temperature[5] = ""; // 温度
char oled_light[5] = ""; // 亮度
char dj_speed[5] = ""; // 舵机速度
/* 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 SSD1306_Init(void);
/* 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_DMA_Init();
MX_SPI2_Init();
MX_TIM1_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
MX_ADC3_Init();
MX_TIM2_Init();
MX_TIM5_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, &uart_data, 1); // 处理不定长数据
HAL_ADCEx_Calibration_Start(&hadc3); // 校准光敏
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // w25q128 使用模式3 初始拉高片选引脚电压
int times = 0; // 写入频率
int len = 0;
SSD1306_Init(); // iic初始化显示文字
tpad_init(); // 初始化tpad默认值
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (tpad_scan(0)) // 如果按下启动舵机
{
dj_flag = !dj_flag;
if (dj_flag) // 启动舵机
{
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
sprintf(dj_speed, "%-4d", htim3.Instance->CCR3); // 获取通道3的值
OLED_ShowStr(64, 0, (uint8_t *)dj_speed, 2); // 0
}
else
{
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3); // 停止舵机
sprintf(dj_speed, "%-4d", 0);
OLED_ShowStr(64, 0, (uint8_t *)dj_speed, 2); // 舵机速度
}
}
times++;
HAL_Delay(100);
// 每秒获取一次数据
if (times % 10 == 0)
{
if (DHT_read()) // 读取温湿度
{
sprintf(oled_temperature, "%-4d", dht11_data[2]); // 温度
sprintf(oled_humidity, "%-4d", dht11_data[0]); // 湿度
OLED_ShowStr(64, 4, (uint8_t *)oled_temperature, 2); // oled显示温度值
OLED_ShowStr(64, 6, (uint8_t *)oled_humidity, 2); // oled显示湿度值
}
HAL_ADC_Start_DMA(&hadc3, (uint32_t *)light_adc_dma_buf, 1); // dma模式获取光亮数值
sprintf(oled_light, "%-4d", light_adc_dma_buf[0]); // 亮度
OLED_ShowStr(64, 2, (uint8_t *)oled_light, 2); // oled显示光照值
// 每5秒写入一次 与 延时中断配合使用
if (times == 50)
{
times = 0;
len = 0;
memset(msg, 0, sizeof(msg));
len = sprintf(msg, "sd=%s wd=%s light=%s", oled_humidity, oled_temperature, oled_light);
memset(write_data, 0, sizeof(write_data));
sprintf(write_data, "%04d%s", len, msg); // 组包写入闪存
W25QXX_Write((uint8_t *)write_data, 0x00, strlen(write_data));
HAL_Delay(100);
memset(read_data, 0, sizeof(read_data));
// 先读出4个字节获取数据长度
W25QXX_Read((uint8_t *)read_data, 0x00, 4);
len = atoi(read_data);
W25QXX_Read((uint8_t *)read_data, 0x04, len);
// 输出读取到的数据
sprintf(msg, "read data: %s", read_data);
HAL_UART_Transmit(&huart1, (uint8_t *)msg, strlen(msg), 1000);
}
}
/* 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};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if ((0x8000 & uart_flag) == 0) // 未收到\n结束
{
if (0x4000 & uart_flag) // 如果标志位当前是\r
{
// 判断本次字符是不是\r
if (uart_data == '\n')
{
uart_flag |= 0x8000; // 如果收到\n更新标志位
}
else
{
uart_flag = 0; // \r后不是\n结束符不合法,重置数据
}
}
else
{
if (uart_data == '\r') // 如果收到了\r更新标志位
{
uart_flag |= 0x4000;
}
else
{
// 正常数据存储到字符数组中
uart_buf[uart_flag & 0x0fff] = uart_data;
uart_flag++; // 下标偏移
}
}
}
if (0x8000 & uart_flag) // 收到完整的指令后
{
// 回显指令
HAL_UART_Transmit(&huart1, (uint8_t *)uart_buf, uart_flag & 0x0fff, 1000);
if (strncmp(uart_buf, "led:on", 6) == 0)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
}
else if (strncmp(uart_buf, "led:off", 7) == 0)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
}
else if (strncmp(uart_buf, "buzzer:on", 9) == 0) // 打开蜂鸣器
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
}
else if (strncmp(uart_buf, "buzzer:off", 10) == 0) // 关闭蜂鸣器
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
}
else if (strncmp(uart_buf, "dj:on", 5) == 0) // 开启舵机
{
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
sprintf(dj_speed, "%-4d", htim3.Instance->CCR3); // 获取通道3的值
OLED_ShowStr(64, 0, (uint8_t *)dj_speed, 2); // 速度值
}
else if (strncmp(uart_buf, "dj:off", 6) == 0) // 关闭舵机
{
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_3);
sprintf(dj_speed, "%-4d", 0);
OLED_ShowStr(64, 0, (uint8_t *)dj_speed, 2); // 0
}
else if (strncmp(uart_buf, "speed:", 6) == 0) // 关闭舵机
{
int speed = atoi(uart_buf + 6);
if (speed > (htim3.Instance->ARR * 0.125)) // 防止速度大于12.5%
speed = (htim3.Instance->ARR * 0.12);
else if (speed < (htim3.Instance->ARR * 0.025)) // 防止速度小于2.5%
speed = (htim3.Instance->ARR * 0.03);
htim3.Instance->CCR3 = speed;
sprintf(dj_speed, "%-4d", htim3.Instance->CCR3);
OLED_ShowStr(64, 0, (uint8_t *)dj_speed, 2); // 速度值
}
uart_flag = 0; // 处理完命令清空数据
memset(uart_buf, 0, sizeof(uart_buf));
}
HAL_UART_Receive_IT(&huart1, &uart_data, 1); // 重新开启中断接收
}
void SSD1306_Init(void)
{
OLED_Init(); // 初始化oled
// 显示默认字样
OLED_ShowCN(0, 0, 0); // 速
OLED_ShowCN(16, 0, 1); // 度
OLED_ShowStr(32, 0, (uint8_t *)":", 2); // :
OLED_ShowStr(64, 0, (uint8_t *)"0", 2); // 0
OLED_ShowCN(0, 2, 2); // 光
OLED_ShowCN(16, 2, 3); // 照
OLED_ShowStr(32, 2, (uint8_t *)":", 2); // :
OLED_ShowStr(64, 2, (uint8_t *)"0", 2); // 0
OLED_ShowCN(0, 4, 5); // 温
OLED_ShowCN(16, 4, 6); // 度
OLED_ShowStr(32, 4, (uint8_t *)":", 2); // :
OLED_ShowStr(64, 4, (uint8_t *)"0", 2); // 0
OLED_ShowCN(0, 6, 4); // 湿
OLED_ShowCN(16, 6, 6); // 度
OLED_ShowStr(32, 6, (uint8_t *)":", 2); // :
OLED_ShowStr(64, 6, (uint8_t *)"0", 2); // 0
}
/* 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 */