STM32F103串口4和串口5程序(中断接收方式)


头文件
 1 ///**************************************///
 2 //author zq&jp                            //
 3 //net zq-jp.cnblogs.com                   //
 4 //qq 627334696                            //
 5 //blog zq-jp.cnblogs.com                  //
 6 ///**************************************///
 7 
 8 #ifndef __USART_4_H
 9 #define __USART_4_H
10 
11 #include "stm32f10x.h"
12 #include "stm32f10x_usart.h"
13 #include "misc.h"
14 #include "stdarg.h"
15 
16 void USART4_GPIO_Configuration(void);
17 void USART4_NVIC_Configuration(void);
18 
19 void USART4_Config(USART_TypeDef* USARTx);
20 
21 
22 
23 #endif
View Code

 

 
配置程序
  1 ///**************************************///
  2 //author zq&jp                            //
  3 //net zq-jp.cnblogs.com                   //
  4 //qq 627334696                            //
  5 //blog zq-jp.cnblogs.com                  //
  6 ///**************************************///
  7 #include "USART_4.h"
  8 
  9 
 10 /* Private variables ---------------------------------------------------------*/
 11 
 12 uint8_t TxBuffer4[] = "USART Interrupt Example: This is USART4 DEMO";  
 13 uint8_t RxBuffer4[];
 14 __IO uint8_t TxCounter4 = 0x00;
 15 __IO uint8_t RxCounter4 = 0x00; 
 16 
 17 
 18 static GPIO_InitTypeDef GPIO_InitStructure;
 19 static USART_InitTypeDef USART_InitStructure;
 20 
 21 
 22 /****************************************************************************
 23 * 名    称:USART_Config(USART_TypeDef* USARTx)
 24 * 功    能:配置串口
 25 * 入口参数:
 26 * 出口参数:无
 27 * 说    明:
 28 * 调用方法:例如: USART_Config(USART1)
 29 ****************************************************************************/
 30 void USART4_Config(USART_TypeDef* USARTx){
 31     
 32     RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);    
 33     
 34   USART_InitStructure.USART_BaudRate = 115200;                        //速率115200bps
 35   USART_InitStructure.USART_WordLength = USART_WordLength_8b;        //数据位8位
 36   USART_InitStructure.USART_StopBits = USART_StopBits_1;            //停止位1位
 37   USART_InitStructure.USART_Parity = USART_Parity_No;                //无校验位
 38   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //无硬件流控
 39   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                    //收发模式
 40 
 41   /* Configure UART4 */
 42   USART_Init(USARTx, &USART_InitStructure);                            //配置串口参数函数
 43  
 44   
 45   /* Enable USART1 Receive and Transmit interrupts */
 46 //    USART_ClearFlag(UART4,USART_IT_RXNE);
 47  // USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);                    //使能接收中断
 48  // USART_ITConfig(UART4, USART_IT_TXE, ENABLE);                        //使能发送缓冲空中断   
 49 
 50   /* Enable the UART4 */
 51   USART_Cmd(UART4, ENABLE);    
 52 }
 53 
 54 
 55 /****************************************************************************
 56 * 名    称:void GPIO_Configuration(void)
 57 * 功    能:通用IO口配置
 58 * 入口参数:无
 59 * 出口参数:无
 60 * 说    明:
 61 * 调用方法:
 62 ****************************************************************************/  
 63 void USART4_GPIO_Configuration(void)
 64 {
 65 
 66     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);         
 67 
 68   /* 默认复用功能 */
 69   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                      //USART4 TX
 70   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;             //复用推挽输出
 71   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 72     GPIO_Init(GPIOC, &GPIO_InitStructure);                     //A端口 
 73   /* 复用功能的输入引脚必须配置为输入模式(浮空/上拉/下拉的一种)*/
 74   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;                  //USART4 RX
 75   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        //复用浮空输入
 76   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 77     GPIO_Init(GPIOC, &GPIO_InitStructure);                      //A端口 
 78 
 79 //    //485使能端
 80 //    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                  //USART5 RX
 81 //  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;             //推挽输出
 82 //  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 83 //    GPIO_Init(GPIOD, &GPIO_InitStructure);                      //D端口 
 84 
 85 }
 86 
 87 /****************************************************************************
 88 * 名    称:void NVIC_Configuration(void)
 89 * 功    能:中断源配置
 90 * 入口参数:无
 91 * 出口参数:无
 92 * 说    明:
 93 * 调用方法:无 
 94 ****************************************************************************/
 95 void USART4_NVIC_Configuration(void)
 96 {
 97   /*  结构声明*/
 98   NVIC_InitTypeDef NVIC_InitStructure;
 99 
100   /* Configure the NVIC Preemption Priority Bits */  
101   /* Configure one bit for preemption priority */
102   /* 优先级组 说明了抢占优先级所用的位数,和响应优先级所用的位数   在这里是0, 4 
103   0组:  抢占优先级占0位, 响应优先级占4位
104   1组:  抢占优先级占1位, 响应优先级占3位
105   2组:  抢占优先级占2位, 响应优先级占2位
106   3组:  抢占优先级占3位, 响应优先级占1位
107   4组:  抢占优先级占4位, 响应优先级占0位  
108   */                    
109   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);        
110 
111   NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;                     //设置串口4中断
112   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;             //抢占优先级 0
113   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //子优先级为0
114   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                    //使能
115   NVIC_Init(&NVIC_InitStructure);
116 
117 }
View Code

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM