|
|
|
@ -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|