0% found this document useful (0 votes)
45 views16 pages

Main Line STM

The document contains code for a microcontroller program that controls motors and reads sensor input. It includes functions for initializing peripherals like GPIO, ADC, and timers. The main program logic includes functions for reading ADC values, controlling motors via PWM, line following using PID control, and testing buttons and LEDs for debugging.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Main Line STM

The document contains code for a microcontroller program that controls motors and reads sensor input. It includes functions for initializing peripherals like GPIO, ADC, and timers. The main program logic includes functions for reading ADC values, controlling motors via PWM, line following using PID control, and testing buttons and LEDs for debugging.

Uploaded by

Thinh Hoang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

/* USER CODE BEGIN Header */

/**
this avsdmu
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 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"

/* 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 */
#define but_gpio GPIOB
#define but_set_gr_pin GPIO_PIN_5
#define but_set_line_pin GPIO_PIN_6
#define but_run_pin GPIO_PIN_7
#define led_gpio GPIOB
#define led_set_gr_pin GPIO_PIN_9
#define led_left GPIO_PIN_10
#define led_right GPIO_PIN_9
#define led_set_line_pin GPIO_PIN_10
#define mor1_gpio GPIOB
#define mor2_gpio GPIOB
#define mor1_pin_1 GPIO_PIN_14
#define mor1_pin_2 GPIO_PIN_15
#define mor2_pin_1 GPIO_PIN_12
#define mor2_pin_2 GPIO_PIN_13
#define hcsr_gpio GPIOB
#define trig_pin GPIO_PIN_10
#define echo_pin GPIO_PIN_11
#define max_speed 1000
#define speed (max_speed*0.6)
#define min_speed max_speed*0
#define ir 7
#define KS 1000
//#define pos_m (ir*KS-KS)/2
#define pos_m 3000
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/


ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;

/* USER CODE BEGIN PV */


volatile uint16_t ADC_VAL[8];
volatile uint16_t min_val[8]={364,384,377,394,379,356,388,0};
volatile uint16_t max_val[8]={2744,2840,2700,2714,2864,2617,2883,0};
volatile uint16_t thresh_hold[8]={1554,1612,1538,1554,1635,1486,1635,0};
volatile uint8_t line_bool[8];
uint8_t get_adc_now=0;
/* 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);
static void MX_TIM3_Init(void);
static void MX_TIM4_Init(void);
/* USER CODE BEGIN PFP */

void ADC_Select_CH0 (void);


