#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 13:05:52 浏览: 137
这是一段使用开发板上的ADC模块进行模拟量采集的代码。先对ADC进行配置,包括时钟分频、扫描模式、连续转换模式、差分模式、分辨率和参考电压等参数。然后设置ADC的通道,这里设置了两个通道,一个是正通道是ADC_CHANNEL_2,负通道是ADC_CHANNEL_GND,另一个是正通道是ADC_CHANNEL_GND,负通道是ADC_CHANNEL_3。接下来进行采样,先启动转换,等待采样完成,然后读取采样数据,解析出通道号和电压值。最后停止转换,等待一段时间后再次进行采样。
相关问题
注释以下每一行代码#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) { } }
这段代码是使用芯片上的ADC模块读取外部电压信号,并将读取结果打印出来。具体注释如下:
```
#include "bflb_adc.h" // 包含 ADC 操作库的头文件
#include "bflb_mtimer.h" // 包含毫秒级延时库的头文件
#include "board.h" // 包含与硬件板子相关的头文件
struct bflb_device_s *adc; // 定义一个指向 ADC 设备的结构体指针
#define TEST_ADC_CHANNELS 2 // 定义测试的 ADC 通道数量为 2
#define TEST_COUNT 10 // 定义测试的次数为 10
struct bflb_adc_channel_s chan[] = { // 定义 ADC 通道的结构体数组
{.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 相关的 GPIO
adc = bflb_device_get_by_name("adc"); // 获取 ADC 设备的结构体指针
/* adc clock = XCLK / 2 / 32 */ // 设置 ADC 的时钟分频为 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); // 初始化 ADC
bflb_adc_channel_config(adc, chan, TEST_ADC_CHANNELS); // 配置 ADC 通道
for (uint32_t i = 0; i < TEST_COUNT; i++) { // 循环读取 ADC 的值
bflb_adc_start_conversion(adc); // 启动 ADC 转换
while (bflb_adc_get_count(adc) < TEST_ADC_CHANNELS) { // 等待 ADC 转换完成
bflb_mtimer_delay_ms(1); // 延时 1 毫秒
}
for (size_t j = 0; j < TEST_ADC_CHANNELS; j++) { // 遍历每个 ADC 通道
struct bflb_adc_result_s result; // 定义保存 ADC 转换结果的结构体
uint32_t raw_data = bflb_adc_read_raw(adc); // 读取 ADC 原始数据
printf("raw data:%08x\r\n", raw_data); // 打印原始数据
bflb_adc_parse_result(adc, &raw_data, &result, 1); // 解析 ADC 转换结果
printf("pos chan %d,neg chan %d,%d mv \r\n", result.pos_chan, result.neg_chan, result.millivolt); // 打印解析后的数据
}
bflb_adc_stop_conversion(adc); // 停止 ADC 转换
bflb_mtimer_delay_ms(100); // 延时 100 毫秒
}
while (1) { // 循环等待
}
}
```
#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) { } }根据以上代码对bl618程序的编写对以下stm32中代码#include "stm32f10x.h" #include "delay.h" #include "FSR.h" #include "usart.h" #include "adc.h" #define PRESS_MIN 20 #define PRESS_MAX 6000 #define VOLTAGE_MIN 150 #define VOLTAGE_MAX 3300 u8 state = 0; u16 val = 0; u16 value_AD = 0; long PRESS_AO = 0; int VOLTAGE_AO = 0; long map(long x, long in_min, long in_max, long out_min, long out_max); int main(void) { delay_init(); NVIC_Configuration(); uart_init(9600); Adc_Init(); delay_ms(1000); printf("Test start\r\n"); while(1) { value_AD = Get_Adc_Average(1,10); VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300); if(VOLTAGE_AO < VOLTAGE_MIN) { PRESS_AO = 0; } else if(VOLTAGE_AO > VOLTAGE_MAX) { PRESS_AO = PRESS_MAX; } else { PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX); } printf("ADÖµ = %d,µçѹ = %d mv,ѹÁ¦ = %ld g\r\n",value_AD,VOLTAGE_AO,PRESS_AO); delay_ms(500); } } long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }移植到bl618进行改写
以下是将上述STM32代码移植到BL618进行改写后的代码:
```c
#include "bl602.h"
#include "bl_adc.h"
#include "bl_gpio.h"
#include "bl_mtimer.h"
#include "stdio.h"
#define PRESS_MIN 20
#define PRESS_MAX 6000
#define VOLTAGE_MIN 150
#define VOLTAGE_MAX 3300
#define ADC_CHANNEL 0
#define ADC_CLK_DIV 5
#define ADC_RESOLUTION ADC_RESOLUTION_12B
#define ADC_VREF ADC_VREF_3V3
long map(long x, long in_min, long in_max, long out_min, long out_max);
int main(void)
{
bl_mtimer_init();
bl_gpio_enable_output(PIN_11, 1);
bl_adc_init();
bl_adc_config_t cfg = {
.inputMode = ADC_INPUT_SINGLE_END_MODE,
.input = ADC_INPUT_CH0,
.clkDiv = ADC_CLK_DIV,
.resolution = ADC_RESOLUTION,
.refSel = ADC_VREF,
};
bl_adc_channel_config(ADC_CHANNEL, &cfg);
printf("Test start\r\n");
while (1)
{
bl_adc_start();
while (!bl_adc_fifo_data_count())
{
bl_mtimer_delay_us(100);
}
uint16_t value_AD = bl_adc_read_fifo_data();
int VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
long PRESS_AO = 0;
if (VOLTAGE_AO < VOLTAGE_MIN)
{
PRESS_AO = 0;
}
else if (VOLTAGE_AO > VOLTAGE_MAX)
{
PRESS_AO = PRESS_MAX;
}
else
{
PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
}
printf("AD value = %d, voltage = %d mV, pressure = %ld g\r\n", value_AD, VOLTAGE_AO, PRESS_AO);
bl_mtimer_delay_us(500000);
}
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
```
在BL618中,需要使用`bl_adc_init()`进行ADC模块的初始化,并通过`bl_adc_channel_config()`配置ADC通道。需要注意的是,BL618的ADC模块不支持差分模式,因此需要将原来的差分模式改为单端模式。在进行ADC转换时,可以使用`bl_adc_start()`触发转换,并通过`bl_adc_fifo_data_count()`和`bl_adc_read_fifo_data()`获取转换结果。此外,由于BL618中没有提供延时函数,需要使用`bl_mtimer_delay_us()`进行延时。
阅读全文