0% found this document useful (0 votes)
56 views

Program CV

The document appears to be source code for an embedded system. It contains initialization code and function definitions for various hardware peripherals including ADC, timers, I2C, and USART. It also includes code for sampling sensor data using ADC and implementing a PSO algorithm to optimize the output for different loads.

Uploaded by

Reza
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)
56 views

Program CV

The document appears to be source code for an embedded system. It contains initialization code and function definitions for various hardware peripherals including ADC, timers, I2C, and USART. It also includes code for sampling sensor data using ADC and implementing a PSO algorithm to optimize the output for different loads.

Uploaded by

Reza
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/ 19

/**

******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
** This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2023 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without
modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */


#include "string.h"

#include "lcd_character.h"
#include "math.h"
#include "stdlib.h"
#define SampleData 2000 // maksimal sampel data
#define nP 6
#define max 10
/* USER CODE END Includes */

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


ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;

TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim4;
TIM_HandleTypeDef htim8;
TIM_HandleTypeDef htim9;
TIM_HandleTypeDef htim13;
TIM_HandleTypeDef htim14;

UART_HandleTypeDef huart3;

/* USER CODE BEGIN PV */


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

/* INISIALISASI RELAY */
int l=0, t=0;

/* INISIALISASI ADC */
__IO uint16_t ADC_value[5];

/* INISISALISASI PWM */
float pwm=1050,pwm1;
char bufferduty[200];
double duty;

/* INISISALISASI USART */
unsigned int len;
char buffer[1000];

/* INISISALISASI SENSOR */
double A1, Isq1[SampleData], Isum1, Iadc1;
double A2, Vsq2[SampleData], Vsum2, Vadc2;
double A3, POsq3[SampleData], POsum3, POadc3;
float V1, I2, Vo, Io, Po, PWM, dc;
int k=0, Z=0, a=0, b=0;
char buffer_ADC_V1[200];
char buffer_ADC_I1[200];
char buffer_ADC_PO[200];
char buffer_ADC_POT[200];
char buffer_V_Input1[200];
char buffer_A_Input1[200];
char buffer_P[200];
char buffer_POT[200];
char buffer_SP[200];

/* 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_TIM4_Init(void);
static void MX_TIM9_Init(void);
static void MX_TIM13_Init(void);
static void MX_TIM14_Init(void);
static void MX_I2C1_Init(void);
static void MX_TIM8_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_TIM3_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

/* USER CODE BEGIN PFP */


/* INISISALISASI PSO */
int j=1, n=1, i=1; //j=jml iterasi, n=jml partikel, i
double s, ts, V, I, Vrand, D;
double Sol[nP+1], Pmppt[nP+1], better[nP+1], Vnew, Inew, Gbest=0, best=0,
v[nP+1];
double r1,r2,w,c1,c2;
double Vnom, Vnom1=2.200, Inom2=0.3, Vnom3=4.000, Inom4=0.5, Vnom5=10.00,
I_iden=0.03, V_iden=0.5;
double SP_3=0.48;
double SP,SP0=0.00, SP1=2.800, SP2=0.48, SP3=4.800, SP4=0.7, SP5=13.800;
double DCmax, DCmin, DCmin=0.05, DCmax=0.90;
//double DCmax1=0.2923, DCmax2=0.5092, DCmax3=0.602, DCmax4=0.612,
DCmax5=0.8542;
//double Sol[nP+1]={0.05, 0.10, 0.15, 0.20, 0.25, 0.28}; //BEBAN 1 - CLUSTER 1

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */


__IO uint16_t Nilai_ADC[5];
int on,i;

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)


