当前位置:网站首页>Summarize and learn STM32 to create project template
Summarize and learn STM32 to create project template
2022-07-26 06:44:00 【Flowers bloom in half ོ】
Because I learned from ant factory , I feel that the method they teach is very good , however , I also have a little opinion on typesetting .
use CUBE MX After generating the code , You can create an intermediate layer BSP.

And then put BSP Move this layer to this position
You can add .c Documents ..
You can add 4 individual .c file , Of course, just follow your habits .
led_key.c
#include "led_key.h"
void Led_Disp(uint8_t ucled)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,ucled<<8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
uint8_t Key_Scans(void)
{
uint8_t ucKey_val = 0;
if(key1 == Key_Down)
ucKey_val = 1;
if(key2 == Key_Down)
ucKey_val = 2;
if(key3 == Key_Down)
ucKey_val = 3;
if(key4 == Key_Down)
ucKey_val = 4;
return ucKey_val;
}
led_key.h
#ifndef _LED_KEY
#define _LED_KEY
#include "main.h"
#define key1 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)
#define key2 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)
#define key3 HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)
#define key4 HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
#define Key_Down GPIO_PIN_RESET
void Led_Disp(uint8_t ucled);
uint8_t Key_Scans(void);
#endif
bsp_adc.c
#include "bsp_adc.h"
/* Use query ADC sampling */
uint32_t Get_Adc2_IN15(void) /* PB15 R37*/
{
uint32_t Adc2_IN15_value = 0;
HAL_ADC_Start(&hadc2);
if(HAL_ADC_PollForConversion(&hadc2,50) == HAL_OK)
{
Adc2_IN15_value = HAL_ADC_GetValue(&hadc2);
}
return Adc2_IN15_value;
}
/* Obtain by interrupt ADC1 passageway 11 Value */
uint32_t Adc1_IN11_value;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
if(hadc->Instance == ADC1)
{
Adc1_IN11_value = HAL_ADC_GetValue(&hadc1);
}
HAL_ADC_Start_IT(&hadc1);
}
bsp_adc.h
#ifndef _BSP_ADC
#define _BSP_ADC
#include "main.h"
#include "adc.h"
extern uint32_t Adc1_IN11_value;
uint32_t Get_Adc2_IN15(void);
#endif
led.c and i2c.c It has been provided to us .
iic and mcp4017 Read and write code
/* EEPROM Related codes */
void Write_AT24C02(uint8_t *buf,uint8_t addr,uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
while(len--)
{
I2CSendByte(*buf++);
I2CWaitAck();
}
I2CStop();
delay1(500);
}
void Read_AT24C02(uint8_t *buf,uint8_t addr,uint8_t len)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
while(len--)
{
*buf++ = I2CReceiveByte();
if(len)
I2CSendAck();
else
I2CSendNotAck();
}
I2CStop();
delay1(500);
}
/* MCP4017*/
void Write_MCP4017(uint8_t value)
{
I2CStart();
I2CSendByte(0x5e);
I2CSendAck();
I2CSendByte(value);
I2CSendAck();
I2CStop();
}
uint8_t Read_MCP4017(void)
{
uint8_t value;
I2CStart();
I2CSendByte(0x5f);
I2CSendAck();
value = I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return value;
}
Then the rest of the functions are main.c Just implement it inside .
Key handling functions
void Key_Proc(void)
{
if(uwTick - key_uwTick < 50)
return;
key_uwTick = uwTick;
//uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
ucKey_Val = Key_Scans();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
switch(ucKey_Down)
{
case 1:
ucled |= 0x88;
count = 1;
break;
case 2:
ucled &= (~0x88);
count = 2;
break;
case 3:
ucled |= 0x55;
count = 3;
break;
case 4:
ucled &= (~0x55);
count = 4;
break;
}
}led Processing function
void Led_Proc(void)
{
if(uwTick - led_uwTick < 200)
return;
led_uwTick = uwTick;
Led_Disp(ucled);
}
lcd Processing function
void Lcd_Proc(void)
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
HAL_RTC_GetTime(&hrtc,&H_M_S,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&Y_M_D,RTC_FORMAT_BIN);
sprintf((char *)lcd_disp_string,"test:%02d i:%d",count,i);
LCD_DisplayStringLine(Line9,lcd_disp_string);
sprintf((char *)lcd_disp_string,"R37_Value:%.2fV",Get_Adc2_IN15()*3.3/4096);
LCD_DisplayStringLine(Line0,lcd_disp_string);
sprintf((char *)lcd_disp_string,"R38_Value:%.2fV",Adc1_IN11_value*3.3/4096);
LCD_DisplayStringLine(Line1,lcd_disp_string);
sprintf((char *)lcd_disp_string,"sbuf2:%d,%d,%d,%d,%d",sbuf2[0],sbuf2[1],sbuf2[2],sbuf2[3],sbuf2[4]);
LCD_DisplayStringLine(Line2,lcd_disp_string);
sprintf((char *)lcd_disp_string,"MCP_4017:%X",MCP_4017);
LCD_DisplayStringLine(Line3,lcd_disp_string);
sprintf((char *)lcd_disp_string,"pwm_duty1:%.2f",pwm_duty1);
LCD_DisplayStringLine(Line4,lcd_disp_string);
sprintf((char *)lcd_disp_string,"pwm_duty2:%.2f",pwm_duty2);
LCD_DisplayStringLine(Line5,lcd_disp_string);
sprintf((char *)lcd_disp_string,"Time:%02d-%02d-%02d",Y_M_D.Year,Y_M_D.Month,Y_M_D.Date);
LCD_DisplayStringLine(Line6,lcd_disp_string);
sprintf((char *)lcd_disp_string,"Time:%02d-%02d-%02d",H_M_S.Hours,H_M_S.Minutes,H_M_S.Seconds);
LCD_DisplayStringLine(Line7,lcd_disp_string);
}Serial port correlation function
void Usart_Proc(void)
{
if(uwTick - usart_uwTick < 1000)
return;
usart_uwTick = uwTick;
HAL_UART_Transmit(&huart1,tx_dat,sizeof(tx_dat),50);
}The basic timer realizes the basic timing function
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
if(++t == 1000)
{
ucled ^= 0x1;
t = 0;
i++;
if(i == 1000)
i = 0;
}
}
}pwm Pulse input
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2) //PA15 R40
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm_count1 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1)+1;
pwm_duty1 = (float)pwm_high1/pwm_count1;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm_high1 = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_2)+1;
}
}
if(htim->Instance == TIM3) //PB4 R39
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
pwm_count2 = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1;
pwm_duty2 = (float)pwm_high2/pwm_count2;
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
pwm_high2 = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1;
}
}
}Square wave output
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
CCR_Value1 = __HAL_TIM_GetCounter(htim) + 100;
__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,CCR_Value1);
}
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
CCR_Value2 = __HAL_TIM_GetCounter(htim) + 400;
__HAL_TIM_SET_COMPARE(htim,TIM_CHANNEL_1,CCR_Value2);
}
}
}main What's in the function
/**
* @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_ADC2_Init();
MX_USART1_UART_Init();
MX_TIM6_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_RTC_Init();
MX_TIM4_Init();
MX_TIM15_Init();
/* USER CODE BEGIN 2 */
/* LCD initialization */
LCD_Init();
LCD_Clear(White);
LCD_SetBackColor(White);
LCD_SetTextColor(Black);
/* start-up ADC1 Channel sampling */
HAL_ADC_Start_IT(&hadc1);
/* Reading and writing EEPROM*/
I2CInit();
Write_AT24C02(sbuf1,0,sizeof(sbuf1));
HAL_Delay(10);
Read_AT24C02(sbuf2,0,sizeof(sbuf2));
/* MCP4017*/
Write_MCP4017(0x45);
MCP_4017 = Read_MCP4017();
/* Turn on the serial port to receive the interrupt */
HAL_UART_Receive_IT(&huart1,&rx_dat,1);
/* Turn on the basic timer interrupt */
HAL_TIM_Base_Start_IT(&htim6);
/* open PWM Input interrupt */
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
/* Square wave output */
HAL_TIM_OC_Start_IT(&htim4,TIM_CHANNEL_1);
HAL_TIM_OC_Start_IT(&htim4,TIM_CHANNEL_2);
/* PWM Output */
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_1);// Duty cycle 30%
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_2);// Duty cycle 70%
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Key_Proc();
Led_Proc();
Lcd_Proc();
// Usart_Proc();
}
/* USER CODE END 3 */
}Related variable definitions
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Key variables */
__IO uint32_t key_uwTick;
uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
uint32_t count;
/* LED Variable */
__IO uint32_t led_uwTick;
uint8_t ucled;
/* LCD Variable */
__IO uint32_t lcd_uwTick;
uint8_t lcd_disp_string[21];
/* EEPROM Related variables */
uint8_t sbuf1[5] = {13,45,24,56,67};
uint8_t sbuf2[5] = {0};
/* MCP4017 Variable */
uint8_t MCP_4017;
/* Serial port related variables */
__IO uint32_t usart_uwTick;
uint8_t tx_dat[20] = {"hello world!\r\n"};
/* Basic timer variables */
uint32_t t,i;
/* PWM The input variable */
uint32_t pwm_count1,pwm_count2;
uint32_t pwm_high1,pwm_high2;
float pwm_duty1,pwm_duty2;
/* rtc Related variables */
RTC_TimeTypeDef H_M_S;
RTC_DateTypeDef Y_M_D;
/* Square wave output related variables */
uint32_t CCR_Value1,CCR_Value2;
/* USER CODE END PV */When writing code before, I felt very messy , But after making a set of provincial competition questions , I suddenly feel that I know how to write , This only represents personal thoughts .
边栏推荐
- C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
- Gdown Access denied:Cannot retrieve the public link of the file.
- Integrated design of signal processing systems - Design of solver functions (continuous and discrete time systems)
- UIToolkit工具模板工程
- 【保姆级】包体积优化教程
- Delete ^m from VIM
- 【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
- 力扣5: 最长回文子串
- Regular expressions and calling related functions in C language
- 【Star项目】小帽飞机大战(二)
猜你喜欢

