STM32F40x学习之USART
来源:互联网 发布:软件自制license实现 编辑:程序博客网 时间:2024/06/05 02:39
STM32F40x系列单片机的外设使用基本上要遵循三步曲
1、时钟使能
2、GPIO配置(如果涉及到GPIO的外设)
3、对应的外设配置并使能
对于USART的配置也不例外,下面以USART3为例进行介绍。
第一步:配置USART3的功能,使之可以通过USART3发送数据。(发送数据接收数据容易多了)
/*
本人使用的芯片信息如下:
名称:STM32F407IGT6
时钟:168MHz
相关硬件连线,USART3的TX/RX对应GPIO引脚如下:
PD8 -- USART3_TX
PD9 -- USART3_RX
*/
/*
三步曲第一步:时钟配置
*/
/*
使能:GPIOD的时钟,查阅STM32F40x的手册我们可以知道GPIOD是AHB1设备
*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);
/*使能:USART3的时钟,查阅STM32F40x的手册我们可以知道USART3是APB1设备*/
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
/*三步曲第二步:GPIO配置*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //使用内部上拉电阻
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //使用复用功能模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Usart3 Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//配置GPIOD的pin8为Tx
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 开启GPIO引脚的复用功能*/
/*要特别注意的是GPIO_PinAFConfig函数配置时用GPIO_PinSource8而不是GPIO_Pin8*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
/*三步曲地三步:USART配置*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //波特率为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位为8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //校验为不校验
USART_InitStructure.USART_HardwareFlowControl
= USART_HardwareFlowControl_None; //关闭硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Tx; //发送模式
/*初始化USART3设备*/
USART_Init(USART3, &USART_InitStructure);
/*使能USART3设备*/
USART_Cmd(USART3, ENABLE);
/*完整的例程如下*/
void usartCfg(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO Clock, gpio的时钟是在AHB1*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);
/*Enable USART3 Clock , usart的时钟是APB1*/
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Usart3 Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStructure.USART_BaudRate = 115200;//BaudRate default value
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_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
/*下面我们就可以屌丝般地通过串口向PC机发送大名鼎鼎的"Hello World!\r\n" 了*/
void usartSend(char*str)
{
int i=0;
char * ptr = (uint8_t*)pbuf;
while ( *ptr != (char)0 )
{
USART_SendData(USART3,*ptr++);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{
}
}
}
/**/
void main(void)
{
/*
这里要先加上你自己的系统时钟初始化函数.
*/
usartCfg();
usartSend("Hello World\r\n");
while(1)
{
}
}
1、时钟使能
2、GPIO配置(如果涉及到GPIO的外设)
3、对应的外设配置并使能
对于USART的配置也不例外,下面以USART3为例进行介绍。
第一步:配置USART3的功能,使之可以通过USART3发送数据。(发送数据接收数据容易多了)
/*
本人使用的芯片信息如下:
名称:STM32F407IGT6
时钟:168MHz
相关硬件连线,USART3的TX/RX对应GPIO引脚如下:
PD8 -- USART3_TX
PD9 -- USART3_RX
*/
/*
三步曲第一步:时钟配置
*/
/*
使能:GPIOD的时钟,查阅STM32F40x的手册我们可以知道GPIOD是AHB1设备
*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);
/*使能:USART3的时钟,查阅STM32F40x的手册我们可以知道USART3是APB1设备*/
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
/*三步曲第二步:GPIO配置*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //使用内部上拉电阻
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //使用复用功能模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Usart3 Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//配置GPIOD的pin8为Tx
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* 开启GPIO引脚的复用功能*/
/*要特别注意的是GPIO_PinAFConfig函数配置时用GPIO_PinSource8而不是GPIO_Pin8*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
/*三步曲地三步:USART配置*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; //波特率为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位为8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //校验为不校验
USART_InitStructure.USART_HardwareFlowControl
= USART_HardwareFlowControl_None; //关闭硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Tx; //发送模式
/*初始化USART3设备*/
USART_Init(USART3, &USART_InitStructure);
/*使能USART3设备*/
USART_Cmd(USART3, ENABLE);
/*完整的例程如下*/
void usartCfg(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO Clock, gpio的时钟是在AHB1*/
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD , ENABLE);
/*Enable USART3 Clock , usart的时钟是APB1*/
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //Usart3 Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStructure.USART_BaudRate = 115200;//BaudRate default value
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_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
/*下面我们就可以屌丝般地通过串口向PC机发送大名鼎鼎的"Hello World!\r\n" 了*/
void usartSend(char*str)
{
int i=0;
char * ptr = (uint8_t*)pbuf;
while ( *ptr != (char)0 )
{
USART_SendData(USART3,*ptr++);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{
}
}
}
/**/
void main(void)
{
/*
这里要先加上你自己的系统时钟初始化函数.
*/
usartCfg();
usartSend("Hello World\r\n");
while(1)
{
}
}
- STM32F40x学习之USART
- STM32之USART学习
- STM32之USART学习
- stm32之USART学习
- STM32F40x/STM32F41x in-application programming using the USART中文版
- STM32F40x/STM32F41x in-application programming using the USART中文版
- STM32F2系列之USART学习
- STM32F2系列之USART学习
- STM32F2系列之USART学习
- STM32学习之USART串口
- STM32学习之:USART中断方式
- STM32学习系列----STM32之USART
- STM32F10x 学习笔记之USART实现串口通讯
- STM32F10x 学习笔记之USART实现串口通讯 DMA 方式
- STM32入门学习之USART(STM32F030F4P6基于CooCox IDE)
- 嵌入式Linux驱动学习之USART串口控制:基于AT91SAM9261EK
- [HAL库学习之路]3.USART-异步串行通信
- STM32F407 之 基于 STM32F40x 固件库的 MDK5 工程
- i2c_device_id数据结构分析
- C++获取输入的空格
- 我的设计模式学习之路2(工厂模式)
- UVa 437 - The Tower of Babylon
- hdu 1238 Substrings
- STM32F40x学习之USART
- 推荐10款来自极客标签的超棒前端特效[第二期]
- UVA 10098 - Generating Fast
- 重载初步
- CentOS 6.4 安装 samba (未完)
- 分数类--运算符重载函数---完整版--gyy
- 虚拟机上初学Linux(Ubuntu)
- 继承初步
- 继承初步2