// stm32f103c8t6实现usart接收到0xFF 0xFF 0xCE 0xCE,
// 发送0xEE 0xEE 0x01 0xCE
// 然后PA6输出高电平,否则为低电平
// 中断写法#include"stm32f10x.h#include"stm32f10x_usart.h"
#defineRX_BUFFER_SIZE4
uint8_t rxBuffer[RX_BUFFER_SIZE];
uint8_t txBuffer[] = {0xEE, 0xEE, 0x01, 0xCE};
void USART1_IRQHandler(void)
{
// check if received a byte in RX register
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // 判断接收缓冲区
{
static uint8_t i = 0;
uint8_t byte = USART_ReceiveData(USART1);
// store received byte in rx buffer
rxBuffer[i++] = byte;
// check if rx buffer is full
if (i == RX_BUFFER_SIZE)
{
if ((rxBuffer[0] == 0xFF) && (rxBuffer[1] == 0xFF) &&
(rxBuffer[2] == 0xCE) && (rxBuffer[3] == 0xCE))
{
// clear rx buffer
memset(rxBuffer, 0, RX_BUFFER_SIZE);
// send tx buffer
for (int i = 0; i < 4; i++)
{
USART_SendData(USART1, txBuffer[i]);
// wait until transmission complete
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
// configure PA6 as output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// set PA6 high
GPIO_SetBits(GPIOA, GPIO_Pin_6);
}
// reset counter
i = 0;
}
}
}