改用定时器发送传感器数据请求命令和环境参数同步命令。尝试将数据同步到串口屏的数据,改为有差异才同步

main
Zeng wei (曾威) 2 years ago
parent 82badf0a0b
commit 68c33afc88

@ -0,0 +1,6 @@
{
"files.associations": {
"w25q128_ins.h": "c",
"usart.h": "c"
}
}

@ -19,9 +19,11 @@ int max_speed_count = 6000;
void PID_Init() void PID_Init()
{ {
// pid.set_tem=tem;// user set temperature // pid.set_tem=tem;// user set temperature
pid.Kp = 110; // if flash have not a vaild value, just set a default value
pid.Ki = 0.001; if (pid.Kp < 1e-7) { pid.Kp = 110; }
pid.Kd = 340; if (pid.Ki < 1e-7) { pid.Ki = 0.001; }
if (pid.Kd < 1e-7) { pid.Kd = 340; }
pid.t = 500; // PID calc period pid.t = 500; // PID calc period
// pid.Ti=5000000;// integral time // pid.Ti=5000000;// integral time
// pid.Td=1000;// differential time // pid.Td=1000;// differential time

@ -8,19 +8,19 @@ void PID_Calc(void);
typedef struct Pid typedef struct Pid
{ {
float set_tem;//用户设定值 float set_tem;// User settings
float now_tem;//当前温度 float now_tem;// current temperature
float Kp; // 110 float Kp; // 110
int t; //PID计算周期--采样周期
float Ti;
float Td;
float Ki; // 0.001 float Ki; // 0.001
float Kd; // 340 float Kd; // 340
int t; // PID calculation cycle - sampling cycle
float Ti;
float Td;
float Ek; //本次偏差 float Ek; // This deviation
float Ek_1;//上次偏差 float Ek_1;// Last deviation
float SEk; //历史偏差之和 float SEk; // The sum of historical deviations
float Iout; float Iout;
float Pout; float Pout;
@ -32,7 +32,7 @@ typedef struct Pid
int C1ms; int C1ms;
int pwmcycle;//pwm周期 int pwmcycle;// PWM cycle
int times; int times;
}PID; }PID;

@ -2,14 +2,16 @@
#include <stdio.h> #include <stdio.h>
#include "rs485.h" #include "rs485.h"
//u8 rx_buf[64]; //u8 rx_buf[64];
extern u8 RS485_RX_BUF_COPY[128]; extern u8 RS485_RX_BUF_COPY[128];
void bufcut_Init(u8 *RX_BUF,u8 *CUT_RX_BUF,u8 start_index, u8 end_index)
void bufcut_Init(u8 *TARGET_RX_BUF,u8 *SOURCE_CUT_RX_BUF,u8 start_index, u8 end_index)
{ {
int i, j = 0; int i, j = 0;
for (i = start_index; i < end_index; i++) for (i = start_index; i < end_index; i++)
{ {
RX_BUF[j] = CUT_RX_BUF[i]; TARGET_RX_BUF[j] = SOURCE_CUT_RX_BUF[i];
j++; j++;
} }
} }
@ -17,14 +19,14 @@ void bufcut_Init(u8 *RX_BUF,u8 *CUT_RX_BUF,u8 start_index, u8 end_index)
void RX_BUF_Init(void) void RX_BUF_Init(void)
{ {
int i=0; int i=0;
for(i=0;i<64;i++) for(i=0;i<128;i++)
{ {
RS485_RX_BUF[i]=0; RS485_RX_BUF[i]=0;
} }
} }
void RX_BUF_ZERO(u8 num)//把处理过的数组变为0 void RX_BUF_ZERO(u8 num)// set RS485_RX_BUF_COPY to 0
{ {
for(int i=0;i<num;i++) for(int i=0;i<num;i++)
{ {

@ -3,7 +3,7 @@
#include "system.h" #include "system.h"
u8 bufcut_Init(u8 *RX_BUF,u8 *CUT_RX_BUF,u8 start_index, u8 end_index); u8 bufcut_Init(u8 *TARGET_RX_BUF,u8 *SOURCE_CUT_RX_BUF,u8 start_index, u8 end_index);
void RX_BUF_Init(void); void RX_BUF_Init(void);
void RX_BUF_ZERO(u8 num); void RX_BUF_ZERO(u8 num);
void RX_BUF_Transfer(u8 zero,u8 transfer_num); void RX_BUF_Transfer(u8 zero,u8 transfer_num);

@ -4,8 +4,10 @@
#include "Relays.h" #include "Relays.h"
#include "myfreertos.h" #include "myfreertos.h"
u8 prev_sendbuf[37] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
u8 sendbuf[37] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, u8 sendbuf[37] = {0xEE, 0xB5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; // 发送给串口屏的实时数据 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
u8 sendbuf_crc[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, u8 sendbuf_crc[20] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
extern u8 global_buffer[64]; extern u8 global_buffer[64];
@ -55,7 +57,7 @@ extern PID pid;
// extern u8 white_light2; // extern u8 white_light2;
// extern u8 blue_light1; // extern u8 blue_light1;
// extern u8 blue_light2; // extern u8 blue_light2;
// CRC校验 自己后面添加的 // CRC verifies the information added later on
const u8 auchCRCHi[] = { const u8 auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
@ -86,11 +88,29 @@ const u8 auchCRCLo[] = {
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40}; 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40};
u8 RS485_RX_BUF[128]; // 接收缓冲,最大64个字节. u8 RS485_RX_BUF[128];
u8 RS485_RX_CNT = 0; // 接收到的数据长度 u8 RS485_RX_CNT = 0;
u8 RS485_RX_Flag = 0; // 接收到的数据标志 u8 RS485_RX_Flag = 0;
/**
* return 1 if diff, 0 if same
*/
u8 cmp_str(u8 *prev, u8*curr, u8 len) {
for (u8 i = 0; i < len; i++) {
if (prev[i] != curr[i]) {
return 1;
}
}
return 0;
}
void cp_str_to_prev(u8 *prev, u8*curr, u8 len) {
for (u8 i = 0; i < len; i++) {
prev[i] = curr[i];
}
}
void GetCRC16(u8 *puchMsg, u16 usDataLen, u8 *uchCRCHi, u8 *uchCRCLo) void GetCRC16(u8 *puchMsg, u16 usDataLen, u8 *uchCRCHi, u8 *uchCRCLo)
{ {
@ -105,7 +125,10 @@ void GetCRC16(u8 *puchMsg, u16 usDataLen, u8 *uchCRCHi, u8 *uchCRCLo)
} }
// return ((uchCRCHi<< 8) | (uchCRCLo)) ; // return ((uchCRCHi<< 8) | (uchCRCLo)) ;
} }
// 查表法计算CRC值并且校验 /**
* Calculate CRC value using lookup table method and verify
* @return int 1 means success, 0 means false
*/
u8 CRC16_check(u8 *puchMsg, u16 usDataLen) u8 CRC16_check(u8 *puchMsg, u16 usDataLen)
{ {
u8 uchCRCHi = 0xFF; u8 uchCRCHi = 0xFF;
@ -119,7 +142,6 @@ u8 CRC16_check(u8 *puchMsg, u16 usDataLen)
} }
if (uchCRCHi == *puchMsg++ && uchCRCLo == *puchMsg++) if (uchCRCHi == *puchMsg++ && uchCRCLo == *puchMsg++)
{ {
// printf("CRC 校验成功\r\n"); // 调试使用
return 1; return 1;
} }
else else
@ -129,13 +151,13 @@ void USART1_IRQHandler(void)
{ {
u8 res; u8 res;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 接收到数据 if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // receive data
{ {
res = USART_ReceiveData(USART1); // 读取接收到的数据 res = USART_ReceiveData(USART1); // read received data
if (RS485_RX_CNT < 128) if (RS485_RX_CNT < 128)
{ {
RS485_RX_BUF[RS485_RX_CNT] = res; // 记录接收到的值 RS485_RX_BUF[RS485_RX_CNT] = res; // Record received values
RS485_RX_CNT++; // 接收数据增加1 RS485_RX_CNT++; // Received data count increased by 1
} }
} }
} }
@ -143,189 +165,185 @@ void USART1_IRQHandler(void)
void SN74CB3Q3253_Init(void) void SN74CB3Q3253_Init(void)
{ {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PB5 PB6 PB7 端口配置, 推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PB5 PB6 PB7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // IO口速度为50MHz GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // IO??????50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); // 推挽输出 IO口速度为50MHz GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_7); // PB7输出低 GPIO_ResetBits(GPIOB, GPIO_Pin_7); // PB7?????
} }
// RS485_1_Init->J6 PB5->S1,PB6->S0,PB7->USART_OE // RS485_1_Init->J6 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为001使1B2 2B2响应 // SN74CB3Q3253??¡¤?????? - ??¡¤???????????????????OE S1 S0???????001???1B2 2B2???
// 初始化IO 串口1
// pclk1:PCLK1时钟频率(Mhz) // pclk1:PCLK1??????(Mhz)
// bound:波特率 // bound:??????
void RS485_1_Init(u32 bound) void RS485_1_Init(u32 bound)
{ {
SN74CB3Q3253_Init(); SN74CB3Q3253_Init();
GPIO_ResetBits(GPIOB, GPIO_Pin_5); // S1配置为低电平 GPIO_ResetBits(GPIOB, GPIO_Pin_5); // S1?????????
GPIO_SetBits(GPIOB, GPIO_Pin_6); // S0配置为高电平 GPIO_SetBits(GPIOB, GPIO_Pin_6); // S0?????????
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure); USART_Init(USART1, &USART_InitStructure);
; // 初始化串口 ;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 先占优先级2级 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // 使能串口 USART_Cmd(USART1, ENABLE);
RS485_1_TX_EN = 0; // 默认为接收模式 RS485_1_TX_EN = 0;
} }
// RS485_2_Init->J7 PB5->S1,PB6->S0,PB7->USART_OE // RS485_2_Init->J7 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为000使1B1 2B1响应 // SN74CB3Q3253??¡¤?????? - ??¡¤???????????????????OE S1 S0???????000???1B1 2B1???
void RS485_2_Init(u32 bound) void RS485_2_Init(u32 bound)
{ {
SN74CB3Q3253_Init(); SN74CB3Q3253_Init();
GPIO_ResetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6); // S1,S0配置为低电平 GPIO_ResetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure); USART_Init(USART1, &USART_InitStructure);
; // 初始化串口 ;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 先占优先级2级 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // 使能串口 USART_Cmd(USART1, ENABLE);
RS485_2_TX_EN = 0; // 默认为接收模式 RS485_2_TX_EN = 0;
} }
// RS485_3_Init->J7 PB5->S1,PB6->S0,PB7->USART_OE // RS485_3_Init->J7 ??PB5->S1,PB6->S0,PB7->USART_OE
// SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为010使1B3 2B3响应 // SN74CB3Q3253??¡¤?????? - ??¡¤???????????????????OE S1 S0???????010???1B3 2B3???
void RS485_3_Init(u32 bound) void RS485_3_Init(u32 bound)
{ {
SN74CB3Q3253_Init(); SN74CB3Q3253_Init();
GPIO_ResetBits(GPIOB, GPIO_Pin_6); // S0配置为低电平 GPIO_ResetBits(GPIOB, GPIO_Pin_6); // S0?????????
GPIO_SetBits(GPIOB, GPIO_Pin_5); // S1配置为高电平 GPIO_SetBits(GPIOB, GPIO_Pin_5); // S1?????????
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // 端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure); USART_Init(USART1, &USART_InitStructure);
; // 初始化串口 ;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 先占优先级2级 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //???????2??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // 使能串口 USART_Cmd(USART1, ENABLE);
RS485_3_TX_EN = 0; // 默认为接收模式 RS485_3_TX_EN = 0;
} }
// RS485发送len个字节.
// buf:发送区首地址
// len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
void RS485_1_Send_Data(u8 *buf, u8 len) void RS485_1_Send_Data(u8 *buf, u8 len)
{ {
u8 t; u8 t;
RS485_1_TX_EN = 1; // 设置为发送模式 RS485_1_TX_EN = 1;
for (t = 0; t < len; t++) // 循环发送数据 for (t = 0; t < len; t++)
{ {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
; ;
@ -336,16 +354,16 @@ void RS485_1_Send_Data(u8 *buf, u8 len)
; ;
RS485_RX_CNT = 0; RS485_RX_CNT = 0;
RS485_1_TX_EN = 0; // 设置为接收模式 RS485_1_TX_EN = 0;
} }
void RS485_1_Send_Data_1(u8 *buf, u8 len) void RS485_1_Send_Data_1(u8 *buf, u8 len)
{ {
u8 t; u8 t;
RS485_1_TX_EN = 1; // 设置为发送模式 RS485_1_TX_EN = 1;
for (t = 0; t < len; t++) // 循环发送数据 for (t = 0; t < len; t++)
{ {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
; ;
@ -356,13 +374,10 @@ void RS485_1_Send_Data_1(u8 *buf, u8 len)
; ;
// RS485_RX_CNT=0; // RS485_RX_CNT=0;
RS485_1_TX_EN = 0; // 设置为接收模式 RS485_1_TX_EN = 0;
} }
// RS485发送len个字节. void RS485_1_Send_Data_2(void)
// buf:发送区首地址
// len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
void RS485_1_Send_Data_2(void) // 上传温湿度二氧化碳
{ {
sendbuf[0] = 0xEE; sendbuf[0] = 0xEE;
sendbuf[1] = 0xB5; sendbuf[1] = 0xB5;
@ -372,16 +387,18 @@ void RS485_1_Send_Data_2(void) //
sendbuf[35] = 0xFF; sendbuf[35] = 0xFF;
sendbuf[36] = 0xFF; sendbuf[36] = 0xFF;
u8 a; u8 a;
RS485_1_TX_EN = 1; // 设置为发送模式 if (T <= 1000 && T >= -1000)
if (T <= 500000)
{ {
TEM1 = RS485_RX_BUF_COPY[3]; if (T < 0) {
TEM2 = RS485_RX_BUF_COPY[4]; T = (~(T - 1)) & 0xFFFF;
}
TEM1 = (T & 0xFF00) >> 8;
TEM2 = T & 0x00FF;
} }
if (H <= 1000) if (H <= 1000)
{ {
HUM1 = RS485_RX_BUF_COPY[5]; HUM1 = (H & 0xFF00) >> 8;
HUM2 = RS485_RX_BUF_COPY[6]; HUM2 = H & 0x00FF;
} }
sendbuf[3] = TEM1; sendbuf[3] = TEM1;
sendbuf[4] = TEM2; sendbuf[4] = TEM2;
@ -406,19 +423,19 @@ void RS485_1_Send_Data_2(void) //
white_light2 = total[14 + 18 * (now_stage - 1)]; white_light2 = total[14 + 18 * (now_stage - 1)];
} }
sendbuf[7] = red_light1; sendbuf[7] = red_light1;
sendbuf[8] = red_light2; // 红光 sendbuf[8] = red_light2;
sendbuf[9] = blue_light1; sendbuf[9] = blue_light1;
sendbuf[10] = blue_light2; // 蓝光 sendbuf[10] = blue_light2;
sendbuf[11] = white_light1; sendbuf[11] = white_light1;
sendbuf[12] = white_light2; // 白光 sendbuf[12] = white_light2;
if (C <= 5000) if (C <= 5000)
{ {
CO2_1 = RS485_RX_BUF_COPY[19]; CO2_1 = (C & 0xFF00) >> 8;
CO2_2 = RS485_RX_BUF_COPY[20]; CO2_1 = C & 0x00FF;
} }
sendbuf[13] = CO2_1; sendbuf[13] = CO2_1;
sendbuf[14] = CO2_2; sendbuf[14] = CO2_2;
if (now_stage < 7 && total[3 + 18 * (now_stage - 1)] < 24 && total[4 + 18 * (now_stage - 1)] < 60 && chour < 24 && cminute < 60) if (now_stage > 0 && now_stage < 7 && total[3 + 18 * (now_stage - 1)] < 24 && total[4 + 18 * (now_stage - 1)] < 60 && chour < 24 && cminute < 60)
{ {
sendbuf[15] = now_stage; // current state sendbuf[15] = now_stage; // current state
sendbuf[16] = total[3 + 18 * (now_stage - 1)]; // stage hour sendbuf[16] = total[3 + 18 * (now_stage - 1)]; // stage hour
@ -426,6 +443,12 @@ void RS485_1_Send_Data_2(void) //
sendbuf[18] = chour; // run hour sendbuf[18] = chour; // run hour
sendbuf[19] = cminute; // run minute sendbuf[19] = cminute; // run minute
} }
// now_stage = 1;
sendbuf[15] = now_stage; // current state
sendbuf[16] = total[3 + 18 * (now_stage - 1)]; // stage hour
sendbuf[17] = total[4 + 18 * (now_stage - 1)]; // stage minute
sendbuf[18] = chour; // run hour
sendbuf[19] = cminute; // run minute
sendbuf[20] = hot_clod_flag; // hot clod state sendbuf[20] = hot_clod_flag; // hot clod state
sendbuf[21] = humidity_flag; // humidity state sendbuf[21] = humidity_flag; // humidity state
sendbuf[22] = ALARM; // ALARM state sendbuf[22] = ALARM; // ALARM state
@ -450,35 +473,37 @@ void RS485_1_Send_Data_2(void) //
// sendbuf[29] = ((int)(pid.OUT * 1000)) / 256; // sendbuf[29] = ((int)(pid.OUT * 1000)) / 256;
// sendbuf[30] = ((int)(pid.OUT * 1000)) % 256; // sendbuf[30] = ((int)(pid.OUT * 1000)) % 256;
bufcut_Init(sendbuf_crc, sendbuf, 3, 31); // bufcut_Init(sendbuf_crc, sendbuf, 3, 31);
GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2); // GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2);
GetCRC16(sendbuf, 31, &crc_num1, &crc_num2);
sendbuf[31] = crc_num1; sendbuf[31] = crc_num1;
sendbuf[32] = crc_num2; sendbuf[32] = crc_num2;
for (a = 0; a < 37; a++) // 循环发送数据
{ // only when modify happen, then send
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) if (cmp_str(prev_sendbuf, sendbuf, 37) != 0) {
;
USART_SendData(USART1, sendbuf[a]); RS485_1_Send_Data(sendbuf, 37);
// if(a==0)printf("%x\r\n",sendbuf[0]);
cp_str_to_prev(prev_sendbuf, sendbuf, 37);
} }
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
;
// RS485_RX_CNT=0;
RS485_1_TX_EN = 0; // 设置为接收模式
} }
/**
* sync environment params
*/
void RS485_1_Send_Data_3(void) void RS485_1_Send_Data_3(void)
{ {
u8 batchbuf[8] = {0xEE, 0xB5, 0x01, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; u8 batchbuf[10] = {0xEE, 0xB5, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
u8 t; u8 t;
RS485_1_TX_EN = 1; // 设置为发送模式 RS485_1_TX_EN = 1;
batchbuf[3] = NUM; batchbuf[3] = NUM;
NUM += 1; NUM += 1;
if (NUM > 6) if (NUM > 6)
NUM = 1; NUM = 1;
for (t = 0; t < 8; t++) // 循环发送数据 GetCRC16(batchbuf, 4, batchbuf + 4, batchbuf + 5);
for (t = 0; t < 10; t++)
{ {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
; ;
@ -489,16 +514,16 @@ void RS485_1_Send_Data_3(void)
; ;
// RS485_RX_CNT=0; // RS485_RX_CNT=0;
RS485_1_TX_EN = 0; // 设置为接收模式 RS485_1_TX_EN = 0;
} }
// void RS485_3_Send_Data_2(void)//上传当前周期(1),周期时长(2),已运行时长(2),红蓝白光(6),加热制冷状态(1),加湿除湿状态(1),报警状态(1) // void RS485_3_Send_Data_2(void)
//{ //{
// delay_ms(50); // delay_ms(50);
// u8 sendbuf[21]={0xEE,0xB5,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFC,0xFF,0xFF};//发送给串口屏的实时数据 // u8 sendbuf[21]={0xEE,0xB5,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFC,0xFF,0xFF};//???????????????????
// u8 a; // u8 a;
// GPIO8_Init(); // GPIO8_Init();
// GPIO_SetBits(GPIOB,GPIO_Pin_8); // GPIO_SetBits(GPIOB,GPIO_Pin_8);
// RS485_3_TX_EN=1; //设置为发送模式 // RS485_3_TX_EN=1;
// sendbuf[3]=t; // sendbuf[3]=t;
// sendbuf[4]=total[3 + 18 * (t - 1)]; // sendbuf[4]=total[3 + 18 * (t - 1)];
// sendbuf[5]=total[4 + 18 * (t - 1)]; // sendbuf[5]=total[4 + 18 * (t - 1)];
@ -515,7 +540,7 @@ void RS485_1_Send_Data_3(void)
// sendbuf[16]=ALARM; // sendbuf[16]=ALARM;
// //
// //
// for(a=0;a<21;a++) //循环发送数据 // for(a=0;a<21;a++)
// { // {
// while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); // while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
// USART_SendData(USART1,sendbuf[a]); // USART_SendData(USART1,sendbuf[a]);
@ -524,19 +549,16 @@ void RS485_1_Send_Data_3(void)
// while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); // while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
// RS485_RX_CNT=0; // RS485_RX_CNT=0;
// GPIO_ResetBits(GPIOB,GPIO_Pin_8); // GPIO_ResetBits(GPIOB,GPIO_Pin_8);
// RS485_3_TX_EN=0; //设置为接收模式 // RS485_3_TX_EN=0;
//} //}
// RS485发送len个字节.
// buf:发送区首地址
// len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
void RS485_3_Send_Data(u8 *buf,u8 len) void RS485_3_Send_Data(u8 *buf,u8 len)
{ {
u8 t; u8 t;
// GPIO8_Init(); // GPIO8_Init();
// GPIO_SetBits(GPIOB,GPIO_Pin_8); // GPIO_SetBits(GPIOB,GPIO_Pin_8);
RS485_3_TX_EN=1; //设置为发送模式 RS485_3_TX_EN=1;
for(t=0;t<len;t++) //循环发送数据 for(t=0;t<len;t++)
{ {
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1,buf[t]); USART_SendData(USART1,buf[t]);
@ -545,25 +567,22 @@ void RS485_3_Send_Data(u8 *buf,u8 len)
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
// RS485_RX_CNT=0; // RS485_RX_CNT=0;
// GPIO_ResetBits(GPIOB,GPIO_Pin_8); // GPIO_ResetBits(GPIOB,GPIO_Pin_8);
RS485_3_TX_EN=0; //设置为接收模式 RS485_3_TX_EN=0;
} }
// RS485查询接收到的数据
// buf:接收缓存首地址
// len:读到的数据长度
void RS485_Receive_Data(u8 *buf, u8 *len) void RS485_Receive_Data(u8 *buf, u8 *len)
{ {
u8 rxlen = RS485_RX_CNT; u8 rxlen = RS485_RX_CNT;
u8 i = 0; u8 i = 0;
*len = 0; // 默认为0 *len = 0;
delay_ms(10); // 等待10ms,连续超过10ms没有接收到一个数据,则认为接收结束 delay_ms(10);
if (rxlen == RS485_RX_CNT && rxlen) // 接收到了数据,且接收完成了 if (rxlen == RS485_RX_CNT && rxlen)
{ {
for (i = 0; i < rxlen; i++) for (i = 0; i < rxlen; i++)
{ {
buf[i] = RS485_RX_BUF[i]; buf[i] = RS485_RX_BUF[i];
} }
*len = RS485_RX_CNT; // 记录本次数据长度 *len = RS485_RX_CNT;
RS485_RX_CNT = 0; // 清零 RS485_RX_CNT = 0;
} }
} }

@ -5,15 +5,14 @@
extern u8 RS485_RX_BUF[128]; //接收缓冲,最大64个字节 extern u8 RS485_RX_BUF[128]; //receive buffer, max 128
extern u8 RS485_RX_CNT; //接收到的数据长度 extern u8 RS485_RX_CNT; // received data length
extern u8 RS485_RX_Flag_Num; extern u8 RS485_RX_Flag_Num;
extern u8 RS485_RX_Flag; extern u8 RS485_RX_Flag;
//模式控制 #define RS485_1_TX_EN PBout(11) // 485 mode control. 0:receiving; 1:Send
#define RS485_1_TX_EN PBout(11) //485模式控制.0,接收;1,发送. #define RS485_2_TX_EN PBout(10) // 485 mode control. 0:receiving; 1:Send
#define RS485_2_TX_EN PBout(10) //485模式控制.0,接收;1,发送. #define RS485_3_TX_EN PBout(4) // 485 mode control. 0:receiving; 1:Send
#define RS485_3_TX_EN PBout(4) //485模式控制.0,接收;1,发送.
void RS485_1_Init(u32 bound); void RS485_1_Init(u32 bound);

@ -5,13 +5,17 @@
#include "rtc.h" #include "rtc.h"
#include "SysTick.h" #include "SysTick.h"
#include "myfreertos.h" #include "myfreertos.h"
extern u8 RS485_RX_BUF[128]; //
// extern u8 buf[51]; // extern u8 buf[51];
extern u8 RS485_RX_BUF[128]; //
extern u8 store_stage; extern u8 store_stage;
u8 read_stage = 1; // 从flash读取出来的阶段号 u8 read_stage = 1; // 从flash读取出来的阶段号
extern u8 now_stage; extern u8 now_stage;
extern u8 chour; extern u8 chour;
extern u8 cminute; extern u8 cminute;
extern PID pid;
u16 current_minute = 0; u16 current_minute = 0;
u16 total_minute = 0; u16 total_minute = 0;
/* /*
@ -27,6 +31,7 @@ u8 total[] = {0xEE, 0XEE, 0x01, 0x04, 0x00, 0x00, 0xFA, 0x02, 0xBC, 0x00, 0x64,
0xEE, 0XEE, 0x05, 0x04, 0x00, 0x00, 0xFA, 0x02, 0xBC, 0x00, 0x64, 0x00, 0x64, 0x00, 0x64, 0xFF, 0xFF, 0xFF, 0xEE, 0XEE, 0x05, 0x04, 0x00, 0x00, 0xFA, 0x02, 0xBC, 0x00, 0x64, 0x00, 0x64, 0x00, 0x64, 0xFF, 0xFF, 0xFF,
0xEE, 0XEE, 0x06, 0x04, 0x00, 0x00, 0xFA, 0x02, 0xBC, 0x00, 0x64, 0x00, 0x64, 0x00, 0x64, 0xFF, 0xFF, 0xFF}; 0xEE, 0XEE, 0x06, 0x04, 0x00, 0x00, 0xFA, 0x02, 0xBC, 0x00, 0x64, 0x00, 0x64, 0x00, 0x64, 0xFF, 0xFF, 0xFF};
void Array(u8 *n, u8 *hour, u8 *min, u16 *tem, u16 *hum, u16 *red, u16 *blue, u16 *white) void Array(u8 *n, u8 *hour, u8 *min, u16 *tem, u16 *hum, u16 *red, u16 *blue, u16 *white)
{ {
*hour = (total[3 + 18 * (*n - 1)]); *hour = (total[3 + 18 * (*n - 1)]);
@ -41,23 +46,63 @@ void Array(u8 *n, u8 *hour, u8 *min, u16 *tem, u16 *hum, u16 *red, u16 *blue, u1
uint8_t ArrayRead[108]; uint8_t ArrayRead[108];
uint8_t ArrayWrite[108]; uint8_t ArrayWrite[108];
uint8_t pidsRead[6];
uint8_t pidsWrite[6];
void Write_Init(void) void Write_Init(void)
{ {
W25QXX_Init(); W25QXX_Init();
W25QXX_Erase_Sector(0x000000); W25QXX_Erase_Sector(0x000000);
delay_ms(150);
W25QXX_Write_NoCheck(total, 0x000000, 108); // 把数据写入flash
W25QXX_Write_NoCheck(&store_stage, 0x00006D, 1); // 把当前运行阶段写入flash W25QXX_Write(total, 0x000000, 108); // write data to flash
if (store_stage <= 0 || store_stage > 6) {
store_stage = 1;
}
W25QXX_Write(&store_stage, 0x001000, 1); // write current stage to flash
pidsWrite[0] = (int)pid.Kp / 256; // Kp 110
pidsWrite[1] = (int)(pid.Kp) % 256; // Kp 110
pidsWrite[2] = ((int)(pid.Ki * 100000)) / 256; // Ti 0.001
pidsWrite[3] = ((int)(pid.Ki * 100000)) % 256; // Ti 0.001
pidsWrite[4] = (int)pid.Kd / 256; // Td 340
pidsWrite[5] = (int)pid.Kd % 256; // Td 340
// // wtite p i d
W25QXX_Write(pidsWrite, 0x002000, 6); // p i d, 2bytes for each
W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来 W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来
} }
void Read_Init(void) void Read_Init(void)
{ {
W25QXX_Init(); W25QXX_Init();
W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来 W25QXX_Read(ArrayRead, 0x000000, 108); // 把数据读出来
bufcut_Init(total, ArrayRead, 0, 109); // 把读出来的数据赋值给total数组 bufcut_Init(total, ArrayRead, 0, 108); // 把读出来的数据赋值给total数组
W25QXX_Read(&read_stage, 0x00006D, 1); W25QXX_Read(&read_stage, 0x001000, 1);
now_stage = read_stage; // 读出来的阶段赋值给当前阶段 now_stage = read_stage; // 读出来的阶段赋值给当前阶段
// if (now_stage <= 0 || now_stage > 6) {
// now_stage = 3;
// }
// now_stage = 3;
// read p i d
W25QXX_Read(pidsRead, 0x002000, 6); // p i d, 2bytes for each
float Kp = pidsRead[0] * 256.0 + pidsRead[1];
float Ki = (pidsRead[2] * 256.0 + pidsRead[3]) / 100000.0;
float Kd = pidsRead[4] * 256.0 + pidsRead[5];
// pid.Kp = 120;
if (Kp > 1e-3) { pid.Kp = Kp; }
if (Ki > 1e-3) { pid.Ki = Ki; }
if (Kd > 1e-3) { pid.Kd = Kd; }
// pid.Ki = 0.002;
// pid.Kd = 300;
// pid.Kp = pidsRead[0] * 256.0 + pidsRead[1];
// pid.Ki = (pidsRead[2] * 256.0 + pidsRead[3]) / 100000.0;
// pid.Kd = pidsRead[4] * 256.0 + pidsRead[5];
// printf("%d\r\n",now_stage); // printf("%d\r\n",now_stage);
// printf("\r\n"); // printf("\r\n");
// printf("\r\n"); // printf("\r\n");

@ -1,76 +1,76 @@
#include "USART.h" #include "USART.h"
// int fputc(int ch, FILE *f)
//int fputc(int ch, FILE *f)
//{ //{
// RS485_4_TX_EN=1; // RS485_4_TX_EN=1;
// USART_SendData(USART2, (uint8_t) ch); // USART_SendData(USART2, (uint8_t) ch);
// while((USART2->SR&0X40)==0);//循环发送,直到发送完毕 // while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
// RS485_4_TX_EN=0; // RS485_4_TX_EN=0;
// return ch; // return ch;
//} // }
int fputc(int ch,FILE *p) //函数默认的在使用printf函数时自动调用 int fputc(int ch, FILE *p) // 函数默认的在使用printf函数时自动调用
{ {
RS485_4_TX_EN=1; RS485_4_TX_EN = 1;
USART_SendData(USART2,(u8)ch); USART_SendData(USART2, (u8)ch);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET); while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
RS485_4_TX_EN=0; ;
RS485_4_TX_EN = 0;
return ch; return ch;
} }
//串口1中断服务程序 // 串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误 // 注意,读取USARTx->SR能避免莫名其妙的错误
u8 USART2_RX_BUF[USART2_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节. u8 USART2_RX_BUF[USART2_REC_LEN]; // 接收缓冲,最大USART_REC_LEN个字节.
//接收状态 // 接收状态
//bit15 接收完成标志 // bit15 接收完成标志
//bit14 接收到0x0d // bit14 接收到0x0d
//bit13~0 接收到的有效字节数目 // bit13~0 接收到的有效字节数目
u16 USART2_RX_STA=0; //接收状态标记 u16 USART2_RX_STA = 0; // 接收状态标记
void USART2_Init(u32 bound) void USART2_Init(u32 bound)
{ {
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART1GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // 使能USART1GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); // 使能USART1GPIOA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //RE485-DE PA8 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // RE485-DE PA8
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //USART1_TX PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // USART1_TX PA9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//USART1_RX PA10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // USART1_RX PA10初始化
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4; // 抢占优先级3
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 NVIC_Init(&NVIC_InitStructure); // 根据指定的参数初始化VIC寄存器
//USART 初始化设置 // USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_BaudRate = bound; // 串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口1 USART_Init(USART2, &USART_InitStructure); // 初始化串口1
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 开启串口接受中断
USART_Cmd(USART2, ENABLE); //使能串口1 USART_Cmd(USART2, ENABLE); // 使能串口1
RS485_4_TX_EN=0; //拉低 RS485_4_TX_EN = 0; // 拉低
} }
//void USART2_IRQHandler(void) //串口1中断服务程序 // void USART2_IRQHandler(void) //串口1中断服务程序
//{ //{
// u8 Res; // u8 Res;
// if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断 // if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
@ -88,7 +88,7 @@ void USART2_Init(u32 bound)
//* 输 入 : bound:波特率 //* 输 入 : bound:波特率
//* 输 出 : 无 //* 输 出 : 无
//*******************************************************************************/ //*******************************************************************************/
//void USART2_Init(u32 bound) // void USART2_Init(u32 bound)
//{ //{
// //GPIO端口设置 // //GPIO端口设置
// GPIO_InitTypeDef GPIO_InitStructure; // GPIO_InitTypeDef GPIO_InitStructure;
@ -133,32 +133,36 @@ void USART2_Init(u32 bound)
//} //}
/******************************************************************************* /*******************************************************************************
* : USART1_IRQHandler * : USART1_IRQHandler
* : USART1 * : USART1
* : * :
* : * :
*******************************************************************************/ *******************************************************************************/
void USART2_IRQHandler(void) //串口1中断服务程序 void USART2_IRQHandler(void) // 串口1中断服务程序
{ {
u8 r; u8 r;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断 if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // 接收中断
{ {
r =USART_ReceiveData(USART2);//(USART1->DR); //读取接收到的数据 r = USART_ReceiveData(USART2); //(USART1->DR); //读取接收到的数据
if((USART2_RX_STA&0x8000)==0)//接收未完成 if ((USART2_RX_STA & 0x8000) == 0) // 接收未完成
{ {
if(USART2_RX_STA&0x4000)//接收到了0x0d if (USART2_RX_STA & 0x4000) // 接收到了0x0d
{ {
if(r!=0x0a)USART2_RX_STA=0;//接收错误,重新开始 if (r != 0x0a)
else USART2_RX_STA|=0x8000; //接收完成了 USART2_RX_STA = 0; // 接收错误,重新开始
else
USART2_RX_STA |= 0x8000; // 接收完成了
} }
else //还没收到0X0D else // 还没收到0X0D
{ {
if(r==0x0d)USART2_RX_STA|=0x4000; if (r == 0x0d)
USART2_RX_STA |= 0x4000;
else else
{ {
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=r; USART2_RX_BUF[USART2_RX_STA & 0X3FFF] = r;
USART2_RX_STA++; USART2_RX_STA++;
if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;//接收数据错误,重新开始接收 if (USART2_RX_STA > (USART2_REC_LEN - 1))
USART2_RX_STA = 0; // 接收数据错误,重新开始接收
} }
} }
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

@ -1,5 +1,7 @@
#include "myfreertos.h" #include "myfreertos.h"
#define SENSOR_ADDRESS 0x02
char pubTopic[512] = "/sys/hp8oQhMZJ67/Device1/thing/event/property/post"; // Publish topic char pubTopic[512] = "/sys/hp8oQhMZJ67/Device1/thing/event/property/post"; // Publish topic
char subTopic[512] = "/sys/hp8oQhMZJ67/Device1/thing/service/property/set"; // Subscribe topic char subTopic[512] = "/sys/hp8oQhMZJ67/Device1/thing/service/property/set"; // Subscribe topic
@ -25,6 +27,24 @@ void Sensor_Communication_task(void *pvParameters);
void Host_Computer_Communication(void); void Host_Computer_Communication(void);
// 10seconds delay
#define SENSOR_DATA_REQUEST_PERIOD pdMS_TO_TICKS(5000)
// 60seconds delay
#define SYNC_ENVIRONMENT_DATA_REQUEST_PERIOD pdMS_TO_TICKS(60000)
/**
* sensor data request: 02 03
*/
void SensorDataRequestTask(void *pvParameters);
/**
* sync environment data: EE B5 01
*/
void SyncEnvironmentDataRequestTask(void *pvParameters);
int isZeros(u8 arr[]); int isZeros(u8 arr[]);
u8 RS485_RX_BUF_COPY[128] = {0}; u8 RS485_RX_BUF_COPY[128] = {0};
u8 cnt_flag = 0, sub_flag = 0, pub_flag = 0; u8 cnt_flag = 0, sub_flag = 0, pub_flag = 0;
@ -51,16 +71,24 @@ u8 Feed_Dog_Count = 0; // Feeding dog timing
u8 now_stage = 1; // Current operational phase u8 now_stage = 1; // Current operational phase
u16 tem, hum, red, blue, white; u16 tem, hum, red, blue, white;
u8 hour, min; u8 hour, min;
u8 change_stage_rev1 = 0; u8 rs485buf[8] = {SENSOR_ADDRESS, 0x03, 0x00, 0x01, 0x00, 0x09, 0xD4, 0x3F}; // Sensor exchange data
u8 change_stage_rev2 = 0;
u8 change_stage_rev3 = 0;
u8 rs485buf[8] = {0x02, 0x03, 0x00, 0x01, 0x00, 0x09, 0xD4, 0x3F}; // Sensor exchange data
u16 sync_cnt = 0; // 同步环境控制参数的计数器到0同步 u16 sync_cnt = 0; // 同步环境控制参数的计数器到0同步
int T = 0, H = 0, C = 0, G = 0, current_T = 0; int T = 0, H = 0, C = 0, G = 0, current_T = 0;
extern u8 total[]; extern u8 total[];
float Humidity = 0; float Humidity = 0;
float temperature = 0; float temperature = 0;
// pages in hmi
#define page_index 43
#define page_curve 45
#define page_setting 46
#define page_upgrade 23
#define page_scan_wifi 48
#define page_wifi_setting 49
#define page_param_adjust 50
#define page_status 51
// u8 RS485_RX_BUF[64]; // u8 RS485_RX_BUF[64];
/********************* /*********************
@ -77,6 +105,13 @@ void os_init(void)
(void *)NULL, (void *)NULL,
(UBaseType_t)START_TASK_PRIO, (UBaseType_t)START_TASK_PRIO,
(TaskHandle_t *)&StartTask_Handler); (TaskHandle_t *)&StartTask_Handler);
// 创建传感器数据请求任务
xTaskCreate(SensorDataRequestTask, "SensorDataRequestTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(SyncEnvironmentDataRequestTask, "SyncEnvironmentDataRequestTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
vTaskStartScheduler(); vTaskStartScheduler();
} }
@ -128,10 +163,33 @@ void start_task(void *pvParameters)
// (TaskHandle_t* )&LEDTask_Handler); // (TaskHandle_t* )&LEDTask_Handler);
// if(handler4==pdPASS){printf("控灯任务创建成功\r\n");} // if(handler4==pdPASS){printf("控灯任务创建成功\r\n");}
// else{printf("控灯任务创建失败\r\n");} // else{printf("控灯任务创建失败\r\n");}
vTaskDelete(StartTask_Handler); // 删除开始任务 vTaskDelete(StartTask_Handler); // 删除开始任务
taskEXIT_CRITICAL(); // 退出临界区 taskEXIT_CRITICAL(); // 退出临界区
} }
void SensorDataRequestTask(void *pvParameters) {
while (1) {
// 发送传感器数据请求的操作
RS485_1_Send_Data_1(rs485buf, 8);
vTaskDelay(SENSOR_DATA_REQUEST_PERIOD);
}
}
void SyncEnvironmentDataRequestTask(void *pvParameters) {
while (1) {
// 发送传感器数据请求的操作
RS485_1_Send_Data_3();
vTaskDelay(SYNC_ENVIRONMENT_DATA_REQUEST_PERIOD);
}
}
u8 temp[4] = {0xaa,0xbb,0xcc,0xdd};
void Sensor_Communication_task(void *pvParameters) void Sensor_Communication_task(void *pvParameters)
{ {
@ -155,133 +213,99 @@ void Sensor_Communication_task(void *pvParameters)
now_stage = timelong_Compare(); // now_stage = timelong_Compare(); //
// printf("%d %d\r\n",chour,cminute); // printf("%d %d\r\n",chour,cminute);
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // 更新对应阶段号的参数 Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // 更新对应阶段号的参数
// printf("current stage: %d\r\n",now_stage); printf("current stage: %d\r\n",now_stage);
RS485_1_Send_Data_1(rs485buf, 8); //(RS485_RX_CNT=0已被注释)
// printf("send finish\r\n"); // RS485_1_Send_Data_1(rs485buf, 8); //(RS485_RX_CNT=0已被注释)
delay_xms(200); // delay_xms(50);
// RX_BUF_Printf(128);
if (sync_cnt <= 0)
{ // // RX_BUF_Printf(128);
RS485_1_Send_Data_3(); // if (sync_cnt <= 0)
sync_cnt = 600; // {
} // RS485_1_Send_Data_3();
else // sync_cnt = 10;
{ // }
sync_cnt--; // else
} // {
delay_xms(200); // sync_cnt--;
// }
delay_xms(50);
// RX_BUF_Printf(128); // RX_BUF_Printf(128);
bufcut_Init(RS485_RX_BUF_COPY, RS485_RX_BUF, 0, 128); bufcut_Init(RS485_RX_BUF_COPY, RS485_RX_BUF, 0, 128);
RS485_RX_CNT = 0; RS485_RX_CNT = 0;
// HC595_Send_Byte(0x00); // close all while (isAllZeros(RS485_RX_BUF_COPY, 128) == 0)
while ((isAllZeros(RS485_RX_BUF_COPY, 128) == 0))
{ {
if (RS485_RX_BUF_COPY[0] == 0x02 && RS485_RX_BUF_COPY[1] == 0x03 && CRC16_check(RS485_RX_BUF_COPY, 21) == 1)
if (RS485_RX_BUF_COPY[0] == 0x02 && RS485_RX_BUF_COPY[1] == 0x03) // 判断是否校验成功
{ {
// -21 // receive message from sensor
// RS485_RX_BUF_COPY[3] = 0xFF;
// RS485_RX_BUF_COPY[4] = 0xB1;
T = RS485_RX_BUF_COPY[3];
T = T << 8 | RS485_RX_BUF_COPY[4];
// 支持负数温度
if (T & 0x8000)
{
T = ((~T + 1) & 0xFFFF);
}
H = RS485_RX_BUF_COPY[5];
H = H << 8 | RS485_RX_BUF_COPY[6];
C = RS485_RX_BUF_COPY[19]; u8 temp_data[23] = {0};
C = C << 8 | RS485_RX_BUF_COPY[20]; for (int i=0;i<23;i++) {
temp_data[i] = RS485_RX_BUF_COPY[i];
/*GPIO1->报警铃 GPIO3->加热棒 GPIO4—>新风风扇 GPIO5->加湿器 GPIO6->压缩机 */
if ((T < (tem - 20) || T > (tem + 20) || H < (hum - 100) || H > (hum + 100)) && (tick > 600)) // 温度偏差2℃报警湿度偏差10报警
{
HC595_Send_Byte(gpio_state |= 0x01); // 打开报警铃 GPIO1->PB1 |=0000 0001 0x01
ALARM = 1;
} }
else process_sensor_data(temp_data);
{
HC595_Send_Byte(gpio_state &= 0xFE); // 关闭报警铃 &=1111 1110 0xFE
ALARM = 0;
}
if (T < 600 && T > -400)
{
diff = current_T - T;
if (diff < 0)
diff = -diff;
if (diff < 3 || current_T == 0) // 最近两次温差相差0.3℃,消除传感器感应出错的影响
{
pid.set_tem = tem / 10.0;
pid.now_tem = T / 10.0;
PID_Calc();
// send sign to 485
// out: 0-200
// num=(((pid.OUT*400)/pid.pwmcycle)-1);//请问这个pid.OUT与pwm占空比的值是如何换算过来的
// TIM_SetCompare3(TIM3,num);
// TIM_SetCompare3(TIM3,0.845*num);
// printf("%d\r\n",num);
// 0-200对应0-100%如果pid.out=50,占空比就是25%//num=50*400/200=100100/400=25%
// printf("%d ,%d ,%d ,%d ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%.3f\r\n",T,H,C,num,(pid.set_tem*10),pid.Kp,ki,kd,pid.Pout,pid.Iout,pid.Dout,num/399.0);
current_T = T;
}
}
if (H > (hum + 50)) // 湿度高于设定值5打开新风风扇 GPIO4->PD1
{
HC595_Send_Byte(gpio_state |= 0x08); // 打开新风风扇 |=0000 1000 0x08
HC595_Send_Byte(gpio_state &= 0xEF); // 关闭加湿器 &=1110 1111 0xEF
humidity_flag = 1; // 除湿
}
if (H < (hum - 50)) // 湿度低于设定值5打开加湿器 GPIO5->PD0
{
HC595_Send_Byte(gpio_state |= 0x10); // 启动加湿器 |=0001 0000 0x10
HC595_Send_Byte(gpio_state &= 0xF7); // 关闭风扇 &=1111 0111 0xF7
humidity_flag = 2;
}
if (humidity_flag == 1 && H <= hum)
{
HC595_Send_Byte(gpio_state &= 0xE7); // 关闭风扇 关闭加湿器 &=1110 0111 0xE7
humidity_flag = 0;
}
if (humidity_flag == 2 && H >= hum)
{
HC595_Send_Byte(gpio_state &= 0xE7); // 关闭加湿器 关闭风扇 &=1110 0111 0xE7
humidity_flag = 0;
}
RS485_1_Send_Data_2(); // 上传参数
delay_xms(200);
RX_BUF_Transfer(0, 23); RX_BUF_Transfer(0, 23);
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x01)
{
// batch update params in a stage
// total length: 22
if (CRC16_check(RS485_RX_BUF_COPY, 16) == 1) {
Batch_synchronization(&n, RS485_RX_BUF_COPY);
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // update param correspond to current stage
}
RX_BUF_Transfer(0, 22);
}
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x03) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x03)
{ {
Analysis(&n, &i, RS485_RX_BUF_COPY); // update single environment param
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // 更新对应阶段号的参数 // total length: 13
Write_Init(); if (CRC16_check(RS485_RX_BUF_COPY, 7) == 1) {
RX_BUF_Transfer(0, 11); Analysis(&n, &i, RS485_RX_BUF_COPY);
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // update param correspond to current stage
Write_Init();
}
RX_BUF_Transfer(0, 13);
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x04) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x04)
{ {
change_stage_rev1 = RS485_RX_BUF_COPY[3]; // change running stage
RTC_synchronization_ins(2023, 9, 1, 0, 00, 00); // total length: 10
now_stage = change_stage_rev1; if (CRC16_check(RS485_RX_BUF_COPY, 4) == 1) {
store_stage = change_stage_rev1; int target_stage = RS485_RX_BUF_COPY[3];
Write_Init(); RTC_synchronization_ins(2023, 9, 1, 0, 00, 00);
RX_BUF_Transfer(0, 8); now_stage = target_stage;
store_stage = target_stage;
Write_Init();
}
RX_BUF_Transfer(0, 10);
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x01) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB6 && RS485_RX_BUF_COPY[2] == 0x05)
{ {
Batch_synchronization(&n, RS485_RX_BUF_COPY); // change pid params
Array(&now_stage, &hour, &min, &tem, &hum, &red, &blue, &white); // 更新对应阶段号的参数 // total length: 12
RX_BUF_Transfer(0, 20); if (CRC16_check(RS485_RX_BUF_COPY, 6) == 1) {
int param_index = RS485_RX_BUF_COPY[3];
switch(param_index) {
case 0x01:
{
pid.Kp = RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5];
break;
}
case 0x02:
{
pid.Ki = (RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5]) / 100000.0;
break;
}
case 0x03:
{
pid.Kd = RS485_RX_BUF_COPY[4] * 256.0 + RS485_RX_BUF_COPY[5];
break;
}
}
Write_Init();
}
RX_BUF_Transfer(0, 12);
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x11 && RS485_RX_BUF_COPY[15] == 0xFF && RS485_RX_BUF[16] == 0xFF) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x11 && RS485_RX_BUF_COPY[15] == 0xFF && RS485_RX_BUF[16] == 0xFF)
{ {
@ -293,6 +317,8 @@ void Sensor_Communication_task(void *pvParameters)
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x11 && RS485_RX_BUF_COPY[12] == 0xFF && RS485_RX_BUF[13] == 0xFF) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x11 && RS485_RX_BUF_COPY[12] == 0xFF && RS485_RX_BUF[13] == 0xFF)
{ {
// btn down or up
process_hmi_btn_event(RS485_RX_BUF_COPY[3] * 256 + RS485_RX_BUF_COPY[4], RS485_RX_BUF_COPY[5] * 256 + RS485_RX_BUF_COPY[6], RS485_RX_BUF_COPY[9]);
RX_BUF_Transfer(0, 14); RX_BUF_Transfer(0, 14);
} }
else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x01 && RS485_RX_BUF_COPY[7] == 0xFF && RS485_RX_BUF_COPY[8] == 0xFF) else if (RS485_RX_BUF_COPY[0] == 0xEE && RS485_RX_BUF_COPY[1] == 0xB1 && RS485_RX_BUF_COPY[2] == 0x01 && RS485_RX_BUF_COPY[7] == 0xFF && RS485_RX_BUF_COPY[8] == 0xFF)
@ -302,8 +328,7 @@ void Sensor_Communication_task(void *pvParameters)
else else
{ {
int ZERO_Count = 0; int ZERO_Count = 0;
// if()与while()的顺序不能变 if ((RS485_RX_BUF_COPY[0] != 0x00))
if ((RS485_RX_BUF_COPY[0] != 0x00)) // 不为0处理掉
{ {
RX_BUF_Transfer(0, 1); RX_BUF_Transfer(0, 1);
} }
@ -316,6 +341,10 @@ void Sensor_Communication_task(void *pvParameters)
} }
} }
if (red <= 1000 && blue <= 1000) if (red <= 1000 && blue <= 1000)
{ {
@ -327,6 +356,108 @@ void Sensor_Communication_task(void *pvParameters)
} }
} }
/**
* process sensor data
*/
void process_sensor_data(u8 *data) {
T = data[3];
T = T << 8 | data[4];
// Negative temperature treatment
if (T & 0x8000)
{
T = ((~T + 1) & 0xFFFF);
}
H = data[5];
H = H << 8 | data[6];
C = data[19];
C = C << 8 | data[20];
/*GPIO1->报警铃 GPIO3->加热棒 GPIO4—>新风风扇 GPIO5->加湿器 GPIO6->压缩机 */
if ((T < (tem - 20) || T > (tem + 20) || H < (hum - 100) || H > (hum + 100)) && (tick > 600)) // 温度偏差2℃报警湿度偏差10报警
{
HC595_Send_Byte(gpio_state |= 0x01); // 打开报警铃 GPIO1->PB1 |=0000 0001 0x01
ALARM = 1;
}
else
{
HC595_Send_Byte(gpio_state &= 0xFE); // 关闭报警铃 &=1111 1110 0xFE
ALARM = 0;
}
if (T < 600 && T > -400)
{
diff = current_T - T;
if (diff < 0)
diff = -diff;
if (diff < 3 || current_T == 0) // 最近两次温差相差0.3℃,消除传感器感应出错的影响
{
pid.set_tem = tem / 10.0;
pid.now_tem = T / 10.0;
PID_Calc();
// send sign to 485
// out: 0-200
// num=(((pid.OUT*400)/pid.pwmcycle)-1);//请问这个pid.OUT与pwm占空比的值是如何换算过来的
// TIM_SetCompare3(TIM3,num);
// TIM_SetCompare3(TIM3,0.845*num);
// printf("%d\r\n",num);
// 0-200对应0-100%如果pid.out=50,占空比就是25%//num=50*400/200=100100/400=25%
// printf("%d ,%d ,%d ,%d ,%f ,%f ,%f ,%f ,%f ,%f ,%f ,%.3f\r\n",T,H,C,num,(pid.set_tem*10),pid.Kp,ki,kd,pid.Pout,pid.Iout,pid.Dout,num/399.0);
current_T = T;
}
}
if (H > (hum + 50)) // 湿度高于设定值5打开新风风扇 GPIO4->PD1
{
HC595_Send_Byte(gpio_state |= 0x08); // 打开新风风扇 |=0000 1000 0x08
HC595_Send_Byte(gpio_state &= 0xEF); // 关闭加湿器 &=1110 1111 0xEF
humidity_flag = 1; // 除湿
}
if (H < (hum - 50)) // 湿度低于设定值5打开加湿器 GPIO5->PD0
{
HC595_Send_Byte(gpio_state |= 0x10); // 启动加湿器 |=0001 0000 0x10
HC595_Send_Byte(gpio_state &= 0xF7); // 关闭风扇 &=1111 0111 0xF7
humidity_flag = 2;
}
if (humidity_flag == 1 && H <= hum)
{
HC595_Send_Byte(gpio_state &= 0xE7); // 关闭风扇 关闭加湿器 &=1110 0111 0xE7
humidity_flag = 0;
}
if (humidity_flag == 2 && H >= hum)
{
HC595_Send_Byte(gpio_state &= 0xE7); // 关闭加湿器 关闭风扇 &=1110 0111 0xE7
humidity_flag = 0;
}
RS485_1_Send_Data_2(); // 上传参数
delay_xms(200);
}
/**
* hmi event process
*/
void process_hmi_btn_event(u8 page, u8 btn_index, u8 value) {
switch(page) {
case page_param_adjust:
{
// change current stage
if (btn_index >= 45 && btn_index <= 50 && value == 0x01) {
int target_stage = btn_index - 44;
RTC_synchronization_ins(2023, 9, 1, 0, 00, 00);
now_stage = target_stage;
store_stage = target_stage;
Write_Init();
}
break;
}
}
}
// void Host_Computer_Communication(void) // void Host_Computer_Communication(void)
//{ //{
// if(RS485_RX_BUF_COPY[0]==0xEE&&RS485_RX_BUF_COPY[1]==0xB6) // if(RS485_RX_BUF_COPY[0]==0xEE&&RS485_RX_BUF_COPY[1]==0xB6)

@ -24,6 +24,8 @@
#include "mqtt_api.h" #include "mqtt_api.h"
void os_init(void); void os_init(void);
void process_sensor_data(u8 *data);
void process_hmi_btn_event(u8 page_index, u8 btn_index, u8 value);
#endif #endif

Loading…
Cancel
Save