可以添加上拉电阻,但会增加功耗,传输距离变长
要添加库函数USART
官方参考文档说明书位置
ALT+左键可实现整体删除(如下图)
输出模式第三种模式AF
----------------------
源码
远程控制pc端
#include <stm32f4xx.h>
#include "led.h"
#include "key.h"
#include "stdio.h"
//#define BIT_BAND(addr,bitnum)((addr&0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define PFout(n) (*(volatile uint32_t *)(0x42000000+(GPIOF_BASE+0x14-0x40000000)*32+(n)*4))
#define PAin(n) (*(volatile uint32_t *)(0x42000000+(GPIOA_BASE+0x10-0x40000000)*32+(n)*4))
#define PEout(n) (*(volatile uint32_t *)(0x42000000+(GPIOE_BASE+0x14-0x40000000)*32+(n)*4))
#define PEin(n) (*(volatile uint32_t *)(0x42000000+(GPIOE_BASE+0x10-0x40000000)*32+(n)*4))
static GPIO_InitTypeDef GPIO_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
void usart1_init(uint32_t baud)
{
//端口A硬件时钟打开
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//串口1硬件时钟打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//配置PA9 PA10为AF模式(复用功能)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 ; //9 10号引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//输出模式第三种模式AF模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;//高速,速度越高,响应越快,但是功耗会更高
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//不使能上下拉电阻
GPIO_Init(GPIOA,&GPIO_InitStructure);
//由于引脚支持很多功能,需要指定该引脚的功能,当前要制定支持USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//配置USART1相关参数:波特率,数据位,停止位,校验位
USART_InitStructure.USART_BaudRate = baud; //波特率,就是通信的速度
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_Rx | USART_Mode_Tx; //串口允许发送和接收数据
USART_Init(USART1, &USART_InitStructure);
//配置中断触发方式,接收到一个字节,就通知CPU处理
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//NVIC配置其优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开通道给NVIC管理
NVIC_Init(&NVIC_InitStructure);
//使能USART1工作
USART_Cmd(USART1, ENABLE);
}
//发送数据
void usart1_send_str(const char *str)
{
const char * p = str;
while(*p!='\0')
{
//发送一个字节
USART_SendData(USART1,*p);
p++;
//等待发送完毕
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
//抢占优先级0~3,支持4级!
//响应优先级0~3,支持4级!
key_init();
Led_init();
//初始化串口1波特率位115200bps,若发送/接收数据有乱码,请检查PLL
usart1_init(115200);
usart1_send_str("hello5555\r\n");
while(1){
}
}
void USART1_IRQHandler(void)
{
static uint8_t d;
if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET)
{
d=USART_ReceiveData(USART1);
if(d == 'A')PFout(9)=0;
if(d == 'a')PFout(9)=1;
//告诉CPU,已经完成接收中断请求,可以响应新的接收中断请求
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}