欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

基于stm32f103c8t6浊度计

程序员文章站 2022-03-08 15:10:03
基于stm32f103c8t6浊度计前言本次设计的基于stm32f103c8t6浊度计主要由硬件部分加软件部分组成硬件部分主要是IR333光源发射部分+红外接收管光源接受部分+放大电路+stm32f103c8t6主控芯片+lcd1602显示模块组成软件部分主要是通过stm32cubemx配置管脚之后添加adc通道函数、lcd1602写命令函数、lcd......
                                                                           基于stm32f103c8t6浊度计

前言

本次设计的基于stm32f103c8t6浊度计主要由硬件部分加软件部分组成
硬件部分主要是IR333光源发射部分+红外接收管光源接受部分+放大电路+stm32f103c8t6主控芯片+lcd1602显示模块组成
软件部分主要是通过stm32cubemx配置管脚之后添加adc通道函数、lcd1602写命令函数、lcd1602读数据函数等生成的

一、硬件部分

发射接收及放大电路硬件部分:
基于stm32f103c8t6浊度计
LCD102显示硬件部分:
基于stm32f103c8t6浊度计
其中最主要的是信号放大电路,由于红外接收管接受的光信号过小,所以在他输入ad转换器之前必须进行信号的放大放大芯片可以选用LM324,部分仿真图如下图所示:
基于stm32f103c8t6浊度计

二、软件部分(主要函数块及总代码)

1.主函数部分

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_ADC1_Init();
/
USER CODE BEGIN 2 */

HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1,ADC_ConvertedValue,1);


HAL_Delay(400);  //延时400ms,等液晶稳定

Lcd_Init(); // 液晶初始化函数*,设置液晶的工作模式*
unsigned char IN[]={0x20,0x20,0x20,0x20,0x44,0x69,0x67,0x69,
0x74,0x61,0x6c,0x3a};

unsigned char UN[]={0x20,0x20,0x20,0x20,0x41,0x6e,0x61,0x6c,
0x6f,0x67,0x3a,0x20};
//uint8_t num[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //0-9
/* USER CODE END 2 */

/* Infinite loop /
/
USER CODE BEGIN WHILE */
unsigned char i;
unsigned int tmp;
for(i=0;i<12;i++)
{
Lcd_Write_Command(0x80+i,1) ; //定位
Lcd_Write_Data((IN[0+i])); //写显示数据
HAL_Delay(1); //延时10ms
}
for(i=0;i<11;i++)
{
Lcd_Write_Command(0xc0+i,1) ; //定位
Lcd_Write_Data(UN[0+i]); //写显示数据
HAL_Delay(1); //延时10ms
}

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */  
 Lcd_Write_Command(0x80+12,1);//Lcd_Write_Command(0x92,1);
Lcd_Write_Data(ADC_ConvertedValue[0]/1000+0x30);
  Lcd_Write_Command(0x80+13,1);
Lcd_Write_Data((ADC_ConvertedValue[0]%1000)/100+0x30);
  Lcd_Write_Command(0x80+14,1);
Lcd_Write_Data((ADC_ConvertedValue[0]%100)/10+0x30);
  Lcd_Write_Command(0x80+15,1);
Lcd_Write_Data(ADC_ConvertedValue[0]%10+0x30);

tmp=(unsigned int)(ADC_ConvertedValue[0]3.31000/4095);
Lcd_Write_Command(0xc0+11,1);
Lcd_Write_Data(tmp/1000+0x30);
Lcd_Write_Command(0xc0+13,1);
Lcd_Write_Data((tmp%1000)/100+0x30);
Lcd_Write_Command(0xc0+12,1);
Lcd_Write_Data(0x2e);
Lcd_Write_Command(0xc0+14,1);
Lcd_Write_Data((tmp%100)/10+0x30);
Lcd_Write_Command(0xc0+15,1);
Lcd_Write_Data(tmp%10+0x30);
}
/* USER CODE END 3 */
}

2.1602显示字符代码

unsigned char IN[]={0x20,0x20,0x20,0x20,0x44,0x69,0x67,0x69,
0x74,0x61,0x6c,0x3a};

