2024/05/21

主函数

#include "led.h"
#include "delay.h"
#include "buzzer.h"
#include "uart4.h"

int main()
{	
	//初始化GPIO外部时钟
	*((unsigned int *)0x50000A28) |= (0x1<<1);
	*((unsigned int *)0x50000A28) |= (0x3<<4);

	uart4_init();
	//char a;
	char buf[32];
	led1_init();
	led2_init();
	led3_init();
	buzzer_init();

	while (1)
	{
		/*a = getchar();
		putchar(a + 1);
		putchar('\n');
		putchar('\r');*/
		gets(buf);
		puts(buf);
		uart4_recv_cmd(buf);
	}	
	return 0;
}

uart.c

#include "uart4.h"
#include "led.h"

// 初始化uart4
void uart4_init()
{
    // 使能uart4、GPIOG\GPIOB的外设时钟
    RCC->MP_APB1ENSETR |= (0x1 << 16);
    RCC->MP_AHB4ENSETR |= (0x1 << 1);
    RCC->MP_AHB4ENSETR |= (0x1 << 6);
    // 将PB2引脚设置为复用功能
    GPIOB->MODER &= (~(0x3 << 4));
    GPIOB->MODER |= (0x2 << 4);
    // 将PB2引脚设置为uart_rx复用功能
    GPIOB->AFRL &= (~(0xF << 8));
    GPIOB->AFRL |= ((0x8 << 8));
    // 将PB11引脚设置为复用功能
    GPIOB->MODER &= (~(0x3 << 22));
    GPIOB->MODER |= (0x1 << 23);
    // 将PB11引脚设置为uart_tx复用功能
    GPIOB->AFRL &= (~(0xF << 12));
    GPIOB->AFRL |= ((0x6 << 12));
    // 禁用串口
    USART4->CR1 &= (~0x1);
    // 设置8位数据位
    USART4->CR1 &= ((~0x1) << 28);
    USART4->CR1 &= ((~0x1) << 12);
    // 设置无奇偶校验位
    USART4->CR1 &= ((~0x1) << 10);
    // 设置1位停止位
    USART4->CR2 &= ((~0x3) << 12);
    // 设置波特率为115200
    USART4->BRR |= (0x22B);
    // 设置频率不分频
    USART4->PRESC &= (~(0xF));
    // 设置16倍过采样倍数
    USART4->CR1 &= ((~0x1) << 15);
    // 使能发送器
    USART4->CR1 |= ((0x1) << 3);
    // 使能接收器
    USART4->CR1 |= ((0x1) << 2);
    // 使能串口
    USART4->CR1 |= (0x1);
}

// 封装单个字符发送函数
void putchar(char c)
{
    // 判断发送数据寄存器是否为空,不为空则阻塞等待
    while (!(USART4->ISR & (0x1 << 7)))
        ;
    // 当为空时将数据写入到数据发送寄存器
    USART4->TDR = c;
    // 阻塞等待数据发送完毕,函数结束
    while (!(USART4->ISR & (0x1 << 6)))
        ;
}

// 封装单个字符接收的函数
char getchar()
{
    // 判断接收数据寄存器中有没有有效数据。如果没有就阻塞等待,如果有就直接读取
    while (!(USART4->ISR & (0x1 << 5)))
        ;
    // 将读取的数据返回
    return USART4->RDR;
}

// 封装字符串发送函数
void puts(char *s)
{
    while (*s)
    {
        putchar(*s);
        s++;
    }
    putchar('\n');
    putchar('\r');
}

// 封装字符串e接收函数
void gets(char *s)
{
    while (1)
    {
        *s = getchar(*s);
        putchar(*s);
        if (*s == '\r')
        {
            break;
        }
        s++;
    }
    *s = '\0';
    putchar('\n');
}

//字符串比较函数
int strcmp(const char *source, const char *dest)
{
    while (*source++ == *dest++ && *source != '\0')
        ;
    return *source == *dest ? 1 : 0;
}