Input the records of 5 students (each record includes student number and grade), form a record array, and then output them in order of grade from high to low The sorting method adopts selective sortin

Force buckle - 4. Find the median of two positive arrays

"Harmonyos" explore harmonyos applications
![[untitled]](/img/42/5e8b62edc0aa289098425b26df2453.jpg)
[untitled]

『牛客|每日一题』有效括号序列

力扣——4. 寻找两个正序数组的中位数

『期末复习』16/32位微处理器(8086)基本寄存器

Address resolution ARP Protocol

Upgrade appium automation framework to the latest 2.0

Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.
随机推荐
Force deduction 5: Longest palindrome substring
C#使用log4net插件,输出日志到文件
Use and analysis of show profile optimized by MySQL
@Constructorproperties annotation understanding and its corresponding usage
[1] Basic knowledge of mathematical modeling
"Harmonyos" explore harmonyos applications
堆排序(heap-sort)
BPG notes (IV)
Advanced C language - archived address book (file)
Design principle of infrared circuit of single chip microcomputer
Go的map字典及约束
Database and the future of open source
抖音web端 s_v_web_id 参数生成分析与实现
UIToolkit工具模板工程
Force buckle - 3. Longest substring without repeated characters
Open includeexceptiondetailinfaults on the server (from servicebehaviorattribute or from & lt; servicedebug & gt; to configure behavior) to send exception information back
力扣——3. 无重复字符的最长子串
Experimental flags: --disable_ admission_ control=false --enable_ rm=false --llama_ callback_ port=28000
MySQL optimized index and index invalidation
What are the main reasons for server crash