【无标题】

1、main.c

#include"led.h"

//手动封装一个延时函数

void delay_ms(int ms)

{

    int i,j;

    for(i=0;i<ms;i++)

    {

        for(j=0;j<2000;j++)

        {

        }

    }

}

int main()

{

    //使能GPIO的外设时钟

    *((unsigned int *)0X50000A28) |= (0X3<<4);

    led1_init();

    led2_init();

    led3_init();

   

    while(1)

    {

        //LED1亮

        led1_ctl(1);

        led2_ctl(0);

        led3_ctl(0);

        delay_ms(500);

        //LED2亮

        led1_ctl(0);

        led2_ctl(1);

        led3_ctl(0);

        delay_ms(500);

        delay_ms(500);

        //LED3亮

        led1_ctl(0);

        led2_ctl(0);

        led3_ctl(1);

        delay_ms(500);

        delay_ms(500);

    }

    return 0;

}

2、c

#include "led.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));

}

//LED1控制

void led1_ctl(int flag)

{

    //根据传递的参数不同决定是开灯还是关灯

    if(flag==1)

    {

        GPIOE->ODR |= (0x1<<10);//开灯

    }

    else if(flag==0)

    {

        GPIOE->ODR &= (~(0x1<<10));//关灯

    }

}

//LED2控制

void led2_ctl(int flag)

{

    if(flag==1)

    {

        GPIOF->ODR |= (0x1<<10);

    }

    else if(flag==0)

    {

        GPIOF->ODR &= (~(0x1<<10));

    }

}

//LED3控制

void led3_ctl(int flag)

{

    if(flag==1)

    {

        GPIOE->ODR |= (0x1<<8);

    }

    else if(flag==0)

    {

        GPIOE->ODR &= (~(0x1<<8));

    }

}

3、led.h

#ifndef __LED_H__

#define __LED_H__

typedef struct{

    unsigned int MODER;

    unsigned int OTYPER;

    unsigned int OSPEEDR;

    unsigned int PUPDR;

    unsigned int IDR;

    unsigned int ODR;

    unsigned int BSRR;

}gpio_t;

#define  GPIOE ((gpio_t *)0x50006000)

#define  GPIOF ((gpio_t *)0x50007000)

void led1_init();

void led2_init();

void led3_init();

void led1_ctl(int flag);

void led2_ctl(int flag);

void led3_ctl(int flag);

#endif

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值