//接收串口消息操作函数
void uart4_recv_cmd(char *s)
{
    if (strcmp(s,"led_on"))
    {
        led_on();
    }
    else if (strcmp(s,"led_off"))
    {
        led_off();
    }
    else if (strcmp(s,"led_breath"))
    {
        led_breath();
    }
    else if (strcmp(s,"led_loop"))
    {
        led_loop();
    }
        
        
}

led.c

#include "../include/led.h"
#include "../include/delay.h"
#include "../include/stm32mp1xx_gpio.h"

void led1_init()
{
    // 设置PE10为输出模式
    GPIOE->MODER &= (~(0x3 << 20));
    GPIOE->MODER |= (0x1 << 20);
    // 设置PE10输出模式为推挽输出
    GPIOE->OTYPER &= (~(0x1 << 10));
    // 设置PE10输出速度为低速
    GPIOE->OSPEEDR &= (~(0x3 << 20));
    // 设置PE10无上拉下拉电阻
    GPIOE->PUPDR &= (~(0x3 << 20));
}

void led2_init()
{
    // 设置PF10为输出模式
    GPIOF->MODER &= (~(0x3 << 20));
    GPIOF->MODER |= (0x1 << 20);
    // 设置PF10输出模式为推挽输出
    GPIOF->OTYPER &= (~(0x1 << 10));
    // 设置PF10输出速度为低速
    GPIOF->OSPEEDR &= (~(0x3 << 20));
    // 设置PF10无上拉下拉电阻
    GPIOF->PUPDR &= (~(0x3 << 20));
}

void led3_init()
{
    // 设置PE8为输出模式
    GPIOE->MODER &= (~(0x3 << 16));
    GPIOE->MODER |= (0x1 << 16);
    // 设置PE8为输出模式
    GPIOE->OTYPER &= (~(0x1 << 8));
    // 设置PE8为输出模式
    GPIOE->OSPEEDR &= (~(0x3 << 16));
    // 设置PE8为输出模式
    GPIOE->PUPDR &= (~(0x3 << 16));
}

void led1_ctl(int flag)
{
    if (flag == 1)
    {
        // 设置PE10输出高电平
        GPIOE->ODR |= (0x1 << 10);
    }
    else
    {
        // 设置PE10输出低电平
        GPIOE->ODR &= (~(0x1 << 10));
    }
}

void led2_ctl(int flag)
{
    if (flag == 1)
    {
        // 设置PF10输出高电平
        GPIOF->ODR |= (0x1 << 10);
    }
    else
    {
        // 设置PF10输出高电平
        GPIOF->ODR &= (~(0x1 << 10));
    }
}

void led3_ctl(int flag)
{
    if (flag == 1)
    {
        // 设置PE8输出高电平
        GPIOE->ODR |= (0x1 << 8);
    }
    else
    {
        // 设置PE8输出高电平
        GPIOE->ODR &= (~(0x1 << 8));
    }
}

void led_breath()
{
    for (int i = 0; i < 500; i++)
    {
        // 设置PE10输出高电平
        GPIOE->ODR |= (0x1 << 10);
        delay_us(i);
        // 设置PE10输出低电平
        GPIOE->ODR &= (~(0x1 << 10));
        delay_us(500 - i);
    }

    for (int i = 0; i < 500; i++)
    {
        // 设置PE10输出低电平
        GPIOE->ODR &= (~(0x1 << 10));
        delay_us(i);
        // 设置PE10输出高电平
        GPIOE->ODR |= (0x1 << 10);
        delay_us(500 - i);
    }
}

void led_loop()
{
    int i = 3;
    while (i--)
    {
        led1_ctl(1);
        led2_ctl(0);
        led3_ctl(0);
        delay_ms(500);

        led1_ctl(0);
        led2_ctl(1);
        led3_ctl(0);
        delay_ms(500);

        led1_ctl(0);
        led2_ctl(0);
        led3_ctl(1);
        delay_ms(500);
    }
}

void led_on()
{
    led1_ctl(1);
    led2_ctl(1);
    led3_ctl(1);
}

void led_off()
{
    led1_ctl(0);
    led2_ctl(0);
    led3_ctl(0);
}

IMG_0507

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值