unsigned char UN[]={0x20,0x20,0x20,0x20,0x41,0x6e,0x61,0x6c,
0x6f,0x67,0x3a,0x20};
//uint8_t num[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //0-9
/* USER CODE END 2 */

/* Infinite loop /
/
USER CODE BEGIN WHILE */
unsigned char i;
unsigned int tmp;
for(i=0;i<12;i++)
{
Lcd_Write_Command(0x80+i,1) ; //定位
Lcd_Write_Data((IN[0+i])); //写显示数据
HAL_Delay(1); //延时10ms
}
for(i=0;i<11;i++)
{
Lcd_Write_Command(0xc0+i,1) ; //定位
Lcd_Write_Data(UN[0+i]); //写显示数据
HAL_Delay(1); //延时10ms
}

3.1602初始化函数

void Lcd_Init( void ) //初始化
{
Lcd_Write_Command( 0x38,1); //显示模式设置
Lcd_Write_Command( 0x01,1); //显示清屏
Lcd_Write_Command( 0x06,1); //显示光标移动设置
Lcd_Write_Command( 0x0C,1); //显示开、光标不显示
}

4.使能脉冲函数

void Lcd_En_Toggle(void) //发使能脉冲,液晶使能管脚拉高再拉低,产生上升沿,将数据线LCD_D0_Pin- LCD_D7_Pin的电平数据,写入44780芯片
{
HAL_GPIO_WritePin(LCD1602_E_GPIO_Port,LCD1602_E_Pin,GPIO_PIN_SET);//LCD1602_E_Pin管脚拉高
HAL_Delay(5); //延时5ms
HAL_GPIO_WritePin(LCD1602_E_GPIO_Port,LCD1602_E_Pin,GPIO_PIN_RESET); //LCD1602_E_Pin管脚拉低
}

5.测忙函数