{
//////////// interrupt ADC (moving average) //////////// (Sampling Freq 5kHz)
if (htim->Instance==TIM14)

{
HAL_ADC_Start_DMA(&hadc1, (uint32_t*) &ADC_value,4); //ADC
DMA 4 channel (DMA Continous Circular Half Word)

//////////// Sesuaikan kebutuhan ADC masing masing ////////////


//Sensor Tegangan Output
Iadc1= ADC_value[2]; //adc0
Isum1-=Isq1[k];
Isq1[k]=Iadc1;
Isum1+=Isq1[k];
A1=Isum1/SampleData;
Io = (0.0139*A1) -28.57; //arus adc2

//Sensor Arus Input


Vadc2= ADC_value[0]; //adc1
Vsum2-=Vsq2[k];
Vsq2[k]=Vadc2;
Vsum2+=Vsq2[k];
A2=Vsum2/SampleData;
Vo= (0.0080960376*A2) - 0.0014560844 ; //adc tegangan adc4

Po= Vo*Io;

k++;
if(k>=SampleData)
k=0;
}

/////////// INTTERUPT ALGORITMA PSO ////////////


if (htim->Instance==TIM9)
{
if (on==1)
{
if (++i>3)
{
i=1;
if (j<=max)
{
V=Vo; I=Io;
}
if (j==1) //iterasi pertama
{
if (n<=nP) //menyeleksi solusi ke n (nP=6)
{

if(n>=1 && V >= Vnom1 && V <= SP1) //BEBAN RAKET NYAMUK
{
Sol[1]=0.05;
Sol[2]=0.10;
Sol[3]=0.15;
Sol[4]=0.20;
Sol[5]=0.25;
Sol[6]=0.28;
if(I>=I_iden)
{
Pmppt[n-1]=V;
SP = SP1;
lcd_gotoxy(8,1);
lcd_puts("RAKET NYAMUK");
if (Pmppt[n-1]>Gbest && Pmppt[n-1]<=SP)
{
Gbest=Pmppt[n-1];
best=Sol[n-1];
}
if (Pmppt[n-1]>SP) //menjaga agar Vo tidak melebihi SP
{
best=Sol[n-1] - 0.02;
}
}
}

else if(n>=2 && V<= SP3) //BEBAN HEADLAMP


{
Sol[1]=0.10;
Sol[2]=0.15;
Sol[3]=0.20;
Sol[4]=0.25;
Sol[5]=0.35;
Sol[6]=0.41;
if(V>=V_iden)
{
Pmppt[n-1]=I;
SP = SP2;
lcd_gotoxy(8,1);
lcd_puts("AKI 4 V ");
if (Pmppt[n-1]>Gbest && Pmppt[n-1]<=SP)
{
Gbest=Pmppt[n-1];
best=Sol[n-1];
}
if (Pmppt[n-1]>SP)
{
best=Sol[n-1] - 0.02;
}
}
}

else if(n>=2 && V>= SP3 && I>=SP_3) //BEBAN MOBIL RC


{
Sol[1]=0.15;
Sol[2]=0.25;
Sol[3]=0.3;
Sol[4]=0.350;
Sol[5]=0.40;
Sol[6]=0.42;
if(I>=I_iden)
{
Pmppt[n-1]=V;
SP = SP4;
lcd_gotoxy(8,1);
lcd_puts("AKI 4V ");
if (Pmppt[n-1]>Gbest && Pmppt[n-1]<=SP)
{
Gbest=Pmppt[n-1];
best=Sol[n-1];
}
if (Pmppt[n-1]>SP)
{
best=Sol[n-1] - 0.02;
}
}
}

else if(n>=2 && V >= Vnom5 && V <= SP5) //BEBAN KAMERA

{
Sol[1]=0.15;
Sol[2]=0.25;
Sol[3]=0.45;
Sol[4]=0.50;
Sol[5]=0.60;
Sol[6]=0.678;
if(I>=I_iden)
{
Pmppt[n-1]=V;
SP = SP5;
lcd_gotoxy(8,1);
lcd_puts(" AKI 12V ");
if (Pmppt[n-1]>Gbest && Pmppt[n-1]<=SP)
{
Gbest=Pmppt[n-1];
best=Sol[n-1];
}
if (Pmppt[n-1]>SP)
{
best=Sol[n-1] - 0.02;
}
}
}

// else if(n>=2 && V >= Vnom3 && V <= SP3) //BEBAN HANDPHONE
//else if(n>=2 && V >= Vnom3 && V <= SP3) //BEBAN HANDPHONE
// else
// {
// Sol[1]=0.10;
// Sol[2]=0.20;
// Sol[3]=0.30;
// Sol[4]=0.45;
// Sol[5]=0.55;
// Sol[6]=0.55; //diganti 55??
// if(I>=I_iden && n>=1 && V >= Vnom3 && V <= SP3)
// {
// Pmppt[n-1]=V;
// SP = SP3;
// lcd_gotoxy(8,1);
// lcd_puts("HANDPHONE ");
// if (Pmppt[n-1]>Gbest && Pmppt[n-1]<=SP)
// {
// Gbest=Pmppt[n-1];
// best=Sol[n-1];
// }
// if (Pmppt[n-1]>SP)
// {
// best=Sol[n-1] - 0.02;
// }
// }
// }

if (n<nP)
{
D=better[n]=Sol[n];
v[n]=0;
n++;
}
else
{
j++; n=1;
}
}
}

else if(j<=max)
{
if (n<=nP)
{
if(n>=2)
{
Vnew=V; Inew=I;
if (Inew>Pmppt[n-1] && Inew<=SP)
{
Pmppt[n-1]=Inew;
better[n-1]=Sol[n-1];

if(Inew>Gbest && Inew<=SP)


{
Gbest=Pmppt[n-1];
best=Sol[n-1];
}
}
}

if (n<nP)
{
r1 = rand()%11*0.1;
r2 = rand()%11*0.1;
w = rand()%50*0.01;
c1=0.8; c2=0.8;
v[n] = w*v[n]+c1*r1*(better[n]-Sol[n])+c2*r2*(best-Sol[n]);
Sol[n] = Sol[n] + v[n];
D = Sol[n];
n++;
}
else
{
j++;
n=1;
}
}
}

else
{
D=best;
}
}

//MENJAGA NILAI SETTING POINT//


if (j >= max && Io > SP)
{
best = D - 0.005;
D = best;
}
if (j >= max && Io < SP)
{
best = D + 0.005;
D = best;
}
if (Io >= SP-0.1 && Io <= SP+0.1)
Io = SP;

//MEMBATASI NILAI DUTY CYCLE//


if (D<=DCmin)
D=DCmin;
if (D>=DCmax)
D=DCmax;

//CETAK NILAI DUTY CYCLE//


duty=D;
pwm1 = duty*2099;
TIM4->CCR1 = pwm1;
}
if (on==0)
{
j=1; n=1; i=1;
D=0;
duty=D;
SP = SP0;
pwm1 = duty*2099;
TIM4->CCR1 = pwm1;
}

}
}

