用C语言实现LED1/LED2/LED3三盏灯点亮

这是一个使用C语言编写的STM32微控制器程序,用于初始化和控制LED灯。在main函数中,对LED1、LED2和LED3进行初始化并交替开关,通过delay_ms函数实现延时。在led.h头文件中定义了GPIO寄存器和LED控制函数的原型,在led.c文件中实现了具体的LED初始化和开关操作。
摘要由CSDN通过智能技术生成

main代码如下:

#include "./include/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()
{
    led1_init();
    led3_init();
    led2_init();// LED灯初始化
    while(1)
    {
        led1_on();
        delay_ms(500);
        led1_off();
        delay_ms(500);

        led2_on();
        delay_ms(500);
        led2_off();
        delay_ms(500);
        
        led3_on();
        delay_ms(500);
        led3_off();
        delay_ms(500);
    }
    return 0;
}

led.h代码如下:

#ifndef __LED_H__
#define __LED_H__

#define GPIOE (*(volatile unsigned int*)0x50000A28)
#define GPIOE_MODER (*(volatile unsigned int*)0x50006000)
#define GPIOE_OTYPER (*(volatile unsigned int*)0x50006004)
#define GPIOE_OSPEEDR (*(volatile unsigned int*)0x50006008)
#define GPIOE_PUPDR (*(volatile unsigned int*)0x5000600C)
#define GPIOE_ODR (*(volatile unsigned int*)0x50006014)

#define GPIOF (*(volatile unsigned int*)0x50000A28)
#define GPIOF_MODER (*(volatile unsigned int*)0x50007000)
#define GPIOF_OTYPER (*(volatile unsigned int*)0x50007004)
#define GPIOF_OSPEEDR (*(volatile unsigned int*)0x50007008)
#define GPIOF_PUPDR (*(volatile unsigned int*)0x5000700C)
#define GPIOF_ODR (*(volatile unsigned int*)0x50007014)

void led1_init();
void led1_on();
void led1_off();
 

void led2_init();
void led2_on();
void led2_off();

void led3_init();
void led3_on();
void led3_off();

#endif

led.c代码如下:

#include "../include/led.h"

void led1_init()
{
    GPIOE        |=(0x1<<4);
    GPIOE_MODER  &=(~(0x3<<20));
    GPIOE_MODER  |=(0x1<<20);
    GPIOE_OTYPER &=(~(0x1<<10));
    GPIOE_OSPEEDR&=(~(0x3<<20));
    GPIOE_PUPDR  &=(~(0x3<<20));

}

void led1_on()
{
    GPIOE_ODR |=(0x1<<10);
}

void led1_off()
{
    GPIOE_ODR &=(0x1<<10);
}


void led2_init()
{

    GPIOF        |=(0x1<<5);
    GPIOF_MODER  &=(~(0x3<<20));
    GPIOF_MODER  |=(0x1<<20);
    GPIOF_OTYPER &=(~(0x1<<10));
    GPIOF_OSPEEDR&=(~(0x3<<20));
    GPIOF_PUPDR  &=(~(0x3<<20));
}
void led2_on()
{

    GPIOF_ODR |=(0x1<<10);
}
void led2_off()
{
    
    GPIOF_ODR &=(0x1<<10);
}
void led3_init()
{
    GPIOE        |=(0x1<<4);
    GPIOE_MODER  &=(~(0x3<<16));
    GPIOE_MODER  |=(0x1<<16);
    GPIOE_OTYPER &=(~(0x1<<8));
    GPIOE_OSPEEDR&=(~(0x3<<16));
    GPIOE_PUPDR  &=(~(0x3<<16));

}
void led3_on()
{
    GPIOE_ODR |=(0x1<<8);

}
void led3_off()
{
    GPIOE_ODR &=(0x1<<8);
}

结果如果所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值