void Lcd_Busy(void)//判断液晶是否忙,不忙才能往里面写数据
{
unsigned int later0=0;//判断是否忙的等待计数器
GPIO_InitTypeDef GPIO_InitStruct = {0};//以下7句话将 LCD_D7_Pin管脚置为输入模式;该管脚是判断忙与否的管脚
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = LCD_D7_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
CLE_RS; HAL_Delay(1); //RS = 0,延时1ms
SET_RW; HAL_Delay(1);//RW = 1 ,延时1ms
SET_EN; HAL_Delay(1); // EN = 1,延时1ms
while ((HAL_GPIO_ReadPin(GPIOB,LCD_D7_Pin))&&(later0<10)) //循环等待忙检测端口 = 0;或者等10毫秒再退出,避免永远等待
{
HAL_Delay(1); //延时1ms
later0++;
}
CLE_EN; //EN = 0
//恢复 LCD_D7_Pin管脚为输出状态
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = LCD_D7_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

6.端口置入函数

void Gpio_data(unsigned char x) //LCD_D0_Pin- LCD_D7_Pin端口置入8位数据x
{
//第一步,将8个管脚都设置为0
HAL_GPIO_WritePin(GPIOB,LCD_D0_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D1_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D2_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D3_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D4_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D5_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D6_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D7_Pin,GPIO_PIN_RESET);
//第二步,8个管脚都设置为x的各个位
if(x&0X01)HAL_GPIO_WritePin(GPIOB,LCD_D0_Pin,GPIO_PIN_SET);//DB0,设置最低位
if(x&0X02)HAL_GPIO_WritePin(GPIOB,LCD_D1_Pin,GPIO_PIN_SET);//DB1
if(x&0X04)HAL_GPIO_WritePin(GPIOB,LCD_D2_Pin,GPIO_PIN_SET);//DB2
if(x&0X08)HAL_GPIO_WritePin(GPIOB,LCD_D3_Pin,GPIO_PIN_SET);//DB3
if(x&0X10)HAL_GPIO_WritePin(GPIOB,LCD_D4_Pin,GPIO_PIN_SET);//DB4
if(x&0X20)HAL_GPIO_WritePin(GPIOB,LCD_D5_Pin,GPIO_PIN_SET);//DB5
if(x&0X40)HAL_GPIO_WritePin(GPIOB,LCD_D6_Pin,GPIO_PIN_SET);//DB6
if(x&0X80)HAL_GPIO_WritePin(GPIOB,LCD_D7_Pin,GPIO_PIN_SET);//DB7//设置为最高位
}

6.写入命令函数

void Lcd_Write_Command(unsigned char x,char Busy)
{
if(Busy) Lcd_Busy();
HAL_Delay(1); //延时1ms
CLE_RS; //RS = 0
HAL_Delay(1); //延时1ms
CLE_RW; //RW = 0
HAL_Delay(1); //延时1ms
Gpio_data(x); //端口置入数据
HAL_Delay(1); //延时1ms
Lcd_En_Toggle(); //发使能脉冲
HAL_Delay(1); //延时1ms
Lcd_Busy(); //测忙
}

6.写入数据函数

void Lcd_Write_Data( unsigned char x) //向液晶里面写入数据
{
Lcd_Busy(); //测忙
HAL_Delay(1); //延时1ms
SET_RS; //RS = 1
HAL_Delay(1); //延时1ms
CLE_RW; //RW = 0
HAL_Delay(1); //延时1ms
Gpio_data(x);
HAL_Delay(1); //延时1ms
Lcd_En_Toggle(); //发使能脉冲
HAL_Delay(1); //延时1ms
Lcd_Busy(); //测忙
}

7.总代码

/* USER CODE BEGIN Header /
/
*


  • @file : main.c
  • @brief : Main program body

  • @attention
  • © Copyright (c) 2020 STMicroelectronics.

  • All rights reserved.
  • This software component is licensed by ST under BSD 3-Clause license,
  • the “License”; You may not use this file except in compliance with the
  • License. You may obtain a copy of the License at:
  •                    opensource.org/licenses/BSD-3-Clause
    

/
/
USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include “main.h”

/* Private includes ----------------------------------------------------------/
/
USER CODE BEGIN Includes */

/* 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 ---------------------------------------------------------/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
/
USER CODE BEGIN PV /
uint32_t ADC_ConvertedValue[1];
/
USER CODE END PV /
/
Private function prototypes -----------------------------------------------/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
/
USER CODE BEGIN PFP /
void Lcd_Init(void) ;//液晶初始化函数
void Lcd_En_Toggle(void);//液晶使能管脚拉低再拉高,将数据写入44780芯片
void Lcd_Busy(void);//判断液晶是否忙;不忙的时候才能往里面写数据
void Gpio_data(unsigned char x);//设置8位的数据线的值;
void Lcd_Write_Command(unsigned char x,char Busy);//写命令,主要用于设置显示模式
void Lcd_Write_Data( unsigned char x);
/
USER CODE END PFP */

/* Private user code ---------------------------------------------------------/
/
USER CODE BEGIN 0 */
void Lcd_Init( void ) //初始化
{
Lcd_Write_Command( 0x38,1); //显示模式设置
Lcd_Write_Command( 0x01,1); //显示清屏
Lcd_Write_Command( 0x06,1); //显示光标移动设置
Lcd_Write_Command( 0x0C,1); //显示开、光标不显示
}

void Lcd_En_Toggle(void) //发使能脉冲,液晶使能管脚拉高再拉低,产生上升沿,将数据线LCD_D0_Pin- LCD_D7_Pin的电平数据,写入44780芯片
{
HAL_GPIO_WritePin(LCD1602_E_GPIO_Port,LCD1602_E_Pin,GPIO_PIN_SET);//LCD1602_E_Pin管脚拉高
HAL_Delay(5); //延时5ms
HAL_GPIO_WritePin(LCD1602_E_GPIO_Port,LCD1602_E_Pin,GPIO_PIN_RESET); //LCD1602_E_Pin管脚拉低
}

void Lcd_Busy(void)//判断液晶是否忙,不忙才能往里面写数据
{
unsigned int later0=0;//判断是否忙的等待计数器
GPIO_InitTypeDef GPIO_InitStruct = {0};//以下7句话将 LCD_D7_Pin管脚置为输入模式;该管脚是判断忙与否的管脚
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = LCD_D7_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
CLE_RS; HAL_Delay(1); //RS = 0,延时1ms
SET_RW; HAL_Delay(1);//RW = 1 ,延时1ms
SET_EN; HAL_Delay(1); // EN = 1,延时1ms
while ((HAL_GPIO_ReadPin(GPIOB,LCD_D7_Pin))&&(later0<10)) //循环等待忙检测端口 = 0;或者等10毫秒再退出,避免永远等待
{
HAL_Delay(1); //延时1ms
later0++;
}
CLE_EN; //EN = 0
//恢复 LCD_D7_Pin管脚为输出状态
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = LCD_D7_Pin ;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void Gpio_data(unsigned char x) //LCD_D0_Pin- LCD_D7_Pin端口置入8位数据x
{
//第一步,将8个管脚都设置为0
HAL_GPIO_WritePin(GPIOB,LCD_D0_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D1_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D2_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D3_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D4_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D5_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D6_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,LCD_D7_Pin,GPIO_PIN_RESET);
//第二步,8个管脚都设置为x的各个位
if(x&0X01)HAL_GPIO_WritePin(GPIOB,LCD_D0_Pin,GPIO_PIN_SET);//DB0,设置最低位
if(x&0X02)HAL_GPIO_WritePin(GPIOB,LCD_D1_Pin,GPIO_PIN_SET);//DB1
if(x&0X04)HAL_GPIO_WritePin(GPIOB,LCD_D2_Pin,GPIO_PIN_SET);//DB2
if(x&0X08)HAL_GPIO_WritePin(GPIOB,LCD_D3_Pin,GPIO_PIN_SET);//DB3
if(x&0X10)HAL_GPIO_WritePin(GPIOB,LCD_D4_Pin,GPIO_PIN_SET);//DB4
if(x&0X20)HAL_GPIO_WritePin(GPIOB,LCD_D5_Pin,GPIO_PIN_SET);//DB5
if(x&0X40)HAL_GPIO_WritePin(GPIOB,LCD_D6_Pin,GPIO_PIN_SET);//DB6
if(x&0X80)HAL_GPIO_WritePin(GPIOB,LCD_D7_Pin,GPIO_PIN_SET);//DB7//设置为最高位
}

//向液晶里面写入指令 时序:RS=L,RW=L,Data0-Data7=指令码,E=高脉冲
void Lcd_Write_Command(unsigned char x,char Busy)
{
if(Busy) Lcd_Busy();
HAL_Delay(1); //延时1ms
CLE_RS; //RS = 0
HAL_Delay(1); //延时1ms
CLE_RW; //RW = 0
HAL_Delay(1); //延时1ms
Gpio_data(x); //端口置入数据
HAL_Delay(1); //延时1ms
Lcd_En_Toggle(); //发使能脉冲
HAL_Delay(1); //延时1ms
Lcd_Busy(); //测忙
}
//向液晶里面写入数据 时序:RS=H,RW=L,Data0-Data7=指令码,E=高脉冲
void Lcd_Write_Data( unsigned char x) //向液晶里面写入数据
{
Lcd_Busy(); //测忙
HAL_Delay(1); //延时1ms
SET_RS; //RS = 1
HAL_Delay(1); //延时1ms
CLE_RW; //RW = 0
HAL_Delay(1); //延时1ms
Gpio_data(x);
HAL_Delay(1); //延时1ms
Lcd_En_Toggle(); //发使能脉冲
HAL_Delay(1); //延时1ms
Lcd_Busy(); //测忙
}

/* 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_ADC1_Init();
/
USER CODE BEGIN 2 */

HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1,ADC_ConvertedValue,1);


HAL_Delay(400);  //延时400ms,等液晶稳定

Lcd_Init(); // 液晶初始化函数*,设置液晶的工作模式*
unsigned char IN[]={0x20,0x20,0x20,0x20,0x44,0x69,0x67,0x69,
0x74,0x61,0x6c,0x3a};

unsigned char UN[]={0x20,0x20,0x20,0x20,0x41,0x6e,0x61,0x6c,
0x6f,0x67,0x3a,0x20};
//uint8_t num[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39}; //0-9
/* USER CODE END 2 */

/* Infinite loop /
/
USER CODE BEGIN WHILE */
unsigned char i;
unsigned int tmp;
for(i=0;i<12;i++)
{
Lcd_Write_Command(0x80+i,1) ; //定位
Lcd_Write_Data((IN[0+i])); //写显示数据
HAL_Delay(1); //延时10ms
}
for(i=0;i<11;i++)
{
Lcd_Write_Command(0xc0+i,1) ; //定位
Lcd_Write_Data(UN[0+i]); //写显示数据
HAL_Delay(1); //延时10ms
}

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */  
 Lcd_Write_Command(0x80+12,1);//Lcd_Write_Command(0x92,1);
Lcd_Write_Data(ADC_ConvertedValue[0]/1000+0x30);
  Lcd_Write_Command(0x80+13,1);
Lcd_Write_Data((ADC_ConvertedValue[0]%1000)/100+0x30);
  Lcd_Write_Command(0x80+14,1);
Lcd_Write_Data((ADC_ConvertedValue[0]%100)/10+0x30);
  Lcd_Write_Command(0x80+15,1);
Lcd_Write_Data(ADC_ConvertedValue[0]%10+0x30);

tmp=(unsigned int)(ADC_ConvertedValue[0]3.31000/4095);
Lcd_Write_Command(0xc0+11,1);
Lcd_Write_Data(tmp/1000+0x30);
Lcd_Write_Command(0xc0+13,1);
Lcd_Write_Data((tmp%1000)/100+0x30);
Lcd_Write_Command(0xc0+12,1);
Lcd_Write_Data(0x2e);
Lcd_Write_Command(0xc0+14,1);
Lcd_Write_Data((tmp%100)/10+0x30);
Lcd_Write_Command(0xc0+15,1);
Lcd_Write_Data(tmp%10+0x30);
}
/* 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 CPU, AHB and APB busses clocks
/
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_MUL6;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/
* Initializes the CPU, AHB and APB busses 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_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}

/**

  • @brief ADC1 Initialization Function
  • @param None
  • @retval None
    */
    static void MX_ADC1_Init(void)
    {

/* USER CODE BEGIN ADC1_Init 0 */

/* USER CODE END ADC1_Init 0 */

ADC_ChannelConfTypeDef sConfig = {0};

/* USER CODE BEGIN ADC1_Init 1 */

/* USER CODE END ADC1_Init 1 /
/
* Common config
/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/
* Configure Regular Channel
/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/
USER CODE BEGIN ADC1_Init 2 */

/* USER CODE END ADC1_Init 2 */

}

