该代码在没有头文件bl_delay.h的情况下怎么修改并给出完整代码#include "bl_mcu_sdk.h"#include "bl_gpio.h"#include "bl_adc.h"#include "bl_uart.h"#include "bl_delay.h"#define PRESS_MIN 20#define PRESS_MAX 6000#define VOLTAGE_MIN 150#define VOLTAGE_MAX 3300long map(long x, long in_min, long in_max, long out_min, long out_max);int main(void){ // 初始化延时函数 bl_delay_init(); // 初始化串口 bl_uart_init(0, 9600, UART_BITWIDTH_8BIT, UART_STOP_BIT_1, UART_PARITY_DISABLE); // 初始化ADC bl_adc_init(ADC_CLK_DIV_2, ADC_RESOLUTION_12BIT, ADC_SCALE_4096, ADC_INPUT_MODE_SINGLE, ADC_ATVCC); bl_delay_ms(1000); bl_uart_printf("Test start\n"); while(1) { // 获取ADC采样值 uint16_t value_AD = bl_adc_single_get_value(ADC_0, ADC_CHANNEL_1); // 转换为电压值 int 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); } // 输出结果 bl_uart_printf("AD value = %d, voltage = %d mV, pressure = %ld g\n", value_AD, VOLTAGE_AO, PRESS_AO); // 延时500ms bl_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;}
时间: 2023-06-19 10:03:41 浏览: 151
如果没有头文件 bl_delay.h,可以将 bl_delay_ms(1000) 和 bl_delay_ms(500) 分别改为延时函数:
```c
void delay_ms(uint32_t ms){
uint32_t i, j;
for(i = 0; i < ms; i++){
for(j = 0; j < 10000; j++);
}
}
int main(void){
// 初始化串口
bl_uart_init(0, 9600, UART_BITWIDTH_8BIT, UART_STOP_BIT_1, UART_PARITY_DISABLE);
// 初始化ADC
bl_adc_init(ADC_CLK_DIV_2, ADC_RESOLUTION_12BIT, ADC_SCALE_4096, ADC_INPUT_MODE_SINGLE, ADC_ATVCC);
delay_ms(1000);
bl_uart_printf("Test start\n");
while(1){
// 获取ADC采样值
uint16_t value_AD = bl_adc_single_get_value(ADC_0, ADC_CHANNEL_1);
// 转换为电压值
int 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);
}
// 输出结果
bl_uart_printf("AD value = %d, voltage = %d mV, pressure = %ld g\n", value_AD, VOLTAGE_AO, PRESS_AO);
// 延时500ms
delay_ms(500);
}
}
```
阅读全文