实现串口控制
【单片机实现串口控制】
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