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

main.c

#include "uart4.h"
 
 
 
int main()
 
{
 
    char a;
 
    char buf[128];
 
    uart4_config();
 
    while (1)
 
    {
 
        /* //接收一个字符数据
 
          a=getchar();
 
          //发送接收的字符+1
 
          putchar(a+1);
 
          putchar('\r');
 
          putchar('\n');     */
 
        gets(buf); // 读取字符串
 
        //puts(buf); // 输出字符串
 
 
 
		if(strcmp(buf,"led1_on")==0)
 
		led1_on();
 
		if(strcmp(buf,"led1_off")==0)
 
		led1_off();
 
		if(strcmp(buf,"led2_on")==0)
 
		led2_on();
 
		if(strcmp(buf,"led2_off")==0)
 
		led2_off();
 
		if(strcmp(buf,"led3_on")==0)
 
		led3_on();
 
		if(strcmp(buf,"led3_off")==0)
 
		led3_off();
 
 
 
		if(strcmp(buf,"fen_on")==0)
 
		fen_on();
 
		if(strcmp(buf,"fen_off")==0)
 
		fen_off();
 
 
 
		if(strcmp(buf,"md_on")==0)
 
		md_on();
 
		if(strcmp(buf,"md_off")==0)
 
		md_off();
 
 
 
		if(strcmp(buf,"fmq_on")==0)
 
		fmq_on();
 
		if(strcmp(buf,"fmq_off")==0)
 
		fmq_off();
 
 
 
    }
 
}

uart4.c

#include"uart4.h"
 
void uart4_config()
{
    //1.使能GPIOB\GPIOG\UART4外设时钟
    RCC->MP_AHB4ENSETR |= (0x1<<1);//gpiob
    RCC->MP_AHB4ENSETR |= (0x1<<6);//gpiog
    RCC->MP_APB1ENSETR |= (0x1<<16);//uart4
    //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->afrl &= (~(0xF<<8));
    GPIOB->afrl |= (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);
 
 
 
 
    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));
 
 
 
    //风扇
     GPIOE->moder &=(~(0X3<<18));//设置PE9输出
    GPIOE->moder |= (0X1<<18);
    //设置PE9为推挽输出
    GPIOE->otyper &=(~(0x1<<9));
    //PE9为低速输出
    GPIOE->ospeedr &= (~(0x3<18));
    //设置无上拉下拉
    GPIOE->pupdr &= (~(0x3<<18));
 
    //蜂鸣器
    GPIOB->moder &=(~(0X3<<12));//设置PB6输出
    GPIOB->moder |= (0X1<<12);
    //设置PE9为推挽输出
    GPIOB->otyper &=(~(0x1<<6));
    //PE9为低速输出
    GPIOB->ospeedr &= (~(0x3<12));
    //设置无上拉下拉
    GPIOB->pupdr &= (~(0x3<<12));
 
    //马达
    GPIOF->moder &=(~(0X3<<12));//设置PF6输出
    GPIOF->moder |= (0X1<<12);
    //设置PE9为推挽输出
    GPIOF->otyper &=(~(0x1<<6));
    //PE9为低速输出
    GPIOF->ospeedr &= (~(0x3<12));
    //设置无上拉下拉
    GPIOF->pupdr &= (~(0x3<<12));
}
 
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;
}
 
//发送一个字符串
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');
 
}
 
 
int strcmp(char *a,char *b)
{
    while(*a!='\0')
    {
        if(*a != *b)
        {
            return -1;
        }
        a++;
        b++;
    }
    return 0;
}
 
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));
}
 
 
void fen_on()
{
    GPIOE->odr |= (0x1<<9);
}
void fen_off()
{
    GPIOE->odr &= (~(0x1<<9));
    
}
 
void md_on()
{
       GPIOF->odr |= (0x1<<6);
}
void md_off()
{
     GPIOF->odr &= (~(0x1<<6));
}
 
void fmq_on()
{
 GPIOB->odr |= (0x1<<6);
}
void fmq_off()
{
     GPIOF->odr &= (~(0x1<<6));
}

uart4.h

#ifndef __UART4_H__
#define __UART4_H__
#include"stm32mp1xx_rcc.h"
//#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_uart.h"
 
typedef struct{
    unsigned int moder;
    unsigned int otyper;
    unsigned int ospeedr;
    unsigned int pupdr;
    unsigned int idr;
    unsigned int odr;
    unsigned int bsrr;
    unsigned int afrh;
    unsigned int afrl;
}gpio_t;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
#define GPIOG ((gpio_t*)0x50008000)
#define GPIOB ((gpio_t*)0x50003000)
#define RCC_GPIO (*(unsigned int*)0x50000a28)
 
void all_led_init();
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void led3_on();
void led3_off();
void fen_on();
void fen_off();
void md_on();
void md_off();
void fmq_on();
void fmq_off();
 
 
void uart4_config();
void putchar(char a);
char getchar();
void gets(char *s);
void puts(char *s);
int strcmp(char *a,char *b);
#endif

你可能感兴趣的:(单片机,嵌入式硬件)