串口控制LED灯亮灭
main.c
#include "uart4.h"int main()
{//串口初始化uart4_init();//led初始化led_init();char i;int count = 0;int count2 = 0;int count3 = 0;while (1){i = getchar();putchar(i);//键盘输入a,串口工具显示aif (i == '1'){count++;if (count % 2 == 1)LED1_ON();elseLED1_OFF();}if (i == '2'){count2++;if (count2 % 2 == 1)LED2_ON();elseLED2_OFF();}if (i == '3'){count3++;if (count3 % 2 == 1)LED3_ON();elseLED3_OFF();}}return 0;
}
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"void uart4_init();
void putchar(char a);
char getchar();
void gets(char *s);
void puts(char *s);void led_init();
void LED1_ON();
void LED1_OFF();
void LED2_ON();
void LED2_OFF();
void LED3_ON();
void LED3_OFF();
int my_strcmp(const char *src1, const char *src2);
#endif
uart4.c
#include "uart4.h"#include "led.h"int my_strcmp(const char *src1, const char *src2)
{while (*src1 == *src2){if (*src1 == 0){return 0;}src1++;src2++;}return (*src1 - *src2);
}void led_init()
{RCC->MP_AHB4ENSETR |= (0x3 << 4); //使能GPIOF、GPIOEGPIOE->MODER &= (~(0x3 << 20)); //PE10清零GPIOE->MODER |= (0x1 << 20); //PE10管脚复用GPIOE->MODER &= (~(0x3 << 16)); //PE8清零GPIOE->MODER |= (0x1 << 16); //PE8管脚复用GPIOF->MODER &= (~(0x3 << 20)); //PF10清零GPIOF->MODER |= (0x1 << 20); //PF10管脚复用//设置推挽模式GPIOE->OTYPER &= (~(0x1 << 10));GPIOF->OTYPER &= (~(0x1 << 10));GPIOE->OTYPER &= (~(0x1 << 8)); //PE8//设置低速GPIOE->OSPEEDR &= (~(0x3 << 20));GPIOF->OSPEEDR &= (~(0x3 << 20));GPIOE->OSPEEDR &= (~(0x3 << 16)); //PE8//设置无上拉下拉GPIOE->PUPDR &= (~(0x3 << 20));GPIOF->PUPDR &= (~(0x3 << 20));GPIOE->PUPDR &= (~(0x3 << 16));//设置低电平输出 灯灭GPIOE->ODR &= (~(0x1 << 10));GPIOF->ODR &= (~(0x1 << 10));GPIOE->ODR &= (~(0x1 << 8));
}void uart4_init()
{//使能GPIOB GPIOG UART4外设时钟RCC->MP_AHB4ENSETR |= (0x1 << 1); //GPIOBRCC->MP_AHB4ENSETR |= (0x1 << 6); //GPIOGRCC->MP_APB1ENSETR |= (0X1 << 16); //UART4//设置PB2和PG11管脚复用GPIOB->MODER &= (~(0x3 << 4));//PB2GPIOB->MODER |= (0x2 << 4);GPIOG->MODER &= (~(0x3 << 22));//PG11GPIOG->MODER |= (0x2 << 22);GPIOB->AFRL &= (~(0XF << 8));GPIOB->AFRL |= (0x8 << 8);GPIOB->AFRH &= (~(0XF << 12));GPIOG->AFRH |= (0x6 << 12);//设置串口不使能 UE=0USART4->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));//设置波特率为115200USART4->BRR = 0X22B;//使能发送器USART4->CR1 |= (0x1 << 3);//使能接收器USART4->CR1 |= (0x1 << 2);//使能uart4USART4->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'); //'\r'回车键,切换到行头
}//接收字符串函数
void gets(char *s)
{while (1){*s = getchar();putchar(*s);if ((*s) == '\r')break;s++;}*s = '\0';putchar('\n'); //换行
}