USART源码
单片机源程序如下:
- #include "usart.h"
- //The Data Buffer
- u8 Usart1_Buffer[DATABUFFER];
- u8 Usart1_Rx = 0;
- u8 Usart1_Tx = 0;
- u8 Usart1_Len=0;
- u8 Usart1_Sta=0;
- /**************************************
- * @brief Configures the SysTem Usart1 *
- * @param None *
- * @retval None *
- **************************************/
- void Usart1_Config(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /*使能usart1的时钟 */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
-
- /*复位USART1*/
- USART_DeInit(USART1);
-
- /*引脚配置*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /*串口通讯配置*/
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
- /*Usart1的优先级设置,优先级:抢占优先级为0,响应优先级为1*/
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- /*抢占优先级设为0*/
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
- /*响应优先级为1*/
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- /*打开接收中断使能 */
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1, ENABLE);
- }
- /**************************************
- * @brief Configures the SysTem Usart2 *
- * @param None *
- * @retval None *
- **************************************/
- void Usart2_Config(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- //使能usart2的时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- USART_DeInit(USART2);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /*配置usart2参数*/
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStructure);
-
- /*USART2的优先级设置,优先级为:抢占优先级为:0,响应优先级为:0*/
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /*打开接收中断使能*/
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART2, ENABLE);
- }
- /**************************************
- * @brief Configures the SysTem Usart3 *
- * @param None *
- * @retval None *
- **************************************/
- void Usart3_Config(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /*使能usart3的时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- USART_DeInit(USART3);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- /*配置usart3参数*/
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART3, &USART_InitStructure);
-
- /*USART3的优先级设置,优先级为:抢占优先级为:1,响应优先级为:0*/
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /*打开接收中断使能*/
- USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART3, ENABLE);
- }
- /**************************************
- * @brief Configures the SysTem Usart4 *
- * @param None *
- * @retval None *
- **************************************/
- void Uart4_Config(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /*使能uart4的时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
- USART_DeInit(UART4);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- /*配置usart4参数*/
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(UART4, &USART_InitStructure);
-
- /*USART4的优先级设置,优先级为:抢占优先级为:1,响应优先级为:1*/
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /*打开接收中断使能*/
- USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
- USART_Cmd(UART4, ENABLE);
- }
- /**************************************
- * @brief Configures the SysTem Usart5 *
- * @param None *
- * @retval None *
- **************************************/
- void Uart5_Config(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /*使能uart5的时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
- USART_DeInit(UART5);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- /*配置usart5参数*/
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(UART5, &USART_InitStructure);
-
- /*USART5的优先级设置,优先级为:抢占优先级为:1,响应优先级为:2*/
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /*打开接收中断使能*/
- USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
- USART_Cmd(UART5, ENABLE);
- }
- /********************************************************
- * @brief This function handles Usart1 interrupt request*
- * @param None *
- * @retval None *
- ********************************************************/
- void USART1_IRQHandler(void)
- {
- /*产生中断 */
- if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
- {
- /*清楚中断标志 */
- USART_ClearITPendingBit(USART1,USART_IT_RXNE);
- Usart1_Buffer[Usart1_Rx] = USART_ReceiveData(USART2);
- Usart1_Rx++;
- Usart1_Rx &= 0xFF;
- }
- if(Usart1_Buffer[Usart1_Rx-1] == 0x5A) //
- Usart1_Tx = Usart1_Rx-1;
- if((Usart1_Buffer[Usart1_Tx] == 0x5A)&&(Usart1_Buffer[Usart1_Rx-1] == 0xA5)) //
- {
- Usart1_Len = Usart1_Rx-1- Usart1_Tx; //
- Usart1_Sta=1; //
- }
- if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) //
- {
- USART_ClearFlag(USART1,USART_FLAG_ORE); //
- USART_ReceiveData(USART1); //
- }
- /*if( Uart2_Sta )
- {
- for(tx2=0;tx2 <= Uart2_Len;tx2++,Uart2_Tx++)
- USART2_SendByte(Uart2_Buffer[Uart2_Tx]); //
- Uart2_Rx = 0; //
- Uart2_Tx = 0;
- Uart2_Sta = 0;
- }*/
-
- }
- /********************************************************
- * @brief This function handles Usart2 interrupt request*
- * @param None *
- * @retval None *
- ********************************************************/
- void USART2_IRQHandler(void)
- {
- }
- /********************************************************
- * @brief This function handles Usart3 interrupt request*
- * @param None *
- * @retval None *
- ********************************************************/
- void USART3_IRQHandler(void)
- {
- }
- /********************************************************
- * @brief This function handles Uart4 interrupt request.*
- * @param None *
- * @retval None *
- ********************************************************/
- void UART4_IRQHandler(void)
- {
- }
- /********************************************************
- * @brief This function handles Uart5 interrupt request.*
- * @param None *
- * @retval None *
- ********************************************************/
- void UART5_IRQHandler(void)
- {
- }
复制代码
所有资料51hei提供下载:
usart通讯.zip
(2.07 KB, 下载次数: 6)
|