/* USER CODE END 0 */

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_TIM4_Init();
MX_TIM9_Init();
MX_TIM13_Init();
MX_TIM14_Init();
MX_I2C1_Init();
MX_TIM8_Init();
MX_USART3_UART_Init();
MX_TIM3_Init();

/* USER CODE BEGIN 2 */


HAL_ADC_Start_DMA(&hadc1, (uint32_t*) &ADC_value,4);
HAL_TIM_Base_Start_IT(&htim14);

// interrupt ADC
HAL_TIM_Base_Start_IT(&htim13);
// Rencana interrupt PWM
HAL_TIM_Base_Start_IT(&htim9);
HAL_TIM_Base_Start(&htim4);
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);

// SERVO

TIM4->CCR1 = 0;
lcd_init();
lcd_gotoxy(0,0);
lcd_puts(" SMART CHARGER USING");
lcd_gotoxy(0,1);
lcd_puts(" PI METHOD ");
lcd_gotoxy(0,2);
lcd_puts(" REZA SAPTA A.P ");
lcd_gotoxy(0,3);
lcd_puts(" 2320500004 ");
HAL_Delay(800);
lcd_clear();

lcd_init();
lcd_gotoxy(0,0);
lcd_puts(" INTEGRASI CLOSELOOP");
HAL_Delay(600);
lcd_clear();

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (j>=7)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_SET);
}

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

sprintf(buffer,"%2.1f,%2.2f,%2.1f,%2.3f\n\r",Vo,Io,Po,D);
len = strlen(buffer);
HAL_UART_Transmit(&huart3,(uint8_t*)buffer,len,500);

HAL_Delay(250);

lcd_gotoxy(0,0);
sprintf(buffer_SP,"SP:%2.2f",SP);
lcd_puts(buffer_SP);
lcd_gotoxy(7,0);
lcd_puts("V");
lcd_gotoxy(0,1);
lcd_puts("BEBAN :");

//////// Tampilan D & V & I & P ////////


lcd_gotoxy(0,2);
sprintf(buffer_P,"DC:%2.3f",D);
lcd_puts(buffer_P);
HAL_Delay(50);

lcd_gotoxy(11,2);

sprintf(buffer_A_Input1,"Io:%2.3f",Io);

