串口控制三盏灯亮灭
视频
串口实验验证.mp4
代码
uart4.c
#include "uart4.h"
void uart4_init()
{
//使能GPIOB GPIOG UART4外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<1);//GPIOB
RCC->MP_AHB4ENSETR |= (0x1<<6);//GPIOG
RCC->MP_APB1ENSETR |= (0X1<<16);//UART4
//设置PB2和PG11管脚复用
//PB2
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |=(0x2<<4);
GPIOB->AFRL &= (~(0XF<<8));
GPIOB->AFRL |= (0x8<<8);
//PG11
GPIOG->MODER &= (~(0x3<<22));
GPIOG->MODER |=(0x2<<22);
GPIOB->AFRH &= (~(0XF<<12));
GPIOB->AFRH |= (0x6<<12);
//设置串口不使能 UE=0
USART4->CR1 &= (~(0X1));
//设置8位数据位
USART4->CR1 &= (~(0x1<<12));
USART4->CR1 &= (~(0x1<<28));
//设置没有奇偶校验位
USART4->CR1 &= (~(0x1<<10));
//设置1位停止位
USART4->CR2 &= (~(0x3<<12));
//设置16倍过采样
USART4->CR1 &= (~(0x1<<15));
//设置时钟不分频
USART4->PRESC &= (~(0xF));
//设置波特率为115200
USART4->BRR=0X22B;
//使能发送器
USART4->CR1 |= (0x1<<3);
//使能接收器
USART4->CR1 |= (0x1<<2);
//使能uart4
USART4->CR1 |= (0x1<<0);
}
//发送一个字符
void putchar(char a)
{
//先判断发送数据寄存器是否为空
//不为空阻塞等待
while(!(USART4->ISR&(0x1<<7)));
//为空向发送数据寄存器写入a的数据
USART4->TDR=a;//写入数据
//写入完成需要判断发送是否完成,不完成阻塞等待,完成了则函数结束
while(!(USART4->ISR&(0x1<<6)));
}
//接收一个字符
char getchar()
{
char a;
//先判断接收数据寄存器中有没有准备好的数据
//如果数据没有准备好则阻塞等待
while(!(USART4->ISR&(0x1<<5)));
//如果数据准备好了则读取
a=USART4->RDR;
//读取完毕将读取到的数据返回
return a;
}
//发送字符串
void puts(char *s)
{
while(*s)
{
putchar(*s);
s++;
}
putchar('\n');// '\n'切换到下一行
putchar('\r');//'\n'回车键,切换到行头
}
//接收字符串函数
void gets(char *s)
{
while(1)
{
*s=getchar();
putchar(*s);
if((*s)=='\r')
break;
s++;
}
*s='\0';
putchar('\n');//换行
}
main.c
#include "uart4.h"
//封装一个strcmp函数
int strcmp(const char *s1, const char *s2)
{
int i=0;
while(s1[i]==s2[i])
{
if(s1[i]=='\0')
break;
i++;
}
int sub=s1[i]-s2[i];
return sub;
}
int main()
{
uart4_init();
all_led_init();
char buf[32];
//char i;
while (1)
{
// i = getchar();
// putchar(i+1);
// putchar('\n');
// putchar('\r');
gets(buf);
if(strcmp(buf,"led1off")==0)
{
LED1_OFF();
}
if(strcmp(buf,"led1on")==0)
{
LED1_ON();
}
if(strcmp(buf,"led2off")==0)
{
LED2_OFF();
}
if(strcmp(buf,"led2on")==0)
{
LED2_ON();
}
if(strcmp(buf,"led3off")==0)
{
LED3_OFF();
}
if(strcmp(buf,"led3on")==0)
{
LED3_ON();
}
}
return 0;
}
led.c
#include "led.h"
void all_led_init()//led灯的初始化
{
//GPIOE/GPIOF时钟使能
RCC->MP_AHB4ENSETR |= (0x3<<4);
//PE10设置为输出
GPIOE->MODER &= (~(0X3<<20));
GPIOE->MODER |= (0X1<<20);
//PF10设置为输出
GPIOF->MODER &= (~(0X3<<20));
GPIOF->MODER |= (0X1<<20);
//PE8设置为输出
GPIOE->MODER &= (~(0X3<<16));
GPIOE->MODER |= (0X1<<16);
//PE10设置为推挽输出
GPIOE->OTYPER &= (~(0X1<<10));
//PF10设置为推挽输出
GPIOF->OTYPER &= (~(0X1<<10));
//PE8设置为推挽输出
GPIOE->OTYPER &= (~(0X1<<8));
//PE10设置为低速输出
GPIOE->OSPEEDR &= (~(0X3<<20));
//PF10设置为低速输出
GPIOF->OSPEEDR &= (~(0X3<<20));
//PE8设置为低速输出
GPIOE->OSPEEDR &= (~(0X3<<16));
//PE10无上拉下拉电阻
GPIOE->PUPDR &= (~(0X3<<20));
//PF10无上拉下拉电阻
GPIOF->PUPDR &= (~(0X3<<20));
//PE8无上拉下拉电阻
GPIOE->PUPDR &= (~(0X3<<16));
//三盏灯默认输出低电平
GPIOE->ODR &= (~(0X1<<10));
GPIOF->ODR &= (~(0X1<<10));
GPIOE->ODR &= (~(0X1<<8));
}
//led1亮
void LED1_ON()
{
GPIOE->ODR |= (0X1<<10);
}
//led2亮
void LED2_ON()
{
GPIOF->ODR |= (0X1<<10);
}
//led1亮
void LED3_ON()
{
GPIOE->ODR |= (0X1<<8);
}
//LED1灭
void LED1_OFF()
{
GPIOE->ODR &= (~(0X1<<10));
}
//LED2灭
void LED2_OFF()
{
GPIOF->ODR &= (~(0X1<<10));
}
//LED3灭
void LED3_OFF()
{
GPIOE->ODR &= (~(0X1<<8));
}
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "led.h"
void uart4_init();
void putchar(char a);
char getchar();
void puts(char *s);
void gets(char *s);
#endif
led.h
#ifndef __LED_H__
#define __LED_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
void all_led_init();
void LED1_ON();
void LED2_ON();
void LED3_ON();
void LED1_OFF();
void LED2_OFF();
void LED3_OFF();
#endif