PIC16 Reading from pin error

Discussion in 'Embedded Systems and Microcontrollers' started by Jswale, Jul 13, 2015.

  1. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    Hi all,

    I am writing a header file to scan a keypad to determine what number has been pressed, see code below;
    Code (Text):
    1. #include <xc.h>
    2.  
    3. #define _XTAL_FREQ 31250
    4. int numberpressed;
    5.  
    6. int Scan()
    7. {
    8. numberpressed = 12;
    9. while (numberpressed==12)        //Starting point, there is no number 12, means its polling
    10.     {
    11.     PORTB=0b11101111;            //B4 is 0, scans for 1,4,7,*
    12.      
    13.     if (PORTBbits.RB0==0)
    14.         {
    15.         __delay_ms(100);                //used for de-bounce purposes
    16.         while (PORTBbits.RB0==0);        //waits for switch to be released
    17.         __delay_ms(100);                //used for de-bounce purposes
    18.         numberpressed=1;
    19.         return numberpressed;
    20.         }
    21.  
    22.     if (PORTBbits.RB1==0)
    23.         {
    24.         __delay_ms(100);
    25.         while (PORTBbits.RB1==0);
    26.         __delay_ms(100);
    27.         numberpressed=4;
    28.         return numberpressed;
    29.         }
    30.          
    31.     if (PORTBbits.RB2==0)
    32.         {
    33.         __delay_ms(100);
    34.         while (PORTBbits.RB2==0);
    35.         __delay_ms(100);
    36.         numberpressed=7;
    37.         return numberpressed;
    38.         }
    39.      
    40.     if (PORTBbits.RB3==0)
    41.         {
    42.         __delay_ms(100);
    43.         while (PORTBbits.RB3==0);
    44.         __delay_ms(100);
    45.         numberpressed=10;
    46.         return numberpressed;
    47.         }
    48.  
    49.  
    50.     PORTB=0b11011111;            //B5 is 0, scans for 2,5,8,0
    51.      
    52.     if (PORTBbits.RB0==0)
    53.         {
    54.         __delay_ms(100);
    55.         while (PORTBbits.RB0==0);
    56.         __delay_ms(100);
    57.         numberpressed=2;
    58.         return numberpressed;
    59.         }
    60.  
    61.     if (PORTBbits.RB1==0)
    62.         {
    63.         __delay_ms(100);
    64.         while (PORTBbits.RB1==0);
    65.         __delay_ms(100);
    66.         numberpressed=5;
    67.         return numberpressed;
    68.         }      
    69.  
    70.     if (PORTBbits.RB2==0)
    71.         {
    72.         __delay_ms(100);
    73.         while (PORTBbits.RB2==0);
    74.         __delay_ms(100);
    75.         numberpressed=8;
    76.         return numberpressed;
    77.         }
    78.      
    79.     if (PORTBbits.RB3==0)
    80.         {
    81.         __delay_ms(100);
    82.         while (PORTBbits.RB3==0);
    83.         __delay_ms(100);
    84.         numberpressed=0;
    85.         return numberpressed;
    86.         }
    87.  
    88.  
    89.     PORTB=0b10111111;            //B6 is 0, scans for 3,6,9,#
    90.      
    91.     if (PORTBbits.RB0==0)
    92.         {
    93.         __delay_ms(100);
    94.         while (PORTBbits.RB0==0);
    95.         __delay_ms(100);
    96.         numberpressed=3;
    97.         return numberpressed;
    98.         }
    99.  
    100.     if (PORTBbits.RB1==0)
    101.         {
    102.         __delay_ms(100);
    103.         while (PORTBbits.RB1==0);
    104.         __delay_ms(100);
    105.         numberpressed=6;
    106.         return numberpressed;
    107.         }
    108.          
    109.     if (PORTBbits.RB2==0)
    110.         {
    111.         __delay_ms(100);
    112.         while (PORTBbits.RB2==0);
    113.         __delay_ms(100);
    114.         numberpressed=9;
    115.         return numberpressed;
    116.         }
    117.      
    118.     if (PORTBbits.RB3==0)
    119.         {
    120.         __delay_ms(100);
    121.         while (PORTBbits.RB3==0);
    122.         __delay_ms(100);
    123.         numberpressed=11;
    124.         return numberpressed;
    125.         }
    126.     }
    127. }
    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
     
  2. Jswale

    Thread Starter Member

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

    Regards
    JSwale
     
  3. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    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 (Text):
    1. #include <xc.h>
    2. int num1;
    3. int numberpressed;
    4.  
    5. #define row1    PORTCbits.RC5
    6. #define row2    PORTCbits.RC6
    7. #define row3    PORTCbits.RC7
    8.  
    9. #define _XTAL_FREQ 31250                        //Clock Frequency used, needed to incoorporate delays
    10.  
    11. //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.
    12. //^^^ all done within the Config bits window
    13.  
    14. //==============================================Main Code====================================================
    15. void main (void)
    16. {
    17.         //SETUP
    18.      
    19.         TRISA=0b00000000;
    20.         LATA=0b00000000;
    21.         ANSELA=0x00;
    22.         TRISB=0b11111111;                          //SETS PORTB AS INPUTS,
    23.         LATB=0b00000000;
    24.         //WPUB=0xF0;                            //TURNS ON INTERNAL PULL UPS FOR PORTB INPUTS--EXTERNAL PREFERRED                              
    25.         ANSELB=0x00;
    26.         TRISC=0b00000000;                             //SETS PORTC TO OUTPUTS,
    27.         LATC=0b00000000;                          //TURNS OFF ALL PINS
    28.         ANSELC=0x00;
    29.                                      
    30.  
    31.  
    32.  
    33.     {    numberpressed=Scan();
    34.         int num1=numberpressed;                    //number pressed is stored in 'num1'
    35.      
    36.         if (num1=2) LATA=0xFF;                    //if 5 is pressed on keypad, LATA=0xFF
    37.         else(LATA=0x00);                        //Else LATA=0x00
    38.  
    39.  
    40.     }
    41.  
    42. }
    43. //-----------------------------------------Keypad Scan---------------------------------------------------------
    44.  
    45. int Scan()
    46. {
    47. numberpressed = 12;
    48. while (numberpressed==12)                //Starting point, there is no number 12, means its polling
    49.     {
    50.     row1=0;
    51.     row2=1;
    52.     row3=1;                                //C5 is 0, scans for 1,4,7,*
    53.      
    54.     if (PORTBbits.RB4==0)  
    55.         {
    56.         __delay_ms(10);                    //used for de-bounce purposes
    57.         while (PORTBbits.RB4==0);        //waits for switch to be released
    58.         __delay_ms(10);                    //used for de-bounce purposes
    59.         numberpressed=1;
    60.         return;                            //'return' leaves current subroutine
    61.         }
    62.  
    63.     if (PORTBbits.RB5==0)              
    64.         {
    65.         __delay_ms(10);
    66.         while (PORTBbits.RB5==0);      
    67.         __delay_ms(10);
    68.         numberpressed=4;
    69.         return;
    70.         }
    71.          
    72.     if (PORTBbits.RB6==0)
    73.         {
    74.         __delay_ms(10);
    75.         while (PORTBbits.RB6==0);
    76.         __delay_ms(10);
    77.         numberpressed=7;
    78.         return;
    79.         }
    80.      
    81.     if (PORTBbits.RB7==0)
    82.         {
    83.         __delay_ms(10);
    84.         while (PORTBbits.RB7==0);
    85.         __delay_ms(10);
    86.         numberpressed=10;
    87.         return;
    88.         }
    89.  
    90.  
    91.     row1=1;                                //C6 is 0, scans for 2,5,8,0
    92.     row2=0;
    93.     row3=1;                  
    94.      
    95.     if (PORTBbits.RB4==0)
    96.         {
    97.         __delay_ms(10);
    98.         while (PORTBbits.RB4==0);
    99.         __delay_ms(10);
    100.         numberpressed=2;
    101.         return;
    102.         }
    103.  
    104.     if (PORTBbits.RB5==0)
    105.         {
    106.         __delay_ms(10);
    107.         while (PORTBbits.RB5==0);
    108.         __delay_ms(10);
    109.         numberpressed=5;
    110.         return;
    111.         }      
    112.  
    113.     if (PORTBbits.RB6==0)
    114.         {
    115.         __delay_ms(10);
    116.         while (PORTBbits.RB6==0);
    117.         __delay_ms(10);
    118.         numberpressed=8;
    119.         return;
    120.         }
    121.      
    122.     if (PORTBbits.RB7==0)
    123.         {
    124.         __delay_ms(10);
    125.         while (PORTBbits.RB7==0);
    126.         __delay_ms(10);
    127.         numberpressed=0;
    128.         return;
    129.         }
    130.  
    131.  
    132.     row1=1;                                    //C7 is 0, scans for 3,6,9,#
    133.     row2=1;
    134.     row3=0;              
    135.      
    136.     if (PORTBbits.RB4==0)
    137.         {
    138.         __delay_ms(10);
    139.         while (PORTBbits.RB4==0);
    140.         __delay_ms(10);
    141.         numberpressed=3;
    142.         return;
    143.         }
    144.  
    145.     if (PORTBbits.RB5==0)
    146.         {
    147.         __delay_ms(10);
    148.         while (PORTBbits.RB5==0);
    149.         __delay_ms(10);
    150.         numberpressed=6;
    151.         return;
    152.         }
    153.          
    154.     if (PORTBbits.RB6==0)
    155.         {
    156.         __delay_ms(10);
    157.         while (PORTBbits.RB6==0);
    158.         __delay_ms(10);
    159.         numberpressed=9;
    160.         return;
    161.         }
    162.      
    163.     if (PORTBbits.RB7==0)
    164.         {
    165.         __delay_ms(10);
    166.         while (PORTBbits.RB7==0);
    167.         __delay_ms(10);
    168.         numberpressed=11;
    169.         return;
    170.         }
    171.     }
    172.  
    173. }
    174.  
    175.  

    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: Jul 20, 2015
  4. Jswale

    Thread Starter Member

    Jun 30, 2015
    121
    6
    All sorted, there was a floating input problem with the pull ups.

    JSwale
     
Loading...