lcd_puts(buffer_A_Input1);
// lcd_gotoxy(17,2);
// lcd_puts("V");
HAL_Delay(50);

lcd_gotoxy(0,3);

sprintf(buffer_V_Input1,"Vo:%2.2f",Vo);
lcd_puts(buffer_V_Input1);
// lcd_gotoxy(7,3);
// lcd_puts("A");
HAL_Delay(50);

lcd_gotoxy(11,3);

sprintf(buffer_POT,"Po:%2.2f",Po);
lcd_puts(buffer_POT);
// lcd_gotoxy(17,3);
// lcd_puts("W");
HAL_Delay(50);

////SISTEM AWAL////
if (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3)==0)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_SET);
on=1;
lcd_gotoxy(10,0);
lcd_puts("SISTEM ON ");
}

if (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3)==1)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_RESET);
on=0;
lcd_gotoxy(10,0);
lcd_puts("SISTEM OFF");
lcd_gotoxy(0,1);
lcd_puts("BEBAN : ");
}

/* USER CODE END 3 */

/** System Clock Configuration


*/
void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Configure the main internal regulator output voltage


*/
__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks


*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**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_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)


{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure the Systick interrupt time


*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick


*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */


HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* ADC1 init function */


static void MX_ADC1_Init(void)
{

ADC_ChannelConfTypeDef sConfig;

/**Configure the global features of the ADC (Clock, Resolution, Data Alignment
and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 4;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**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_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure for the selected ADC regular channel its corresponding rank in the
sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure for the selected ADC regular channel its corresponding rank in the
sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = 3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**Configure for the selected ADC regular channel its corresponding rank in the
sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}

/* I2C1 init function */


static void MX_I2C1_Init(void)
{

hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 40000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/* TIM3 init function */


static void MX_TIM3_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim3.Instance = TIM3;
htim3.Init.Prescaler = 168-1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 10000-1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

HAL_TIM_MspPostInit(&htim3);

/* TIM4 init function */


static void MX_TIM4_Init(void)
{

TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim4.Instance = TIM4;
htim4.Init.Prescaler = 0;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.Period = 2099;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 5000;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

HAL_TIM_MspPostInit(&htim4);

/* TIM8 init function */


static void MX_TIM8_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

htim8.Instance = TIM8;
htim8.Init.Prescaler = 167;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 65535;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
if (HAL_TIM_Base_Init(&htim8) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/* TIM9 init function */


static void MX_TIM9_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;

htim9.Instance = TIM9;
htim9.Init.Prescaler = 1999;
htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
htim9.Init.Period = 41999;
htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim9) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim9, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/* TIM13 init function */


static void MX_TIM13_Init(void)
{

TIM_OC_InitTypeDef sConfigOC;

htim13.Instance = TIM13;
htim13.Init.Prescaler = 0;
htim13.Init.CounterMode = TIM_COUNTERMODE_UP;
htim13.Init.Period = 2099;
htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim13) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

if (HAL_TIM_PWM_Init(&htim13) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim13, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/* TIM14 init function */


static void MX_TIM14_Init(void)
{

TIM_OC_InitTypeDef sConfigOC;

htim14.Instance = TIM14;
htim14.Init.Prescaler = 0;
htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
htim14.Init.Period = 4119;
htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

if (HAL_TIM_PWM_Init(&htim14) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/* USART3 init function */


static void MX_USART3_UART_Init(void)
{

huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}

/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA2_CLK_ENABLE();
__HAL_RCC_DMA1_CLK_ENABLE();

/* DMA interrupt init */


/* DMA1_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
/* DMA2_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);

/** Configure pins as


* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */


__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */


HAL_GPIO_WritePin(GPIOE, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13
|GPIO_PIN_14, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7,
GPIO_PIN_RESET);

/*Configure GPIO pin : PA3 */


GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pin : PC4 */


GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/*Configure GPIO pins : PE10 PE11 PE12 PE14 */


GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

/*Configure GPIO pin : PE13 */


GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

/*Configure GPIO pins : PC6 PC7 PC8 PC9 */


GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/*Configure GPIO pins : PD0 PD1 PD2 PD3


PD4 PD5 PD6 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void _Error_Handler(char * file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
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

/**
* @}
*/

/**
* @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

You might also like