ARM(串口控制硬件) 2023.12.11

2023-12-15 10:53:17

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

uart.h

#ifndef __MYUART_H__
#define __MYUART_H__


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

//串口初始化
void uart_init();
//LED灯的初始化;
void led_init();
//接收单个字符
char mygetchar();
//发送单个字符
void myputchar(char c);
//接收字符串
char* mygets();
//发送字符串
void myputs(char * p);
//灯的亮灭
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void led3_on();
void led3_off();
//strcmp
int mystrcmp(const char* p,const char* q);




#endif

uart.c

#include "myuart.h"


char instruct[51] = {0};

//串口初始化
void uart_init()
{
    //RCC对UART,GPIOB,GPIOG使能
    RCC->MP_AHB4ENSETR |= (0x1 << 1);
    RCC->MP_AHB4ENSETR |= (0x1 << 6);
    RCC->MP_APB1ENSETR |= (0x1 << 16);
    //对GPIOB,GPIOG设置复用模式,并且设置为uart模式
    GPIOB->MODER &= (~(0x3 << 4));
    GPIOB->MODER |= (0x1 << 5);
    GPIOB->AFRL &= (~(0xF << 8));
    GPIOB->AFRL |= (0x1 << 11);
    GPIOG->MODER &= (~(0x3 << 22));
    GPIOG->MODER |= (0x1 << 23);
    GPIOB->AFRH &= (~(0xF << 12));
    GPIOB->AFRH |= (0x3 << 13);
    //禁用串口
    USART4->CR1 &= (~0x1);
    //设置数据位为8位
    USART4->CR1 &= (~(0x1 << 12));
    USART4->CR1 &= (~(0x1 << 28));
    //设置无奇偶校验
    USART4->CR1 &= (~(0x1 << 10));
    //设置停止位为一位
    USART4->CR2 &= (~(0x3 << 12));
    //设置波特率
    USART4->BRR = 0x22b;
    //设置过采样为16位
    USART4->CR1 &= (~(0x1 << 15));
    //设置1分频
    USART4->PRESC &= (~0xF);
    //设置TE/RE使能
    USART4->CR1 |= (0x3 << 2);
    //设置串口使能
    USART4->CR1 |= (0x1);

}

//LED灯的初始化;
void led_init()
{
    //设置为输出模式
    GPIOE->MODER &= (~(0x3 << 20));
    GPIOE->MODER |= (0x1 << 20);
    GPIOE->MODER &= (~(0x3 << 16));
    GPIOE->MODER |= (0x1 << 16);
    GPIOF->MODER &= (~(0x3 << 20));
    GPIOF->MODER |= (0x1 << 20);
}

//接收单个字符
char mygetchar()
{
    //判断是否存在数据待读取
    while(!(USART4->ISR & (0x1 << 5)));
    char c = USART4->RDR;
    return c;
}

//发送单个字符
void myputchar(char c)
{
    while(!(USART4->ISR & (0x1 << 7)));
    USART4->TDR = c;
    while(!(USART4->ISR & (0x1 << 6)));
    return;
}

//接收字符串
char* mygets()
{
    unsigned int i;
    for(i = 0;i< 50 ;i++)
    {
        instruct[i] = mygetchar();
        myputchar(instruct[i]);    
        if(instruct[i] == '\r')
        break;
    }
    instruct[i] = '\0';
    myputchar('\n');
    return instruct;
}

//发送字符串
void myputs(char* p)
{
    while(*p)
    {
        myputchar(*p);
        p++;
    }
    myputchar('\n');
    myputchar('\r');
}


//亮灯,灭灯
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));
}

//strcmp
int mystrcmp(const char* p,const char* q)
{
    while(*p != '\0' && *q != '\0' && *p == *q)
    {
        p++;
        q++;
    }
    return *p - *q;
}

main.c

#include "myuart.h"





//封装延时函数

void delay(int ms)

{

  int i,j;

  for(i=ms;i>=0;i--)

  {

  for(j=10;j>=0;j--);

  }

}



int main(int argc, char const *argv[])

{

  uart_init();

  led_init();

  myputs("qqq\n");

  myputchar('\r');

  myputs("please input instruct");

  

  while (1)

  {

    /* code */

    /*

    myputchar('\n');//切换到下一行

    myputchar('\r');//切换到一行的开头

    char c = mygetchar();

    myputchar(c);

    */

    char* ptr;

    ptr = mygets();

    if(!mystrcmp(ptr,"led1_on"))

      led1_on();

    else if(!mystrcmp(ptr,"led2_on"))

      led2_on();

    else if(!mystrcmp(ptr,"led3_on"))

      led3_on();

    else if(!mystrcmp(ptr,"led1_off"))

      led1_off();

    else if(!mystrcmp(ptr,"led2_off"))

      led2_off();

    else if(!mystrcmp(ptr,"led3_off"))

      led3_off();

    else

    {

      myputs("input error");

      continue;

    }

    myputs("input success");   

  }

  

  return 0;

}

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