- gpio.h
- gpio.c
- main.c
c语言点灯 gpio.h
#ifndef __GPIO_H__
#define __GPIO_H__
//1.RCC定义寄存器封装
#define RCC_AHB4_ENSETR *((volatile unsigned int*)0x50000A28)
//2.GPIO封装
typedef struct
{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)
//3.LED灯初始化
void LED_init();
//4.LED灯亮
void LED_on();
//5.LED灯灭
void LED_off();
#endif
gpio.c
#include
void LED_init(gpio_t* gpiox, int pin )//输入gpio组,并初始化配置
{
gpiox->MODER&=(~(0x3<<(pin*2)));
gpiox->MODER|=(0x1<<(pin*2));
gpiox->OTYPER&=(~(0x1<<pin));
gpiox->OSPEEDR&=(~(0x3<<(pin*2)));
gpiox->PUPDR&=(~(0X3<<pin*2));
}
void LED_on(gpio_t* gpiox,int pin)//组+pin选择点亮
{
gpiox->ODR|=(0x1<<pin);
}
void LED_off(gpio_t* gpiox,int pin )
{
gpiox->ODR&=(~(0x1<<pin));
}
main.c
#include "gpio.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()
{
RCC_AHB4_ENSETR|=(0x3<<4);//使能E,F时钟
gpio_t* led1,*led2,*led3;//
led1=led3=GPIOE;
led2=GPIOF;
int pin1=10;
int pin2=8;
LED_init(led1,pin1);
LED_init(led2,pin1);
LED_init(led3,pin2);
//初始化(配置)gpio
while(1)
{
LED_on(led1,pin1);
delay_ms(500);
LED_off(led1,pin1);
delay_ms(500);
LED_on(led2,pin1);
delay_ms(500);
LED_off(led2,pin1);
delay_ms(500);
LED_on(led3,pin2);
delay_ms(500);
LED_off(led3,pin2);
delay_ms(500);
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)