Open errolt opened 1 year ago
An example for ADC vbat31 channel sampling and data converting
#include <stdio.h>
#include <stdarg.h>
#include "tremo_uart.h"
#include "tremo_gpio.h"
#include "tremo_rcc.h"
#include "tremo_regs.h"
#include "tremo_adc.h"
#include "tremo_delay.h"
#include "tremo_flash.h"
#define ADC_SAMPLE_NUM 10
#define VBAT_FACTOR 3.06f
uint16_t adc_data[ADC_SAMPLE_NUM] = {0};
float calibrated_sample[ADC_SAMPLE_NUM] = {0.0};
float vbat = 0.0f;
void adc_continue_mode_test(void)
{
uint32_t i;
float gain_value = 1.188f;
float dco_value = -0.107f;
adc_get_calibration_value(false, &gain_value, &dco_value);
adc_enable_vbat31(true);
adc_init();
delay_us(100);
adc_config_clock_division(20); //sample rate 75K
adc_config_sample_sequence(0, 15);
adc_config_conv_mode(ADC_CONV_MODE_CONTINUE);
adc_enable(true);
adc_start(true);
for (i = 0; i < ADC_SAMPLE_NUM; i++)
{
while(!adc_get_interrupt_status(ADC_ISR_EOC));
adc_data[i] = adc_get_data();
}
adc_start(false);
adc_enable(false);
printf("gain:%f dco: %f\r\n", gain_value, dco_value);
for (i = 0; i < ADC_SAMPLE_NUM; i++)
{//calibration and convert sample value
calibrated_sample[i] = ((1.2/4096) * adc_data[i] - dco_value) / gain_value;
printf("VBAT31: %0.3fv \r\n", calibrated_sample[i] );
vbat += calibrated_sample[i] * VBAT_FACTOR;
}
vbat /= ADC_SAMPLE_NUM;
printf("VBAT: %0.3fv \r\n", vbat );
}
void uart_log_init(void)
{
// uart0
gpio_set_iomux(GPIOB, GPIO_PIN_0, 1);
gpio_set_iomux(GPIOB, GPIO_PIN_1, 1);
/* uart config struct init */
uart_config_t uart_config;
uart_config_init(&uart_config);
uart_config.baudrate = UART_BAUDRATE_115200;
uart_init(CONFIG_DEBUG_UART, &uart_config);
uart_cmd(CONFIG_DEBUG_UART, ENABLE);
}
int main(void)
{
rcc_set_adc_clk_source(RCC_ADC_CLK_SOURCE_PCLK1);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_UART0, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOA, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOB, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOC, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOD, true);
rcc_enable_peripheral_clk(RCC_PERIPHERAL_ADC, true);
uart_log_init();
printf("adc vbat31 demo\r\n");
adc_continue_mode_test();
/* Infinite loop */
while (1) { }
}
Hi ASR,
Where do I find a sample for adc_enable_vbat31()?
Thank you, Errol