一、串口通信:实现PC端串口助手与开发板的字符串通信。
代码:
main:
#include "uart4.h"
int main()
{
uart4_config();
//char a;
char s[64];
while (1)
{
//a = getchar();
//putchar(a+1);
gets(s);
puts(s);
}
return 0;
}
usrt4.c:
#include "uart4.h"
void uart4_config()
{
//使能GPIOB\GPIOG\UART4的外设时钟
RCC->MP_AHB4ENSETR |= (0X1<<1);
RCC->MP_AHB4ENSETR |=(0X1<<6);
RCC->MP_APB1ENSETR |=(0X1<<16);
//设置GP11和PB2为管脚复用功能
//PB2
GPIOB->MODER &=(~(0X3<<4));
GPIOB->MODER |=(0X2<<4);
//PG11
GPIOG->MODER &= (~(0X3<<22));
GPIOG->MODER |= (0X2<<22);
//设置PG11为UART4_TX功能
GPIOG->AFRH &= (~(0XF<<12));
GPIOG->AFRH |=(0X6<<12);
//设置PB2为UART4_RX功能
GPIOG->AFRH &= (~(0XF<<8));
GPIOG->AFRH |= (0X8<<8);
//设置串口不使能
USART4->CR1 &= (~0x1);
//设置8位数据位
USART4->CR1 &= (~(0x1<<12));
USART4->CR1 &= (~(0x1<<28));
//设置没有校验位
USART4->CR1 &= (~(0x1<<10));
//设置时钟频率不分频
USART4->PRESC &= (~0xf);
//设置16倍过采样
USART4->CR1 &= (~(0x1<<15));
//设置1位停止位
USART4->CR2 &= (~(0x3<<12));
//设置波特率为115200
USART4->BRR=0X22B;
//使能发送器
USART4->CR1 |= (0X1<<3);
//使能接收器
USART4->CR1 |= (0X1<<2);
//使能串口
USART4->CR1 |= (0X1<<0);
}
void putchar(char dat)
{
while (!(USART4->ISR & (0X1<<7)));
USART4->TDR = dat;
while (!(USART4->ISR & (0X1<<7)));
}
char getchar()
{
while (!(USART4->ISR & (0X1<<5)));
return USART4->RDR;
}
//发送字符串
void puts(char *s)
{
putchar('\r');
while (1)
{
if(*s == '\0')
break;
putchar(*s);
s++;
}
}
//接收字符串
void gets(char *s)
{
while (1)
{
*s = getchar();
if(*s == '\r')
break;
putchar(*s);
s++;
}
*s = '\0';
putchar('\n');
}
运行截图:
二、根据输入的字符串 控制开发板的灯亮灭(本次只设置LED1,LED2)
main:
#include "uart4.h"
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
int main()
{
uart4_config();
led_init();
//char a;
char s[64];
while (1)
{
//a = getchar();
//putchar(a+1);
gets(s);
if(!strcmp(s,"ON"))
{
LED1_CTRL(1);
LED2_CTRL(1);
}else if(!strcmp(s,"OFF"))
{
LED1_CTRL(0);
LED2_CTRL(0);
}
puts(s);
}
return 0;
}
surt4.c:
#include "uart4.h"
void led_init()
{
//使能GPIO的外设时钟
RCC_01 |= (0x3<<4);
//设置输出
GPIOE->MODER &= (~(0x3<<20));
GPIOE->MODER |= (0x1<<20);
GPIOF->MODER &= (~(0x3<<20));
GPIOF->MODER |= (0x1<<20);
GPIOE->MODER &= (~(0x3<<16));
GPIOE->MODER |= (0x1<<16);
//设置默认关闭状态
GPIOE->ODR &= (~(0x1<<10));
GPIOF->ODR &= (~(0x1<<10));
GPIOE->ODR &= (~(0x1<<8));
}
void LED1_CTRL(int flag)
{
if(flag == 1)
{
GPIOE->ODR |= (0x1<<10);
}else if(flag == 0)
{
GPIOE->ODR &= (~(0x1<<10));
}
}
void LED2_CTRL(int flag)
{
if(flag == 1)
{
GPIOF->ODR |= (0x1<<10);
}else if(flag == 0)
{
GPIOF->ODR &= (~(0x1<<10));
}
}
void uart4_config()
{
//使能GPIOB\GPIOG\UART4的外设时钟
RCC->MP_AHB4ENSETR |= (0X1<<1);
RCC->MP_AHB4ENSETR |=(0X1<<6);
RCC->MP_APB1ENSETR |=(0X1<<16);
//设置GP11和PB2为管脚复用功能
//PB2
GPIOB->MODER &=(~(0X3<<4));
GPIOB->MODER |=(0X2<<4);
//PG11
GPIOG->MODER &= (~(0X3<<22));
GPIOG->MODER |= (0X2<<22);
//设置PG11为UART4_TX功能
GPIOG->AFRH &= (~(0XF<<12));
GPIOG->AFRH |=(0X6<<12);
//设置PB2为UART4_RX功能
GPIOG->AFRH &= (~(0XF<<8));
GPIOG->AFRH |= (0X8<<8);
//设置串口不使能
USART4->CR1 &= (~0x1);
//设置8位数据位
USART4->CR1 &= (~(0x1<<12));
USART4->CR1 &= (~(0x1<<28));
//设置没有校验位
USART4->CR1 &= (~(0x1<<10));
//设置时钟频率不分频
USART4->PRESC &= (~0xf);
//设置16倍过采样
USART4->CR1 &= (~(0x1<<15));
//设置1位停止位
USART4->CR2 &= (~(0x3<<12));
//设置波特率为115200
USART4->BRR=0X22B;
//使能发送器
USART4->CR1 |= (0X1<<3);
//使能接收器
USART4->CR1 |= (0X1<<2);
//使能串口
USART4->CR1 |= (0X1<<0);
}
void putchar(char dat)
{
while (!(USART4->ISR & (0X1<<7)));
USART4->TDR = dat;
while (!(USART4->ISR & (0X1<<7)));
}
char getchar()
{
while (!(USART4->ISR & (0X1<<5)));
return USART4->RDR;
}
//发送字符串
void puts(char *s)
{
putchar('\r');
while (1)
{
if(*s == '\0')
break;
putchar(*s);
s++;
}
}
//接收字符串
void gets(char *s)
{
while (1)
{
*s = getchar();
if(*s == '\r')
break;
putchar(*s);
s++;
}
*s = '\0';
putchar('\n');
}
int strcmp(char *s,char *p)
{
while (*s==*p && *s )
{
s++;
p++;
}
return *s-*p;
}
uart4.h:
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
#define RCC_01 *((unsigned int*)0x50000a28)
void uart4_config();
void putchar(char dat);
char getchar();
void puts(char *s);
void gets(char *s);
int strcmp(char *s,char *p);
void led_init();
void LED1_CTRL(int flag);
void LED2_CTRL(int flag);
#endif