#include "stm32c0xx_hal.h"
#include <stdio.h> // Required for printf
#include <stdint.h>
#include <string.h>
// Define pin connections
#define SERVO_PIN GPIO_PIN_6 // PA6 as PWM output for servo (TIM3_CH1)
#define SERVO_PORT GPIOA
#define ADC_CHANNEL ADC_CHANNEL_0 // PA0 as ADC input from potentiometer
// Peripheral Handles
ADC_HandleTypeDef hadc;
TIM_HandleTypeDef htim3;
UART_HandleTypeDef huart2; // UART handle for Serial Monitor/Debug output
// Global Variables
uint32_t adcValue = 0;
uint32_t pulseWidth = 0;
// Servo PWM Definitions (Based on 16 MHz HSI, 1 MHz Timer Clock)
// Timer Clock (Tclk) = 16 MHz / (Prescaler + 1) = 16MHz / (15 + 1) = 1 MHz
// 1 tick = 1 microsecond (us)
#define SERVO_MIN_PULSE 500 // Min pulse width (0 degrees): 0.5ms pulse -> 500 ticks
#define SERVO_MAX_PULSE 2400 // Max pulse width (180 degrees): 2.4ms pulse -> 2400 ticks
#define SERVO_PERIOD 20000 // PWM period (us): 20ms period (50Hz) -> 20000 ticks
// Function Prototypes
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_ADC_Init(void);
void MX_TIM3_Init(void);
void MX_USART2_UART_Init(void); // UART init for serial monitor
void App_Error_Handler(void);
// ====================================================================
// Standard Redirection for printf (required for Wokwi Serial Monitor)
// ====================================================================
#ifdef _GNUC_
int __io_putchar(int ch)
{
// Transmit character over UART2
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#else
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif
// ====================================================================
// MAIN APPLICATION
// ====================================================================
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC_Init();
MX_TIM3_Init();
MX_USART2_UART_Init(); // Initialize UART for Serial Output
// Start the PWM signal generation on TIM3 Channel 1
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
printf("--- Servo ADC Controller Started ---\r\n");
printf("ADC Range: 0-4095 | Pulse Range: %lu-%lu ticks\r\n", SERVO_MIN_PULSE, SERVO_MAX_PULSE);
while (1)
{
// 1. Read ADC Value
HAL_ADC_Start(&hadc);
// Wait indefinitely for the conversion to complete
if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) == HAL_OK)
{
adcValue = HAL_ADC_GetValue(&hadc);
}
// NOTE: HAL_ADC_Stop is not strictly necessary here since we restart it
// on the next loop iteration, but leaving it in as it was in the original structure.
HAL_ADC_Stop(&hadc);
// 2. Map ADC value (0-4095) to Servo Pulse Width (500us - 2400us)
// Note: The integer math provides sufficient resolution for servo control.
pulseWidth = SERVO_MIN_PULSE + (adcValue * (SERVO_MAX_PULSE - SERVO_MIN_PULSE) / 4095U);
// 3. Set the new PWM capture/compare value
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pulseWidth);
// 4. Print values to Serial Monitor
printf("ADC: %4lu | Pulse: %4lu ticks\r\n", adcValue, pulseWidth);
// 5. Wait a short period
HAL_Delay(20); // Delay for 20ms (one servo cycle)
}
}
// ====================================================================
// PERIPHERAL INITIALIZATION FUNCTIONS
// ====================================================================
/**
* @brief System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Configure HSI as the main clock source
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
App_Error_Handler();
}
// Configure the system clock, AHB and APB bus clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; // 16 MHz
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
App_Error_Handler();
}
}
/**
* @brief GPIO Initialization
* Configures PA6 (TIM3_CH1) and PA2/PA3 (USART2).
*/
void MX_GPIO_Init(void)
{
// Enable GPIOA clock
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Configure PA6 as alternate function (TIM3 CH1 PWM)
GPIO_InitStruct.Pin = SERVO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM3; // PA6 maps to TIM3_CH1 on AF1
HAL_GPIO_Init(SERVO_PORT, &GPIO_InitStruct);
// Configure PA2 (USART2_TX) and PA3 (USART2_RX) for Alternate Function
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2; // PA2/PA3 maps to USART2 on AF1
HAL_GPIO_Init(SERVO_PORT, &GPIO_InitStruct);
}
/**
* @brief ADC Initialization (ADC1)
* Configures the ADC for 12-bit resolution and single-conversion mode on Channel 0 (PA0).
*/
void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable ADC clock and GPIOA for PA0 (ADC input)
__HAL_RCC_ADC_CLK_ENABLE();
// Configure PA0 as Analog input
GPIO_InitStruct.Pin = GPIO_PIN_0; // PA0 is ADC Channel 0
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hadc.Instance = ADC1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_12CYCLES_5;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
App_Error_Handler();
}
// Configure ADC Channel 0 (PA0)
sConfig.Channel = ADC_CHANNEL;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
App_Error_Handler();
}
}
/**
* @brief Timer3 Initialization for PWM (channel 1, PA6)
* Configures TIM3 for 50 Hz PWM with a 1 MHz counter frequency.
*/
void MX_TIM3_Init(void)
{
TIM_OC_InitTypeDef sConfigOC = {0};
__HAL_RCC_TIM3_CLK_ENABLE();
htim3.Instance = TIM3;
// PCLK1 = 16 MHz. Prescaler = (16MHz / 1MHz) - 1 = 15.
htim3.Init.Prescaler = 15;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = SERVO_PERIOD - 1; // 19999 ticks for 20ms (50 Hz)
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
App_Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = SERVO_MIN_PULSE; // Initial pulse width
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
App_Error_Handler();
}
}
/**
* @brief USART2 Initialization Function
* For debugging/Serial Monitor at 115200 baud.
*/
void MX_USART2_UART_Init(void)
{
__HAL_RCC_USART2_CLK_ENABLE(); // Enable UART2 clock
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
App_Error_Handler();
}
}
/**
* @brief Custom Error Handler
*/
void App_Error_Handler(void)
{
// In a real application, you might blink an LED or log an error.
// Here, we halt the program.
__disable_irq();
while (1)
{
// Program stuck here if a HAL error occurs
}
}