ARM12.25

串口发送控制命令,实现一些外设LED 风扇 马达 运转

下实现灯亮

uart4.h

#ifndef __UART4_H__
#define __UART4_H__
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_uart.h"
void uart4_config();
void putchar(char a);
char getchar();
void puts(char *s);
void gets(char *s);
int strcmp(char *s1,char *s2);

#endif

led.h

#ifndef __LED_H__
#define __LED_H__

#define RCC_GPIO (*(unsigned int *)0x50000a28)
#define GPIOE_MODER (*(unsigned int *)0x50006000)
#define GPIOF_MODER (*(unsigned int *)0x50007000)
#define GPIOE_OTYPER (*(unsigned int *)0x50006004)
#define GPIOF_OTYPER (*(unsigned int *)0x50007004)
#define GPIOE_OSPEEDR (*(unsigned int *)0x50006008)
#define GPIOF_OSPEEDR (*(unsigned int *)0x50007008)
#define GPIOE_PUPDR (*(unsigned int *)0x5000600C)
#define GPIOF_PUPDR (*(unsigned int *)0x5000700c)
#define GPIOE_ODR (*(unsigned int *)0x50006014)
#define GPIOF_ODR (*(unsigned int *)0x50007014)

void all_led_init();
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void led3_on();
void led3_off();

#endif

uart4.c

#include "uart4.h"
void uart4_config()
{
        //1.使能GPIOB\GPIOG\UART4外设时钟
        
        RCC->MP_AHB4ENSETR |=(0X1<<1);
        RCC->MP_AHB4ENSETR |=(0X1<<6);
        RCC->MP_APB1ENSETR |=(0X1<<16);
        //2.设置PB2\PG11用于UART4的管脚复用
        //设置PG11
        GPIOG->MODER &= (~(0X3<<22));
        GPIOG->MODER |= (0X2<<22);
        GPIOG->AFRH &= (~(0XF<<12));
        GPIOG->AFRH |=(0X6<<12);
        //设置PB2
        GPIOB->MODER &= (~(0X3<<4));
        GPIOB->MODER |= (0X2<<4);
        GPIOB->AFRH &= (~(0XF<<8));
        GPIOB->AFRH |=(0X8<<8);
             //串口禁用
        USART4->CR1 &=(~0X1);
        //3.设置数据位宽为8位
        USART4->CR1 &=(~(0X1<<12)); 
        USART4->CR1 &=(~(0X1<<28));
        //4.设置无奇偶校验位
        USART4->CR1 &=(~(0X1<<10));
        //5.设置16倍过采样
        USART4->CR1 &=(~(0X1<<15));
        //6.设置1位停止位
        USART4->CR2 &=(~(0X3<<12));
        //7.设置不分频
        USART4->PRESC &= (~0XF);
        //8.设置波特率为115200
        USART4->BRR = 0X22B;
        //9.使能发送器
        USART4->CR1 |=(0X1<<3);
        //10.使能接收器
        USART4->CR1 |=(0X1<<2);
        //11.使能串口
        USART4->CR1 |=(0X1);

}

void putchar(char a)
{
        //1.先判断发送器是否为空,不为空等待
        while(!(USART4->ISR & (0X1<<7)));
        //2.向发送寄存器写入数据
        USART4->TDR=a;
        //3.等待发送完成
        while(!(USART4->ISR & (0X1<<6)));
}

char getchar()
{
        char a;    
         //1.判断接收器是否有准备好的数据,没有就等待
         while(!(USART4->ISR & (0X1<<5)));
        //2.读取数据
        a=USART4->RDR;
        //3.返回
        return a;
}

int strcmp(char *s1,char *s2)
{
    while(*s1 != '\0')
    {
        if(*s1 != *s2)
        {
            return -1;
        }
        s1++;
        s2++;
    }
        return 0;
}



void puts(char *s)
{
    while(*s)
    {
        putchar(*s);
        s++;
    }
    putchar('\r');
	putchar('\n');
}

void gets(char *s)
{
    while(1)
    {
        *s=getchar();
        putchar(*s);
        if(*s=='\r')
        break;
        s++;
    }
*s='\0';
putchar('\n');
}

led.c

#include "led.h"
void all_led_init()
{
    RCC_GPIO |= (0X3<<4);//时钟使能
    GPIOE_MODER &=(~(0X3<<20));//设置PE10输出
    GPIOE_MODER |= (0X1<<20);
    //设置PE10为推挽输出
    GPIOE_OTYPER &=(~(0x1<<10));
    //PE10为低速输出
    GPIOE_OSPEEDR &= (~(0x3<<20));
    //设置无上拉下拉
    GPIOE_PUPDR &= (~(0x3<<20));

    //LED2
    GPIOF_MODER &=(~(0X3<<20));//设置Pf10输出
    GPIOF_MODER |= (0X1<<20);
    //设置Pf10为推挽输出
    GPIOF_OTYPER &=(~(0x1<<10));
    //Pf10为低速输出
    GPIOF_OSPEEDR &= (~(0x3<<20));
    //设置无上拉下拉
    GPIOF_PUPDR &= (~(0x3<<20));

    //LED3
    GPIOE_MODER &=(~(0X3<<16));//设置PE8输出
    GPIOE_MODER |= (0X1<<16);
    //设置PE8为推挽输出
    GPIOE_OTYPER &=(~(0x1<<8));
    //PE8为低速输出
    GPIOE_OSPEEDR &= (~(0x3<16));
    //设置无上拉下拉
    GPIOE_PUPDR &= (~(0x3<<16));
}
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));
}

main.c

#include"uart4.h"

#include"led.h"

int main()

{

	//char a ;

	char buf[128];

	uart4_config();

	all_led_init();

	while(1)

	{

	/*	a=getchar();

		putchar(a+1);

		putchar('\r');

		putchar('\n');*/

	gets(buf);

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

		led1_on();

		if(strcmp(buf,"led2_on")==0)

		led2_on();

		if(strcmp(buf,"led3_on")==0)

		led3_on();

		if(strcmp(buf,"led1_off")==0)

		led1_off();

		if(strcmp(buf,"led2_off")==0)

		led2_off();

		if(strcmp(buf,"led3_off")==0)

		led3_off();

	}
}

效果图:

相关推荐
学习路上_write25 分钟前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念31 分钟前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
jjjxxxhhh1232 小时前
FPGA,使用场景,相比于单片机的优势
单片机·嵌入式硬件·fpga开发
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_2 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
changingshow3 小时前
Arduino IDE Windows 系统 离线安装 esp32 开发板 亲测好用。
单片机·嵌入式硬件
7yewh6 小时前
嵌入式硬件杂谈(一)-推挽 开漏 高阻态 上拉电阻
驱动开发·stm32·嵌入式硬件·mcu·物联网·硬件架构·pcb工艺
Chervin13 小时前
Windows,虚拟机Ubuntu和开发板三者之间的NFS服务器搭建
linux·单片机·ubuntu·arm
TeYiToKu14 小时前
笔记整理—linux驱动开发部分(8)framebuffer类设备
linux·驱动开发·笔记·嵌入式硬件·arm
7yewh15 小时前
嵌入式硬件电子电路设计(五)MOS管详解(NMOS、PMOS、三极管跟mos管的区别)
stm32·嵌入式硬件·mcu·物联网·硬件架构·硬件工程·pcb工艺