12月25日作业

发布时间:2023年12月26日

串口发送控制命令,实现一些外设LED 风扇

uart4.c

#include "uart4.h"

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

  
//===============================================
  //led时钟使能
  RCC->MP_AHB4ENSETR |= (0x3 << 4);
  //设置PE10输出
  GPIOE->MODER &= (~(0x3<<20));
  GPIOE->MODER |= (0x1<<20);
  //设置PE10为推晚输出
  GPIOE->OTYPER &= (~(0x1<<10));
  //PE10为低速输出 
  GPIOE->OSPEEDR &= (~(0x1<<10));
  //设置无上拉下拉
  GPIOE->PUPDR &= (~(0x3<<20));
//===============================================
  //设置PE9输出
  GPIOE->MODER &= (~(0x3<<18));
  GPIOE->MODER |= (0x1<<18);
  //设置PE10为推晚输出
  GPIOE->OTYPER &= (~(0x1<<18));
  //PE9为低速输出 
  GPIOE->OSPEEDR &= (~(0x1<<18));
  //设置无上拉下拉
  GPIOE->PUPDR &= (~(0x3<<18));
//===============================================

  //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 |= (0x1<<11);
  
  //3.0串口禁用
  USART4->CR1 &= (~0x1);
  //4.设置数据位宽为8位
  USART4->CR1 &= (~(0x1<<12));
  USART4->CR1 &= (~(0x1<<28));
  //5.设置无奇偶校验位
  USART4->CR1 &= (~(0x1<<10));
  //6.设置16倍过采样
  USART4->CR1 &= (~(0x1<<15));
  //7.设置1位停止位
  USART4->CR2 &= (~(0x3<<12));
  //8.设置不分频
  USART4->PRESC &= (~0xf);
  //9.设置波特率为115200
  USART4->BRR = 0x22B;
  //10.使能发送器
  USART4->CR1 |= (0x1<<3);
  //11.使能接收器
  USART4->CR1 |= (0x1<<2);
  //12.使能串口
  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 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');

}

int strcmp(char *src,char *dst)  //指令比较
{
  while(*src != '\0' && *dst != '\0')
  {
    if(*src != *dst)
    {
      return *src - *dst;
    }
    src++;
    dst++;
  }
  if(*src == *dst)
  {
    return 0;
  }
  else if(*src == '\0')
  {
    return *dst;
  }
  else if(*dst == '\0')
  {
    return *src;
  }

}

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 *src,char *dst);

#endif

main.c

#include"uart4.h"

int main()
{
    //char a;
    char buf[128];
    uart4_config();
    while(1)
    {
        /*
        //接收一个字符数据
         a = getchar();
         //发送 接收的字符+1
         putchar(a+1); 
         putchar('\r');//回车,切换到一行开头
         putchar('\n');//切换到下一行   
         */
        puts("please enter order");
        gets(buf);
        if(strcmp(buf,"led_on") == 0)
        {
            GPIOE->ODR |= (0x1<<10);
        }
        else if(strcmp(buf,"led_off") == 0)
        {
            GPIOE->ODR &= (~(0x1<<10));
        }
        else if(strcmp(buf,"fen_on") == 0)
        {
            GPIOE->ODR |= (0x1<<9);
        }
        else if(strcmp(buf,"fen_off") == 0)
        {
            GPIOE->ODR &= (~(0x1<<9));
        }
        else
        {
            puts("puts error\n\r");
        }
        //puts(buf);  
    }
}

效果演示

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