Ex.
no: User space to test the (ADC) microcontroller
Date:
AIM:
To write a user space to test the (ADC) microcontroller.
ALGORITHM:
Step 1: Configure the ADC GPIO pin
-Enable clock access to GPIOA.
- Set the mode of PAI to analog.
Step 2: Configure the ADC module
- Enable clock access to ADC.
Step 3: Conversion sequence start.
-In microcontroller ADC conversion sequences,analog signals are
transformed into digital.
Step 4: Conversion sequence length.
-refers to the number of sampling and conversion stages involved in
transforming an analog input signal into digital data.
Step 5: Enable ADC module.
-Activate the ADC module in the microcontroller to enable analog-to-digital
conversion.
Step 6:Enable continuous conversion.
-uninterrupted analog-to-digital signal processing.
Step 7: Start adc conversion.
-Initiate ADC conversion in the microcontroller to begin analog-to-digital
signal acquisition.
Step 8: Wait fot conversion to be complete.
-ADC conversion in the microcontroller before proceeding with further
operations.
Step 9: Read converted result.
-Retrieve the converted result from the microcontroller ADC after the
conversion process.
PROGRAM:
ADC.C
#include "stm32f4xx.h"
#include "adc.h"
#define GPIOAEN (1U<<0)
#define ADC1EN (1U<<8)
#define ADC_CH1 (1U<<0)
#define ADC_SEQ_LEN_1 0x00
#define CR2_AD0N (1U<<0)
#define CR2_SWSTART (1U<<30)
#define SR_EOC (1U<<1)
#define CR2_CONT (1U<<1)
void pa1_adc_init(void)
/***Configure the ADC GPIO pin ***/
/*Enable clock access to GPIOA*/
RCC->AHB1ENR |= GPIOAEN;
/*Set the mode of PA1 to analog*/
GPIOA->MODER |=(1U<<2);
GPIOA->MODER |=(1U<<3);
/***Configure the ADC module***/
/*Enable clock access to ADC */
RCC->APB2ENR |= ADC1EN;
/*Conversion sequence start*/
ADC1->SQR3 = ADC_CH1;
/*Conversion sequence length*/
ADC1->SQR1 = ADC_SEQ_LEN_1;
/*Enable ADC module*/
ADC1->CR2 |= CR2_AD0N;
void start_converstion(void)
/*Enable continuous conversion*/
ADC1->CR2 |= CR2_CONT;
/*Start adc conversion*/
ADC1->CR2 |= CR2_SWSTART;
uint32_t adc_read(void)
/*Wait for conversion to be complete*/
while(!(ADC1->SR & SR_EOC)){}
/*Read converted result*/
return (ADC1->DR);
ADC.h
#ifndef ADC_H_
#define ADC_H_
#include <stdint.h>
void pa1_adc_init(void);
uint32_t adc_read(void);
void start_converstion(void);
#endif
main.c:
#include <stdio.h>
#include <stdint.h>
#include "stm32f4xx.h"
#include "uart.h"
#include "adc.h"
uint32_t sensor_value;
int main(void)
uart2_tx_init();
pa1_adc_init();
start_converstion();
while(1)
sensor_value = adc_read();
printf("Sensor value : %d \n\r",(int)sensor_value);
UART.C
#include "uart.h"
#define GPIOAEN (1U<<0)
#define UART2EN (1U<<17)
#define CR1_TE (1U<<3)
#define CR1_RE (1U<<2)
#define CR1_UE (1U<<13)
#define SR_TXE (1U<<7)
#define SR_RXNE (1U<<5)
#define SYS_FREQ 16000000
#define APB1_CLK SYS_FREQ
#define UART_BAUDRATE 115200
static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,
uint32_t BaudRate);
static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate);
void uart2_write(int ch);
int __io_putchar(int ch)
uart2_write(ch);
return ch;
void uart2_rxtx_init(void)
/****************Configure uart gpio pin***************/
/*Enable clock access to gpioa */
RCC->AHB1ENR |= GPIOAEN;
/*Set PA2 mode to alternate function mode*/
GPIOA->MODER &=~(1U<<4);
GPIOA->MODER |= (1U<<5);
/*Set PA2 alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<8);
GPIOA->AFR[0] |= (1U<<9);
GPIOA->AFR[0] |= (1U<<10);
GPIOA->AFR[0] &= ~(1U<<11);
/*Set PA3 mode to alternate function mode*/
GPIOA->MODER &=~(1U<<6);
GPIOA->MODER |= (1U<<7);
/*Set PA3 alternate function type to UART_RX (AF07)*/
GPIOA->AFR[0] |= (1U<<12);
GPIOA->AFR[0] |= (1U<<13);
GPIOA->AFR[0] |= (1U<<14);
GPIOA->AFR[0] &= ~(1U<<15);
/****************Configure uart module ***************/
/*Enable clock access to uart2 */
RCC->APB1ENR |= UART2EN;
/*Configure baudrate*/
uart_set_baudrate(USART2,APB1_CLK,UART_BAUDRATE);
/*Configure the transfer direction*/
USART2->CR1 = (CR1_TE | CR1_RE);
/*Enable uart module*/
USART2->CR1 |= CR1_UE;
void uart2_tx_init(void)
/****************Configure uart gpio pin***************/
/*Enable clock access to gpioa */
RCC->AHB1ENR |= GPIOAEN;
/*Set PA2 mode to alternate function mode*/
GPIOA->MODER &=~(1U<<4);
GPIOA->MODER |= (1U<<5);
/*Set PA2 alternate function type to UART_TX (AF07)*/
GPIOA->AFR[0] |= (1U<<8);
GPIOA->AFR[0] |= (1U<<9);
GPIOA->AFR[0] |= (1U<<10);
GPIOA->AFR[0] &= ~(1U<<11);
/****************Configure uart module ***************/
/*Enable clock access to uart2 */
RCC->APB1ENR |= UART2EN;
/*Configure baudrate*/
uart_set_baudrate(USART2,APB1_CLK,UART_BAUDRATE);
/*Configure the transfer direction*/
USART2->CR1 = CR1_TE;
/*Enable uart module*/
USART2->CR1 |= CR1_UE;
char uart2_read(void)
/*Make sure the receive data register is not empty*/
while(!(USART2->SR & SR_RXNE)){}
/*Read data*/
return USART2->DR;
void uart2_write(int ch)
/*Make sure the transmit data register is empty*/
while(!(USART2->SR & SR_TXE)){}
/*Write to transmit data register*/
USART2->DR = (ch & 0xFF);
static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,
uint32_t BaudRate)
USARTx->BRR = compute_uart_bd(PeriphClk,BaudRate);
static uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)
return ((PeriphClk + (BaudRate/2U))/BaudRate);
UART.h
#ifndef UART_H_
#define UART_H_
#include <stdint.h>
#include "stm32f4xx.h"
void uart2_tx_init(void);
char uart2_read(void);
void uart2_rxtx_init(void);
#endif /* UART_H_ */
OUTPUT:
RESULT: