任务:
通过操作Cortex-A7核,串口输入相应的命令,控制LED灯进行工作
1.例如在串口输入led1on,开饭led1灯点亮
2.例如在串口输入led1off,开饭led1灯熄灭
3.例如在串口输入led2on,开饭led2灯点亮
4.例如在串口输入led2off,开饭led2灯熄灭
5.例如在串口输入led3on,开饭led3灯点亮
6.例如在串口输入led3off,开饭led3灯熄灭
头文件代码:
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//串口初始化
void uart_init();
//发送一个字符
void uart_put_char(const char str);
//发送一个字符串
void uart_put_string(const char* str);
//接收一个字符
char uart_get_char();
//接收一个字符串
char* uart_get_string();
//LED1---->PE10
//LED1初始化函数
void led1_init();
//控制LED1灯亮函数
void led1_on();
//控制LED1灯灭函数
void led1_off();
//LED2---->PF10
void led2_init();
void led2_on();
void led2_off();
//LED3---->PE8
void led3_init();
void led3_on();
void led3_off();
#endif
串口源文件:
#include "uart4.h"
//串口初始化
void uart_init()
{
/*********RCC章节初始化************/
RCC->MP_AHB4ENSETR |= (0x1<<1); //使能GPIOB
RCC->MP_AHB4ENSETR |= (0X1<<6); //使能GPIOG
RCC->MP_APB1ENSETR |= (0X1<<16); //使能UART4
/*********GPIO章节初始化************/
GPIOB->MODER &= (~(0x3<<4)); //PB2--->UART4_RX 复用
GPIOB->MODER |= (0x1<<5);
GPIOG->MODER &= (~(0x3<<22)); //PG11--->UART4_TX 复用
GPIOG->MODER |= (0x1<<23);
GPIOB->AFRL &= (~(0xF<<8)); //设置GPIOB引脚功能复用模式
GPIOB->AFRL |= (0x1<<11);
GPIOG->AFRH &= (~(0xF<<12)); //设置GPIOG引脚功能复用模式
GPIOG->AFRH |= (0x6<<12);
/*********UART4章节初始化************/
USART4->CR1 &= (~0x1); //设置UE位为0
//USART_CR1串口相关操作
USART4->CR1 &= (~(0X1<<10)); //设置串口无奇偶校验位
USART4->CR1 &= (~(0x1<<12)); //设置数据位宽度为8位
USART4->CR1 &= (~(0x1<<28));
//USART_CR2相关操作
USART4->CR2 &= (~(0x3<<12)); //设置1位停止位
USART4->CR1 &= (~(0X1<<15)); //设置串口采样率
//USART_PRESC相关操作
USART4->PRESC &= (~0xF); //不分频
//USART_BRR相关操作
USART4->BRR = 0x22B; //设置波动率位115200
USART4->CR1 |= (0X1<<3); //设置串口发送器使能
USART4->CR1 |= (0X1<<2); //设置串口接收器使能
USART4->CR1 |= 0x1; //设置UE位为1
}
//发送一个字符
void uart_put_char(const char str)
{
//1.判断发送数据寄存器是否为空,为空,才可以发送下一个字节
//ISR[7]
//读0:发送数据寄存器满,需要等待
//读1:发送数据寄存器空,才可以发送下一位字节数据
while(!(USART4->ISR & (0x1 << 7)));
//2.将要发送的字符,写到发送数据寄存器;
USART4->TDR = str;
//3.判断发送数据是否完成 ISR[6]
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void uart_put_string(const char* str)
{
//判断是否为'\0',一个字符一个字符发送
for(int i=0;str[i]!='\0';i++)
{
uart_put_char(str[i]);
}
uart_put_char('\n');
uart_put_char('\r');
}
//接收一个字符
char uart_get_char()
{
char ch;
//1.判断接收数据寄存器是否有数据可读 ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//2.将接收到的数据读出来
ch = USART4->RDR;
return ch;
}
char buffer[50] = {0};
//接收一个字符串
char* uart_get_string()
{
//for循环
//当键盘的回车键'\r'按下之后,字符串输入完成
int i;
for(i=0;i<49;i++)
{
buffer[i] = uart_get_char();
uart_put_char(buffer[i]);
if(buffer[i] == '\r')
{
break;
}
}
//字符串补'\0'
buffer[i] = '\0';
uart_put_char('\n');
//控制led1亮灭
if(buffer[3] == '1' && buffer[5] == 'o' && buffer[6] == 'n')
{
led1_on();
}
else if(buffer[3] == '1' && buffer[5] == 'o' && buffer[6] == 'f')
{
led1_off();
}
//控制led2亮灭
if(buffer[3] == '2' && buffer[5] == 'o' && buffer[6] == 'n')
{
led2_on();
}
else if(buffer[3] == '2' && buffer[5] == 'o' && buffer[6] == 'f')
{
led2_off();
}
//控制led3亮灭
if(buffer[3] == '3' && buffer[5] == 'o' && buffer[6] == 'n')
{
led3_on();
}
else if(buffer[3] == '3' && buffer[5] == 'o' && buffer[6] == 'f')
{
led3_off();
}
return buffer;
}
LED 灯源文件:
#include "uart4.h"
void led1_init()
{
//设置GPIOE组时钟使能
RCC->MP_AHB4ENSETR = RCC->MP_AHB4ENSETR | (0x1 << 4);
//设置PE10引脚为输出模式
GPIOE->MODER=GPIOE->MODER & (~(0x1<<21));
GPIOE->MODER=GPIOE->MODER | ((0x1<<20));
//设置PE10引脚为推挽输出模式
GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1<<10));
//设置PE10引脚为低速输出模式
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x1<<20));
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x1<<21));
//设置PE10引脚禁止上下拉电阻
GPIOE->PUPDR = GPIOE->PUPDR & (~(0x1<<20));
GPIOE->PUPDR = GPIOE->PUPDR & (~(0x1<<21));
}
void led1_on()
{
//高电平亮
GPIOE->ODR = GPIOE->ODR | (0x1<<10);
}
void led1_off()
{
//低电平熄灭
GPIOE->ODR = GPIOE->ODR & (~(0x1<<10));
}
/******************LED2**************/
void led2_init()
{
//设置GPIOF组时钟使能
RCC->MP_AHB4ENSETR = RCC->MP_AHB4ENSETR | (0x1 << 5);
//设置PF10引脚为输出模式
GPIOF->MODER = GPIOE->MODER & (~(0x1<<21));
GPIOF->MODER = GPIOE->MODER | ((0x1<<20));
//设置PF10引脚为推挽输出模式
GPIOF->OTYPER = GPIOF->OTYPER & (~(0x1<<10));
//设置PF10引脚为低速输出模式
GPIOF->OSPEEDR = GPIOF->OSPEEDR & (~(0x1<<20));
GPIOF->OSPEEDR = GPIOF->OSPEEDR & (~(0x1<<21));
//设置PF10引脚禁止上下拉电阻
GPIOF->PUPDR = GPIOF->PUPDR & (~(0x1<<20));
GPIOF->PUPDR = GPIOF->PUPDR & (~(0x1<<21));
}
void led2_on()
{
//高电平亮
GPIOF->ODR = GPIOF->ODR | (0x1<<10);
}
void led2_off()
{
//低电平熄灭
GPIOF->ODR = GPIOE->ODR & (~(0x1<<10));
}
void led3_init()
{
//设置GPIOE组时钟使能
RCC->MP_AHB4ENSETR = RCC->MP_AHB4ENSETR | (0x1 << 4);
//设置PE10引脚为输出模式
GPIOE->MODER = GPIOE->MODER & (~(0x1<<17));
GPIOE->MODER = GPIOE->MODER | ((0x1<<16));
//设置PE10引脚为推挽输出模式
GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1<<8));
//设置PE10引脚为低速输出模式
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x1<<17));
GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x1<<16));
//设置PE10引脚禁止上下拉电阻
GPIOE->PUPDR = GPIOE->PUPDR & (~(0x1<<17));
GPIOE->PUPDR = GPIOE->PUPDR & (~(0x1<<16));
}
void led3_on()
{
//高电平亮
GPIOE->ODR = GPIOF->ODR | (0x1<<8);
}
void led3_off()
{
//低电平灭
GPIOE->ODR = GPIOE->ODR & (~(0x1<<8));
}
主函数代码:
#include "uart4.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
//串口初始化
uart_init();
//LED初始化
led1_init();
led2_init();
led3_init();
//实现串口数据收发
while(1)
{
// uart_put_char(uart_get_char() + 1);
uart_put_string(uart_get_string());
}
}
运行结果: