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

📄 bsp_pca9551.c

📁 嵌入式的tcpip协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <includes.h>
/*
*********************************************************************************************************
*                                             LED_On()
*
* Description : Turn ON any or all the LEDs on the board.
*
* Argument(s) : led     The ID of the LED to control:
*
*                       0    turn ON all LEDs on the board
*                       1    turn ON LED1
*                       2    turn ON LED2
*                       3    turn ON LED3
*                       4    turn ON LED4
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  LED_On (CPU_INT08U led)
{
    CPU_INT08U  reg;
    CPU_INT08U  data;
    CPU_INT08U  buf[2];


    reg  = 0x06;
    data = 0xFF;

    I2C_ReadReg(PCA9551_ADDRESS,&data, 1, reg);

    switch (led) {
        case 0:
             data   =  0x55;
             break;

        case 1:
             data  &= ~0x03;
             data  |=  0x01;
             break;

        case 2:
             data  &= ~0x0C;
             data  |=  0x04;
             break;

        case 3:
             data  &= ~0x30;
             data  |=  0x10;
             break;

        case 4:
             data  &= ~0xC0;
             data  |=  0x40;
             break;

        default:
             return;
    }

    buf[0] = 0x06;
    buf[1] = data;

    I2C_Write(PCA9551_ADDRESS,buf, 2);
}


/*
*********************************************************************************************************
*                                             LED_Off()
*
* Description : Turn OFF any or all the LEDs on the board.
*
* Argument(s) : led     The ID of the LED to control:
*
*                       0    turn OFF all LEDs on the board
*                       1    turn OFF LED1
*                       2    turn OFF LED2
*                       3    turn OFF LED3
*                       4    turn OFF LED4
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  LED_Off (CPU_INT08U led)
{
    CPU_INT08U  reg;
    CPU_INT08U  data;
    CPU_INT08U  buf[2];


    reg  = 0x08;
    data = 0xFF;

    I2C_ReadReg(PCA9551_ADDRESS,&data, 1, reg);

    switch (led) {
        case 0:
             data   =  0x00;
             break;

        case 1:
             data  &= ~0x03;
             break;

        case 2:
             data  &= ~0x0C;
             break;

        case 3:
             data  &= ~0x30;
             break;

        case 4:
             data  &= ~0xC0;
             break;

        default:
             return;
    }

    buf[0] = 0x06;
    buf[1] = data;

    I2C_Write(PCA9551_ADDRESS,buf, 2);
}


/*
*********************************************************************************************************
*                                             LED_Toggle()
*
* Description : TOGGLE any or all the LEDs on the board.
*
* Argument(s) : led     The ID of the LED to control:
*
*                       0    TOGGLE all LEDs on the board
*                       1    TOGGLE LED1
*                       2    TOGGLE LED2
*                       3    TOGGLE LED3
*                       4    TOGGLE LED4
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  LED_Toggle (CPU_INT08U led)
{
    CPU_INT08U  reg;
    CPU_INT08U  data;
    CPU_INT08U  buf[2];


    reg  = 0x06;
    data = 0xFF;

    I2C_ReadReg(PCA9551_ADDRESS,&data, 1, reg);

    switch (led) {
        case 0:
             data  ^=  0x55;
             break;

        case 1:
             if ((data & 0x03) == 0) {
                 data  &= ~0x03;
                 data  |=  0x01;
             } else {
                 data  &= ~0x03;
             }
             break;

        case 2:
             if ((data & 0x0C) == 0) {
                 data  &= ~0x0C;
                 data  |=  0x04;
             } else {
                 data  &= ~0x0C;
             }
             break;

        case 3:
             if ((data & 0x30) == 0) {
                 data  &= ~0x30;
                 data  |=  0x10;
             } else {
                 data  &= ~0x30;
             }
             break;

        case 4:
             if ((data & 0xC0) == 0) {
                 data  &= ~0xC0;
                 data  |=  0x40;
             } else {
                 data  &= ~0xC0;
             }
             break;

        default:
             return;
    }

    buf[0] = 0x06;
    buf[1] = data;

    I2C_Write(PCA9551_ADDRESS,buf, 2);
}


/*
*********************************************************************************************************
*                                           PB_GetStatus()
*
* Description : Get the status of a push button on the board.
*
* Argument(s) : pb      The ID of the push button to probe
*
*                       1    probe the push button B1
*                       2    probe the push button B2
*                       3    probe the push button B3
*                       4    probe the push button B4
*                       5    probe the push button B5
*
* Return(s)   : DEF_FALSE   if the push button is pressed
*               DEF_TRUE    if the push button is not pressed
*********************************************************************************************************
*/

CPU_BOOLEAN  PB_GetStatus (CPU_INT08U pb)
{
    CPU_INT08U   reg;
    CPU_INT08U   data;
    CPU_BOOLEAN  status;


    status      = DEF_FALSE;

    if ((pb >= 1) && (pb <= 4)) {
        reg  = 0x00;
        data = 0xFF;
        I2C_ReadReg(PCA9551_ADDRESS,&data, 1, reg);
    }


    switch (pb) {
        case 1:
             if ((data & 0x01) == 0) {
                 status = DEF_TRUE;
             }
             break;

        case 2:
             if ((data & 0x02) == 0) {
                 status = DEF_TRUE;
             }
             break;

        case 3:
             if ((data & 0x04) == 0) {
                 status = DEF_TRUE;
             }
             break;

        case 4:
             if ((data & 0x08) == 0) {
                 status = DEF_TRUE;
             }
             break;

        case 5:
             if ((FIO2PIN & DEF_BIT_10) == 0) {
                 status = DEF_TRUE;
             }
             break;

        default:
             break;
    }

    return (status);
}


/*********************************************************************************************************
*                                           LED_Blinker_Write()
*
* Description : Function writing to a specific register of the PCA9551
*
* Argument(s) : Address of the register to be accessed and data to write to that register
*
* Return(s)   : none.
*********************************************************************************************************/
void LED_Blinker_Write(CPU_INT08U Reg_Ptr, CPU_INT08U Data_Write)
{ 
  CPU_INT08U I2C_Data_Write_Buffer[2];
    
  I2C_Data_Write_Buffer[0] = Reg_Ptr;
  I2C_Data_Write_Buffer[1] = Data_Write;
  I2C_Write(PCA9551_ADDRESS,I2C_Data_Write_Buffer, 2);  // Write to PCA9551
}


/*********************************************************************************************************
*                                           SetPeriodPattern0()
*
* Description : Set Pattern 0 Period
*
* Argument(s) : Pattern 0 Period value
*
* Return(s)   : none.
*********************************************************************************************************/
void SetPeriodPattern0(CPU_INT08U PeriodPattern0)
{
  LED_Blinker_Write(0x01, PeriodPattern0);
}


/*********************************************************************************************************
*                                           SetDutyCyclePattern0()
*
* Description : Set Pattern 0 Duty Cycle
*
* Argument(s) : Pattern 0 Duty Cycle value
*
* Return(s)   : none.
*********************************************************************************************************/
void SetDutyCyclePattern0(CPU_INT08U DutyCyclePattern0)
{
  LED_Blinker_Write(0x02, DutyCyclePattern0);
}


/*********************************************************************************************************
*                                           SetPeriodPattern1()
*
* Description : Set Pattern 1 Period
*
* Argument(s) : Pattern 1 Period value
*
* Return(s)   : none.
*********************************************************************************************************/
void SetPeriodPattern1(CPU_INT08U PeriodPattern1)

⌨️ 快捷键说明

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