ARMday6
2023-12-25 19:45:32
串口发送控制命令,实现一些外设LED 风扇 马达。。。
uart4.h
#ifndef __UART4_H__
#define __UART4_H__
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_uart.h"
#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();
int strcmp(const char *s1, const char *s2);
void uart4_config();
void putchar(char a);
char getchar();
void gets(char *s);
void puts(char *s);
#endif
main.c
#include"uart4.h"
#include"led.h"
int main()
{
uart4_config();
all_led_init();
//char a;
char buf[128];
while(1)
{
/*//接收一个字符数据
a=getchar();
//发送接收的字符+1
putchar(a+1);
putchar('\r');
putchar('\n'); */
gets(buf); // 读取字符串
if(strcmp(buf,"led1_on") == 0)
{
//LED1亮
led1_on();
}
if(strcmp(buf,"led1_off") == 0)
{
//LED1灭
led1_off();
}
if(strcmp(buf,"led2_on") == 0)
{
//LED2亮
led2_on();
}
if(strcmp(buf,"led2_off") == 0)
{
//LED2灭
led2_off();
}
if(strcmp(buf,"led3_on") == 0)
{
//LED3亮
led3_on();
}
if(strcmp(buf,"led3+_off") == 0)
{
//LED3灭
led3_off();
}
//puts(buf); // 输出字符串
}
}
led.c
#include "uart4.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));
}
uart4.c
#include"uart4.h"
void uart4_config()
{
//1.使能GPIOB\GPIOG\UART4外设时钟
RCC->MP_AHB4ENSETR |= 0X42;
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->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;
}
//字符串比较
int strcmp(const char *s1, const char *s2)
{
for(int i=0; s1[i]!='\0' || s2[i] !='\0'; i++)
{
if(s1[i]>s2[i])
{
return 1;
}
if(s1[i]<s2[i])
{
return -1;
}
}
return 0;
}
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')
{
*s='\0';
break;
}
s++;
}
putchar('\n');
}
文章来源:https://blog.csdn.net/qq_56986199/article/details/135206790
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!