⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led.c

📁 本文件为新一代基于ARM7构造的微处理器资料
💻 C
字号:
//  包含必要的头文件
#include <includes.h>

//  将较长的标识符定义成较短的形式
#define  SysCtlPeriEnable       SysCtlPeripheralEnable
#define  SysCtlPeriDisable      SysCtlPeripheralDisable
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput


/***************************************************************************************************
功能:初始化指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Init(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOD);                  //  使能GPIOD端口
        GPIOPinTypeOut(GPIO_PORTD_BASE , GPIO_PIN_0);           //  设置PD0为输入类型
        LED_Off(LED1);                                          //  熄灭LED1
    }

    if ( ucLED & LED2 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG);                  //  使能GPIOG端口
        GPIOPinTypeOut(GPIO_PORTG_BASE , GPIO_PIN_2);           //  设置PG2输入类型
        LED_Off(LED2);                                          //  熄灭LED2
    }

    if ( ucLED & LED3 )
    {
        SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG);                  //  使能GPIOG端口
        GPIOPinTypeOut(GPIO_PORTG_BASE , GPIO_PIN_3);           //  设置PG3输入类型
        LED_Off(LED3);                                          //  熄灭LED3
    }
}


/***************************************************************************************************
功能:点亮指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_On(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , 0x00);      //  PD0输出低电平
    }

    if ( ucLED & LED2 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , 0x00);      //  PG2输出低电平
    }

    if ( ucLED & LED3 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , 0x00);      //  PG3输出低电平
    }
}


/***************************************************************************************************
功能:熄灭指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Off(unsigned char  ucLED)
{
    if ( ucLED & LED1 )
    {
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , 0x01);          //  PD0输出高电平
    }

    if ( ucLED & LED2 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , 0x01 << 2);     //  PG2输出高电平
    }

    if ( ucLED & LED3 )
    {
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , 0x01 << 3);     //  PG3输出高电平
    }
}


/***************************************************************************************************
功能:反转指定的LED
参数:ucLED是LED名称,取值下列值之一或者它们之间的“或运算”组合形式
            LED1
            LED2
            LED3
返回:无
***************************************************************************************************/
void  LED_Toggle(unsigned char  ucLED)
{
    unsigned char  ucVal;

    if ( ucLED & LED1 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTD_BASE , GPIO_PIN_0);   //  读取PD0的输出状态
        ucVal  ^=  0x01;                                        //  状态反转
        GPIOPinWrite(GPIO_PORTD_BASE , GPIO_PIN_0 , ucVal);     //  更新PD0的输出状态
    }

    if ( ucLED & LED2 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTG_BASE , GPIO_PIN_2);   //  读取PG2的输出状态
        ucVal  ^=  0x01 << 2;                                   //  状态反转
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_2 , ucVal);     //  更新PG2的输出状态
    }

    if ( ucLED & LED3 )
    {
        ucVal   =  GPIOPinRead(GPIO_PORTG_BASE , GPIO_PIN_3);   //  读取PG3的输出状态
        ucVal  ^=  0x01 << 3;                                   //  状态反转
        GPIOPinWrite(GPIO_PORTG_BASE , GPIO_PIN_3 , ucVal);     //  更新PG3的输出状态
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -