STM32F103F3P6串口中断收发
扫描二维码
随时随地手机看文章
使用固件版本:STM8S_StdPeriph_Lib_V2.1.0.zip
主程序如下:
void main(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
UART1_Init((uint32_t)9600,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
/* Enable the UART Receive interrupt: this interrupt is generated when the UART receive data register is not empty */
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
/* Enable the UART Transmit complete interrupt: this interrupt is generated when the UART transmit Shift Register is empty */
UART1_ITConfig(UART1_IT_TXE, ENABLE);
/* Enable UART */
UART1_Cmd(ENABLE);
/* Enable general interrupts */
enableInterrupts();
while (1)
{
}
}
中断程序如下:
#define TX_BUFFER_SIZE (countof(TxBuffer) - 1)
#define countof(a) (sizeof(a) / sizeof(*(a)))
uint8_t TxBuffer[] = "UART1-communication using Interrupt modelsim_tyc@163.comn";
__IO uint8_t TxCounter = 0;
INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)
{
UART1_SendData8(TxBuffer[TxCounter++]);
if (TxCounter == TX_BUFFER_SIZE)
UART1_ITConfig(UART1_IT_TXE, DISABLE);
}
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
uint8_t temp;
/* Read one byte from the receive data register and send it back */
temp = UART1_ReceiveData8();
UART1_SendData8(temp);
}