ARM作业

串口控制三盏灯亮灭

uart4.h

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



void uart4_init();
void putchar(char a);
char getchar();
#endif

led.h

#ifndef __LED_H__
#define __LED_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
// typedef struct 
// {
//     unsigned int MODER;
//     unsigned int OTYPER;
//     unsigned int OSPEEDR;
//     unsigned int  PUPDR;
//     unsigned int IDR;
//     unsigned int ODR;
//     unsigned int  BSRR;
// }gpio_t;
// #define GPIOE ((gpio_t *)0x50006000)
// #define GPIOF ((gpio_t *)0x50007000)
// #define RCC (*(unsigned int *)0X50000A28)
// #define GPIOE_MODER (*(unsigned int *)0X50006000)
// #define GPIOF_MODER (*(unsigned int *)0X50007000)
// #define GPIOE_OTYPER (*(unsigned int *)0X50006004)
// #define GPIOE_OSPEEDR (*(unsigned int *)0X50006008)
// #define GPIOE_OPUPDR (*(unsigned int *)0X5000600C)
// #define GPIOE_ODR (*(unsigned int *)0X50006014)
// #define GPIOF_OTYPER (*(unsigned int *)0X50007004)
// #define GPIOF_OSPEEDR (*(unsigned int *)0X50007008)
// #define GPIOF_OPUPDR (*(unsigned int *)0X5000700C)
// #define GPIOF_ODR (*(unsigned int *)0X50007014)
void all_led_init();
void LED1_ON();
void LED2_ON();
void LED3_ON();
void LED1_OFF();
void LED2_OFF();
void LED3_OFF();

#endif

uart4.c

 #include "uart4.h"

void uart4_init()
{
    //使能GPIOB GPIOG UART4外设时钟
    RCC->MP_AHB4ENSETR |= (0x1<<1);
    RCC->MP_AHB4ENSETR |= (0x1<<6);
    RCC->MP_APB1ENSETR |= (0x1<<16);

    //设置PB2和PG11管脚复用
    GPIOB->MODER &= (~(0x3<<4));
     GPIOB->MODER |= (0x1<<5);
     GPIOG->MODER &= (~(0x3<<22));
     GPIOG->MODER |= (0x1<<23);
     GPIOB->AFRL &=(~(0xF<<8));
     GPIOB->AFRL |= (0X1<<11);
     GPIOG->AFRH &=(~(0xF<<12));
     GPIOG->AFRH |=  (0X3<<13);
    //设置串口不使能 UE=0
    USART4->CR1 &= (~(0X1));
    //设置8位数据位
    USART4->CR1 &= (~(0X1<<12));
    USART4->CR1 &= (~(0X1<<28));
    //设置没有奇偶校验位
    USART4->CR1 &= (~(0X1<<10));
    //设置1位停止位
    USART4->CR2 &= (~(0X3<<12));
    //设置16倍过采样
    USART4->CR1 &= (~(0X1<<15));
    //设置时钟不分频
    USART4->PRESC &= (~(0XF));
    //设置波特率为115200
    USART4->BRR=0x22B;
    //使能发送器
    USART4->CR1 |= (0X1<<3);
    //使能接收器
       USART4->CR1 |= (0X1<<2);
    //使能uart4
       USART4->CR1 |= (0X1);
    
}

void putchar(char a)
{
    //先判断发送数据寄存器是否为空
    //不为空阻塞等待
        while(!(USART4->ISR&(0x1<<7)));
    //为空向发送数据寄存器写入a的数据
    USART4->TDR=a;
    //写入完成需要判断发送是否完成,不完成阻塞等待,完成了则函数结束
            while(!(USART4->ISR&(0x1<<6)));
}

char getchar()
{
    char a;
    //先判断接收数据寄存器种有没有准备好的数据
    //如果数据没有准备好则阻塞等到
    while(!(USART4->ISR&(0x1<<5)));
    //如果数据准备好了则读取
    a=USART4->RDR;
    //读取完毕将读取到的数据返回
    return a;
}

led.c

#include "led.h"

