ARM day6

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

结果:

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;

}
相关推荐
韦德斯16 小时前
嵌入式Linux的RTC读写操作应用
linux·运维·c语言·arm开发·实时音视频
byte轻骑兵16 小时前
嵌入式 ARM Linux 系统构成全解:从硬件到应用层层剖析
linux·arm开发·arm·嵌入式开发
思尔芯S2C19 小时前
面向未来的智能视觉参考设计与汽车架构,思尔芯提供基于Arm技术的创新方案
arm开发·架构·汽车·iot·fpga原型验证·prototyping·智慧视觉
Eternal-Student2 天前
【docker了解】如何将x86镜像转换为适用于Jetson的ARM镜像
arm开发·docker·容器
不怕犯错,就怕不做2 天前
修复kernel编译栈帧大小异常问题error: the frame size of 1928 bytes is larger than 1024 bytes
linux·arm开发·驱动开发
憧憬一下3 天前
UART硬件介绍
arm开发·嵌入式硬件·串口·嵌入式·linux驱动开发
Petal9909123 天前
UEFI学习笔记(十八):ARM电源管理之PSCI和SCMI概述
arm开发·笔记·学习·uefi
古月居GYH4 天前
一文了解ARM内部架构
arm开发·架构
白书宇4 天前
13.100ASK_T113-PRO RTC实验
linux·arm开发·驱动开发·嵌入式硬件·物联网·硬件工程
简简单单一天吃六顿4 天前
rootfs根文件系统在Linux下制作动态库
linux·服务器·arm开发·iot