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,在中断函数中加入相应的程序。
简单的配置例程:
- /**************************************************************
- 文件名:USART.c
- 功能:串口初始化配置以及相关函数
- 串口配置注意事项:
- 1、 stm32f10x_conf.h 中使能
- #include "stm32f10x_usart.h"
- #include "misc.h"
- 2、 本文件中定义的串口相关函数需要在头文件中做外部函数声明
- ***************************************************************/
- #include "STM32Lib//stm32f10x.h"
- void USART_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- //使能串口1,PA,AFIO总线
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
- RCC_APB2Periph_AFIO |
- RCC_APB2Periph_USART1 ,
- ENABLE);
- /* A9 USART1_Tx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* A10 USART1_Rx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入-RX
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 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;
- USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- USART_ClockInit(USART1, &USART_ClockInitStructure);
- USART_Init(USART1, &USART_InitStructure);
- /* Enable the USARTx */
- USART_Cmd(USART1, ENABLE);
- }
- void USART1_Putc(unsigned char c)
- {
- USART_SendData(USART1, c);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
- }
- void USART1_Puts(char * str)
- {
- while(*str)
- {
- USART_SendData(USART1, *str++);
- /* Loop until the end of transmission */
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- }
- }
- STM32串口通信程序设计要点
- STM32串口通信程序设计要点
- STM32串口通信程序设计要点
- STM32的串口通信
- STM32 串口USART 通信
- stm32串口间通信
- stm32串口通信
- STM32----------串口通信(USART)
- stm32-8串口通信
- STM32串口通信
- stm32串口通信
- STM32串口通信
- STM32之串口通信
- STM32 --UART串口通信
- stm32串口通信
- stm32串口通信
- stm32与串口通信
- STM32串口通信:串口通信库
- debian学习笔记1, su和su - root切换
- 关于import dalvik.system.VMRuntime;报错问题
- php多层数组和对象的转换
- delphi 解析Json格式
- ubuntu 绝技
- STM32串口通信程序设计要点
- tomcat在linux中的启动和查看相关信息
- java反射小例子
- Linux input子系统分析
- 基于 UML 的业务建模举例
- Android图形报表之AchartEngine(附开发包+API文档)
- 第十章 Java语言的输入输出与文件处理
- c/c++: float和double、long double
- 配置数据源,VC++ ADO连接ACCESS详解