12.25 串口通信控制外设小灯

led.c

#include "led.h"
void all_led_init()
{
    RCC->MP_AHB4ENSETR |= (0x3<<4);

    GPIOE->MODER &=(~(0X3<<20));//设置PG11复用
    GPIOE->MODER |= (0X1<<20);
    GPIOE->MODER &=(~(0X3<<16));//设置PG11复用
    GPIOE->MODER |= (0X1<<16);
    //设置PG11为推挽输出
    GPIOE->OTYPER &=(~(0x1<<10));
     GPIOE->OTYPER &=(~(0x1<<8));
    //PE10为低速输出
    GPIOE->OSPEEDR &= (~(0x3<<20));
        GPIOE->OSPEEDR &= (~(0x3<<16));
    //设置无上拉下拉
    GPIOE->PUPDR &= (~(0x3<<20));
    GPIOE->PUPDR &= (~(0x3<<16));
    
    //PF10
    GPIOF->MODER &=(~(0X3<<20));//设置Pf10输出
    GPIOF->MODER |= (0X1<<20);
    //设置Pf10为推挽输出
    GPIOF->OTYPER &=(~(0x1<<10));
    //Pf10为低速输出
    GPIOF->OSPEEDR &= (~(0x3<<20));
    //设置无上拉下拉
    GPIOF->PUPDR &= (~(0x3<<20));

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

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

void led2_off()
{
    GPIOF->ODR &= (~(0x1<<10));
}
void led3_on()
{
    GPIOE->ODR |= (0x1<<8);
}

void led3_off()
{
    GPIOE->ODR &= (~(0x1<<8));
}

led.h

#ifndef __LED_H__
#define __LED_H__
#include  "stm32mp1xx_gpio.h"
#include  "stm32mp1xx_rcc.h"

void all_led_init();
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void led3_on();
void led3_off();

#endif

uart4.c

#include "uart4.h"

void  uart4_config()
{
    //配置时钟
    RCC->MP_AHB4ENSETR |= (0x1<<1);
    RCC->MP_AHB4ENSETR |= (0x1<<6);
    RCC->MP_APB1ENSETR |= (0x1<<16);

    //设置PG11
    GPIOG->MODER &=(~(0x3<<22));
    GPIOG->MODER |=(0x2<<22);
    GPIOG->AFRH &=(~(0xf<<12));
    GPIOG->AFRH |=(0x6<<12);

    //设置PB2
    GPIOB->MODER &=(~(0x3<<4));
    GPIOB->MODER |=(0x2<<4);
    GPIOB->AFRL &=(~(0xf<<8));
    GPIOB->AFRL  |=(0x8<<8);

    //设置禁用端口
    USART4->CR1 &=(~0x1);

    //设置数据长度为8位
    USART4->CR1 &=(~(0x1<<12));
    USART4->CR1 &=(~(0x1<<28));

    //设置无校验
    USART4->CR1 &=(~(0x1<<10));

    // 设置16倍过采样
       USART4->CR1 &=(~(0x1<<15));

    //设置停止位位宽
    USART4->CR2 &=(~(0x3<<12));

    //设置不分频
    USART4->PRESC &=(~0xf);
    //设置波特率
    USART4->BRR=0X22B;

    //设置使能
    USART4->CR1 |=(0x1<<3);
    USART4->CR1 |=(0x1<<2);
    USART4->CR1 |=(0x1);
}

void  putchar(char a)
{
    //判断是否为空
    while(!(USART4->ISR &(0x1<<7)));
    //发送数据
    USART4->TDR=a;
    //等待发送完成
    while(!(USART4->ISR &(0x1<<6)));
}

char getchar()
{
    char a;
    //判断有没有数据可读
    while(!(USART4->ISR &(0x1<<5)));
    //读数据
    a=USART4->RDR;
    //返回
    return a;
}

int strcmp(char *s,char *d)
{
    while(*s)
    {
        if((*s) != (*d))
        {
            return -1;
        }
        s++;
        d++;
    }
    return 0;
}

void puts(char *s)
{
    while(*s)
    {
        putchar(*s);
        s++;
    }
    putchar('\r');
    putchar('\n');
}

void gets(char *s)
{
    while(1)
    {
        *s=getchar();
        putchar(*s);
        if(*s=='\r')
        break;
        s++;
    }
    *s='\0';
    putchar('\n');
}

uart4.h

#ifndef  __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"
void  uart4_config();
void  putchar(char a);
char getchar();
void puts(char *s);
void gets(char *s);
int strcmp(char *s,char *d);
#endif

你可能感兴趣的:(linux,运维,服务器)