256 lines
6.7 KiB
C
256 lines
6.7 KiB
C
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
*
|
|||
|
* 模块名称 : LED指示灯驱动模块
|
|||
|
* 文件名称 : bsp_led.c
|
|||
|
* 版 本 : V1.0
|
|||
|
* 说 明 : 驱动LED指示灯
|
|||
|
*
|
|||
|
* 修改记录 :
|
|||
|
* 版本号 日期 作者 说明
|
|||
|
* V1.0 2013-02-01 armfly 正式发布
|
|||
|
*
|
|||
|
* Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
|
|||
|
*
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
|
|||
|
#include "bsp.h"
|
|||
|
|
|||
|
/*
|
|||
|
该程序适用于安富莱STM32-X4、STM32-F4开发板
|
|||
|
|
|||
|
如果用于其它硬件,请修改GPIO定义和 IsKeyDown1 - IsKeyDown8 函数
|
|||
|
|
|||
|
如果用户的LED指示灯个数小于4个,可以将多余的LED全部定义为和第1个LED一样,并不影响程序功能
|
|||
|
*/
|
|||
|
|
|||
|
#ifdef STM32F103C8T6 /* STM32F103C8T6 开发板 */
|
|||
|
/*
|
|||
|
STM32F103C8 LED口线分配:
|
|||
|
LED1 : PC13 (低电平点亮,高电平熄灭)
|
|||
|
LED2 :
|
|||
|
LED3 :
|
|||
|
LED4 :
|
|||
|
*/
|
|||
|
#define RCC_ALL_LED RCC_APB2Periph_GPIOC /* 按键口对应的RCC时钟 */
|
|||
|
|
|||
|
#define GPIO_PORT_LED1 GPIOC
|
|||
|
#define GPIO_PIN_LED1 GPIO_Pin_13
|
|||
|
|
|||
|
// #define GPIO_PORT_LED2 GPIOE
|
|||
|
// #define GPIO_PIN_LED2 GPIO_Pin_3
|
|||
|
|
|||
|
// #define GPIO_PORT_LED3 GPIOE
|
|||
|
// #define GPIO_PIN_LED3 GPIO_Pin_4
|
|||
|
|
|||
|
// #define GPIO_PORT_LED4 GPIOE
|
|||
|
// #define GPIO_PIN_LED4 GPIO_Pin_5
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
#ifdef STM32_F4
|
|||
|
/* STM32_F4 */
|
|||
|
/*
|
|||
|
安富莱STM32-V5 开发板LED口线分配:
|
|||
|
LD1 : PI10/TP_NCS (低电平点亮,高电平熄灭)
|
|||
|
LD2 : PF7/NRF24L01_CSN (低电平点亮,高电平熄灭)
|
|||
|
LD3 : PF8/SF_CS (低电平点亮,高电平熄灭)
|
|||
|
LD4 : PC2/NRF905_CSN/VS1053_XCS (低电平点亮,高电平熄灭)
|
|||
|
*/
|
|||
|
|
|||
|
/* 按键口对应的RCC时钟 */
|
|||
|
#define RCC_ALL_LED (RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOI)
|
|||
|
|
|||
|
#define GPIO_PORT_LED1 GPIOI
|
|||
|
#define GPIO_PIN_LED1 GPIO_Pin_10
|
|||
|
|
|||
|
#define GPIO_PORT_LED2 GPIOF
|
|||
|
#define GPIO_PIN_LED2 GPIO_Pin_7
|
|||
|
|
|||
|
#define GPIO_PORT_LED3 GPIOF
|
|||
|
#define GPIO_PIN_LED3 GPIO_Pin_8
|
|||
|
|
|||
|
#define GPIO_PORT_LED4 GPIOC
|
|||
|
#define GPIO_PIN_LED4 GPIO_Pin_2
|
|||
|
#endif
|
|||
|
|
|||
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
* 函 数 名: bsp_InitLed
|
|||
|
* 功能说明: 配置LED指示灯相关的GPIO, 该函数被 bsp_Init() 调用。
|
|||
|
* 形 参: 无
|
|||
|
* 返 回 值: 无
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void bsp_InitLed(void)
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
/* 打开GPIO时钟 */
|
|||
|
RCC_APB2PeriphClockCmd(RCC_ALL_LED, ENABLE);
|
|||
|
|
|||
|
/*
|
|||
|
配置所有的LED指示灯GPIO为推挽输出模式
|
|||
|
由于将GPIO设置为输出时,GPIO输出寄存器的值缺省是0,因此会驱动LED点亮.
|
|||
|
这是我不希望的,因此在改变GPIO为输出前,先关闭LED指示灯
|
|||
|
*/
|
|||
|
bsp_LedOff(1);
|
|||
|
// bsp_LedOff(2);
|
|||
|
// bsp_LedOff(3);
|
|||
|
// bsp_LedOff(4);
|
|||
|
|
|||
|
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED1;
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 设为 输出推挽模式 */
|
|||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* IO口最大速度 */
|
|||
|
|
|||
|
GPIO_Init(GPIO_PORT_LED1, &GPIO_InitStructure);
|
|||
|
|
|||
|
// GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED2;
|
|||
|
// GPIO_Init(GPIO_PORT_LED2, &GPIO_InitStructure);
|
|||
|
|
|||
|
// GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED3;
|
|||
|
// GPIO_Init(GPIO_PORT_LED3, &GPIO_InitStructure);
|
|||
|
|
|||
|
// GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED4;
|
|||
|
// GPIO_Init(GPIO_PORT_LED4, &GPIO_InitStructure);
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
* 函 数 名: bsp_LedOn
|
|||
|
* 功能说明: 点亮指定的LED指示灯。
|
|||
|
* 形 参: _no : 指示灯序号,范围 1 - 4
|
|||
|
* 返 回 值: 无
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void bsp_LedOn(uint8_t _no)
|
|||
|
{
|
|||
|
_no--;
|
|||
|
|
|||
|
if (_no == 0)
|
|||
|
{
|
|||
|
// STM32F103C8T6开发板 1是亮,0是灭
|
|||
|
// GPIO_PORT_LED1->BRR = GPIO_PIN_LED1;
|
|||
|
GPIO_PORT_LED1->BSRR |= GPIO_PIN_LED1;
|
|||
|
}
|
|||
|
// else if (_no == 1)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED2->BSRRH = GPIO_PIN_LED2;
|
|||
|
// }
|
|||
|
// else if (_no == 2)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED3->BSRRH = GPIO_PIN_LED3;
|
|||
|
// }
|
|||
|
// else if (_no == 3)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED4->BSRRH = GPIO_PIN_LED4;
|
|||
|
// }
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
* 函 数 名: bsp_LedOff
|
|||
|
* 功能说明: 熄灭指定的LED指示灯。
|
|||
|
* 形 参: _no : 指示灯序号,范围 1 - 4
|
|||
|
* 返 回 值: 无
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void bsp_LedOff(uint8_t _no)
|
|||
|
{
|
|||
|
_no--;
|
|||
|
|
|||
|
if (_no == 0)
|
|||
|
{
|
|||
|
// STM32F103C8T6开发板 1是亮,0是灭
|
|||
|
GPIO_PORT_LED1->BRR = GPIO_PIN_LED1;
|
|||
|
}
|
|||
|
// else if (_no == 1)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED2->BSRRL = GPIO_PIN_LED2;
|
|||
|
// }
|
|||
|
// else if (_no == 2)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED3->BSRRL = GPIO_PIN_LED3;
|
|||
|
// }
|
|||
|
// else if (_no == 3)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED4->BSRRL = GPIO_PIN_LED4;
|
|||
|
// }
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
* 函 数 名: bsp_LedToggle
|
|||
|
* 功能说明: 翻转指定的LED指示灯。
|
|||
|
* 形 参: _no : 指示灯序号,范围 1 - 4
|
|||
|
* 返 回 值: 按键代码
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void bsp_LedToggle(uint8_t _no)
|
|||
|
{
|
|||
|
if (_no == 1)
|
|||
|
{
|
|||
|
GPIO_PORT_LED1->ODR ^= GPIO_PIN_LED1;
|
|||
|
}
|
|||
|
// else if (_no == 2)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED2->ODR ^= GPIO_PIN_LED2;
|
|||
|
// }
|
|||
|
// else if (_no == 3)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED3->ODR ^= GPIO_PIN_LED3;
|
|||
|
// }
|
|||
|
// else if (_no == 4)
|
|||
|
// {
|
|||
|
// GPIO_PORT_LED4->ODR ^= GPIO_PIN_LED4;
|
|||
|
// }
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
*********************************************************************************************************
|
|||
|
* 函 数 名: bsp_IsLedOn
|
|||
|
* 功能说明: 判断LED指示灯是否已经点亮。
|
|||
|
* 形 参: _no : 指示灯序号,范围 1 - 4
|
|||
|
* 返 回 值: 1表示已经点亮,0表示未点亮
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
uint8_t bsp_IsLedOn(uint8_t _no)
|
|||
|
{
|
|||
|
if (_no == 1)
|
|||
|
{
|
|||
|
if ((GPIO_PORT_LED1->ODR & GPIO_PIN_LED1) == 0)
|
|||
|
{
|
|||
|
return 1;
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
// else if (_no == 2)
|
|||
|
// {
|
|||
|
// if ((GPIO_PORT_LED2->ODR & GPIO_PIN_LED2) == 0)
|
|||
|
// {
|
|||
|
// return 1;
|
|||
|
// }
|
|||
|
// return 0;
|
|||
|
// }
|
|||
|
// else if (_no == 3)
|
|||
|
// {
|
|||
|
// if ((GPIO_PORT_LED3->ODR & GPIO_PIN_LED3) == 0)
|
|||
|
// {
|
|||
|
// return 1;
|
|||
|
// }
|
|||
|
// return 0;
|
|||
|
// }
|
|||
|
// else if (_no == 4)
|
|||
|
// {
|
|||
|
// if ((GPIO_PORT_LED4->ODR & GPIO_PIN_LED4) == 0)
|
|||
|
// {
|
|||
|
// return 1;
|
|||
|
// }
|
|||
|
// return 0;
|
|||
|
// }
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/
|