ARM:作业2

发布时间:2023年12月17日

1.复习串口实验的流程,熟练编写代码

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

a b c d

led1_on led1_off

代码:

main.c

#include "uartt.h"

//封装延时函数

void delay(int ms)

{

	int i,j;

	for(i=0;i<ms;i++)

	{

		for(j=0;j<2000;j++);

	}

}

int main()

{

	all_led_init1();

	//1.串口初始化

	uart4_init();

	char a;

	//现象是发送一个a串口工具打印一个b

    while(1)

    {

        //2.从串口读取一个字符

        a=mygetchar();

        //3.将读取到的字符+1发送回去

        myputchar(a);

		if(a == 'a')

		{

			led1_on1();

			led2_off1();

			led3_off1();

		}

		else if(a == 'b')

		{

			led1_off1();

			led2_on1();

			led3_off1();

		}

		else if(a == 'c')

		{

			led1_off1();

			led2_off1();

			led3_on1();

		}

    }



	return 0;

}

uartt.c

#include "uartt.h"

//串口初始化
void uart4_init()
{
    //1.UART4和GPIOB\GPIOG的时钟使能
    RCC->MP_AHB4ENSETR |= (0x1<<2);//使能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<<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 << 0));

    //9.设置波特率为115200bps
    USART4->BRR = 0x22b;

    //10.发送器、接收器使能
    USART4->CR1 |=(0x1<<3);
    USART4->CR1 |=(0x1<<2);
    
    //11.串口使能
	USART4->CR1 |= (0x1 << 0);

}

//封装函数发送一个字符数据
void myputchar(char i)
{
    //1.判断TDR寄存器是否为空,如果为空,向TDR寄存器中写入数据
    while(!(USART4->ISR & (0x1 << 7)));
    USART4->TDR = i;
    //2.阻塞等待数据传输完成,函数返回
    while(!(USART4->ISR & (0x1 << 6)));
}

char mygetchar()
{
    char c;
    //判断RDR寄存器是否有就绪数据,如果有就读取,否则等待
    while(!(USART4->ISR&(0X1<<5)));//数据传输完成,函数结束
    c=USART4->RDR;
    return c;
}


void all_led_init1()
{
  // 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_on1()
{

  GPIOE->ODR |= (0x1 << 10);
}
void led2_on1()
{

  GPIOF->ODR|= (0x1 << 10);
}
void led3_on1()
{

  GPIOE->ODR |= (0x1 << 8);
}

void led1_off1()
{
  GPIOE->ODR &= (~(0x1 << 10));
}
void led2_off1()
{
  GPIOF->ODR &= (~(0x1 << 10));
}
void led3_off1()
{
  GPIOE->ODR &= (~(0x1 << 8));
}

uartt.h

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

#define RCC_MP_AHB4ENSETR (*(unsigned int *)0X50000a28)

void uart4_init();
void myputchar(char i);
char mygetchar();

void all_led_init1();
void led1_on1();
void led2_on1();
void led3_on1();
void led1_off1();
void led2_off1();
void led3_off1();
#endif

运行结果:

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