STM32串口通信程序设计要点

来源:互联网 发布:2017网络效应的答案 编辑:程序博客网 时间:2024/05/18 05:41

http://blog.csdn.net/haozi_1989/article/details/6109593

设计要点:

1、要是能串口时钟同时要是能复用总线时钟和对应的IO时钟,如:

 

//使能串口1,PA,AFIO总线

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | 

            RCC_APB2Periph_AFIO |

            RCC_APB2Periph_USART1 , 

            ENABLE);

2、stm32f10x_conf.h 中使能 

#include "stm32f10x_usart.h"  

#include "misc.h"

3、使能中断的话要配置NVIC,在中断函数中加入相应的程序。

简单的配置例程:

view plaincopy to clipboardprint?
  1. /************************************************************** 
  2. 文件名:USART.c 
  3. 功能:串口初始化配置以及相关函数 
  4. 串口配置注意事项: 
  5. 1、  stm32f10x_conf.h 中使能  
  6.     #include "stm32f10x_usart.h"   
  7.     #include "misc.h" 
  8. 2、  本文件中定义的串口相关函数需要在头文件中做外部函数声明  
  9. ***************************************************************/  
  10. #include "STM32Lib//stm32f10x.h"   
  11. void USART_Configuration(void)  
  12. {  
  13.     GPIO_InitTypeDef GPIO_InitStructure;  
  14.     USART_InitTypeDef USART_InitStructure;  
  15.     USART_ClockInitTypeDef USART_ClockInitStructure;  
  16.       
  17.     //使能串口1,PA,AFIO总线   
  18.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |   
  19.             RCC_APB2Periph_AFIO |  
  20.             RCC_APB2Periph_USART1 ,   
  21.             ENABLE);  
  22.     /* A9 USART1_Tx */  
  23.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  
  24.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  25.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //推挽输出-TX  
  26.     GPIO_Init(GPIOA, &GPIO_InitStructure);  
  27.     /* A10 USART1_Rx  */  
  28.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;  
  29.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX  
  30.     GPIO_Init(GPIOA, &GPIO_InitStructure);  
  31.   
  32.     USART_InitStructure.USART_BaudRate = 9600;  
  33.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;  
  34.     USART_InitStructure.USART_StopBits = USART_StopBits_1;  
  35.     USART_InitStructure.USART_Parity = USART_Parity_No;  
  36.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
  37.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  
  38.       
  39.     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;  
  40.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;  
  41.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;  
  42.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  
  43.     USART_ClockInit(USART1, &USART_ClockInitStructure);  
  44.     USART_Init(USART1, &USART_InitStructure);  
  45.     /* Enable the USARTx */  
  46.     USART_Cmd(USART1, ENABLE);  
  47. }  
  48. void USART1_Putc(unsigned char c)  
  49. {  
  50.     USART_SendData(USART1, c);  
  51.     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );  
  52. }  
  53. void USART1_Puts(char * str)  
  54. {  
  55.     while(*str)  
  56.     {  
  57.         USART_SendData(USART1, *str++);  
  58.         /* Loop until the end of transmission */  
  59.         while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  
  60.     }  
  61. }