djc
Advanced Member level 1
- Joined
- Jan 27, 2013
- Messages
- 402
- Helped
- 3
- Reputation
- 6
- Reaction score
- 2
- Trophy points
- 1,298
- Location
- India
- Activity points
- 4,554
Hello,
I wrote a program for interfacing 4*4 matrix keypad with PIC18F4520. However when I make particular ROW as zero, it's not getting grounded. I tried it with all the ROW pins .What must be the issue. I am posting code here. Plz guide.
I am trying for one ROW only, code for rest of the rows are commented.
I wrote a program for interfacing 4*4 matrix keypad with PIC18F4520. However when I make particular ROW as zero, it's not getting grounded. I tried it with all the ROW pins .What must be the issue. I am posting code here. Plz guide.
Code:
#define ROW0 PORTB.B1
#define ROW1 PORTB.B3
#define ROW2 PORTB.B5
#define ROW3 PORTB.B7
#define COL0 RB0_bit //For defining PORT as input
#define COL1 RB2_bit //For defining PORT as input
#define COL2 RB4_bit //For defining PORT as input
#define COL3 RB6_bit //For defining PORT as input
#define Select_DS PORTD.B7 //May only DS,STCP and SHCP pins are used
#define Select_Clk PORTD.B6
#define Data_DS PORTD.B5
#define Data_Clk PORTD.B4
unsigned int arr_large[16] = {17,125,35,37,77,133,129,61,1,5,17,125,35,37,77,133};
unsigned int decimalNumber,remainder,quotient,decimalNumber_1,remainder_1,quotient_1;
unsigned int binaryNumber[100],binaryNumber_1[100],i=1,j,k,l=0,m=0,col,row;
void Key_Detect()
{
Delay_ms(20);
quotient = arr_large[l];
i=1;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
for(i=i;i<=8;i++)
{
binaryNumber[i] = 0;
}
for(j = i-1;j> 0;j--){
Data_DS = binaryNumber[j];
Data_Clk = 1;
Delay_us(10);
Data_Clk = 0;
Delay_us(10);
}
Data_Clk = 1;
Delay_us(10);
Data_Clk = 0;
//Delay_ms(1000);
}
void main ()
{
TRISD = 0x00;
TRISB = 0x55; //Making ROW as output and Column as input input as high and output as low
PORTB = 0xff; //Rows and columns as zero
// PORTB = 0b11111101;
Select_DS = 1; //1
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //2
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //3
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //4
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //5
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //6
Select_Clk = 1;
Select_Clk = 0;
/*Select_DS = 0; //7
Select_Clk = 1;
Select_Clk = 0;
Select_DS = 0; //8
Select_Clk = 1;
Select_Clk = 0; */
Select_Clk = 1;
Select_Clk = 0;
//while(1){
l=0;
/*do{
PORTB &= 0xaa; //Ground all the rows at once
//col = PORTB;
//col &= 0xaa; //mask unused bits
Delay_ms(50);
}while(PORTB != 0xaa); //wait till all keys released
do
{
do
{
PORTB &= 0xaa; //Ground all the rows at once
//col = PORTB;
//col &= 0xaa; //mask unused bits
Delay_ms(50);
}while(PORTB == 0xaa); //wait till all keys released
do
{
Delay_ms(50);
//col = PORTB;
PORTB &= 0xaa; //mask unused bits
}while(PORTB == 0xaa);
Delay_ms(50);
PORTB &= 0xaa;
}while(PORTB == 0xaa);*/
PORTB.B7 = 0;
PORTB.B3 = 0;
PORTB.B5 = 0;
while(1){
//ROW0 = 0;
ROW0 = 0;
PORTB.B1=0;
Delay_ms(30);
//Key_Detect();
if(COL0==0)
{
while(COL0==0)
{
Delay_ms(50);
}
while(COL0==0)
{
Delay_ms(50);
}
while(COL0==0)
{
Delay_ms(50);
}
l=5;
Key_Detect();
//break;
}
/*while(COL1!=0){
do{
Delay_ms(50);
}while(COL1!=1);
l=1;
Key_Detect();
//break;
}
while(COL2!=0){
do{
Delay_ms(50);
}while(COL2!=1);
l=2;
Key_Detect();
// break;
}
while(COL3!=0){
do{
Delay_ms(50);
}while(COL3!=1);
l=3;
Key_Detect();
//break;
}
ROW1 = 0;
if(COL0==0){
l=4;
Key_Detect();
//break;
}
else if(COL1==0){
l=5;
Key_Detect();
//break;
}
else if(COL2==0){
l=6;
Key_Detect();
//break;
}
else if(COL3==0){
l=7;
Key_Detect();
//break;
}
ROW2 = 0;
if(COL0==0){
l=8;
Key_Detect();
//break;
}
else if(COL1==0){
l=9;
Key_Detect();
//break;
}
else if(COL2==0){
l=0;
Key_Detect();
//break;
}
else if(COL3==0){
l=1;
Key_Detect();
//break;
}
ROW3 = 0;
if(COL0==0){
l=2;
Key_Detect();
//break;
}
else if(COL1==0){
l=3;
Key_Detect();
//break;
}
else if(COL2==0){
l=4;
Key_Detect();
//break;
}
else if(COL3==0){
l=5;
Key_Detect();
//break;
}*/
}
}
I am trying for one ROW only, code for rest of the rows are commented.