Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nucleof072rb/Core/Inc/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2026 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
Expand Down
52 changes: 52 additions & 0 deletions nucleof072rb/Core/Inc/spi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
******************************************************************************
* @file spi.h
* @brief This file contains all the function prototypes for
* the spi.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2026 STMicroelectronics.
* All rights reserved.</center></h2>
*
* 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
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SPI_H__
#define __SPI_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_SPI1_Init(void);

/* USER CODE BEGIN Prototypes */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __SPI_H__ */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
4 changes: 2 additions & 2 deletions nucleof072rb/Core/Inc/stm32f0xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
/*#define HAL_LPTIM_MODULE_ENABLED */
/*#define HAL_RNG_MODULE_ENABLED */
/*#define HAL_RTC_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_TIM_MODULE_ENABLED */
#define HAL_SPI_MODULE_ENABLED
#define HAL_TIM_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
/*#define HAL_USART_MODULE_ENABLED */
/*#define HAL_IRDA_MODULE_ENABLED */
Expand Down
54 changes: 54 additions & 0 deletions nucleof072rb/Core/Inc/tim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
******************************************************************************
* @file tim.h
* @brief This file contains all the function prototypes for
* the tim.c file
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2026 STMicroelectronics.
* All rights reserved.</center></h2>
*
* 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
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TIM_H__
#define __TIM_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern TIM_HandleTypeDef htim1;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_TIM1_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

/* USER CODE BEGIN Prototypes */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __TIM_H__ */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2 changes: 1 addition & 1 deletion nucleof072rb/Core/Inc/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2026 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
Expand Down
3 changes: 2 additions & 1 deletion nucleof072rb/Core/Src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2026 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
Expand Down Expand Up @@ -47,6 +47,7 @@ void MX_GPIO_Init(void)
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
Expand Down
66 changes: 66 additions & 0 deletions nucleof072rb/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

Expand All @@ -34,6 +36,9 @@

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define ADC_MAX_VALUE 1023

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
Expand All @@ -48,9 +53,14 @@
/* USER CODE END PV */

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

void SystemClock_Config(void);

/* USER CODE BEGIN PFP */

uint16_t ADC_Read(void);
void Set_PWM_From_ADC(uint16_t adc_val);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
Expand All @@ -71,33 +81,54 @@ int main(void)
/* 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_USART2_UART_Init();
MX_SPI1_Init();
MX_TIM1_Init();

/* USER CODE BEGIN 2 */

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

/* USER CODE END 2 */

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



/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

uint16_t adc_val;

adc_val = ADC_Read();
Set_PWM_From_ADC(adc_val);

HAL_Delay(10);

}
/* USER CODE END 3 */
}
Expand All @@ -118,12 +149,15 @@ void SystemClock_Config(void)
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
Expand All @@ -143,7 +177,39 @@ void SystemClock_Config(void)
}

/* USER CODE BEGIN 4 */
uint16_t ADC_Read(void)
{
uint8_t tx[3] = {0x01, 0x80, 0x00};
uint8_t rx[3] = {0};
uint16_t adc_value;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you meant to allocate 2 bytes?


HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

HAL_SPI_TransmitReceive(&hspi1, tx, rx, 3, HAL_MAX_DELAY);

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);

adc_value = ((rx[1] & 0x03) << 8) | rx[2];

return adc_value;
}



void Set_PWM_From_ADC(uint16_t adc_val)
{
uint32_t pwm_min;
uint32_t pwm_max;
uint32_t compare_val;

pwm_min = (htim1.Init.Period * 5) / 100;
pwm_max = (htim1.Init.Period * 10) / 100;

compare_val = pwm_min +
((adc_val * (pwm_max - pwm_min)) / ADC_MAX_VALUE);

__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, compare_val);
}
/* USER CODE END 4 */

/**
Expand Down
Loading