1.q,w,e亮led1,2,3;
a,s,d灭led1,2,3;
main.c
#include "uar1.h"
#include "led.h"
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
int main()
{
//1.串口的初始化
uart4_init();
//LED初始化
all_led_init();
char a;
//现象是发送一个a串口工具打印一个b
while(1)
{
//2.从串口读取一个字符
a=mygetchar();
//3.将读取到的字符+1发送回去
myputchar(a);
switch (a)
{
case 'q': led1_on();
break;
case 'w': led2_on();
break;
case 'e': led3_on();
break;
case 'a': led1_off();
break;
case 's': led2_off();
break;
case 'd': led3_off();
break;
}
}
return 0;
}
led.h
#ifndef __LED_H__
#define __LED_H__
typedef struct
{
unsigned int moder;
unsigned int otyper;
unsigned int ospeedr;
unsigned int pupdr;
unsigned int idr;
unsigned int odr;
unsigned int bsrr;
} gpio_t1;
#define GPIOE1 ((gpio_t1 *)0X50006000)
#define GPIOF1 ((gpio_t1 *)0X50007000)
#define RCC_MP_AHB4ENSETR (*(unsigned int *)0X50000a28)
void all_led_init();
void led1_on();
void led2_on();
void led3_on();
void led1_off();
void led2_off();
void led3_off();
#endif
uar1.h
#ifndef __UART_H__
#define __UART_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_uart.h"
void uart4_init();
void myputchar(char i);
char mygetchar();
#endif
led.c
#include "led.h"
void all_led_init()
{
// 1.使能外设时钟
RCC_MP_AHB4ENSETR |= (0x3 << 4);
// 2.设置PF10 PE10 PE8为输出输出
GPIOE1->moder &= (~(0x3 << 20));
GPIOE1->moder |= (0x1 << 20);
GPIOF1->moder &= (~(0x3 << 20));
GPIOF1->moder |= (0x1 << 20);
GPIOE1->moder &= (~(0x3 << 16));
GPIOE1->moder |= (0x1 << 16);
// 3.设置推挽输出
GPIOE1->otyper &= (~(0x1 << 10));
GPIOF1->otyper &= (~(0x1 << 10));
GPIOE1->otyper &= (~(0x1 << 8));
// 4.设置输出速度为低速
GPIOE1->ospeedr &= (~(0x3 << 20));
GPIOF1->ospeedr &= (~(0x3 << 20));
GPIOE1->ospeedr &= (~(0x3 << 16));
// 5.设置无上拉下拉
GPIOE1->pupdr &= (~(0x3 << 20));
GPIOF1->pupdr &= (~(0x3 << 20));
GPIOE1->pupdr &= (~(0x3 << 16));
}
void led1_on()
{
GPIOE1->odr |= (0x1 << 10);
}
void led2_on()
{
GPIOF1->odr |= (0x1 << 10);
}
void led3_on()
{
GPIOE1->odr |= (0x1 << 8);
}
void led1_off()
{
GPIOE1->odr &= (~(0x1 << 10));
}
void led2_off()
{
GPIOF1->odr &= (~(0x1 << 10));
}
void led3_off()
{
GPIOE1->odr &= (~(0x1 << 8));
}
uar1.c
#include"uar1.h"
//串口数据初始化
void uart4_init()
{
//设置UART4的RCc时钟使能
//RCC_MP_APB1ENSETR[16]->1
RCC->MP_APB1ENSETR |= (0x1<<16);
//设置GPIOB和GPIOG的时钟使能
//RCC_MP_AHB4ENSETR[6]->1
//RCC_MP_AHB4ENSETR[1]->1
RCC->MP_AHB4ENSETR |= (0x1<<1);
RCC->MP_AHB4ENSETR |= (0X1<<6);
//设置PG11和PB2功能复用为UART4功能
//PG11
//GPIOG_MODER[23:22]->10
GPIOG->MODER &= (~(0X3<<22));
GPIOG->MODER |= (0X2<<22);
//GPIOG_AFRH[15:12]->0110
GPIOG->AFRH &= (~(0xf<<12));
GPIOG->AFRH |= (0x6<<12);
//PB2
//GPIOB_MODER[5:4]->10
//GPIOB_AFRL[11:8]->1000
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |= (0X2<<4);
GPIOB->AFRL &= (~(0xF<<8));
GPIOB->AFRL |= (0x8<<8);
//禁用串口
USART4->CR1 &=(~0x1);
//设置数据8个数据位 CR1[28]->0 CR1[12]-》0
USART4->CR1 &= (~(0X1<<28));
USART4->CR1 &= (~(0X1<<12));
//设置没有校验位CR1[10]->0
USART4->CR1 &= (~(0X1<<10));
//设置1个停止位CR2[13:12]->00
USART4->CR2 &= (~(0x3<<12));
//设置16倍采样 CR1[15]->0
USART4->CR1 &= (~(0X1<<15));
//设置波特率为115200 BRR=0X22B
USART4->BRR |= 0X22B;
//设置1分频 PRESC[3:0]->0000
USART4->PRESC &= (~(0XF));
//使能发送器 CR1[3]->1
USART4->CR1 |= (0X1<<3);
//使能接收器 CR1[2]->1
USART4->CR1 |= (0X1<<2);
//使能串口 CR1[0]->1
USART4->CR1 |= (0X1);
}
//封装函数发送一个字符数据
void myputchar(char c)
{
//判断发送数据寄存器有没有数据,没有数据时可以发送
while(!(USART4->ISR&(0X1<<7)));
USART4->TDR=c;//将要发送的数据保存在发送寄存器中
while(!(USART4->ISR&(0X1<<6)));//数据传输完成,函数结束
}
char mygetchar()
{
char c;
//判断是否有数据准备好
while(!(USART4->ISR&(0X1<<5)));//数据传输完成,函数结束
c=USART4->RDR;
return c;
}
2.字符串点亮
uar1.c
#include"uar1.h"
#include"led.h"
char buf[51]={0};
//串口数据初始化
void uart4_init()
{
//设置UART4的RCc时钟使能
//RCC_MP_APB1ENSETR[16]->1
RCC->MP_APB1ENSETR |= (0x1<<16);
//设置GPIOB和GPIOG的时钟使能
//RCC_MP_AHB4ENSETR[6]->1
//RCC_MP_AHB4ENSETR[1]->1
RCC->MP_AHB4ENSETR |= (0x1<<1);
RCC->MP_AHB4ENSETR |= (0X1<<6);
//设置PG11和PB2功能复用为UART4功能
//PG11
//GPIOG_MODER[23:22]->10
GPIOG->MODER &= (~(0X3<<22));
GPIOG->MODER |= (0X2<<22);
//GPIOG_AFRH[15:12]->0110
GPIOG->AFRH &= (~(0xf<<12));
GPIOG->AFRH |= (0x6<<12);
//PB2
//GPIOB_MODER[5:4]->10
//GPIOB_AFRL[11:8]->1000
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |= (0X2<<4);
GPIOB->AFRL &= (~(0xF<<8));
GPIOB->AFRL |= (0x8<<8);
//禁用串口
USART4->CR1 &=(~0x1);
//设置数据8个数据位 CR1[28]->0 CR1[12]-》0
USART4->CR1 &= (~(0X1<<28));
USART4->CR1 &= (~(0X1<<12));
//设置没有校验位CR1[10]->0
USART4->CR1 &= (~(0X1<<10));
//设置1个停止位CR2[13:12]->00
USART4->CR2 &= (~(0x3<<12));
//设置16倍采样 CR1[15]->0
USART4->CR1 &= (~(0X1<<15));
//设置波特率为115200 BRR=0X22B
USART4->BRR |= 0X22B;
//设置1分频 PRESC[3:0]->0000
USART4->PRESC &= (~(0XF));
//使能发送器 CR1[3]->1
USART4->CR1 |= (0X1<<3);
//使能接收器 CR1[2]->1
USART4->CR1 |= (0X1<<2);
//使能串口 CR1[0]->1
USART4->CR1 |= (0X1);
}
//封装函数发送一个字符数据
void myputchar(char c)
{
//判断发送数据寄存器有没有数据,没有数据时可以发送
while(!(USART4->ISR&(0X1<<7)));
USART4->TDR=c;//将要发送的数据保存在发送寄存器中
while(!(USART4->ISR&(0X1<<6)));//数据传输完成,函数结束
}
char mygetchar()
{
char c;
//判断是否有数据准备好
while(!(USART4->ISR&(0X1<<5)));//数据传输完成,函数结束
c=USART4->RDR;
return c;
}
//输出一个字符串
void puts(char *s)
{
while(*s)
{
myputchar(*s);
s++;
}
myputchar('\n');//切换到下一行
myputchar('\r');//切换到一行的开头
}
//读取一个字符串
char *gets()
{
unsigned int i;
for(i=0;i<50;i++)
{
buf[i]=mygetchar();//接受字符串
myputchar(buf[i]);//回显
if(buf[i]=='\r')
break;
}
buf[i]='\0';
myputchar('\n');//换行
return buf;
}
//字符串比较
int strcmp(char *str,char *dst)
{
while(*str)
{
if(*str != *dst)
{
return 1;
}
str++;
dst++;
}
return 0;
}
void deal(char*str)
{
if(strcmp(str,"led1_on")==0)
{
led1_on();
}
else if(strcmp(str,"led1_off")==0)
{
led1_off();
}
else if(strcmp(str,"led2_on")==0)
{
led2_on();
}
else if(strcmp(str,"led2_off")==0)
{
led2_off();
}
else if(strcmp(str,"led3_on")==0)
{
led3_on();
}
else if(strcmp(str,"led3_off")==0)
{
led3_off();
}
}
main.c
#include "uar1.h"
#include "led.h"
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
int main()
{
all_led_init();
//1.串口的初始化
uart4_init();
char a;
char *str;
//现象是发送一个a串口工具打印一个b
while(1)
{
myputchar('\n');//切换到下一行
myputchar('\r');//切换到一行的开头
//2.从串口读取一个字符
//a=mygetchar();
//3.将读取到的字符+1发送回去
//myputchar(a+1);
str=gets();
// puts(str);
deal(str);
}
return 0;
}