1.LED灯实验(A7核/M4核)
.h
#ifndef __GPIO_H__
#define __GPIO_H__
//RCC寄存器封装,用宏定义替换
#define rcc (*(volatile unsigned int*)0x50000A28)
//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)
//LED1灯初始化
void led1_init();
//LED1灯点亮
void led1_on();
//LED1灯熄灭
void led1_off();
//LED2灯初始化
void led2_init();
//LED2灯点亮
void led2_on();
//LED2灯熄灭
void led2_off();
//LED3灯初始化
void led3_init();
//LED3灯点亮
void led3_on();
//LED3灯熄灭
void led3_off();
#endif
.c
#include "gpio.h"
//LED1灯初始化
void led1_init()
{
//RCC使能E组
rcc |= (0x1 << 4);
//设置为输出模式
gpioe->moder &= (~(0x3 <<20));
gpioe->moder |= (0x1 <<20);
//设置为推挽输出
gpioe->otyper &= (~(0x1 << 10));
//设置为低速
gpioe->ospeedr &= (~(0x3 << 20));
//设置为禁止上下拉电阻
gpioe->pupdr &= (~(0x3 << 20));
}
//LED1灯点亮
void led1_on()
{
//设置输出高电平
gpioe->odr |= (0x1 << 10);
}
//LED1灯熄灭
void led1_off()
{
//设置输出低电平
gpioe->odr &= (~(0x1 << 10));
}
//LED2灯初始化
void led2_init()
{
//RCC使能F组
rcc |= (0x1 << 5);
//设置为输出模式
gpiof->moder &= (~(0x3 <<20));
gpiof->moder |= (0x1 <<20);
//设置为推挽输出
gpiof->otyper &= (~(0x1 << 10));
//设置为低速
gpiof->ospeedr &= (~(0x3 << 20));
//设置为禁止上下拉电阻
gpiof->pupdr &= (~(0x3 << 20));
}
//LED2灯点亮
void led2_on()
{
//设置输出高电平
gpiof->odr |= (0x1 << 10);
}
//LED2灯熄灭
void led2_off()
{
//设置输出低电平
gpiof->odr &= (~(0x1 << 10));
}
//LED3灯初始化
void led3_init()
{
//RCC使能E组
rcc |= (0x1 << 4);
//设置为输出模式
gpioe->moder &= (~(0x3 <<16));
gpioe->moder |= (0x1 <<16);
//设置为推挽输出
gpioe->otyper &= (~(0x1 << 8));
//设置为低速
gpioe->ospeedr &= (~(0x3 << 16));
//设置为禁止上下拉电阻
gpioe->pupdr &= (~(0x3 << 16));
}
//LED3灯点亮
void led3_on()
{
//设置输出高电平
gpioe->odr |= (0x1 << 8);
}
//LED3灯熄灭
void led3_off()
{
//设置输出低电平
gpioe->odr &= (~(0x1 << 8));
}
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()
{
led1_init();
led2_init();
led3_init();
while(1)
{
led1_on();
delay_ms(700);
led2_on();
delay_ms(700);
led3_on();
delay_ms(700);
led1_off();
led2_off();
led3_off();
delay_ms(700);
led1_on();
led2_on();
led3_on();
delay_ms(700);
led1_off();
led2_off();
led3_off();
delay_ms(700);
led3_on();
delay_ms(700);
led2_on();
delay_ms(700);
led1_on();
delay_ms(700);
led1_off();
led2_off();
led3_off();
delay_ms(700);
}
return 0;
}