PIC16 Reading from pin error

Thread Starter

Jswale

Joined Jun 30, 2015
121
Hi all,

I am writing a header file to scan a keypad to determine what number has been pressed, see code below;
Code:
#include <xc.h>

#define _XTAL_FREQ 31250
int numberpressed;

int Scan()
{
numberpressed = 12;
while (numberpressed==12)        //Starting point, there is no number 12, means its polling
    {
    PORTB=0b11101111;            //B4 is 0, scans for 1,4,7,*
      
    if (PORTBbits.RB0==0)
        {
        __delay_ms(100);                //used for de-bounce purposes
        while (PORTBbits.RB0==0);        //waits for switch to be released
        __delay_ms(100);                //used for de-bounce purposes
        numberpressed=1;
        return numberpressed;
        }
  
    if (PORTBbits.RB1==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB1==0);
        __delay_ms(100);
        numberpressed=4;
        return numberpressed;
        }
          
    if (PORTBbits.RB2==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB2==0);
        __delay_ms(100);
        numberpressed=7;
        return numberpressed;
        }
      
    if (PORTBbits.RB3==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB3==0);
        __delay_ms(100);
        numberpressed=10;
        return numberpressed;
        }


    PORTB=0b11011111;            //B5 is 0, scans for 2,5,8,0
      
    if (PORTBbits.RB0==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB0==0);
        __delay_ms(100);
        numberpressed=2;
        return numberpressed;
        }
  
    if (PORTBbits.RB1==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB1==0);
        __delay_ms(100);
        numberpressed=5;
        return numberpressed;
        }      
  
    if (PORTBbits.RB2==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB2==0);
        __delay_ms(100);
        numberpressed=8;
        return numberpressed;
        }
      
    if (PORTBbits.RB3==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB3==0);
        __delay_ms(100);
        numberpressed=0;
        return numberpressed;
        }


    PORTB=0b10111111;            //B6 is 0, scans for 3,6,9,#
      
    if (PORTBbits.RB0==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB0==0);
        __delay_ms(100);
        numberpressed=3;
        return numberpressed;
        }
  
    if (PORTBbits.RB1==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB1==0);
        __delay_ms(100);
        numberpressed=6;
        return numberpressed;
        }
          
    if (PORTBbits.RB2==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB2==0);
        __delay_ms(100);
        numberpressed=9;
        return numberpressed;
        }
      
    if (PORTBbits.RB3==0)
        {
        __delay_ms(100);
        while (PORTBbits.RB3==0);
        __delay_ms(100);
        numberpressed=11;
        return numberpressed;
        }
    }
}
I am getting some errors to do with the (PORTbits.RB0==0).

Error [255] C:\Program Files (x86)\Microchip\xc8\v1.33\include\KeypadScan.h; 14.18 not a member of the struct/union ""
Error [207] C:\Program Files (x86)\Microchip\xc8\v1.33\include\KeypadScan.h; 14.21 simple type required for "=="

I am using MPLAB v8.92 with a XC8 complier (FYI)

Any ideas?

Cheers
JSwale
 

Thread Starter

Jswale

Joined Jun 30, 2015
121
All sorted. Changed the PORTB to PORTC because there are no unimplemented pins in that register.

Regards
JSwale
 

Thread Starter

Jswale

Joined Jun 30, 2015
121
Help Needed;

I have the Keypad wired up correctly using external pull ups, but sometimes the keypad works (active low) and sometimes the key pressed doesn't do anything. I think maybe it has something to do with my delays but I am not sure.

Any help would be appreciated,

Here is my code.

Code:
#include <xc.h>
int num1;
int numberpressed;

#define row1    PORTCbits.RC5
#define row2    PORTCbits.RC6
#define row3    PORTCbits.RC7

#define _XTAL_FREQ 31250                        //Clock Frequency used, needed to incoorporate delays

