这次提交基于FreeRTOS能正常运行。

还存在微笑问题,由于CO2传感器必须通过看门狗重启一次后才能正常,这个问题还没解决。(喂狗只在CO2函数内,保证能重启RP2040)
This commit is contained in:
aixiao 2024-08-11 22:03:46 +08:00
parent 60542be965
commit 1e7e438091
10 changed files with 74 additions and 139 deletions

View File

@ -5,150 +5,48 @@
QueueHandle_t xQueue;
#define MAX_TIMINGS 85
void ds18b20_init() {
gpio_init(DS18B20_PIN);
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 1); // 上拉电阻
}
void ds18b20_write_bit(uint8_t bit) {
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(bit ? 10 : 60);
gpio_put(DS18B20_PIN, 1);
sleep_us(bit ? 55 : 5);
}
uint8_t ds18b20_read_bit() {
uint8_t bit = 0;
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(3);
gpio_set_dir(DS18B20_PIN, GPIO_IN);
sleep_us(10);
bit = gpio_get(DS18B20_PIN);
sleep_us(50);
return bit;
}
void ds18b20_write_byte(uint8_t byte) {
for (int i = 0; i < 8; i++) {
ds18b20_write_bit(byte & 0x01);
byte >>= 1;
}
}
uint8_t ds18b20_read_byte() {
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
byte >>= 1;
if (ds18b20_read_bit()) {
byte |= 0x80;
}
}
return byte;
}
int ds18b20_reset() {
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(480);
gpio_put(DS18B20_PIN, 1);
gpio_set_dir(DS18B20_PIN, GPIO_IN);
sleep_us(70);
int presence = !gpio_get(DS18B20_PIN);
sleep_us(410);
return presence;
}
float ds18b20_read_temperature() {
if (!ds18b20_reset()) {
printf("DS18B20 not found\n");
return -1;
}
ds18b20_write_byte(0xCC); // 跳过ROM指令
ds18b20_write_byte(0x44); // 启动温度转换
sleep_ms(750); // 温度转换时间,具体时间请参考传感器数据手册
if (!ds18b20_reset()) {
printf("DS18B20 not found\n");
return -1;
}
ds18b20_write_byte(0xCC); // 跳过ROM指令
ds18b20_write_byte(0xBE); // 读取暂存器
uint8_t lsb = ds18b20_read_byte();
uint8_t msb = ds18b20_read_byte();
int16_t temp = (msb << 8) | lsb;
// 检查传感器数据有效性
if (temp == 0xFFFF) {
printf("Invalid temperature data\n");
return -1;
}
return temp / 16.0; // 转换为摄氏度
}
// 温度传感器
void DS18B20(void *pvParameters)
{
// One_wire 第三方库方法
float TEMPERATURE = -1;
One_wire one_wire(DS18B20_PIN);
one_wire.init();
rom_address_t address {
};
_printTaskStackHighWaterMark("DS18B20");
while (1) {
one_wire.single_device_read_rom(address);
one_wire.convert_temperature(address, true, false);
TEMPERATURE = one_wire.temperature(address);
sleep_ms(300);
printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
sleep_ms(1000);
printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 Temperature: %3.1f°C\n", \
address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], \
address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
if (TEMPERATURE != 85) {
char TEMPERATURE_TEMP[256] = { 0 };
char TEMPERATURE_TEMP[BUFER] = { 0 };
sprintf(TEMPERATURE_TEMP, "Temperature: %.3f°C\n", TEMPERATURE);
_HC_12(TEMPERATURE_TEMP);
//TEMPERATURE = -1;
TEMPERATURE = -1;
}
/*
// 发送数据到队列
xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(1000));
_printTaskStackHighWaterMark("DS18B20");
watchdog_update(); // 喂狗
*/
//_printTaskStackHighWaterMark("DS18B20");
//watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
/*
ds18b20_init();
while (1) {
float temperature = ds18b20_read_temperature();
if (temperature != -1) {
printf("DS18B20 Temperature: %.2f°C\n", temperature);
} else {
printf("Failed to read temperature\n");
}
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
*/
return;
return ;
}

View File

@ -15,6 +15,7 @@
#include "../../SOFTWARE/Lib/pico-onewire/api/one_wire.h"
#define DS18B20_PIN 15 // DS18B20 引脚
#define BUFER 256
extern QueueHandle_t xQueue;
extern void DS18B20(void *pvParameters);

View File

@ -1,5 +1,6 @@
#include "MHZ14B.hpp"
#include "common.hpp"
#include "HC-12.hpp"
void MH_Z14B_INIT()
{
@ -9,12 +10,13 @@ void MH_Z14B_INIT()
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART1, false, false);
uart_set_format(UART1, DATA_BITS, STOP_BITS, PARITY);
return ;
}
// CO2
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
{
// 0x86 读气体浓度值
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
uart_write_blocking(UART1, CMD, 9);
@ -28,7 +30,8 @@ static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
uint16_t CO2_CONC = (256 * CO2_DATA[2]) + CO2_DATA[3];
// 校验
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + \
CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
if (CO2_DATA[8] == CHECKSUM && CO2_DATA[1] == 0x86) {
//printf("CHECKSUM: %X = %X\n", CO2_DATA[8], CHECKSUM);
@ -49,6 +52,7 @@ static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
printf("CO2 concentration reading failed!\n");
}
return CO2_CONC;
}
@ -63,11 +67,18 @@ void CO2(void *pvParameters)
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
char CO2_DATA_TEMP[BUFER] = { 0 };
sprintf(CO2_DATA_TEMP, "CO2 Concentration: %d ppm\n", CO2_DATA);
_HC_12(CO2_DATA_TEMP);
}
//_printTaskStackHighWaterMark("CO2");
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
printf("\n");
}
return ;
}

