串口通信
成果图
头文件uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
//灯初始化
void led_init();
//灯1
void led_1_on();
void led_1_off();
//灯2
void led_2_on();
void led_2_off();
//风扇
void staff_on();
void staff_off();
int my_strcmp(char *str1,char *str2);
void uart4_config();
void putchar(char a);
void puts(char *s);
char getchar();
void gets(char *s);
#endif
函数库文件
=============================uart4.c=======================================
#include "uart4.h"
void uart4_config()
{
// PG11 50008000
// PB2 50003000
//使能gpiob\GPIOG\UART4
RCC->MP_AHB4ENSETR |= (0X1 << 1);//GPIOB
RCC->MP_AHB4ENSETR |= (0X1 << 6);//GPIOG
RCC->MP_APB1ENSETR |= (0X1 << 16);//UART4
// PG11
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x2 << 22);
GPIOG->AFRH &= (~(0xf << 12));
GPIOG->AFRH |= (0x6 << 12);
// PB2
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x2 << 4);
GPIOB->AFRL &= (~(0xf << 8));
GPIOB->AFRL |= (0x8 << 8);
// 禁用串口
USART4->CR1 &= (~0x1);
// 设置数据位宽位8位
USART4->CR1 &= (~(0x1 << 12));
USART4->CR1 &= (~(0x1 << 28));
// 设置奇偶位
USART4->CR1 &= (~(0x1 << 10));
// 设置16倍采样
USART4->CR1 &= (~(0x1 << 15));
// 设置设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
// 设置不分频
USART4->PRESC &= (~(0xf));
// 设置波特率115200
USART4->BRR = 0x22B;
// 使能发送器
USART4->CR1 |= (0x1 << 3);
// 使能接收器
USART4->CR1 |= (0x1 << 2);
// 使能串口
USART4->CR1 |= (0x1);
}
int my_strcmp(char *str1,char *str2)
{
int i=0;
while (*(str1+i)==*(str2+i))
{
if(*(str1+i)=='\0')
{
break;
}
i++;
}
return *(str1+i)-*(str2+i);
}
void putchar(char a)
{
while (!(USART4->ISR & (0x1 << 7)));
USART4->TDR = a;
while (!(USART4->ISR & (0x1 << 6)));
}
void puts(char *s){
while(*s)
{
putchar(*s);
s++;
}
putchar('\r');
putchar('\n');
}
char getchar()
{
char a;
//1.判断数据
while (!(USART4->ISR & (0x1 << 5)));
//2.读取数据
a = USART4->RDR;
return a;
}
void gets(char *s)
{
while(1)
{
*s=getchar();
putchar(*s);
if(*s=='\r')
{
break;
}
s++;
}
*s='\0';
putchar('\n');
}
====================led.c=================================================
#include "uart4.h"
void led_init()
{
//LED 1 PE10或风扇 PE9
RCC->MP_AHB4ENSETR |=(0x3<<4) ;
//LED 2 PF10
RCC->MP_AHB4ENSETR |=(0x3<<5) ;
//LED1 PE10
GPIOE->MODER &=(~(0x3)<<20);
GPIOE->MODER |=((0x1)<<20);
GPIOE->OTYPER &=(~(0x1<<10));
GPIOE->OSPEEDR &=(~(0x3)<<20);
GPIOE->PUPDR &=(~(0x3)<<20);
//LED2 PF10
GPIOF->MODER &=(~(0x3)<<20);
GPIOF->MODER |=(0x1)<<20;
GPIOF->OTYPER &=(~(0x1<<10));
GPIOF->OSPEEDR &=(~(0x3)<<20);
GPIOF->PUPDR &=(~(0x3)<<20);
//风扇 PE9
GPIOE->MODER &=(~(0x3)<<18);
GPIOE->MODER |=((0x1)<<18);
GPIOE->OTYPER &=(~(0x1<<9));
GPIOE->OSPEEDR &=(~(0x3)<<18);
GPIOE->PUPDR &=(~(0x3)<<18);
}
//PE10
void led_1_on(){
GPIOE->ODR |=((0x1)<<10);
}
void led_1_off(){
GPIOE->ODR &=(~(0x1<<10));
}
//PF10
void led_2_on(){
GPIOF->ODR |=((0x1)<<10);
}
void led_2_off(){
GPIOF->ODR &=(~(0x1<<10));
}
//PE9
void staff_on(){
GPIOE->ODR |=((0x1)<<9);
}
void staff_off(){
GPIOE->ODR &=(~(0x1<<9));
}
==========================main 函数========================================
#include "gpio.h"
#include "uart4.h"
int main()
{
char buf[128];
uart4_config();
led_init();
while (1)
{
gets(buf);
puts(buf);
if(my_strcmp(buf,"led_1_on")==0)
{
led_1_on();
}else if(my_strcmp(buf,"led_1_off")==0)
{
led_1_off();
}else if(my_strcmp(buf,"led_2_on")==0)
{
led_2_on();
}
else if(my_strcmp(buf,"led_2_off")==0)
{
led_2_off();
}
else if(my_strcmp(buf,"staff_on")==0)
{
staff_on();
}
else if(my_strcmp(buf,"staff_off")==0)
{
staff_off();
}
else if(my_strcmp(buf,"quit")==0||my_strcmp(buf,"Quit")==0){
break;
}
}
return 0;
}