注释该代码#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 07:04:11 浏览: 113
// 包含头文件
#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;
}
阅读全文