stm32串口中断接收发送实验

 

int main()

 

{

 

  u32 i = 0;

 

  /*芯片配置初始化----------------------------------------------------------*/

 

  RCC_Configuration();                         //复位和时钟控制配置

 

  NVIC_Configuration();                        //嵌套中断向量控制器配置

 

  GPIO_Configuration();                        //I/O配置  

 

  USART_Configuration();                         //串口配置

 

  /*指示灯初始化------------------------------------------------------------*/

 

  GPIO_SetBits(GPIOA, GPIO_Pin_12);          //熄灭LED1

 

  GPIO_SetBits(GPIOA, GPIO_Pin_15);          //熄灭LED2

 

  while(1)

 

  {

 

     i++;

 

    if (500000 == i)      //LED1闪烁,表示正常。

 

    {

 

      GPIO_WriteBit(GPIOA, GPIO_Pin_12, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_12))); 

 

      i = 0;

 

    } 

 

  }

 

}

/******************************************************************************************

函数名称    :RCC_Configuration

功能描述    :

参数        :

*

返回值      :

作者        :

创建日期  :201352

全局变量    :

全局静态变量:

局部静态变量:

******************************************************************************************/

void RCC_Configuration(void)

{

  /* RCC system reset(for debug purpose) */

    RCC_DeInit();                                             //RCC系统初始化,设为默认值

 

    /* Enable HSE */

    RCC_HSEConfig(RCC_HSE_ON);                                //使能外部晶振(8MHz),关闭旁路

 

    /* Wait till HSE is ready */

    HSEStartUpStatus = RCC_WaitForHSEStartUp();               //等待外部高速时钟稳定

 

    if(HSEStartUpStatus == SUCCESS)                           //时钟就绪成功

    {

        /* Enable Prefetch Buffer */

        /* AHB预分频系数不为时,必须置预取缓冲区处于开启状态

           应与闪存存储器的访问时间一起使用

           激活和禁止预取模式时应该在禁止快速时钟时进行(关闭AHB的分频器) */ 

        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);     //

    

        /* HCLK = SYSCLK */

        RCC_HCLKConfig(RCC_SYSCLK_Div1);                       //SYSCLK不分频,作为AHB时钟(HCLK,闪存、SRAM等外设时钟设置):36M

  

        /* PCLK2 = HCLK */

        RCC_PCLK2Config(RCC_HCLK_Div1);                        //AHBCLK不分频,作为高速APB(APB2)时钟(PCLK2):36M

        /* PCLK1 = HCLK/2 */

        RCC_PCLK1Config(RCC_HCLK_Div1);                        //AHBCLK二分频,作为低速APB(APB1)时钟(PCLK1):36M

 

        /* ADCCLK = PCLK2/6 */

        RCC_ADCCLKConfig(RCC_PCLK2_Div6);                      //PCLK2 6分频,作为ADC时钟(ADCCLK):6M

 

        /* Flash 2 wait state */ 

        /* (SYSCLK)频率与闪存访问时间的关系:

           0等待周期,当0 < SYSCLK < 24MHz

           1等待周期,当24MHz < SYSCLK ≤ 48MHz

           2等待周期,当48MHz < SYSCLK ≤ 72MHz

        */

        FLASH_SetLatency(FLASH_Latency_1);                     //设置1个等待周期

 

   

    /* PLLCLK = 8MHz/2 * 9 = 36 MHz */

    RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);       //外部时钟2分频后,9倍频(PLL),36MHz

 

    /* Enable PLL */

    RCC_PLLCmd(ENABLE);                                        //使能PLL电路 

 

    /* Wait till PLL is ready */

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)         //等待PLL电路就绪 

    {

    }

 

    /* Select PLL as system clock source */

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);                 //选择PLL倍频时钟作为系统时钟(SYSCLK):36MHz

 

    /* Wait till PLL is used as system clock source */

    while(RCC_GetSYSCLKSource() != 0x08)                       //等待PLL切换成系统时钟源 

    {

    }

    

        

    /* Select HSE as system clock source */

  //  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);                   //选择外部晶振作为系统时钟源(SYSCLK)

    

    /* Wait till HSE is used as system clock source */

 //   while(RCC_GetSYSCLKSource() != 0x04)                      //等待HSE切换成系统时钟源,并作为输出时钟(该时钟可能会有毛刺,仅作调试)

 //  {

 //  }

  }

  

    /* GPIOAGPIOBGPIOCGPIODGPIOEGPIOFAFIO 时钟使能 */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC

                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_AFIO, ENABLE);     

 

 

 

  

    /* USART3 Periph clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);        //USART3时钟使能(APB2)

}

 

/******************************************************************************************

函数名称    :NVIC_Configuraion

功能描述    :

参数        :

*

返回值      :

作者        :

创建日期  :201352

全局变量    :

全局静态变量:

局部静态变量:

******************************************************************************************/

void NVIC_Configuration(void)

{

  NVIC_InitTypeDef NVIC_InitStructure;

  #ifdef  VECT_TAB_RAM    

          /* Set the Vector Table base location at 0x20000000 */ 

          NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);                      //RAM  

  #else  /* VECT_TAB_FLASH  */  

          /* Set the Vector Table base location at 0x08000000 */ 

          NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);                    //FLASH

  #endif

 

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                  //选择第2组为优先级分组方式:高2位指定抢占优先级,低2位指定响应优先级

 

  /* enabling interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;             //USART中断

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //占先级为0级,最高

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;                  //响应级最高0

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                     //启动中断

  NVIC_Init(&NVIC_InitStructure);                                     //初始化嵌套向量中断控制器

}

 

/******************************************************************************************

函数名称    :GPIO_Configuration

功能描述    :

参数        :

*

返回值      :

作者        :

创建日期  :201352

全局变量    :

全局静态变量:

局部静态变量:

******************************************************************************************/

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  

  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);    //关掉JTAG,释放PA15PB3PB4

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_15;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          //推挽输出

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);       

  

  /* Configure USART3 Tx (PB.10) as alternate function push-pull */

  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);

  

  /* Configure USART3 Rx (PB.11) as input floating */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOB, &GPIO_InitStructure);                            //RX初始化 

}

 

/******************************************************************************************

函数名称    :GPIO_Configuration

功能描述    :

参数        :

*

返回值      :

作者        :

创建日期  :201352

全局变量    :

全局静态变量:

局部静态变量:

******************************************************************************************/

void USART_Configuration(void)

  USART_InitTypeDef USART_InitStructure;

 

  USART_InitStructure.USART_BaudRate = 9600;//波特率9600

  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;//串口发送和接收模式

 

  /* Configure USART3 basic and asynchronous paramters */

  USART_Init(USART3, &USART_InitStructure);//串口初始化

 

  /* Enable USART3 */

  USART_Cmd(USART3, ENABLE);//指令打开

  /* Enables COM transmit interrupt */

  USART_ITConfig(USART3, USART_IT_TC,ENABLE);//发送中断打开

      

  /* Enables COM receive interrupt */

  USART_ITConfig(USART3, USART_IT_RXNE,ENABLE);//接收中断打开

}

 

void Delay(vu32 nCount)

{

  for(; nCount != 0; nCount--);

}

原文地址:https://www.cnblogs.com/xqzhao/p/3071315.html