#include "rs485.h" #include "SysTick.h" #include "USART.h" #include "Relays.h" #include "myfreertos.h" 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}; // 发送给串口屏的实时数据 u8 sendbuf_crc[20] = {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 RS485_RX_BUF_COPY[128]; extern u8 now_stage; extern u8 chour; extern u8 cminute; extern u8 total[]; extern u8 hot_clod_flag; extern u8 humidity_flag; extern u8 ALARM; extern u8 NUM; u8 TEM1; u8 TEM2; u8 HUM1; u8 HUM2; u8 CO2_1; u8 CO2_2; u16 RED_LIGHT; u16 WHITE_LIGHT; u16 BLUE_LIGHT; u8 red_light1; u8 red_light2; u8 white_light1; u8 white_light2; u8 blue_light1; u8 blue_light2; u8 crc_num1 = 0xFF; u8 crc_num2 = 0xFF; // extern u8 TEM1; // extern u8 TEM2; // extern u8 HUM1; // extern u8 HUM2; // extern u8 CO2_1; // extern u8 CO2_2; extern int T; extern int H; extern int C; extern PID pid; // extern u16 RED_LIGHT; // extern u16 WHITE_LIGHT; // extern u16 BLUE_LIGHT; // extern u8 red_light1; // extern u8 red_light2; // extern u8 white_light1; // extern u8 white_light2; // extern u8 blue_light1; // extern u8 blue_light2; // CRC校验 自己后面添加的 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, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 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, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 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, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 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, 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, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40}; const u8 auchCRCLo[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 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}; u8 RS485_RX_BUF[128]; // 接收缓冲,最大64个字节. u8 RS485_RX_CNT = 0; // 接收到的数据长度 u8 RS485_RX_Flag = 0; // 接收到的数据标志 void GetCRC16(u8 *puchMsg, u16 usDataLen, u8 *uchCRCHi, u8 *uchCRCLo) { *uchCRCHi = 0xFF; *uchCRCLo = 0xFF; u32 uIndex; while (usDataLen--) { uIndex = *uchCRCHi ^ *puchMsg++; *uchCRCHi = *uchCRCLo ^ auchCRCHi[uIndex]; *uchCRCLo = auchCRCLo[uIndex]; } // return ((uchCRCHi<< 8) | (uchCRCLo)) ; } // 查表法计算CRC值并且校验 u8 CRC16_check(u8 *puchMsg, u16 usDataLen) { u8 uchCRCHi = 0xFF; u8 uchCRCLo = 0xFF; u32 uIndex; while (usDataLen--) { uIndex = uchCRCHi ^ *puchMsg++; uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex]; uchCRCLo = auchCRCLo[uIndex]; } if (uchCRCHi == *puchMsg++ && uchCRCLo == *puchMsg++) { // printf("CRC 校验成功\r\n"); // 调试使用 return 1; } else return 0; } void USART1_IRQHandler(void) { u8 res; if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 接收到数据 { res = USART_ReceiveData(USART1); // 读取接收到的数据 if (RS485_RX_CNT < 128) { RS485_RX_BUF[RS485_RX_CNT] = res; // 记录接收到的值 RS485_RX_CNT++; // 接收数据增加1 } } } void SN74CB3Q3253_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitTypeDef GPIO_InitStructure; 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_Speed = GPIO_Speed_50MHz; // IO口速度为50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); // 推挽输出 ,IO口速度为50MHz GPIO_ResetBits(GPIOB, GPIO_Pin_7); // PB7输出低 } // RS485_1_Init->J6 ,PB5->S1,PB6->S0,PB7->USART_OE // SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为001,使1B2 2B2响应 // 初始化IO 串口1 // pclk1:PCLK1时钟频率(Mhz) // bound:波特率 void RS485_1_Init(u32 bound) { SN74CB3Q3253_Init(); GPIO_ResetBits(GPIOB, GPIO_Pin_5); // S1配置为低电平 GPIO_SetBits(GPIOB, GPIO_Pin_6); // S0配置为高电平 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_Init(USART1, &USART_InitStructure); ; // 初始化串口 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 先占优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_Cmd(USART1, ENABLE); // 使能串口 RS485_1_TX_EN = 0; // 默认为接收模式 } // RS485_2_Init->J7 ,PB5->S1,PB6->S0,PB7->USART_OE // SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为000,使1B1 2B1响应 void RS485_2_Init(u32 bound) { SN74CB3Q3253_Init(); GPIO_ResetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6); // S1,S0配置为低电平 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_Init(USART1, &USART_InitStructure); ; // 初始化串口 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 先占优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_Cmd(USART1, ENABLE); // 使能串口 RS485_2_TX_EN = 0; // 默认为接收模式 } // RS485_3_Init->J7 ,PB5->S1,PB6->S0,PB7->USART_OE // SN74CB3Q3253多路复用器 - 多路信号分离器的引脚配置为OE S1 S0对应的值为010,使1B3 2B3响应 void RS485_3_Init(u32 bound) { SN74CB3Q3253_Init(); GPIO_ResetBits(GPIOB, GPIO_Pin_6); // S0配置为低电平 GPIO_SetBits(GPIOB, GPIO_Pin_5); // S1配置为高电平 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOA,D时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // 端口配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); // 复位串口1 RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); // 停止复位 USART_InitStructure.USART_BaudRate = bound; // 波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据长度 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No; /// 奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收发模式 USART_Init(USART1, &USART_InitStructure); ; // 初始化串口 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 使能串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 先占优先级2级 // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道 NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 开启中断 USART_Cmd(USART1, ENABLE); // 使能串口 RS485_3_TX_EN = 0; // 默认为接收模式 } // RS485发送len个字节. // buf:发送区首地址 // len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节) void RS485_1_Send_Data(u8 *buf, u8 len) { u8 t; RS485_1_TX_EN = 1; // 设置为发送模式 for (t = 0; t < len; t++) // 循环发送数据 { while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; USART_SendData(USART1, buf[t]); } while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; RS485_RX_CNT = 0; RS485_1_TX_EN = 0; // 设置为接收模式 } void RS485_1_Send_Data_1(u8 *buf, u8 len) { u8 t; RS485_1_TX_EN = 1; // 设置为发送模式 for (t = 0; t < len; t++) // 循环发送数据 { while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; USART_SendData(USART1, buf[t]); } while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; // RS485_RX_CNT=0; RS485_1_TX_EN = 0; // 设置为接收模式 } // RS485发送len个字节. // buf:发送区首地址 // len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节) void RS485_1_Send_Data_2(void) // 上传温湿度二氧化碳 { sendbuf[0] = 0xEE; sendbuf[1] = 0xB5; sendbuf[2] = 0x05; sendbuf[33] = 0xFF; sendbuf[34] = 0xFC; sendbuf[35] = 0xFF; sendbuf[36] = 0xFF; u8 a; RS485_1_TX_EN = 1; // 设置为发送模式 if (T <= 500000) { TEM1 = RS485_RX_BUF_COPY[3]; TEM2 = RS485_RX_BUF_COPY[4]; } if (H <= 1000) { HUM1 = RS485_RX_BUF_COPY[5]; HUM2 = RS485_RX_BUF_COPY[6]; } sendbuf[3] = TEM1; sendbuf[4] = TEM2; sendbuf[5] = HUM1; sendbuf[6] = HUM2; RED_LIGHT = total[9 + 18 * (now_stage - 1)] << 8 | total[10 + 18 * (now_stage - 1)]; BLUE_LIGHT = total[11 + 18 * (now_stage - 1)] << 8 | total[12 + 18 * (now_stage - 1)]; WHITE_LIGHT = total[13 + 18 * (now_stage - 1)] << 8 | total[14 + 18 * (now_stage - 1)]; if (RED_LIGHT <= 1000) { red_light1 = total[9 + 18 * (now_stage - 1)]; red_light2 = total[10 + 18 * (now_stage - 1)]; } if (BLUE_LIGHT <= 1000) { blue_light1 = total[11 + 18 * (now_stage - 1)]; blue_light2 = total[12 + 18 * (now_stage - 1)]; } if (WHITE_LIGHT <= 1000) { white_light1 = total[13 + 18 * (now_stage - 1)]; white_light2 = total[14 + 18 * (now_stage - 1)]; } sendbuf[7] = red_light1; sendbuf[8] = red_light2; // 红光 sendbuf[9] = blue_light1; sendbuf[10] = blue_light2; // 蓝光 sendbuf[11] = white_light1; sendbuf[12] = white_light2; // 白光 if (C <= 5000) { CO2_1 = RS485_RX_BUF_COPY[19]; CO2_2 = RS485_RX_BUF_COPY[20]; } sendbuf[13] = CO2_1; 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) { 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[21] = humidity_flag; // humidity state sendbuf[22] = ALARM; // ALARM state sendbuf[23] = (int)pid.Kp / 256; // Kp 110 sendbuf[24] = (int)pid.Kp % 256; // Kp 110 sendbuf[25] = ((int)(pid.Ki * 100000)) / 256; // Ti 0.001 sendbuf[26] = ((int)(pid.Ki * 100000)) % 256; // Ti 0.001 sendbuf[27] = (int)pid.Kd / 256; // Td 340 sendbuf[28] = (int)pid.Kd % 256; // Td 340 // num = (((pid.OUT * 350) / pid.pwmcycle) - 1); int out1 = pid.OUT; // speed count int speed_count = out1 / 200.0 * (6000 - 1500) + 1500; if (speed_count > 6000) { speed_count = 6000; } sendbuf[29] = (speed_count) / 256; sendbuf[30] = (speed_count) % 256; // sendbuf[29] = ((int)(pid.OUT * 1000)) / 256; // sendbuf[30] = ((int)(pid.OUT * 1000)) % 256; bufcut_Init(sendbuf_crc, sendbuf, 3, 31); GetCRC16(sendbuf_crc, 28, &crc_num1, &crc_num2); sendbuf[31] = crc_num1; sendbuf[32] = crc_num2; for (a = 0; a < 37; a++) // 循环发送数据 { while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; USART_SendData(USART1, sendbuf[a]); // if(a==0)printf("%x\r\n",sendbuf[0]); } while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; // RS485_RX_CNT=0; RS485_1_TX_EN = 0; // 设置为接收模式 } void RS485_1_Send_Data_3(void) { u8 batchbuf[8] = {0xEE, 0xB5, 0x01, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; u8 t; RS485_1_TX_EN = 1; // 设置为发送模式 batchbuf[3] = NUM; NUM += 1; if (NUM > 6) NUM = 1; for (t = 0; t < 8; t++) // 循环发送数据 { while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; USART_SendData(USART1, batchbuf[t]); } while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) ; // RS485_RX_CNT=0; RS485_1_TX_EN = 0; // 设置为接收模式 } // void RS485_3_Send_Data_2(void)//上传当前周期(1),周期时长(2),已运行时长(2),红蓝白光(6),加热制冷状态(1),加湿除湿状态(1),报警状态(1) //{ // 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 a; // GPIO8_Init(); // GPIO_SetBits(GPIOB,GPIO_Pin_8); // RS485_3_TX_EN=1; //设置为发送模式 // sendbuf[3]=t; // sendbuf[4]=total[3 + 18 * (t - 1)]; // sendbuf[5]=total[4 + 18 * (t - 1)]; // sendbuf[6]=chour; // sendbuf[7]=cminute; // sendbuf[8]=total[9 + 18 * (t - 1)]; // sendbuf[9]=total[10 + 18 * (t - 1)]; // sendbuf[10]=total[11 + 18 * (t - 1)]; // sendbuf[11]=total[12 + 18 * (t - 1)]; // sendbuf[12]=total[13 + 18 * (t - 1)]; // sendbuf[13]=total[14 + 18 * (t - 1)]; // sendbuf[14]=hot_clod_flag; // sendbuf[15]= humidity_flag; // sendbuf[16]=ALARM; // // // for(a=0;a<21;a++) //循环发送数据 // { // while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); // USART_SendData(USART1,sendbuf[a]); // } // while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); // RS485_RX_CNT=0; // GPIO_ResetBits(GPIOB,GPIO_Pin_8); // RS485_3_TX_EN=0; //设置为接收模式 //} // RS485发送len个字节. // buf:发送区首地址 // len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节) void RS485_3_Send_Data(u8 *buf,u8 len) { u8 t; // GPIO8_Init(); // GPIO_SetBits(GPIOB,GPIO_Pin_8); RS485_3_TX_EN=1; //设置为发送模式 for(t=0;t