main.c
#include"uart-led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
int main()
{
hal_uart4_led_init();
cmd_t * res;
gotwo();
while(1)
{
char* str = hal_get_string();
cmd_t* res;
res = find_command(str);
if(res == 0){
hal_put_string("没有找到该命令\n");
}else{
res->gpio_write_p(res->gpio,res->pin,res->status);
}
}
return 0;
}
uart-led.h
#ifndef __UART4_H__
#define __UART4_H__
#include "stm32mp1xx_uart.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
//初始化相关操作
void hal_uart4_led_init();
//发送一个字符
void hal_put_char(const char str);
//发送一个字符串
void hal_put_string(const char* string);
//接收一个字符
char hal_get_char();
//接受一个字符串
char* hal_get_string();
typedef enum{
GPIO_RESET,
GPIO_SET,
}status_t;
//操作灯亮灭
void hal_gpio_write(gpio_t* gpios,unsigned int pin,status_t gpio_status);
//换行
void gotwo();
//定义结构体
typedef struct{
char* cmd_char;
gpio_t* gpio;
unsigned int pin;
status_t status;
void (*gpio_write_p)(gpio_t* gpio,unsigned int pin,status_t status);
}cmd_t;
//命令对比函数
cmd_t* find_command(char* str);
#define GPIO_PIN_10 10
#define GPIO_PIN_8 8
#endif
uart-led.c文章来源:https://www.toymoban.com/news/detail-531786.html
#include"uart-led.h"
static char p[128]={};
//初始化相关操作
void hal_uart4_led_init(){
/*******RCC初始化***********/
//pb2使能
RCC->MP_AHB4ENSETR |= (0x1 << 1);
//pg11使能
RCC->MP_AHB4ENSETR |= (0x1 << 6);
//uart4使能
RCC->MP_APB1ENSETR |= (0x1 << 16);
//pe10 pe8使能
RCC->MP_AHB4ENSETR |= (0x1 << 4);
//pf10使能
RCC->MP_AHB4ENSETR |= (0x1 << 5);
/*******GPIO初始化***********/
//pg11引脚初始化 模式设置为复用模式
GPIOG->MODER &= (~(0x3 << 22));
GPIOG->MODER |= (0x1 << 23);
//pg11引脚复用功能为TX_AFRH
GPIOG->AFRH &=(~(0xf << 12));
GPIOG->AFRH |=(0x6 << 12);
//pb2引脚模式设置为复用模式
GPIOB->MODER &= (~(0x3 << 4));
GPIOB->MODER |= (0x1 << 5);
//pb2引脚复用功能为TX_AFRH
GPIOB->AFRL &=(~(0xf << 8));
GPIOB->AFRL |=(0x8 << 8);
//PE10引脚初始化
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
GPIOE->OTYPER &= (~(0x1 << 10));
GPIOE->OSPEEDR &= (~(0x3 << 20));
GPIOE->PUPDR &= (~(0x3 << 20));
//pf10引脚初始化
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOF->OTYPER &= (~(0x1 << 10));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0xd8 << 20));
//pe8引脚初始化
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
GPIOE->OTYPER &= (~(0x1 << 8));
GPIOE->OSPEEDR &= (~(0x3 << 16));
GPIOE->PUPDR &= (~(0x3 << 16));
// GPIOE->ODR |= (0x1 << 8);
//GPIOE->ODR &= (~(0x1 << 8));
/*******UART4初始化***********/
//设置串口UE=0
USART4->CR1 &= (~(0x1));
//设置1位起始位8位数据位
USART4->CR1 &= (~(0x1 << 28));
USART4->CR1 &= (~(0x1 << 12));
//没有奇偶校验位
USART4->CR1 &= (~(0x1 << 10));
//设置1位停止位
USART4->CR2 &= (~(0x3 << 12));
//设置16倍采样率
USART4->CR1 &= (~(0x1 << 15));
//设置串口不分频
USART4->PRESC &= (~(0xf));
//设置串口波特率位115200
USART4->BRR = 0x22b;
//设置串口发送器使能
USART4->CR1 &= (~(0x1 << 2));
USART4->CR1 |= (0x1 << 2);
//设置接口接收器使能
USART4->CR1 &= (~(0x1 << 3));
USART4->CR1 |= (0x1 << 3);
//设置串口使能
USART4->CR1 |= (0x1);
}
/*
* function: hal_gpio_write 开关灯
* @param [ in]
* @param [out]
* @return
*/
void hal_gpio_write(gpio_t* gpios,unsigned int pin,status_t gpio_status){
if(gpio_status == GPIO_RESET){
gpios->ODR &= (~(0x1 << pin));
}else{
gpios->ODR |=(gpio_status << pin);
}
}
//发送一个字符
void hal_put_char(const char str){
//判断发送数据寄存器是否为空ISR[7]
//特点:为空才可以满足下一位的数据,为满则需要等待
//读0:满需要等待 读1:发送数据
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = str;
//判断一帧数据是否发送完成
//读0:没有发送完成,需要等待 读1:发送完成
while(!(USART4->ISR & (0x1 << 6)));
}
//发送一个字符串
void hal_put_string(const char* string){
//判断是否为'\0'
//一个字符一个字符进行发送
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\r';
while(!(USART4->ISR & (0x1 << 6)));
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\n';
while(!(USART4->ISR & (0x1 << 6)));
while(*string){
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = *string;
//判断一帧数据是否发送完成
//读0:没有发送完成,需要等待 读1:发送完成
while(!(USART4->ISR & (0x1 << 6)));
string++;
}
}
//接收一个字符
char hal_get_char(){
//判断接收数据寄存器中是否接收到数据 ISR[5]
while(!(USART4->ISR & (0x1 << 5)));
//将接收数据寄存器中的内容读出来
char ch;
ch = USART4->RDR;
// while(!(USART4->ISR & (0x1 << 6)));
return ch;
}
//接受一个字符串
char* hal_get_string(){
//循环进行接收
//当键盘的回车键按键之后,代表字符串输出完成'\r'
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\r';
while(!(USART4->ISR & (0x1 << 6)));
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\n';
while(!(USART4->ISR & (0x1 << 6)));
int i=0;
while(1){
while(!(USART4->ISR & (0x1 << 5)));
p[i]= USART4->RDR;
while(!(USART4->ISR & (0x1 << 6)));
if(p[i] == '\r'){
p[i]='\0';
break;
}
hal_put_char(p[i]);
i++;
}
return p;
}
//换行
void gotwo(){
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\r';
while(!(USART4->ISR & (0x1 << 6)));
while(!(USART4->ISR & (0x1 << 7)));
//将发送的内容,赋值给发送数据寄存器
USART4->TDR = '\n';
while(!(USART4->ISR & (0x1 << 6)));
}
cmd_t cmd_arr[6]={
{"led1on",GPIOE,GPIO_PIN_10,GPIO_SET,hal_gpio_write},\
{"led1off",GPIOE,GPIO_PIN_10,GPIO_RESET,hal_gpio_write},\
{"led2on",GPIOF,GPIO_PIN_10,GPIO_SET,hal_gpio_write},\
{"led2off",GPIOF,GPIO_PIN_10,GPIO_RESET,hal_gpio_write},\
{"led3on",GPIOE,GPIO_PIN_8,GPIO_SET,hal_gpio_write},\
{"led3off",GPIOE,GPIO_PIN_8,GPIO_RESET,hal_gpio_write},\
};
cmd_t* find_command(char* str){
int i,flag=0;
char *p;char*q;
for(i=0;i<6;i++){
p=str;
q=(cmd_arr+i)->cmd_char;
flag=1;
while(*p){
if(*p!=*q){
flag=0;
break;
}
p++;
q++;
}
if(!(*q) && flag==1){
break;
}
}
hal_put_char(i);
if(flag==1){
return (cmd_arr+i);
}else{
return 0;
}
}
文章来源地址https://www.toymoban.com/news/detail-531786.html
到了这里,关于arm学习stm32串口指令点亮led灯的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!