Пример #1
0
//------------------------------------------------------------------
void keypad_4x4_scan()
{
        int value=0;
        BBBIO_GPIO_low(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_13);     //line 1
        value = BBBIO_GPIO_get(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_12 |BBBIO_GPIO_PIN_10 |BBBIO_GPIO_PIN_8 |BBBIO_GPIO_PIN_6 );
        BBBIO_GPIO_high(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_13);
        key_status[0][0] = value >> 6 & 0x01;
        key_status[0][1] = value >> 8 & 0x01;
        key_status[0][2] = value >> 10 & 0x01;
        key_status[0][3] = value >> 12 & 0x01;

        BBBIO_GPIO_low(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_11);     //line 2
        value = BBBIO_GPIO_get(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_12 |BBBIO_GPIO_PIN_10 |BBBIO_GPIO_PIN_8 |BBBIO_GPIO_PIN_6 );
        BBBIO_GPIO_high(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_11);
        key_status[1][0] = value >> 6 & 0x01;
        key_status[1][1] = value >> 8 & 0x01;
        key_status[1][2] = value >> 10 & 0x01;
        key_status[1][3] = value >> 12 & 0x01;

        BBBIO_GPIO_low(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_9);     //line 3
        value = BBBIO_GPIO_get(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_12 |BBBIO_GPIO_PIN_10 |BBBIO_GPIO_PIN_8 |BBBIO_GPIO_PIN_6 );
        BBBIO_GPIO_high(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_9);
        key_status[2][0] = value >> 6 & 0x01;
        key_status[2][1] = value >> 8 & 0x01;
        key_status[2][2] = value >> 10 & 0x01;
        key_status[2][3] = value >> 12 & 0x01;

        BBBIO_GPIO_low(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_7);     //line 4
        value = BBBIO_GPIO_get(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_12 |BBBIO_GPIO_PIN_10 |BBBIO_GPIO_PIN_8 |BBBIO_GPIO_PIN_6 );
        BBBIO_GPIO_high(BBBIO_GPIO2 ,BBBIO_GPIO_PIN_7);
        key_status[3][0] = value >> 6 & 0x01;
        key_status[3][1] = value >> 8 & 0x01;
        key_status[3][2] = value >> 10 & 0x01;
        key_status[3][3] = value >> 12 & 0x01;
}
void SPI_read()
{
    unsigned long pin_num=0x00;
    unsigned long sVal[NUM_ADC];
    unsigned long commandout=0x00;
    
    /* SPI */
    for(pin_num=0;pin_num<NUM_ADC_PORT;pin_num++){
        for(int j=0;j<NUM_ADC;j++){
            sVal[j]=0x00;
        }
        
        // set CS
        BBBIO_GPIO_high(BBBIO_GPIO1 ,port_cs_SPI[0]);
        BBBIO_GPIO_low(BBBIO_GPIO1 ,port_clk_SPI[0] | port_din_SPI[0]);
        BBBIO_GPIO_low(BBBIO_GPIO1 ,port_cs_SPI[0]);
        
        commandout=pin_num;
        commandout|=0x18;
        commandout<<=3;
        
        //wait_SPI();
        
        for(int i=0;i<5;i++){
            if(commandout&0x80){
                BBBIO_GPIO_high(BBBIO_GPIO1 ,port_din_SPI[0]);
            }
            else{
                BBBIO_GPIO_low(BBBIO_GPIO1 ,port_din_SPI[0]);
            }
            commandout<<=1;
            BBBIO_GPIO_high(BBBIO_GPIO1 ,port_clk_SPI[0]);
            BBBIO_GPIO_low(BBBIO_GPIO1 ,port_clk_SPI[0]);
        }
        
        //sleep(1);
        
        for(int i=0;i<2;i++){
            BBBIO_GPIO_high(BBBIO_GPIO1 ,port_clk_SPI[0]);
            BBBIO_GPIO_low(BBBIO_GPIO1 ,port_clk_SPI[0]);
        }
        for(int i=0;i<12;i++){
            BBBIO_GPIO_high(BBBIO_GPIO1 ,port_clk_SPI[0]);
            for(int j=0;j<NUM_ADC;j++){
                sVal[j]<<=1;
            }
            for(int j=0;j<NUM_ADC;j++){
                if(BBBIO_GPIO_get(BBBIO_GPIO1 ,port_dout_SPI[j])){
                    sVal[j]|=0x01;
                }
            }
            BBBIO_GPIO_low(BBBIO_GPIO1 ,port_clk_SPI[0]);
        }
        for(int j=0;j<NUM_ADC;j++){
            SPI_sensor_value[j][pin_num]=sVal[j];
        }
    }
}