0% found this document useful (0 votes)
6 views2 pages

UART and ADC Integration Example

The document contains a C program for an embedded system that initializes ADC and UART peripherals. It continuously reads ADC values, maps them to PWM duty cycle, and transmits the data via UART. Additionally, it includes a callback function to control a GPIO pin based on received UART data ('1' to turn on and '0' to turn off).

Uploaded by

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

UART and ADC Integration Example

The document contains a C program for an embedded system that initializes ADC and UART peripherals. It continuously reads ADC values, maps them to PWM duty cycle, and transmits the data via UART. Additionally, it includes a callback function to control a GPIO pin based on received UART data ('1' to turn on and '0' to turn off).

Uploaded by

hammemi imen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include "main.

h"
#define BUFFER_LEN 1
ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart1;
uint8_t RX_BUFFER[BUFFER_LEN] = {0};
uint8_t TX_BUFFER[BUFFER_LEN] = {0};
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
uint16_t AD_RES = 0;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, RX_BUFFER, BUFFER_LEN);
while (1)
{
// Start ADC Conversion
HAL_ADC_Start(&hadc1);
// Poll ADC1 Perihperal & TimeOut = 1mSec
HAL_ADC_PollForConversion(&hadc1, 1);
// Read The ADC Conversion Result & Map It To PWM DutyCycle
AD_RES = HAL_ADC_GetValue(&hadc1);
TX_BUFFER[0] = AD_RES>>4;
HAL_UART_Transmit(&huart1, TX_BUFFER, sizeof(TX_BUFFER), 100);
HAL_Delay(25);
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == [Link])
{
if(RX_BUFFER[0] == '1')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, 1);
}
else if(RX_BUFFER[0] == '0')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, 0);
}
HAL_UART_Receive_IT(&huart1, RX_BUFFER, BUFFER_LEN);
}
}

You might also like