实验要求:实现stm32的单个字符传输
第一步`在这里插入
//(1)第一步,使能串口时钟和对应GPIO口的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能 GPIOA 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE );//使能 USART1
第二步
// (2)设置引脚复用器映射:调用 GPIO_PinAFConfig 函数。
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);//PA9 复用为 USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//PA10 复用为 USART1
第三步
// (3)GPIO 初始化设置:要设置模式为复用功能。
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;//GPIO口A9和A10
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Initstructure.GPIO_Speed = GPIO_Fast_Speed;//速度 50MHz
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_Init(GPIOA,&GPIO_Initstructure); //初始化 PA9,PA10
第四步
//(4)USART 初始化设置
USART_Initstructure.USART_BaudRate =115200;//波特率
USART_Initstructure.USART_WordLength = USART_WordLength_8b;//字长为八位数据格式
USART_Initstructure.USART_StopBits =USART_StopBits_1 ;//一个停止位
USART_Initstructure.USART_Parity = USART_Parity_No ;//无奇偶校验
USART_Initstructure.USART_Mode =USART_Mode_Rx|USART_Mode_Tx ;//接收和发送模式
USART_Initstructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None ;//异步不需要硬件流控制
USART_Init(USART1,&USART_Initstructure);//初始化串口
第五步
//配置中断
NVIC_Configuration();
//使能串口中断
USART_Cmd(USART1, ENABLE);
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
//使能串口接收中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
第七步
//发送单个字符函数
void Usart_SendByte(USART_TypeDef *pUSART,uint8_t ch)
{
USART_SendData(USART1,ch);
while( USART_GetITStatus(USART1,USART_IT_TXE)==RESET);//获取中断状态标志位.等待发送完
}
****led.h****
#include "led.h"
//初始化PF9和PF10为输出口.并使能这两个口的时钟
//LED IO初始化
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
//GPIOF9,F10初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
}
key.c
#include "key.h"
#include "delay.h"
/*按键初始化*/
void Key_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO的初始化
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //KEY1 KEY0 对应引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PB8,PB9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//WK_UP对应引脚PA0
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;//下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA0
}
main.c
#include "sys.h"
#include "delay.h"
#include "My_usart.h"
#include "led.h"
#include "key.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //延时初始化
My_usart_init();
LED_Init() ; //初始化与LED连接的硬件接口
Usart_SendByte(USART1,'a');
while(1)
{
;
}
}
#include "My_usart.h"
#include "sys.h"
void My_usart_init()
{
GPIO_InitTypeDef GPIO_Initstructure;//GPIO初始化结构体
USART_InitTypeDef USART_Initstructure;//串口初始化结构体
//(1)第一步,使能串口时钟和对应GPIO口的时钟
//GPIOA 和 USART1 时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能 GPIOA 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE );//使能 USART1
// (2)设置引脚复用器映射:调用 GPIO_PinAFConfig 函数。
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);//PA9 复用为 USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);//PA10 复用为 USART1
// (3)GPIO 初始化设置:要设置模式为复用功能。
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;//GPIO口A9和A10
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Initstructure.GPIO_Speed = GPIO_Fast_Speed;//速度 50MHz
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;//推挽复用输出
GPIO_Init(GPIOA,&GPIO_Initstructure); //初始化 PA9,PA10
//(4)USART 初始化设置
USART_Initstructure.USART_BaudRate =115200;//波特率
USART_Initstructure.USART_WordLength = USART_WordLength_8b;//字长为八位数据格式
USART_Initstructure.USART_StopBits =USART_StopBits_1 ;//一个停止位
USART_Initstructure.USART_Parity = USART_Parity_No ;//无奇偶校验
USART_Initstructure.USART_Mode =USART_Mode_Rx|USART_Mode_Tx ;//接收和发送模式
USART_Initstructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None ;//异步不需要硬件流控制
USART_Init(USART1,&USART_Initstructure);//初始化串口
//配置中断
NVIC_Configuration();
//使能串口中断
USART_Cmd(USART1, ENABLE);
}
//中断服务配置
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
//使能串口接收中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
//发送单个字符函数
void Usart_SendByte(USART_TypeDef *pUSART,uint8_t ch)
{
USART_SendData(USART1,ch);
while( USART_GetITStatus(USART1,USART_IT_TXE)==RESET);//获取中断状态标志位.等待发送完
}
My_usart.h
#ifndef __MY_USART_H
#define __MY_USART_H
#include "stdio.h"
#include "stm32f4xx_conf.h"
#include "sys.h"
#define EN_USART1_RX 1 //使能(1)/禁止(0)串口1接收
//如果想串口中断接收,请不要注释以下宏定义
void My_usart_init(void);//串口初始化
void NVIC_Configuration(void);//中断配置函数
void Usart_SendByte(USART_TypeDef *pUSART,uint8_t ch);//单个数据发送函数
#endif