注释以下代码#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; }
时间: 2023-06-19 14:05:13 浏览: 93
这是一段使用 STM32F10x 微控制器的 C 语言代码,它包含了几个头文件和一些变量的定义。其中,宏定义了压力和电压的最小值和最大值,state 和 val 分别用于保存状态和数值,value_AD 是 AD 转换后的数值,PRESS_AO 和 VOLTAGE_AO 分别是压力和电压的模拟输出值。
在主函数中,首先初始化了延时、NVIC、串口和 ADC,然后进入一个 while 循环,不断获取 ADC 转换后的数值并将其转换为对应的压力和电压数值,并通过串口输出。其中,使用了一个自定义函数 map,用于将输入值在两个区间之间映射。循环中添加了一个延时以降低 CPU 占用率。
相关问题
#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; }将该代码从stm32中移植到bl618中进行改写,要求使用头文件#include "bflb_adc.h" #include "bflb_mtimer.h" #include "board.h",使用通道2struct bflb_adc_channel_s chan[] = { { .pos_chan = ADC_CHANNEL_2, .neg_chan = ADC_CHANNEL_GND },
#include "bflb_adc.h"
#include "bflb_mtimer.h"
#include "board.h"
#define PRESS_MIN 20
#define PRESS_MAX 6000
#define VOLTAGE_MIN 150
#define VOLTAGE_MAX 3300
struct bflb_adc_channel_s chan[] = {
{
.pos_chan = ADC_CHANNEL_2,
.neg_chan = ADC_CHANNEL_GND
}
};
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)
{
bflb_platform_init();
bflb_adc_init();
bflb_adc_channel_config(chan, ARRAY_SIZE(chan));
bflb_adc_enable();
bflb_mtimer_init();
printf("Test start\n");
while (1) {
bflb_adc_get_data(chan, ARRAY_SIZE(chan), &value_AD);
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 value = %d, voltage = %d mV, pressure = %ld g\n", value_AD, VOLTAGE_AO, PRESS_AO);
bflb_mdelay(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;
}
注释该代码#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; }
// 包含头文件
#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
NVIC_Configuration();
// 初始化串口
uart_init(9600);
// 初始化ADC
Adc_Init();
delay_ms(1000);
printf("Test start\r\n");
while(1) {
// 获取ADC采样值
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);
// 延时500ms
delay_ms(500);
}
}
// 实现map函数
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;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)