led.c
#include "led.h"
#define GPIOD_MODER (*(volatile unsigned long *)0x40020C00)
#define GPIOD_OTYPER (*(volatile unsigned long *)0x40020C04)
#define GPIOD_PUPDR (*(volatile unsigned long *)0x40020C0C)
#define GPIOD_IDR (*(volatile unsigned long *)0x40020C10)
#define GPIOD_ODR (*(volatile unsigned long *)0x40020C14)
#define RCC_AHB1ENR (*(volatile unsigned long *)0x40023830)
//LED状态输出初始化
void led_set_init(){
//1、使能GPIOD时钟
RCC_AHB1ENR |= (0x01<<3);
//2、后八位置为 01010101 PD0~PD3通用输出
GPIOD_MODER = (GPIOD_MODER|0x000000ff)&0xffffff55;
//3、PD0~PD3设为推挽输出
GPIOD_OTYPER = GPIOD_OTYPER & 0xfffffff0;
}
void led_on(unsigned char site){
led_set_init();
switch(site){
case 0:
GPIOD_ODR &= ~(0x01); //PD0置0
break;
case 1:
GPIOD_ODR &= ~(0x01<<1); //PD1置0
break;
case 2:
GPIOD_ODR &= ~(0x01<<2); //PD2置0
break;
case 3:
GPIOD_ODR &= ~(0x01<<3); //PD3置0
break;
default:
break;
}
}
void led_off(unsigned char site){
led_set_init();
switch(site){
case 0:
GPIOD_ODR |= (0x01); //PD0置1
break;
case 1:
GPIOD_ODR |= (0x01<<1); //PD1置1
break;
case 2:
GPIOD_ODR |= (0x01<<2); //PD2置1
break;
case 3:
GPIOD_ODR |= (0x01<<3); //PD3置1
break;
default:
break;
}
}
char get_led_status(unsigned char site){
switch(site){
case 0:
return (GPIOD_IDR >> 0) & (0x01);
case 1:
return (GPIOD_IDR >> 1) & (0x01);
case 2:
return (GPIOD_IDR >> 2) & (0x01);
case 3:
return (GPIOD_IDR >> 3) & (0x01);
default:
return -1;
}
}
//on_off 0:开灯 1:关灯
void led_operate(unsigned char site,unsigned char on_off){
if(on_off == 0){
led_on(site);
}else if(on_off == 1){
led_off(site);
}
}
|