ARMday6作業

2023-12-15 09:35:27

?串口发送指令控制硬件工作

uart1.h

#ifndef __UART1_H__
#define __UART1_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h" 
void all_led_init();
void led1_on();
void led2_on();
void led3_on();
void led1_off();
void led2_off();
void led3_off();
void uart4_init();
void myputchar(char i);
char mygetchar();
char *gets();
void puts(char *s);
int mystrcmp(char *p1,char *p2);
#endif

?uart1.c

#include"uart1.h"
char buf[51]={0};
//串口数据初始化
void uart4_init()
{
  //1.UART4和GPIOB\GPIOG的时钟使能
  RCC->MP_AHB4ENSETR|=(0x1<<1);//使能GPIOB时钟
  RCC->MP_AHB4ENSETR|=(0x1<<6);//使能GPIOG时钟
  RCC->MP_AHB4ENSETR|=(0x1<<16);//使能UART4的时钟
  //2.设置PB2和PG11的管脚复用
  GPIOB->MODER&=(~(0x3<<4));
  GPIOB->MODER|=(0x2<<4);//设置复用
  GPIOB->AFRL &=(~(0xF<<8));
  GPIOB->AFRL |=(0x8<<8);//设置uart4功能复用
  //设置PB2和PG11的管脚复用
  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));
  //9.设置波特率115200bps
    USART4->BRR|=0x22B;
  //10.发送器、接收器使能
  USART4->CR1|=(0x1<<3);
  USART4->CR1|=(0x1<<2);
  //11.
  USART4->CR1|=(0x1);
  //
}
void myputchar(char c)
{

  //1.判断TDR寄存器是否为空,如果为空,向TDR寄存器写入数据
  while(!(USART4->ISR&(0x1<<7)));
  USART4->TDR=c;//
  //阻塞等待数据传输完成,函数返回
  while(!(USART4->ISR&(0x1<<6)));//

}
char mygetchar()
{ 
  char c;
  //判断RDR寄存器是否有就绪的数据,如果有就读取,否则等待
  while(!(USART4->ISR&(0x1<<5)));
  c=USART4->RDR;
  return c;
}
//输出一个字符串
void puts(char *s)
{
  while(*s)
  {

    myputchar(*s);
    s++;

  }
  myputchar('\n');
  myputchar('\r');
}

//读取一个字符串
char *gets()
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
      buf[i]=mygetchar();
      myputchar(buf[i]);
      if(buf[i]=='\r')
      break;

  }
  buf[i]='\0';
  myputchar('\n');
  return buf;
}
void all_led_init()
{
  // 1.使能外设时钟
  RCC->MP_AHB4ENSETR|=(0X3<<4);
  // 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 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));
}
int mystrcmp(char *p1,char *p2)
{

   int ret;
    while(*p1 == *p2 && *p1 != '\0' && *p2 != '\0')
    {
        p1++;
        p2++;
    }
    
     ret = *p1 - *p2; // 返回值
 
   if(ret==0)
   return 0;
  else
  return 1;
}

?main.c

#include"uart1.h"
//封装延时函数
void delay(int ms)
{
  int i,j;
  for(i=0;i<ms;i++)
  {
  for(j=0;j<2000;j++);
  }
}
int main()
{
    //串口的初始化
    uart4_init();
    char *str;
    all_led_init();//LED初始化
    
    //现象是发送一个a串口工具打印一个b
    while(1)
    {
      myputchar('\n');
      myputchar('\r');
      //2.从串口读取一个字符串
      str=gets();
      puts(str);
      //3.回显输入的字符串
      if(mystrcmp(str,"led1_on")==0)
      {
        led1_on();
      }else if(mystrcmp(str,"led1_off")==0)
      {
        led1_off();
      }else if(mystrcmp(str,"led2_on")==0)
      {
         led2_on();
      }
      else if(mystrcmp(str,"led2_off")==0)
      {
         led2_off();
      }
      else if(mystrcmp(str,"led3_on")==0)
      {
         led3_on();
      }
      else if(mystrcmp(str,"led3_off")==0)
      {
         led3_off();
      }

  
        
    }
    
    return 0;
}

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