2025-08-22 2:56 AM - edited 2025-08-22 2:58 AM
NUCLEO-F411RE
I am trying to read data from a ADXL1002Z.
I have set up my board to use PA1 (A1 on the Arduino header) as ADC1_1
/* adc.h */
#ifndef INC_ADC_H_
#define INC_ADC_H_
#include <stdint.h>
void pa1_adc_init(void);
uint32_t adc_read(void);
void start_conversion(void);
#endif /* ADC_H_ */
and...
#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 CR2_CONT (1U<<1)
#define CR1_EOCIE (1U<<5)
#define SR_EOC (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_conversion(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);
}
Use in `main.c`
// I am using a HAL UART & stdio hence the use of printf
#include "adc.h"
uint16_t g_sensor_value;
main()
{
pa1_adc_init();
start_conversion();
while(1)
{
g_sensor_value = adc_read();
printf("%d,\n", (int)g_sensor_value);
HAL_Delay(500);
}
}
I just get noise around the same level from the sensor regardless of its orientation. To test of the ADC was working I put a jumper from PA1 (A1 on the board header) to GND on the board & nothing changed, still the same noise at the same level, connected to 3.3V, still no change.
I have FreeRTOS running that has control over SysTick. To get the HAL_UART working I had to change System Core -> SYS -> Timebase Source to TIM1.
Was this a mistake? Is this affecting how ADC1_1 is behaving?
Solved! Go to Solution.
2025-08-22 4:39 AM
OK after a day & half trying to debug this. I just needed a delay after the print()
2025-08-22 4:39 AM
OK after a day & half trying to debug this. I just needed a delay after the print()