ARM day6

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

结果:

ARM day6_第1张图片

uart.h

#ifndef  __UART_H__
#define __UART_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h" 
#include "stm32mp1xx_uart.h"

void myuart4_init();
void myputchar(char i);
char mygetchar();
char *mygets();
void myputs(char *s);
void all_led_init();
void led1_on();
void led2_on();
void led3_on();
void led1_off();
void led2_off();
void led3_off();
 int mystrcmp(char *ptr,char *str);
#endif

uart.c


#include "myuart.h"
char buf[100]={0};

//串口数据初始化
void myuart4_init()
{
 //1.UART4和GPIOB\GPIOG的时钟使能
    RCC->MP_AHB4ENSETR |= (0x1<<1);//使能GPIOB时钟
    RCC->MP_AHB4ENSETR |= (0x1<<6);//使能GPIOG时钟
    RCC->MP_APB1ENSETR |= (0X1<<16);//使能UART4时钟
    //2.设置PB2和PG11的管脚复用
    GPIOB->MODER&= (~(0x3<<4));
    GPIOB->MODER|= (0x2<<4); //设置复用
    GPIOB->AFRL &= (~(0XF<<8));
    GPIOB->AFRL |= (0X8<<8);//设置uart4功能复用
    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<<12));
        USART4->CR1 &=(~(0x1<<28));
    //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 i)
{
        //1.判断TDR寄存器是否为空,如果为空,向TDR寄存器写入数据
        while(((USART4->ISR)&(0x1<<7))==0);
            USART4->TDR=i;
    //2.阻塞等待数据传输完成,函数返回
    while(((USART4->ISR)&(0x1<<6))==0);

}

char mygetchar()
{
    //判断RDR寄存器是否有就绪的数据,如果有就读取,否则等待
         char a;
        while(((USART4->ISR)&(0x1<<5))==0);
        a=USART4->RDR;
    return a;
}



void myputs(char *s)
{
    while(*s)
    {
        myputchar(*s);
        s++;
    }
    myputchar('\n');//切换到下一行
       myputchar('\r');//切换到一行的开头
}

char *mygets()
{
    unsigned int i=0;
        for(i=0;i<100;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 *ptr,char *str)
{
    while(*ptr)
    {
        if(*ptr!=*str)
        return -1;
        ptr++;
        str++;
    }
    return 0;
}

main.c

 #include "myuart.h"

int main()

{

	myuart4_init();

	all_led_init();



	char *buf;

	while(1)

	{	

		myputchar('\n');

		myputchar('\r');

		

		buf=mygets();

		

		if(mystrcmp(buf,"led1_on")==0)

		led1_on();

		else if(mystrcmp(buf,"led1_off")==0)

		led1_off();

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

		led2_on();	

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

		led2_off();

		else if(mystrcmp(buf,"led3_on")==0)

		led3_on();

		else if(mystrcmp(buf,"led3_off")==0)

		led3_off();

	}

	return 0;

}

你可能感兴趣的:(arm开发)