#include "bflb_adc.h" #include "bflb_mtimer.h" #include "board.h" struct bflb_device_s *adc; #define TEST_ADC_CHANNELS 2 #define TEST_COUNT 10 struct bflb_adc_channel_s chan[] = { { .pos_chan = ADC_CHANNEL_2, .neg_chan = ADC_CHANNEL_GND }, { .pos_chan = ADC_CHANNEL_GND, .neg_chan = ADC_CHANNEL_3 }, }; int main(void) { board_init(); board_adc_gpio_init(); adc = bflb_device_get_by_name("adc"); /* adc clock = XCLK / 2 / 32 */ struct bflb_adc_config_s cfg; cfg.clk_div = ADC_CLK_DIV_32; cfg.scan_conv_mode = true; cfg.continuous_conv_mode = false; cfg.differential_mode = true; cfg.resolution = ADC_RESOLUTION_16B; cfg.vref = ADC_VREF_3P2V; bflb_adc_init(adc, &cfg); bflb_adc_channel_config(adc, chan, TEST_ADC_CHANNELS); for (uint32_t i = 0; i < TEST_COUNT; i++) { bflb_adc_start_conversion(adc); while (bflb_adc_get_count(adc) < TEST_ADC_CHANNELS) { bflb_mtimer_delay_ms(1); } for (size_t j = 0; j < TEST_ADC_CHANNELS; j++) { struct bflb_adc_result_s result; uint32_t raw_data = bflb_adc_read_raw(adc); printf("raw data:%08x\r\n", raw_data); bflb_adc_parse_result(adc, &raw_data, &result, 1); printf("pos chan %d,neg chan %d,%d mv \r\n", result.pos_chan, result.neg_chan, result.millivolt); } bflb_adc_stop_conversion(adc); bflb_mtimer_delay_ms(100); } while (1) { } }学习该代码
时间: 2023-06-17 14:05:52 浏览: 128
01.库表说明(旧版)1
这是一段使用开发板上的ADC模块进行模拟量采集的代码。先对ADC进行配置,包括时钟分频、扫描模式、连续转换模式、差分模式、分辨率和参考电压等参数。然后设置ADC的通道,这里设置了两个通道,一个是正通道是ADC_CHANNEL_2,负通道是ADC_CHANNEL_GND,另一个是正通道是ADC_CHANNEL_GND,负通道是ADC_CHANNEL_3。接下来进行采样,先启动转换,等待采样完成,然后读取采样数据,解析出通道号和电压值。最后停止转换,等待一段时间后再次进行采样。
阅读全文