void all_led_init()//led灯的初始化
{
    //GPIOE/GPIOF时钟使能
   RCC->MP_AHB4ENSETR  |= (0x3<<4);
    //PE10设置为输出
    GPIOE->MODER &= (~(0X3<<20));
    GPIOE->MODER |= (0X1<<20);
    //PF10设置为输出
    GPIOF->MODER &= (~(0X3<<20));
    GPIOF->MODER |= (0X1<<20);
    //PE8设置为输出
    GPIOE->MODER &= (~(0X3<<16));
    GPIOE->MODER |= (0X1<<16);
    //PE10设置为推挽输出
    GPIOE->OTYPER &= (~(0X1<<10));
    //PF10设置为推挽输出
    GPIOF->OTYPER &= (~(0X1<<10));
    //PE8设置为推挽输出
    GPIOE->OTYPER &= (~(0X1<<8));
    //PE10设置为低速输出
    GPIOE->OSPEEDR &= (~(0X3<<20));
    //PF10设置为低速输出
    GPIOF->OSPEEDR &= (~(0X3<<20));
    //PE8设置为低速输出
   GPIOE->OSPEEDR &= (~(0X3<<16));
    //PE10无上拉下拉电阻
    GPIOE->PUPDR &= (~(0X3<<20));
    //PF10无上拉下拉电阻
    GPIOF->PUPDR &= (~(0X3<<20));
    //PE8无上拉下拉电阻
    GPIOE->PUPDR &= (~(0X3<<16));

    //三盏灯默认输出低电平
    GPIOE->ODR &= (~(0X1<<10));
   GPIOF->ODR &= (~(0X1<<10));
    GPIOE->ODR &= (~(0X1<<8));
}

//led1亮
void LED1_ON()
{
    GPIOE->ODR |= (0X1<<10);
}
//led2亮
void LED2_ON()
{
   GPIOF->ODR |= (0X1<<10);
}
//led1亮
void LED3_ON()
{
    GPIOE->ODR |= (0X1<<8);
}

//LED1灭
void LED1_OFF()
{
     GPIOE->ODR &= (~(0X1<<10));
}
//LED2灭
void LED2_OFF()
{
     GPIOF->ODR &= (~(0X1<<10));
}
//LED3灭
void LED3_OFF()
{
     GPIOE->ODR &= (~(0X1<<8));
}    

main.c

#include"led.h"

// #include "fan.h"

// #include "bee.h"

// #include"motor.h"

#include"uart4.h"

//封装延时函数

void delay(int ms)

{

    int i,j;

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

    {

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

        {}



    }

}

int main()

{

    //led初始化

    all_led_init();

	// fan_init();

	// bee_init();

	// motor_init();

    while(1)

    {

        // LED1_ON();

        // LED2_OFF();

        // LED3_OFF();

        // delay(500);

        // LED2_ON();

        // LED1_OFF();

        // LED3_OFF();

        // delay(500);

        // LED3_ON();

        // LED2_OFF();

        // LED1_OFF();

        // delay(500);



		// FAN_ON();

		// delay(5000);

		// FAN_OFF();

		// delay(1000);



		// BEE_ON();

		// delay(1000);

		// BEE_OFF();

		// delay(1000);



		// MOTOR_ON();

		// delay(1000);

		// MOTOR_OFF();

		// delay(1000);



         //串口初始化

    uart4_init();

    char i;

    while(1)

    {

        i=getchar();

        putchar(i);

        if(i=='1')

        {

            LED1_ON();

            LED2_OFF();

            LED3_OFF();

        }

       if(i=='2')

        {

            LED2_ON();

            LED1_OFF();

            LED3_OFF();

        }

               if(i=='3')

        {

            LED3_ON();

            LED2_OFF();

            LED1_OFF();

        }

     

    }

    }

    return 0;

}
相关推荐
憧憬一下2 天前
PCIe_Host驱动分析_地址映射
arm开发·嵌入式硬件·嵌入式·linux驱动开发·pci/pcie
楼兰公子2 天前
相机主要调试参数
arm开发·驱动·camera·v4l2
7yewh2 天前
嵌入式驱动RK3566 HDMI eDP MIPI 背光 屏幕选型与调试提升篇-eDP屏
linux·arm开发·驱动开发·嵌入式硬件·嵌入式linux·rk·edp
千千道4 天前
深入理解 Linux 内核启动流程
linux·arm开发·驱动开发
徐某人..4 天前
ARM嵌入式学习--第八天(PWM)
arm开发·学习·arm
aiamia4 天前
CAN配置---波特率中断引脚等---autochips-AC7811-ARM-M3内核
arm开发·单片机·mcu·车载系统·汽车
驯龙高手_追风4 天前
ARM32位MCU开发板调试经验总结
arm开发·单片机·嵌入式硬件
重生之我是数学王子4 天前
ARM原理
arm开发·系统架构
qq762118224 天前
arm Rk3588 更新固件
arm开发
@嵌入式Linux小白5 天前
了解ARM的千兆以太网——RK3588
linux·arm开发·驱动开发