新手上路
- 积分
- 32
- 金钱
- 32
- 注册时间
- 2021-10-12
- 在线时间
- 7 小时
|
本帖最后由 wkkkk 于 2021-11-1 16:15 编辑

#include "stm32f4xx.h"
#include "sys.h"
static GPIO_InitTypeDef GPIO_InitStructure;
static USART_InitTypeDef USART_InitStructure;
static NVIC_InitTypeDef NVIC_InitStructure;
void delay_us(uint32_t n)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168*n)-1; // 配置计数值(168*n)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
SysTick->CTRL = 0; // Disable SysTick
}
void delay_ms(uint32_t n)
{
while(n--)
{
SysTick->CTRL = 0; // Disable SysTick,关闭系统定时器
SysTick->LOAD = (168000)-1; // 配置计数值(168000)-1 ~ 0
SysTick->VAL = 0; // Clear current value as well as count flag
SysTick->CTRL = 5; // Enable SysTick timer with processor clock
while ((SysTick->CTRL & 0x10000)==0);// Wait until count flag is set
}
SysTick->CTRL = 0; // Disable SysTick
}
void usart1_init(uint32_t baud)
{
//打开PA口硬件时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//打开串口1的硬件时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//初始化GPIOA引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //第9和10根引脚
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF; //复用功能模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出,增加输出电流能力。
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//高速响应
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //没有使能上下拉电阻
GPIO_Init(GPIOA,&GPIO_InitStructure);
//将PA9和PA10连接到串口1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//串口1的配置
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);
//打开串口1的中断:接收一个字节触发中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//优先级的配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能串口1工作
USART_Cmd(USART1, ENABLE);
}
int main(void)
{
//使能(打开)端口F的硬件时钟,就是对端口F供电
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
//初始化GPIOF引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //第9根引脚
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF; //复用功能模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出,增加输出电流能力。
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//高速响应
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //没有使能上下拉电阻
GPIO_Init(GPIOF,&GPIO_InitStructure);
PFout(9)=1;
PFout(10)=1;
//串口1初始化 波特率115200bps
usart1_init(115200);
//串口1发送数据
USART_SendData(USART1,'W');
//检测串口1的数据缓冲区 若没有发送完毕,继续等待;发送完毕跳出循环
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
//串口1发送数据
USART_SendData(USART1,'K');
//检测串口1的数据缓冲区 若没有发送完毕,继续等待;发送完毕跳出循环
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
//串口1发送数据
USART_SendData(USART1,'K');
//检测串口1的数据缓冲区 若没有发送完毕,继续等待;发送完毕跳出循环
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
while(1)
{
}
}
void USART1_IRQHandler(void)
{
uint8_t d;
//检测中断标志位
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
//接收数据
d=USART_ReceiveData(USART1);
//串口1发送数据 将接收的数据发送给PC
USART_SendData(USART1,d);
//检测串口1的数据缓冲区 若没有发送完毕,继续等待;发送完毕跳出循环
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
//控制LED1
if(d == 0x01)
{
PFout(9)=0;
}
if(d == 0xF1)
{
PFout(9)=1;
}
//清空中断标志位
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
|
|