1. 程式人生 > >[STM32F103]串列埠UART配置

[STM32F103]串列埠UART配置

l 串列埠時鐘使能,GPIO時鐘使能:

  RCC_APB2PeriphClockCmd();

l 串列埠復位:

  USART_DeInit(); 這一步不是必須的

l GPIO埠模式設定:

  GPIO_Init();

l 串列埠引數初始化:

  USART_Init();

l 開啟中斷並且初始化NVIC(如果需要開啟中斷才需要這個步驟)

       NVIC_Init();

       USART_ITConfig();

l 使能串列埠:

  USART_Cmd();

l 編寫中斷處理函式:

  USARTx_IRQHandler();

l 串列埠資料收發:

  void USART_SendData();//傳送資料到串列埠,DR

  uint16_t USART_ReceiveData();//接受資料,從DR讀取接受到的資料

l 串列埠傳輸狀態獲取:

  FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);

  void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);

 

l 原始碼:

 1 #pragma import(__use_no_semihosting)  
 2 struct __FILE 
 3 { 
 4     int handle; 
 5 }; 
 6 
 7 FILE __stdout;       
 8 void _sys_exit(int x) 
 9 { 
10     x = x; 
11 } 
12 int fputc(int ch, FILE *f)
13 {      
14 while((USART1->SR&0X40)==0); 
15     USART1->DR = (u8) ch;      
16 return ch; 17 } 18 19 void uart_init(u32 bound){ 20 //GPIO¶Ë¿ÚÉèÖà 21 GPIO_InitTypeDef GPIO_InitStructure; 22 USART_InitTypeDef USART_InitStructure; 23 NVIC_InitTypeDef NVIC_InitStructure; 24 25 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ 26 27 //USART1_TX GPIOA.9 28 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 29 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 30 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö 31 GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9 32 33 //USART1_RX GPIOA.10³õʼ»¯ 34 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 35 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë 36 GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10 37 38 //Usart1 NVIC ÅäÖà 39 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 40 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3 41 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3 42 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ 43 NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ 44 45 //USART ³õʼ»¯ÉèÖà 46 USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ 47 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ 48 USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ 49 USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ 50 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ 51 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½ 52 53 USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1 54 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖÐ¶Ï 55 USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1 56 57 } 58 59 void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò 60 { 61 u8 Res; 62 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ) 63 { 64 Res =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý 65 66 if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É 67 { 68 if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d 69 { 70 if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼ 71 else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË 72 } 73 else //»¹Ã»ÊÕµ½0X0D 74 { 75 if(Res==0x0d)USART_RX_STA|=0x4000; 76 else 77 { 78 USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ; 79 USART_RX_STA++; 80 if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ 81 } 82 } 83 } 84 }