darts/User/task/ForceSensor.c
2026-02-15 23:43:19 +08:00

57 lines
1.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
ForceSensor Task
力传感器
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/ADS1115.h"
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
int16_t adcDataTemp[4] = {0};
float SensorData[4] = {0}; // 定义传感器数据数组
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_ForceSensor(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / FORCESENSOR_FREQ;
osDelay(FORCESENSOR_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
//单次
ADS1115_UserConfig_Single();
ADS1115_ScanChannel(0);// 让通道0开始第一次转换内部会设置 OS=Start 并写入
//连续
//ADS1115_UserConfig_Continue();
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
static uint8_t chan = 0;
if(ADS1115_ReadRawData(&adcDataTemp[chan]) != 0) {
SensorData[chan] = ADS1115_RawDataToVoltage(adcDataTemp[chan]);
}
chan++;if(chan >= 2)chan=0;
ADS1115_ScanChannel(chan);
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}