作业 11.27 C语言点亮LED灯

代码:

头文件:led.h

#ifndef __LED_H__
#define __LED_H__

//结构体封装
typedef struct
{
	volatile unsigned int MODER;
	volatile unsigned int OTYPER;
	volatile unsigned int OSPEED;
	volatile unsigned int PUPDR;
	volatile unsigned int IRD;
	volatile unsigned int ODR;
}gpio_t;

#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)

//LED1灯初始化
void led1_init(void);
//LED1灯点亮
void led1_on(void);
//LED1灯熄灭
void led1_off(void);


//LED2灯初始化
void led2_init(void);
//LED2灯点亮
void led2_on(void);
//LED2灯熄灭
void led2_off(void);

//LED3灯初始化
void led3_init(void);
//LED3灯点亮
void led3_on(void);
//LED3灯熄灭
void led3_off(void);
#endif

 功能函数:led.c

#include "led.h"

//LED1灯初始化
void led1_init(void)
{
	//1.设置GPIOE控制器使能
	*(volatile unsigned int*)0x50000A28 |=(0x1 << 4);
	//2.设置PE10引脚为输出模式
	GPIOE->MODER &=(~(0x3 << 20));
	GPIOE->MODER |=(0x1 << 20);
	//3.设置PE10引脚为推晚输出模式
	GPIOE->OTYPER &= (~(0x1 << 10));
	//4.设置PE10引脚为低速输出
 	GPIOE->OSPEED &= (~(0x3 << 20));
	//5.设置PE10引脚禁止上下拉电阻
	GPIOE->PUPDR &= (~(0x3 << 20));
}
//LED1灯点亮
void led1_on(void)
{
	//1.设置PE10引脚输出高电平
	GPIOE->ODR |= (0x1 << 10);
}
//LED1灯熄灭
void led1_off(void)
{
	//1.设置PE10引脚输出低电平
	GPIOE->ODR &= (~(0x1 << 10));
}


//LED2灯初始化
void led2_init(void)
{
	//1.设置GPIOF控制器使能
	*(volatile unsigned int*)0x50000A28 |= (0x1 << 5);
	//2.设置PF10引脚为输出模式
	GPIOF->MODER &= (~(0x3 << 20));
	GPIOF->MODER |= (0x1 << 20);
	//3.设置PF10引脚为推晚输出模式
	GPIOF->OTYPER &= (~(0x3 << 20));
	//4.设置PF10引脚为低速输出
	GPIOF->OSPEED &= (~(0x3 << 20));
	//5.设置PF10引脚禁止上下拉电阻
	GPIOF->PUPDR &= (~(0x3 << 20));
}
//LED2灯点亮
void led2_on(void)
{
	//1.设置PF10引脚输出高电平
	GPIOF->ODR |= (0x1 << 10);
}
//LED2灯熄灭
void led2_off(void)
{
	//1.设置PE10引脚输出低电平
	GPIOF->ODR &= (~(0x1 << 10));
}



//LED3灯初始化
void led3_init(void)
{
	//1.设置GPIOE控制器使能
	*(volatile unsigned int*)0x50000A28 |= (0x1 << 4);
	//2.设置PE8引脚为输出模式
	GPIOE->MODER &= (~(0x3 << 16));
	GPIOE->MODER |= (0x1 << 16);
	//3.设置PE8引脚为推晚输出模式
	GPIOE->OTYPER &= (~(0x1 << 8));
	//4.设置PE8引脚为低速输出
	GPIOE->OSPEED &= (~(0x3 << 16));
	//5.设置PE8引脚禁止上下拉电阻
	GPIOE->PUPDR &= (~(0x3 << 16));
}
//LED3灯点亮
void led3_on(void)
{
	//1.设置PE8引脚输出高电平
	GPIOE->ODR |= (0x1 << 8);
}
//LED3灯熄灭
void led3_off(void)
{
	//1.设置PE8引脚输出低电平
	GPIOE->ODR &= (~(0x1 << 8));
}

测试函数:main.c

#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();
	led2_init();
	led3_init();
	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;
}

 现象:

作业 11.27 C语言点亮LED灯_第1张图片

 

你可能感兴趣的:(c语言,开发语言)