View File

@ -26,6 +26,7 @@
#define UART1_TX_PIN 5 // MH-Z14B T
#define UART1_RX_PIN 4 // MH-Z14B R
#define BUFER 256
extern void CO2(void *pvParameters);
#endif

View File

@ -1,5 +1,6 @@
#include "ZC13.hpp"
#include "common.hpp"
#include "HC-12.hpp"
int ZC13_INIT()
{
@ -33,7 +34,7 @@ int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
if (uart_rx_program_available(pio, sm)) {
c = uart_rx_program_getc(pio, sm);
DATA[received_count++] = c;
//printf("0x%X ", c);
printf("0x%X ", c);
if (c == '\n') {
// 接收到换行符,停止接收数据
break;
@ -66,8 +67,8 @@ int ZC13(const char *model)
// 接收指令
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
//printf("\n");
// printf("%d\n", CH4_DATA_LEN);
printf("\n");
printf("%d\n", CH4_DATA_LEN);
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
@ -104,13 +105,19 @@ int ZC13(const char *model)
void CH4(void *pvParameters)
{
ZC13_INIT();
_printTaskStackHighWaterMark("ZC13");
_printTaskStackHighWaterMark("CH4");
while (1) {
while (1)
{
ZC13("ZC05");
//_printTaskStackHighWaterMark("ZC13");
watchdog_update(); // 喂狗
char CH4_DATA[BUFER] = { 0 };
sprintf(CH4_DATA, "CH4 Concentration: %d\n", ZC13("ZC05"));
_HC_12(CH4_DATA);
//_printTaskStackHighWaterMark("CH4");
//watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
return ;
}

View File

@ -22,6 +22,7 @@
#define ZC13_PIO_SM_RX 1
#define ZC13_PIO_SERIAL_BAUD 9600
#define BUFER 256
extern void CH4(void *pvParameters);
#endif

View File

@ -1,4 +1,5 @@
#include "ZE07CO.hpp"
#include "HC-12.hpp"
#include "common.hpp"
void ZE07CO_INIT()
@ -71,9 +72,15 @@ void CO(void *pvParameters)
CO_DATA = ZE07CO(2, &ZE07_CO_DATA_IS_OK);
if (CO_DATA != -1 && ZE07_CO_DATA_IS_OK == 1) {
printf("CO Concentration: %d ppm\n", CO_DATA);
char CO_DATA_TEMP[BUFER] = { 0 };
sprintf(CO_DATA_TEMP, "CO Concentration: %d ppm\n", CO_DATA);
_HC_12(CO_DATA_TEMP);
}
//_printTaskStackHighWaterMark("CO");
watchdog_update(); // 喂狗
//watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}

View File

@ -26,6 +26,7 @@
#define UART0_TX_PIN 1 // T
#define UART0_RX_PIN 0 // R
#define BUFER 256
extern void CO(void *pvParameters);
#endif

View File

@ -49,7 +49,7 @@ void Led_Blinky(void *pvParameters)
// 处理接收到的数据
printf("%f\n", TEMPERATURE);
}
//_printTaskStackHighWaterMark("Led_Blinky");
_printTaskStackHighWaterMark("Led_Blinky");
}
}
@ -61,7 +61,7 @@ void CPU(void *pvParameters)
adc_set_temp_sensor_enabled(true);
adc_select_input(4); // Input 4 is the onboard temperature sensor.
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
_printTaskStackHighWaterMark("CPU_");
while (1) {
const float conversionFactor = 3.3f / (1 << 12);
@ -71,7 +71,12 @@ void CPU(void *pvParameters)
printf("CPU temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
char ONBOARD_TEMPERATURE_TEMP[BUFER] = { 0 };
sprintf(ONBOARD_TEMPERATURE_TEMP, "Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
_HC_12(ONBOARD_TEMPERATURE_TEMP);
_printTaskStackHighWaterMark("CPU_");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
@ -89,7 +94,7 @@ int main(void)
printf("Clean boot\n");
}
watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒)
watchdog_start_tick(12);
//watchdog_start_tick(12);
// 创建队列
xQueue = xQueueCreate(10, sizeof(long));
@ -125,7 +130,7 @@ int main(void)
}
// CH4
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
xReturned = xTaskCreate(CH4, "CH4 task", 2048, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CH4() Task Error!");
}
@ -137,11 +142,12 @@ int main(void)
}
// CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
xReturned = xTaskCreate(CO2, "CO2 task", 2048, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CO2() Task Error!");
}
vTaskStartScheduler();
while (1) {};
return 0;

View File

@ -14,4 +14,6 @@
#include "hardware/pwm.h"
#include "hardware/adc.h"
#define BUFER 256
#endif