void ADC_Select_CH1 (void);
void ADC_Select_CH2 (void);
void ADC_Select_CH3 (void);
void ADC_Select_CH4 (void);
void ADC_Select_CH5 (void);
void ADC_Select_CH6 (void);
void ADC_Select_CH7 (void);
void ADC_Select_CH8 (void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/


/* USER CODE BEGIN 0 */
volatile uint32_t time_process;
volatile uint8_t count_=0;
volatile uint16_t distance_arr[5];
volatile uint16_t distance_now;
/// pid

int last_err,output,err_sum;
float p = 0.5; //0.215
float i =0;
float d = 0; /// 0.1
uint8_t run_ =0;
volatile uint16_t left_motor;
volatile uint16_t right_motor;
uint8_t first_line=0;
volatile int pos_x=0;

volatile uint32_t avgs=0;


volatile uint32_t sums=0;
volatile uint32_t map_adc[8];
volatile uint8_t state=0;
///
void delay_us(uint16_t time)
{
__HAL_TIM_SET_COUNTER(&htim3,0); //
while (__HAL_TIM_GET_COUNTER(&htim3) < time); //
}
uint16_t read_timer3()
{
return TIM3->CNT;
}
void hcsr_test()
{
state=0;
HAL_TIM_Base_Start(&htim3);
HAL_GPIO_WritePin(GPIOB,trig_pin,GPIO_PIN_SET);
delay_us(5);
state=1;
HAL_GPIO_WritePin(GPIOB,trig_pin,GPIO_PIN_RESET);
uint16_t timeout_count=HAL_GetTick();
while(HAL_GPIO_ReadPin(GPIOB,echo_pin)==0 || (HAL_GetTick()-
timeout_count)>300);
state=2;
__HAL_TIM_SET_COUNTER(&htim3,0); //
timeout_count=HAL_GetTick();
while((HAL_GPIO_ReadPin(GPIOB,echo_pin)==1)|| (HAL_GetTick()-
timeout_count)>300)
{
// timeout_count=HAL_GetTick();
}
state=3;
uint16_t time_pulse=read_timer3();
distance_now = (uint16_t)(time_pulse/2.0/29.412); // s=v*t --> 29.412 cm / ms
--> s=v*t
if(distance_now > 300)distance_now=300;
else if(distance_now < 2)distance_now=2;
HAL_TIM_Base_Stop(&htim3);
}

void get_adc_value()
{
uint32_t start_ = HAL_GetTick();
/*
ADC_Select_CH1();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[0] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
ADC_Select_CH2();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[1] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
ADC_Select_CH8();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[7] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
*/

ADC_Select_CH3();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[2] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
ADC_Select_CH4();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[3] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH5();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[4] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH6();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[5] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH7();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[6] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

time_process=HAL_GetTick()-start_;
}

void get_adc_value2()
{
uint32_t start_ = HAL_GetTick();
ADC_Select_CH0();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[0] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
ADC_Select_CH1();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[1] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH2();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[2] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
ADC_Select_CH3();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[3] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH4();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[4] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH5();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[5] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

ADC_Select_CH6();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
ADC_VAL[6] = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

time_process=HAL_GetTick()-start_;
}

void motor1_act(int speed_)


{
TIM4->CCR1=speed_;
HAL_GPIO_WritePin(mor1_gpio,mor1_pin_1,GPIO_PIN_SET);
HAL_GPIO_WritePin(mor1_gpio,mor1_pin_2,GPIO_PIN_RESET);
//HAL_GPIO_WritePin(mor1_gpio,GPIO_PIN_6,GPIO_PIN_SET);
}
void motor2_act(int speed_)
{
TIM4->CCR2=speed_;
HAL_GPIO_WritePin(mor1_gpio,mor2_pin_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(mor1_gpio,mor2_pin_1,GPIO_PIN_RESET);
}
void line_follow()
{
int err = ADC_VAL[3]-ADC_VAL[5]; ////// 7 6 5 4 3 2 1 0 --> middle = 3 4
5
output=p*err + d*(err-last_err); /// err > 0 -> 3>5 -> can quay phai -> l>r
last_err=err;
left_motor=speed+output;
right_motor=speed-output;
if(left_motor >max_speed)left_motor=max_speed;
else if(left_motor<min_speed)left_motor=min_speed;
if(right_motor>max_speed)right_motor=max_speed;
else if(right_motor<min_speed)right_motor=min_speed;
//motor_act();
}
void button_test()
{
if(HAL_GPIO_ReadPin(but_gpio,but_set_gr_pin) == 0)
{
HAL_GPIO_WritePin(led_gpio,led_set_gr_pin,GPIO_PIN_SET);
//get_sample();
//HAL_Delay(100);
}
if(HAL_GPIO_ReadPin(but_gpio,but_set_line_pin)==0)
{
//HAL_GPIO_WritePin(led_gpio,led_set_gr_pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(led_gpio,led_set_line_pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(led_gpio,led_set_gr_pin,GPIO_PIN_SET);
run_=0;
left_motor=0;
right_motor=0;
first_line=0;
//motor_act();
}
if(HAL_GPIO_ReadPin(but_gpio,but_run_pin)==0)
{
HAL_GPIO_WritePin(led_gpio,led_set_line_pin,GPIO_PIN_SET);
run_=1;
HAL_Delay(100);
//count_++;
}
}
void motor_act_2()
{
left_motor=speed-output;
right_motor=speed+output;
if(left_motor >max_speed)left_motor=max_speed;
else if(left_motor<min_speed)left_motor=min_speed;
if(right_motor>max_speed)right_motor=max_speed;
else if(right_motor<min_speed)right_motor=min_speed;
motor1_act(left_motor);
motor2_act(500);
}
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
long constrain(long val,long val_min,long val_max)
{
if(val>val_max)return val_max;
if(val<val_min)return val_min;
}
void get_sample()
{
static uint8_t first=0;
get_adc_value2();
//while(get_adc_now==0);
for(int i = 0 ;i<7;i++)
{
min_val[i]=ADC_VAL[i];
max_val[i]=ADC_VAL[i];
}
first=1;
while(first==1)
{
avgs=0;
sums=0;
uint8_t get_line=0;
get_adc_value2();
for(int i = 0 ;i<7;i++)
{
if(min_val[i] >ADC_VAL[i])min_val[i]=ADC_VAL[i]; // 0- 4095 -->
nen trang nho nhat nên den lon nhat
if(max_val[i] <ADC_VAL[i])max_val[i]=ADC_VAL[i];
thresh_hold[i]=(min_val[i]+max_val[i])/2; // < thresh_hold -->
nen trang > nen den

map_adc[i]=map(ADC_VAL[i],min_val[i],max_val[i],0,KS);
map_adc[i]=constrain(map_adc[i],0,KS); /// input --> 0-> 7000 ---> 0-
4095 10 bit ADC stm32 3700 1000 --> 4000 500 --> 0-1000 230->1000 --> <50
if(map_adc[i]>230)get_line=1;
if(map_adc[i]>45)
{
avgs+=(long)map_adc[i]*(i*KS); /// lech trai hay lech phai -->
3500 < 3500 -> lech phai hoac trai -> > 3500 -> nguoc lai
sums+=map_adc[i];
}
}
if(get_line)pos_x=avgs/sums; // posx -> vi tri --> neu ma dang ben trai ->
quay sang phai
else if(pos_x<pos_m)pos_x=0; // --> PID -> input 3500 output 0-7000 ->
>3500 --> lech trai <3500 -> lech phai
else pos_x=pos_m*2;
}
}
void move_robot_2()
{
if(thresh_hold[3] != 0)
{

uint8_t get_line=0;
get_adc_value2();
avgs=0;
sums=0;
uint8_t total_line=0;
for (uint8_t i = 0 ;i<7;i++)
{
/*
uint16_t denominator = max_val[i] - min_val[i];

if (denominator != 0)
{
ADC_VAL[i] = (((int32_t)ADC_VAL[i]) - min_val[i]) * 1000 / denominator;
}
*/
if(ADC_VAL[i]>thresh_hold[i])total_line+=1; //
map_adc[i]=map(ADC_VAL[i],min_val[i],max_val[i],0,KS);
map_adc[i]=constrain(map_adc[i],0,KS); /// input --> 0-> 7000 ---> 0-
4095 10 bit ADC stm32 3700 1000 --> 4000 500 --> 0-1000 230->1000 --> <50
if(map_adc[i]>230)get_line=1;
if(map_adc[i]>45)
{
avgs+=(long)map_adc[i]*(i*KS); /// lech trai hay lech phai -->
3500 < 3500 -> lech phai hoac trai -> > 3500 -> nguoc lai
sums+=map_adc[i];
}
}
/*
if(total_line>=5 && first_line==0)
{
left_motor=0;
right_motor=0;
motor1_act(0);
motor2_act(0);
HAL_Delay(10000);
}
*/
if(get_line)pos_x=avgs/sums; // posx -> vi tri --> neu ma dang ben trai ->
quay sang phai
else if(pos_x<pos_m)pos_x=0; // --> PID -> input 3500 output 0-7000 ->
>3500 --> lech trai <3500 -> lech phai
else pos_x=pos_m*2;
int err_now =pos_x-pos_m; // >3500 -> right > left --> right+=output >3500 ->
4000 --> 3500 --> 100
static int sum_ ;
sum_+=err_now;
sum_=constrain(sum_,-500,500); //
output=p*err_now+d*(err_now-last_err)+i*sum_; // 400 --> lech trai --> quay
phai --> toc do banh trai > toc do banh phai
last_err=err_now; /// --> 18 --> toc do banh trai +18 -> toc do banh phai -18

motor_act_2();
}
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
get_adc_now=1;
//move_robot();
}
/* 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();
MX_TIM3_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
//HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_VAL, 8); // start adc in DMA mode

//servo(10);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


//button_test();
//get_sample();

//motor1_act(400);
// motor2_act(700);

//get_adc_value();
//motor_test();
move_robot_2();
//motor_test();
// //servo(10);
// hcsr_test();
}
/* 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();
}
}

/**
* @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_ENABLE;
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();
}

/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{

/* USER CODE BEGIN TIM3_Init 0 */

/* USER CODE END TIM3_Init 0 */

TIM_ClockConfigTypeDef sClockSourceConfig = {0};


TIM_MasterConfigTypeDef sMasterConfig = {0};

/* USER CODE BEGIN TIM3_Init 1 */

/* USER CODE END TIM3_Init 1 */


htim3.Instance = TIM3;
htim3.Init.Prescaler = 71;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 65535;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */

/* USER CODE END TIM3_Init 2 */

/**
* @brief TIM4 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM4_Init(void)
{

/* USER CODE BEGIN TIM4_Init 0 */

/* USER CODE END TIM4_Init 0 */

TIM_MasterConfigTypeDef sMasterConfig = {0};


TIM_OC_InitTypeDef sConfigOC = {0};

/* USER CODE BEGIN TIM4_Init 1 */

/* USER CODE END TIM4_Init 1 */


htim4.Instance = TIM4;
htim4.Init.Prescaler = 71;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 999;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM4_Init 2 */

/* USER CODE END TIM4_Init 2 */


HAL_TIM_MspPostInit(&htim4);

/**
* 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_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_9, GPIO_PIN_RESET);

/*Configure GPIO pins : PB10 PB12 PB13 PB14


PB15 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/*Configure GPIO pin : PB11 */


GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* USER CODE BEGIN 4 */


void ADC_Select_CH0 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH1 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}

void ADC_Select_CH2 (void)


{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH4 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}

void ADC_Select_CH3 (void)


{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH6 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}

void ADC_Select_CH5 (void)


{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH7(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}

void ADC_Select_CH8 (void)


{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank
in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
/* 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 */

You might also like