uartt.c
#include "uartt.h"
char buf[51];
//初始化串口数据
void uart4_init()
{
//1.UART4和GPIOB、GPIOG的时钟使能
RCC->MP_AHB4ENSETR |= (0x1 << 2); //GPIOB时钟
RCC->MP_AHB4ENSETR |= (0x1 << 6); //GPIOG时钟
RCC->MP_APB1ENSETR |= (0x1 << 16); //UART4时钟
RCC->MP_APB1ENSETR |= (0x3 << 4);
//设置PB2和PG11的管脚复用
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x2 << 4); //设置复用
GPIOB->AFRL &= (~(0xF << 8));
GPIOB->AFRL |= (0x8 << 8); //设置uart4功能复用
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x2 << 22); //设置复用
GPIOG->AFRH &= (~(0xF << 12));
GPIOG->AFRH |= (0x6 << 12); //设置uart4功能复用
//3.先去设置串口禁用,方便设置数据格式
USART4->CR1 &= (~0x1);
//4.设置8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//5.设置没有奇偶校验
USART4->CR1 &= (~(0x1 << 10));
//6.设置16倍采样
USART4->CR1 &= (~(0x1 << 15));
//7.设置1位停止位
USART4->CR2 &= (~(0x3) << 12);
//8.设置1分频
USART4->PRESC &= (~(0xF) << 0);
//9.设置波特率为115208bps
USART4->BRR = 0x22B;
//10.发送器、接收器使能
USART4->CR1 |= (0x1 << 3); //发送
USART4->CR1 |= (0x1 << 2); //接收
//11.串口使能
USART4->CR1 |= (0x1 << 0);
// 2.设置PF10 PE10 PE8为输出输出
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
// 3.设置推挽输出
GPIOE->OTYPER &= (~(0x1 << 10));
GPIOF->OTYPER &= (~(0x1 << 10));
GPIOE->OTYPER &= (~(0x1 << 8));
// 4.设置输出速度为低速
GPIOE->OSPEEDR &= (~(0x3 << 20));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOE->OSPEEDR &= (~(0x3 << 16));
// 5.设置无上拉下拉
GPIOE->PUPDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0x3 << 20));
GPIOE->PUPDR &= (~(0x3 << 16));
}
//封装函数发送一个字符数据
void myputchar(char i)
{
//1.判断TDR寄存器是否为空,如果为空,向TDR寄存器写入数据
while (!(USART4->ISR & (0x1 << 7)))
;
USART4->TDR = i;
//2.阻塞等待数据传输完成,函数返回
while (!(USART4->ISR & (0x1 << 6)))
;
}
//
char mygetchar()
{
char a;
//判断RDR寄存器是否有就绪的数据,如果有就读取,否则等待
while (!(USART4->ISR & (0x1 << 5)))
;
a = USART4->RDR;
return a;
}
void led1_on()
{
GPIOE->ODR |= (0x1 << 10);
}
void led2_on()
{
GPIOF->ODR |= (0x1 << 10);
}
void led3_on()
{
GPIOE->ODR |= (0x1 << 8);
}
void led1_off()
{
GPIOE->ODR &= (~(0x1 << 10));
}
void led2_off()
{
GPIOF->ODR &= (~(0x1 << 10));
}
void led3_off()
{
GPIOE->ODR &= (~(0x1 << 8));
}
main.c
#include "uartt.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();
char a;
//char *str;
//现象是发送一个a串口工具打印一个b
while (1)
{
myputchar('\n');
myputchar('\r');
//2.从串口读取一个字符
a = mygetchar();
switch (a)
{
case 'a':
led1_on();
break;
case 'b':
led2_on();
break;
case 'c':
led3_on();
break;
case 'e':
led1_off();
break;
case 'f':
led2_off();
break;
case 'g':
led3_off();
break;
}
//3.将读取到的字符发送回去
myputchar(a);
}
return 0;
}
uartt.h
#ifndef __UARTTT_H__
#define __UARTTT_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"
//初始化串口数据
void uart4_init();
void myputchar(char i);
char mygetchar();
void led1_on();
void led2_on();
void led3_on();
void led1_off();
void led2_off();
void led3_off();
#endif
文章来源:https://www.toymoban.com/news/detail-758574.html
文章来源地址https://www.toymoban.com/news/detail-758574.html
到了这里,关于ARM按键控制灯光的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!