STM32 Serial USART1 interrupt receive and interrupt send
First post the interrupt function:
void Usart1_irqhandler (void) {
if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
Usart_ Clearitpendingbit (USART1, usart_it_rxne);
Usart1_buffer[i++]=usart_receivedata (USART1); Usart1_buffesh is a self-defined receive array
if (i>3) {
sendflag = 1;
}
}
if (Usart_getitstatus (USART1, usart_it_txe)! = RESET) { //USART_IT_TC can also be replaced here
if (open_send_flag) { // This is a flag sent by
uart1_sendstring (temp_data);
Usart_itconfig (USART1, Usart_it_txe, DISABLE); USART_IT_TC
open_send_flag = 0;
}
else{
usart_itconfig (USART1, Usart_it_txe, DISABLE); USART_IT_TC
}}}
function to send a string:
void Uart1_sendstring (uint8_t* cp)
{
while ((*CP)! = ') ')
{
Usart1_send_isr (*CP);
cp++;
}
}
A function that sends a single character:
void Usart1_send_isr (uint8_t ch) {
usart_senddata (USART1, (uint8_t) ch);
while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET); Do you want the buffer to be sent after the decision?
}
Then open the send interrupt when the data is sent outside the interrupt function. Usart_itconfig (USART1, Usart_it_txe, ENABLE); Usart_it_tc
Then put out two simple serial reception and transmission of the test small example:
The first type: Interrupt Receive and send the received data directly.
void Usart1_irqhandler (void) {
unsigned char RxData;
if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
usart_clearitpendingbit (USART1, usart_it_rxne);
Rxdata=usart_receivedata (USART1);
RxData = RxData + 1;
Usart_senddata (Usart1,rxdata);
}
}
Advantages: simple, suitable for very small data transmission.
Disadvantage: No buffer, and the correctness of the data is not judged, the amount of data is slightly larger may lead to data loss.
The second kind: After the interrupt receives to judge the data head and the data end, if correctly sends out directly.
void Usart1_irqhandler (void) {
if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET) {
Usart_ Clearitpendingbit (USART1, usart_it_rxne);
Usart1_buffer[i++]=usart_receivedata (USART1);
}
if ((usart1_buffer[0] = = 0x01) && (usart1_buffer[i-1] = = 0x02)) {
Flag = 1;
}
if (Usart_getflagstatus (usart1,usart_flag_ore) = = SET) { //overflow, not understood.
Usart_clearflag (usart1,usart_flag_ore);
Usart_receivedata (USART1);
}
if (Flag) {for
(j = 0;j<20;j++) {
usart_senddata (usart1,usart1_buffer[j]);
}
}
}