当前位置: 首页 > article >正文

实现串口控制

【单片机实现串口控制】
ex:LED1_ON->LED1开
LED1_OFF->LED1关

可以控制蜂鸣器风扇、灯......

编译工具:keil

开发板:stm32u5

void mystrcmp(char* buf)
{
	if(strcmp(buf,"LED1_ON")==0)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_SET);
	}
  else if(strcmp(buf,"LED1_OFF")==0)
	{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_RESET);
	}
	else if(strcmp(buf,"LED3_ON")==0)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
	}
  else if(strcmp(buf,"LED3_OFF")==0)
	{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
	}
	else if(strcmp(buf,"FAN_ON")==0)
	{
		printf("222%s\n\r",buf);
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_SET);
	}
  else if(strcmp(buf,"FAN_OFF")==0)
	{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET);
	}
	else if(strcmp(buf,"MOTOR_ON")==0)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
	}
  else if(strcmp(buf,"MOTOR_OFF")==0)
	{
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
	}
}
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* Configure the System Power */
  SystemPower_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_ICACHE_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	char buf[32];
  while (1)
  {
		//printf("hello world\n\r");
		//HAL_Delay(1000);
		memset(buf,0,sizeof(buf));
		HAL_UART_Receive(&huart1,(uint8_t*)buf,sizeof(buf),10000);
		HAL_UART_Transmit(&huart1,buf,strlen(buf),5);
		printf("111%s\n\r",buf);
		mystrcmp(buf);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

编译工具:vim

开发板:FS_MP1A

//主函数
#include "uart4.h"

void led_ctl_main();
void uart4_ctl();
void usart_ctl(char* buf);

int main()
{
	uart4_init();
	while(1){
		char buf[128];
		gets(buf);
		puts(buf);
		usart_ctl(buf);
	}
	return 0;
}

void uart4_ctl()
{
	uart4_init();
	char ch;
	while(1){
		ch = getchar();
		putchar(ch);
		putchar('\n');
		putchar('\r');
	}
}

void usart_ctl(char* buf)
{
	if(my_strcmp(buf,"LED1_ON") == 0)
	{
		GPIOE->ODR |= (0X1 << 10);
	}
  	else if(my_strcmp(buf,"LED1_OFF")==0)
	{
		GPIOE->ODR &= (~(0X1 << 10));
	}
	else if(my_strcmp(buf,"LED2_ON")==0)
	{
		GPIOF->ODR |= (0X1 << 10);
	}
  	else if(my_strcmp(buf,"LED2_OFF")==0)
	{
		GPIOF->ODR &= (~(0X1 << 10));
	}
	else if(my_strcmp(buf,"LED3_ON")==0)
	{
		GPIOE->ODR |= (0X1 << 8);
	}
  	else if(my_strcmp(buf,"LED3_OFF")==0)
	{
		GPIOE->ODR &= (~(0X1 << 8));		
	}
	else if(my_strcmp(buf,"FAN_ON")==0)
	{
		GPIOE->ODR |= (0X1 << 9);
	}
  	else if(my_strcmp(buf,"FAN_OFF")==0)
	{
		GPIOE->ODR &= (~(0X1 << 9));		
	}
	else if(my_strcmp(buf,"MOTOR_ON")==0)
	{
		GPIOF->ODR |= (0X1 << 6);
	}
  	else if(my_strcmp(buf,"MOTOR_OFF")==0)
	{
		GPIOF->ODR &= (~(0X1 << 6));		
	}
	else if(my_strcmp(buf,"BEEP_ON")==0)
	{
		GPIOB->ODR |= (0X1 << 6);
	}
  	else if(my_strcmp(buf,"BEEP_OFF")==0)
	{
		GPIOB->ODR &= (~(0X1 << 6));		
	}
}
#include "uart4.h"

void uart4_init()
{
	// 外设时钟
	// RCC
	RCC->MP_APB1ENSETR |= (0X1 << 16);
	RCC->MP_AHB4ENSETR |= (0X1 << 1);
	RCC->MP_AHB4ENSETR |= (0X1 << 6);
	RCC->MP_AHB4ENSETR |= (0X1 << 4);
	RCC->MP_AHB4ENSETR |= (0X1 << 5);
	// GPIOB
	GPIOB->MODER &= (~(0X3 << 4));
	GPIOB->MODER |= (0X2 << 4);
	GPIOB->AFRL &= (~(0XF << 8));
	GPIOB->AFRL |= (0X8 << 8);
	// GPIOG
	GPIOG->MODER &= (~(0X3 << 22));
	GPIOG->MODER |= (0x2 << 22);
	GPIOG->AFRH &= (~(0XF << 12));
	GPIOG->AFRH |= (0X6 << 12);
	// 禁用串口UE=0
	USART4->CR1 &= (~(0X1 << 0));
	// 8位数据位
	USART4->CR1 &= (~(0X1 << 12));
	USART4->CR1 &= (~(0X1 << 28));
	USART4->CR1 &= (~(0X1 << 10));	// 没有校验位
	USART4->PRESC &= (~(0XF << 0)); // 不分频
	USART4->CR1 &= (~(0X1 << 15));	// 16倍过采样
	USART4->CR2 &= (~(0X3 << 12));	// 1位停止位
	USART4->BRR = 0X22B;			// 115200波特率
	USART4->CR1 |= (0X1 << 3);		// 使能发送
	USART4->CR1 |= (0X1 << 2);		// 使能接收
	USART4->CR1 |= 0X1;				// 使能串口

	// 设置输出模式
	// 清零输出模式位
	GPIOE->MODER &= (~(0X3 << 20));//LED1
	GPIOF->MODER &= (~(0X3 << 20));//LED2
	GPIOE->MODER &= (~(0X3 << 16));//LED3
	GPIOE->MODER &= (~(0X3 << 18)); //FAN
	GPIOF->MODER &= (~(0X3 << 12)); //MOTOR
	GPIOB->MODER &= (~(0X3 << 12)); //BEEP

	// 置位输出模式
	GPIOE->MODER |= (0X1 << 20);
	GPIOF->MODER |= (0X1 << 20);
	GPIOE->MODER |= (0X1 << 16);
	GPIOE->MODER |= (0X1 << 18); 	
	GPIOF->MODER |= (0X1 << 12);
	GPIOB->MODER |= (0X1 << 12);

	// 设置推挽输出
	GPIOE->OTYPER &= (~(0X3 << 10));
	GPIOF->OTYPER &= (~(0X3 << 10));
	GPIOE->OTYPER &= (~(0X3 << 8));
	GPIOE->OTYPER &= (~(0X3 << 9));
	GPIOF->OTYPER &= (~(0X3 << 6));
	GPIOB->OTYPER &= (~(0X3 << 6));

	// 设置低速输出
	GPIOE->OSPEEDR &= (~(0X3 << 20));
	GPIOF->OSPEEDR &= (~(0X3 << 20));
	GPIOE->OSPEEDR &= (~(0X3 << 16));
	GPIOE->OSPEEDR &= (~(0X3 << 18));
	GPIOF->OSPEEDR &= (~(0X3 << 12));
	GPIOB->OSPEEDR &= (~(0X3 << 12));

	// 设置无上拉下拉电阻
	GPIOE->PUPDR &= (~(0X3 << 20));
	GPIOF->PUPDR &= (~(0X3 << 20));
	GPIOE->PUPDR &= (~(0X3 << 16));
	GPIOE->PUPDR &= (~(0X3 << 18));
	GPIOF->PUPDR &= (~(0X3 << 12));
	GPIOB->PUPDR &= (~(0X3 << 12));
}

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

char getchar()
{
	while (!(USART4->ISR & (0x1 << 5)))
		;
	return USART4->RDR;
}

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

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

int my_strcmp(const char* str,const char* ptr)
{
	while(*str != '\0' && *ptr != '\0')
	{
		if(*str != *ptr)
		{
			return (unsigned char)*str - (unsigned char)*ptr;
		}
		str++;
		ptr++;	
	}
	return ((unsigned char)*str - (unsigned char)*ptr);
}
//头文件
#ifndef __UART_H__
#define __UART_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_uart.h"

void uart4_init();
void putchar(char ch);
char getchar();
void gets(char* s);
void puts(char *s);
int my_strcmp(const char* str,const char* ptr);

#endif

http://www.kler.cn/a/472847.html

相关文章:

  • 使用python将多个Excel表合并成一个表
  • Java-编写的一个生产者-消费者模式
  • 大数据架构设计:数据分层治理的全景指南
  • 浙江安吉成新的分布式光伏发电项目应用
  • 机器学习笔记 - 单幅图像深度估计的最新技术
  • 51单片机——蜂鸣器模块
  • 计算机网络 (31)运输层协议概念
  • JVM实战—11.OOM的原因和模拟以及案例
  • python代码实现了一个金融数据处理和分析的功能,主要围绕国债期货及相关指数数据展开
  • el-table表格合并某一列
  • 【集成学习】Bootstrap抽样
  • 深度学习——回归实战
  • rust学习——环境搭建
  • 海思Linux-DEMO(1)-sample_venc(h265,h264)视频流文件的获取
  • TRAVEO™ T2G的SWAP功能
  • 服务器及MySQL安全设置指南
  • 使用Postman进行Base64解码
  • 使用 Rust 实现零拷贝数据处理:性能优化的极致探索
  • 如何监听Vuex数据的变化?
  • 第四届智能系统、通信与计算机网络国际学术会议(ISCCN 2025)
  • 虚拟机配置静态ip后出现两个ip问题
  • 单片机毕业设计项目分享(4)
  • 实验四 数组和函数
  • Mysql--基础篇--事务(ACID特征及实现原理,事务管理模式,隔离级别,并发问题,锁机制,行级锁,表级锁,意向锁,共享锁,排他锁,死锁,MVCC)
  • 深入学习RabbitMQ的Direct Exchange(直连交换机)
  • 常见的http状态码 + ResponseEntity