include "Key.h" /* ---------------------------------------------------------------------------- * @breif:发送按键信息给从机 * @input:NONE * @retval:NONE */ void Send_Key_Inf(void) { uint16_t crc16=0; //校验码 if(sending_flag[USART1_Locat] == 0) { //没有报文正在发送 if(Dealwith.Key1_REG != 0 || Dealwith.Key2_REG != 0) { //有按键按下 Txbuf[0] = SLAVE_ADDRESS; //装置地址 Txbuf[1] = MOD_WRITE_M_REGISTER; Txbuf[2] = (uint8_t)(ADR_KEY_START>>8); Txbuf[3] = (uint8_t)(ADR_KEY_START); Txbuf[4] = 0x00; Txbuf[5] = 0x02; Txbuf[6] = 0x04; Txbuf[7] = (uint8_t)(Dealwith.Key1_REG>>8); Txbuf[8] = (uint8_t)(Dealwith.Key1_REG); Txbuf[9] = (uint8_t)(Dealwith.Key2_REG>>8); Txbuf[10] = (uint8_t)(Dealwith.Key2_REG); crc16 = CRC16(Txbuf,11); Txbuf[11] = (uint8_t)(crc16); Txbuf[12] = (uint8_t)(crc16>>8); Txlen = 13; //发送数据 DMA_Usart1_Send(Txbuf,Txlen); } } }将代码修改为被动发送数据
时间: 2024-04-12 17:28:52 浏览: 98
下面是将代码修改为被动发送数据的版本:
```c
#include "Key.h"
/* ----------------------------------------------------------------------------
* @brief:发送按键信息给从机
* @input:NONE
* @retval:NONE
*/
void Send_Key_Inf(void)
{
uint16_t crc16 = 0; //校验码
if (sending_flag[USART1_Locat] == 0)
{
//没有报文正在发送
if (Dealwith.Key1_REG != 0 || Dealwith.Key2_REG != 0)
{
//有按键按下
Txbuf[0] = SLAVE_ADDRESS; //装置地址
Txbuf[1] = MOD_WRITE_M_REGISTER;
Txbuf[2] = (uint8_t)(ADR_KEY_START >> 8);
Txbuf[3] = (uint8_t)(ADR_KEY_START);
Txbuf[4] = 0x00;
Txbuf[5] = 0x02;
Txbuf[6] = 0x04;
Txbuf[7] = (uint8_t)(Dealwith.Key1_REG >> 8);
Txbuf[8] = (uint8_t)(Dealwith.Key1_REG);
Txbuf[9] = (uint8_t)(Dealwith.Key2_REG >> 8);
Txbuf[10] = (uint8_t)(Dealwith.Key2_REG);
crc16 = CRC16(Txbuf, 11);
Txbuf[11] = (uint8_t)(crc16);
Txbuf[12] = (uint8_t)(crc16 >> 8);
Txlen = 13;
//发送数据
DMA_Usart1_Send(Txbuf, Txlen);
// 设置发送标志位,表示数据已被动发送
sending_flag[USART1_Locat] = 1;
}
}
}
```
这里将原来的代码加了一行 `sending_flag[USART1_Locat] = 1;` ,设置发送标志位,表示数据已被动发送。这样在下次调用 `Send_Key_Inf` 函数时,会先检查 `sending_flag` 标志位,如果为1,则说明数据已经被动发送,不会再次发送。这样就实现了被动发送数据的功能。
阅读全文