STM32 UART串口驱动程序
文章原始地址: http://feotech.com/?p=56
示例1.通過UART1進行數(shù)據(jù)發(fā)送
UART 1 的初始化
將數(shù)據(jù)0xBA通過UART1發(fā)送出
USART_SendData(USART1,0xBA); //Send Data 0xBA---------- 示例2.通過對UART1進行接收中斷配置,收到外部數(shù)據(jù)后進入中斷讀取數(shù)據(jù) UART 1 的初始化 開啟接收中斷/**
-
@brief UART1 Initialise.
-
@param None.
-
@retval None.
-
@brief Data transfer PC
*/
void UART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //UART1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); ////UART1_TX GPIOA_Pin_9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOC_Pin_9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
GPIO_Init(GPIOA, &GPIO_InitStructure); ////UART1_RX GPIOA_Pin_10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //GPIOA_Pin_10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);//UART1通訊參數(shù)配置
USART_InitStructure.USART_BaudRate = 115200; //通訊波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8個數(shù)據(jù)位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //
USART_Init(USART1, &USART_InitStructure);//Uart1 NVIC 中斷配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //數(shù)據(jù)接收中斷使能
NVIC_Init(&NVIC_InitStructure);USART_Cmd(USART1, ENABLE);
}
中斷入口函數(shù),變量rxdat用于存放接收到的1個字節(jié)數(shù)據(jù)
void USART1_IRQHandler(void) {unsigned char rxdat ;if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){USART_ClearITPendingBit(USART1, USART_IT_RXNE); //Clear flagrxdat = USART_ReceiveData(USART1);} }總結(jié)
以上是生活随笔為你收集整理的STM32 UART串口驱动程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Visual Studio 2013运行
- 下一篇: ST单片机使用ST Visual Pro