//tasks performed before the code is run, Watch dog timer OFF, oscillator is configured to run internally, the power up timer is ON (allows voltages to settle), Low Voltage Programming is turned OFF because it interferes with I/Os.
//^^^ all done within the Config bits window

//==============================================Main Code====================================================
void main (void)
{
        //SETUP
      
        TRISA=0b00000000;
        LATA=0b00000000;
        ANSELA=0x00;
        TRISB=0b11111111;                          //SETS PORTB AS INPUTS,
        LATB=0b00000000;
        //WPUB=0xF0;                            //TURNS ON INTERNAL PULL UPS FOR PORTB INPUTS--EXTERNAL PREFERRED                               
        ANSELB=0x00;
        TRISC=0b00000000;                             //SETS PORTC TO OUTPUTS,
        LATC=0b00000000;                          //TURNS OFF ALL PINS
        ANSELC=0x00;
                                      

  
  
    {    numberpressed=Scan();
        int num1=numberpressed;                    //number pressed is stored in 'num1'
      
        if (num1=2) LATA=0xFF;                    //if 5 is pressed on keypad, LATA=0xFF
        else(LATA=0x00);                        //Else LATA=0x00


    }

}
//-----------------------------------------Keypad Scan---------------------------------------------------------

int Scan()
{
numberpressed = 12;
while (numberpressed==12)                //Starting point, there is no number 12, means its polling
    {
    row1=0;
    row2=1;
    row3=1;                                //C5 is 0, scans for 1,4,7,*
      
    if (PORTBbits.RB4==0)  
        {
        __delay_ms(10);                    //used for de-bounce purposes
        while (PORTBbits.RB4==0);        //waits for switch to be released
        __delay_ms(10);                    //used for de-bounce purposes
        numberpressed=1;
        return;                            //'return' leaves current subroutine
        }
  
    if (PORTBbits.RB5==0)              
        {
        __delay_ms(10);
        while (PORTBbits.RB5==0);      
        __delay_ms(10);
        numberpressed=4;
        return;
        }
          
    if (PORTBbits.RB6==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB6==0);
        __delay_ms(10);
        numberpressed=7;
        return;
        }
      
    if (PORTBbits.RB7==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB7==0);
        __delay_ms(10);
        numberpressed=10;
        return;
        }
  

    row1=1;                                //C6 is 0, scans for 2,5,8,0
    row2=0;
    row3=1;                  
      
    if (PORTBbits.RB4==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB4==0);
        __delay_ms(10);
        numberpressed=2;
        return;
        }
  
    if (PORTBbits.RB5==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB5==0);
        __delay_ms(10);
        numberpressed=5;
        return;
        }      
  
    if (PORTBbits.RB6==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB6==0);
        __delay_ms(10);
        numberpressed=8;
        return;
        }
      
    if (PORTBbits.RB7==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB7==0);
        __delay_ms(10);
        numberpressed=0;
        return;
        }

  
    row1=1;                                    //C7 is 0, scans for 3,6,9,#
    row2=1;
    row3=0;              
      
    if (PORTBbits.RB4==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB4==0);
        __delay_ms(10);
        numberpressed=3;
        return;
        }
  
    if (PORTBbits.RB5==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB5==0);
        __delay_ms(10);
        numberpressed=6;
        return;
        }
          
    if (PORTBbits.RB6==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB6==0);
        __delay_ms(10);
        numberpressed=9;
        return;
        }
      
    if (PORTBbits.RB7==0)
        {
        __delay_ms(10);
        while (PORTBbits.RB7==0);
        __delay_ms(10);
        numberpressed=11;
        return;
        }
    }

}

I am using MPLAB v8.92 with a XC8 complier (FYI)

Keypad used is
MULTICOMP MCAK304NBWB


EDIT: Everything works perfectly if I place my thumb over the connections on the keypad (the solder joints). I have had a look and all joints look to be flowed correctly so I am stumped. Any suggestions?
 
Last edited:
Top