/**

  • Enable DMA controller clock
    */
    static void MX_DMA_Init(void)
    {

/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();

/* DMA interrupt init /
/
DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}

/**

  • @brief GPIO Initialization Function
  • @param None
  • @retval None
    */
    static void MX_GPIO_Init(void)
    {
    GPIO_InitTypeDef GPIO_InitStruct = {0};

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, LCD1602_E_Pin|LCD1602_RS_Pin|LCD1602_RW_Pin|LCD_D2_Pin
|LCD_D3_Pin|LCD_D4_Pin|LCD_D5_Pin|LCD_D6_Pin
|LCD_D7_Pin|LCD_D0_Pin|LCD_D1_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pins : LCD1602_E_Pin LCD1602_RS_Pin LCD1602_RW_Pin LCD_D2_Pin
LCD_D3_Pin LCD_D4_Pin LCD_D5_Pin LCD_D6_Pin
LCD_D7_Pin LCD_D0_Pin LCD_D1_Pin */
GPIO_InitStruct.Pin = LCD1602_E_Pin|LCD1602_RS_Pin|LCD1602_RW_Pin|LCD_D2_Pin
|LCD_D3_Pin|LCD_D4_Pin|LCD_D5_Pin|LCD_D6_Pin
|LCD_D7_Pin|LCD_D0_Pin|LCD_D1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* 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 */

/* 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,
    tex: printf(“Wrong parameters value: file %s on line %d\r\n”, file, line) /
    /
    USER CODE END 6 /
    }
    #endif /
    USE_FULL_ASSERT */

/************************ © COPYRIGHT STMicroelectronics *END OF FILE/

本文地址:https://blog.csdn.net/the_shy__/article/details/108570577