MCP23S17 Interfacing With 4x4 Keypad

Thread Starter

ashkarmalik

Joined Jul 1, 2012
92
Code:
int keypad_scanner(void)
{
    KEYPAD_IO=MCP23S17_read(OGPIOA);
            MCP23S17_write(0X40,OLATA,(1<<7)|(1<<6)|(1<<5)|(0<<4));

             if (KEYPAD_IO &(0<<0))
                 var=1;
            if (KEYPAD_IO &(0<<1))
                var=2;
            if (KEYPAD_IO &(0<<2))
                var=3;
            if (KEYPAD_IO &(0<<3))
                var=4;
            KEYPAD_IO=MCP23S17_read(OGPIOA);
            MCP23S17_write(0X40,OLATA,(1<<7)|(1<<6)|(0<<5)|(1<<4));

            if (KEYPAD_IO &(1<<0))
                 var=5;
            if (KEYPAD_IO &(0<<1))
                var=6;
            if (KEYPAD_IO &(0<<2))
                var=7;
            if (KEYPAD_IO &(0<<3))
                var=8;
            KEYPAD_IO=MCP23S17_read(OGPIOA);
           MCP23S17_write(0X40,OLATA,(1<<7)|(0<<6)|(1<<5)|(1<<4));

             if (KEYPAD_IO &(0<<0))
                 var=9;
            if (KEYPAD_IO &(0<<1))
                var=0;
            if (KEYPAD_IO &(0<<2))
                var=76;
            if (KEYPAD_IO &(0<<3))
                var=99;
            KEYPAD_IO=MCP23S17_read(OGPIOA);
           MCP23S17_write(0X40,OLATA,(0<<7)|(1<<6)|(1<<5)|(1<<4));

             if (KEYPAD_IO &(0<<0))
                 var=88;
            if (KEYPAD_IO &(0<<1))
                var=99;
            if (KEYPAD_IO &(0<<2))
                var=55;
            if (KEYPAD_IO &(0<<3))
                var=44;
           return var;
}
Code:
void MCP23S17_write(uint8_t device_op,uint8_t address,uint8_t data)
{

SPI2_TX[0]=device_op;
SPI2_TX[1]=address;
SPI2_TX[2]=data;

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_RESET);//CS ENABLE
    HAL_Delay(1);
    HAL_SPI_Transmit(&hspi2, SPI2_TX, 3, 1);///////////
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_SET);//CS DISABLE

}

uint8_t MCP23S17_read(uint8_t address)
{
     uint8_t received_data[2]={0};
     SPI2_RX[0]=0X41;
     SPI2_RX[1]=address;

     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,0);                //CS ENABLE
    // HAL_Delay(1);
     HAL_SPI_Transmit(&hspi2,(uint8_t *)&SPI2_RX, 2, 100);
     HAL_SPI_Receive(&hspi2,(uint8_t *)received_data, 1, 100);
     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,1);                //CS DISABLE

      return received_data[0];
}
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_RESET);
HAL_Delay(5);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,GPIO_PIN_SET);//RESET ENABLE
HAL_Delay(10);
MCP23S17_write(0X40,IOCON, 0x80);
MCP23S17_write(0X40,IODIRA,0x0F);
MCP23S17_write(0X40,IODIRB,0x00);//no
MCP23S17_write(0X40,GPPUA, 0x0F);
MCP23S17_write(0X40,GPPUB, 0x00);///no
So the problem is that I am only able to Scan Single Colum and remaing 3 colum are unchanged.
 
Top