專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

STM32 串口中斷接收數(shù)據(jù)

作者:黃賓山   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月03日   【字體:

#include "stm32f10x.h"
/***********************************************************************
***********************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void delay(vu32 nCount)
 {
 for(; nCount != 0; nCount--);
 }
/***********************************************************************
************************************************************************/


main()
{
 RCC_Configuration();//系統(tǒng)時(shí)鐘配置
  
 NVIC_Configuration();//中斷配置
 
 GPIO_Configuration();//GPIO口配置

 while(1)          //LED燈循環(huán)亮滅,串口循環(huán)發(fā)送ASCII“9”
 {
  delay(5000000);
  GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_RESET); 
  USART_SendData(USART1,'9');
     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
  {
  }
  delay(5000000);
  GPIO_WriteBit(GPIOD,GPIO_Pin_2,Bit_SET);       
 }
}
/*****************************************************************************
*****************************************************************************/
//系統(tǒng)時(shí)鐘配置
void RCC_Configuration(void)

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能串口1的GPIO時(shí)鐘
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE); //pa.9 pa.10 led0
}
/*****************************************************************************
*****************************************************************************/
//串口GPIO口配置
void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 
 /* LED0*/
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_Init(GPIOD, &GPIO_InitStructure);

// GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //復(fù)用推挽輸出
 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 = 9600;      //設(shè)定波特率
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //傳輸數(shù)據(jù)位數(shù)
 USART_InitStructure.USART_StopBits = USART_StopBits_1;   //停止位1
 USART_InitStructure.USART_Parity = USART_Parity_No;    //不用校驗(yàn)位
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不用流量控制
 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //使用接收和發(fā)送功能
 USART_Init(USART1, &USART_InitStructure);       //初始化串口1
 USART_Cmd(USART1,ENABLE);            //串口1使能

 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);     //使能串口1 讀中斷


}

/*****************************************************************************
*****************************************************************************/
//中斷配置
void NVIC_Configuration(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);     //先占優(yōu)先權(quán)2,從優(yōu)先級2位
 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;    //開串口中斷1
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;   //指定搶占優(yōu)先級別1
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;    //指定相應(yīng)優(yōu)先級別0
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
}
/*****************************************************************************
*****************************************************************************/
void USART1_IRQHandler(void)       //串口接收中斷,并將接收到得數(shù)據(jù)發(fā)送出
{
 u16 temp_trx;
 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//判斷 是否 接收中斷 
 {
  temp_trx = USART_ReceiveData(USART1);
  USART_SendData(USART1,temp_trx);
     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//判斷 發(fā)送標(biāo)志
 }
}
/*****************************************************************************
*****************************************************************************/

關(guān)閉窗口

相關(guān)文章