【无标题】

2023-12-25 20:40:20
#include "uart4.h"

void  uart4_config()
{
    //使能GPIOG,GPIOB,UART4外设时钟
    RCC->MP_AHB4ENSETR |= (0x1<<1);
    RCC->MP_AHB4ENSETR |= (0x1<<6);
    RCC->MP_APB1ENSETR |= (0x1<<16);


    //使能GPIOE 外设时钟
    RCC->MP_AHB4ENSETR |= (0x1<<4);
    GPIOE->MODER &=(~(0X3<<20));//设置PE10输出
    GPIOE->MODER |= (0X1<<20);

    GPIOE->MODER &=(~(0X3<<18));//设置PE9输出
    GPIOE->MODER |= (0X1<<18);
    //设置PE10,PE9为推挽输出
    GPIOE->OTYPER &=(~(0x3<<9));
    //PE10,PE9为低速输出
    GPIOE->OSPEEDR &= (~(0x3<<20));
    GPIOE->OSPEEDR &= (~(0x3<<18));
    //设置无上拉下拉
    GPIOE->PUPDR&= (~(0x3<<20));
    GPIOE->PUPDR&= (~(0x3<<18));

   
    //设置PB2  复用功能 uart4
    GPIOB->MODER  &=(~(0x3<<4));
    GPIOB->MODER |= (0x2<<4);

    GPIOB->AFRL &=(~(0xf<<8));
    GPIOB->AFRL |= (0x8<<8);

     //设置PG11 复用功能 uart4
     GPIOG->MODER  &=(~(0x3<<22));
     GPIOG->MODER |= (0x2<<22);

     GPIOG->AFRH &=(~(0xf<<12));
     GPIOG->AFRH |= (0x6<<12);
     

     //禁用串口   第0位设置为0
     USART4->CR1 &=(~(0x1));
     //3.设置数据位宽为8位          第12、28位设置为0
     USART4->CR1 &= (~(0x1<<12));
     USART4->CR1 &= (~(0x1<<28));
     //4.设置无奇偶校验位   第10位设置为0
     USART4->CR1 &= (~(0x1<<10));
     //5.设置16倍过采样   第15设置为0
     USART4->CR1 &= (~(0x1<<15));



    //6.设置停止位  设置[13:12]为0    
    USART4->CR2 &= (~(0x3<<12));

    //7.设置不分频
    USART4->PRESC &= (~0xf);

    //8.设置波特率为115200
    USART4->BRR=0X22B;


    //9.使能发送器
    USART4->CR1 |= (0x1<<3);
    //10.使能接收器
    USART4->CR1 |= (0x1<<2);
    //11.使能串口
    USART4->CR1 |= (0x1);
}

void putchar(char a)
{
    //1.先判断发送器是否为空,不为空等待
    while(!(USART4->ISR &(0x1<<7)));
    //2.向发送寄存器写入数据
    USART4->TDR=a;
    //3.等待发送完成
    while(!(USART4->ISR &(0x1<<6)));
}

char getchar()
{
    char a;
    //1.判断接收器是否有准备好的数据,没有就等待
    while(!(USART4->ISR &(0x1<<5)));
    //2.读取数据
    a=USART4->RDR;
    //3.返回
    return a;
}

void led1_on()
{
    GPIOE->ODR |= (0x1<<10);
}
void led1_off()
{
    GPIOE->ODR &= (~(0x1<<10));
}

void fan1_on()
{
    GPIOE->ODR |= (0x1<<9);
}
void fan1_off()
{
    GPIOE->ODR &= (~(0x1<<9));
}



#include "uart4.h"



int main()

{
    char a;
    uart4_config();
    while(1)
    {
        a=getchar();
        putchar(a);
        if(a=='a')
        {
            led1_on();
        }
        else if(a=='b')
        {

            led1_off();

        }

        else if(a=='c')
        {

            fan1_on();

        }

        else if(a=='d')
        {
            fan1_off();

        }

        putchar('\r');

        putchar('\n');


    }


	return 0;

}



#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
void  uart4_config();
void putchar(char a);
char getchar();
void led1_on();
void led1_off();
void fan1_on();
void fan1_off();

#endif

文章来源:https://blog.csdn.net/2301_79965609/article/details/135207454
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。