12.25 作业

1,

uart4.h

#ifndef __UART4_H__
#define __UART4_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"

void putchar(char a);
char getchar();
void puts(char *s);
char *gets();
void uart4_config();
void compare(char a);
int strcmp(char a,char b);
void string_compare(char *s);
int  string_strcmp(char *a,char *b);
#endif

uart4.c

#include"uart4.h"

void uart4_config()
{
    //1.使能GPIOB\GPIOG\UART4外设时钟
    RCC->MP_AHB4ENSETR |= (0x1<<1);//gpiob
    RCC->MP_AHB4ENSETR |= (0x1<<6);//gpiog
    RCC->MP_APB1ENSETR |= (0x1<<16);//uart4
    //2.设置PB2\PG11用于UART4的管脚复用
    //设置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);
    //3.设置数据位宽为8位
    USART4->CR1 &= (~(0x1<<12));
    USART4->CR1 &= (~(0x1<<28));
    //4.设置无奇偶校验位
    USART4->CR1 &= (~(0x1<<10));
    //5.设置16倍过采样
    USART4->CR1 &= (~(0x1<<15));
    //6.设置1位停止位
    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);


    //设置PE10/PE10/PE8的时钟使能
    RCC->MP_AHB4ENSETR |= (0x3<<4);
    //PE10
    GPIOE->MODER &=(~(0X3<<20));
    GPIOE->MODER |=(0X1<<20);
    //PF10
    GPIOF->MODER &=(~(0X3<<20));
    GPIOF->MODER |=(0X1<<20);
    //PE8
    GPIOE->MODER &=(~(0X3<<16));
    GPIOE->MODER |=(0X1<<16);

    //风扇PE9
    GPIOE->MODER &=(~(0X3<<18));
    GPIOE->MODER |=(0X1<<18);
}

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 compare(char a){

        switch(a)
        {
        case 'A':
            GPIOE->ODR |=(0X1<<10);
            break;
        case 'S':
            GPIOF->ODR |=(0X1<<10);
            break;
        case 'D':
            GPIOE->ODR |=(0X1<<8);
            break;
        case 'F':
            GPIOE->ODR |=(0X1<<9);
            break;  
        case 'Z':
            GPIOE->ODR &=(~(0X1<<10));
            break;
        case 'X':
            GPIOF->ODR &=(~(0X1<<10));
            break;
        case 'C':
            GPIOE->ODR &=(~(0X1<<8));
            break;
        case 'V':
            GPIOE->ODR &=(~(0X1<<9));
            break;
        default:
            break;
        }

}

int strcmp(char a,char b){
    if(a==b){
        return 0;
    }else{
        return -1;
    }
}

mian.c

#include "uart4.h"



int main()

{

    char a;

    uart4_config();

    while (1)

    {

    	a=getchar();

		if(strcmp(a,'Q')==0){

			break;

		}

        compare(a);

	

    }

}



// 最终现象:键盘输入a,串口工具显示b



你可能感兴趣的:(单片机,嵌入式硬件)