PIC18 - switch not working as expected, can anyone help please

Discussion in 'Embedded Systems and Microcontrollers' started by Hoylegj, Mar 21, 2016.

  1. Hoylegj

    Thread Starter New Member

    Mar 10, 2016
    16
    1
    using p18f45k50

    Issue.. if (INTCONbits.IOCIE == 1 && INTCONbits.IOCIF == 1)
    if (PORTBbits.RB4)

    These just don't work and I don't understand why, pulling my hair out :-(
    And if I just use if (PORTBbits.RB4) it has to be if (!PORTBbits.RB4) and then it works on button push, don't understand
    the ! not ? when pushed

    Mod edit: code tags

    Code (C):
    1.  
    2. /******************************************************************************/
    3. /* Files to Include                                                           */
    4. /******************************************************************************/
    5. #include<p18f45k50.h>
    6.  
    7. // CONFIG1L
    8. #pragma config PLLSEL = PLL4X   // PLL Selection (4x clock multiplier)
    9. #pragma config CFGPLLEN = OFF   // PLL Enable Configuration bit (PLL Disabled (firmware controlled))
    10. #pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
    11. #pragma config LS48MHZ = SYS24X4// Low Speed USB mode with 48 MHz system clock (System clock at 24 MHz, USB clock divider is set to 4)
    12.  
    13. // CONFIG1H
    14. #pragma config FOSC = INTOSCIO  // Oscillator Selection (Internal oscillator)
    15. #pragma config PCLKEN = ON      // Primary Oscillator Shutdown (Primary oscillator enabled)
    16. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
    17. #pragma config IESO = OFF       // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)
    18.  
    19. // CONFIG2L
    20. #pragma config nPWRTEN = OFF    // Power-up Timer Enable (Power up timer disabled)
    21. #pragma config BOREN = SBORDIS  // Brown-out Reset Enable (BOR enabled in hardware (SBOREN is ignored))
    22. #pragma config BORV = 190       // Brown-out Reset Voltage (BOR set to 1.9V nominal)
    23. #pragma config nLPBOR = OFF     // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)
    24.  
    25. // CONFIG2H
    26. #pragma config WDTEN = ON       // Watchdog Timer Enable bits (WDT enabled in hardware (SWDTEN ignored))
    27. #pragma config WDTPS = 32768    // Watchdog Timer Postscaler (1:32768)
    28.  
    29. // CONFIG3H
    30. #pragma config CCP2MX = RC1     // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    31. //#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
    32. #pragma config PBADEN = OFF     // PORTB<4:0> pins are configured as digital I/O on Reset)
    33. #pragma config T3CMX = RC0      // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
    34. #pragma config SDOMX = RB3      // SDO Output MUX bit (SDO function is on RB3)
    35. #pragma config MCLRE = ON       // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)
    36.  
    37. // CONFIG4L
    38. #pragma config STVREN = ON      // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
    39. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
    40. #pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
    41. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)
    42.  
    43. // CONFIG5L
    44. #pragma config CP0 = OFF        // Block 0 Code Protect (Block 0 is not code-protected)
    45. #pragma config CP1 = OFF        // Block 1 Code Protect (Block 1 is not code-protected)
    46. #pragma config CP2 = OFF        // Block 2 Code Protect (Block 2 is not code-protected)
    47. #pragma config CP3 = OFF        // Block 3 Code Protect (Block 3 is not code-protected)
    48.  
    49. // CONFIG5H
    50. #pragma config CPB = OFF        // Boot Block Code Protect (Boot block is not code-protected)
    51. #pragma config CPD = OFF        // Data EEPROM Code Protect (Data EEPROM is not code-protected)
    52.  
    53. // CONFIG6L
    54. #pragma config WRT0 = OFF       // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
    55. #pragma config WRT1 = OFF       // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
    56. #pragma config WRT2 = OFF       // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
    57. #pragma config WRT3 = OFF       // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)
    58.  
    59. // CONFIG6H
    60. #pragma config WRTC = OFF       // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
    61. #pragma config WRTB = OFF       // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
    62. #pragma config WRTD = OFF       // Data EEPROM Write Protect (Data EEPROM is not write-protected)
    63.  
    64. // CONFIG7L
    65. #pragma config EBTR0 = OFF      // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
    66. #pragma config EBTR1 = OFF      // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
    67. #pragma config EBTR2 = OFF      // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
    68. #pragma config EBTR3 = OFF      // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)
    69.  
    70. // CONFIG7H
    71. #pragma config EBTRB = OFF      // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)
    72.  
    73. /*  ***************  TIMER *************** */
    74.         void delayzz(void)
    75.         {   int i, j;
    76.             for(i=0;i<250;i++)
    77.             {
    78.                 for(j=0;j<2;j++)  {  }         /* Well its Just a Timer */
    79.             }  
    80.         }
    81.  
    82. /* ****************** MAIN ****************** */
    83.  
    84. void main(void)
    85. {      
    86.     int RB4_ON_OFF = 0;
    87.     // ANSEL - Analog Input = 1,  Digital = 0
    88.     ANSELAbits.ANSA0=0;
    89.     ANSELAbits.ANSA1=0;
    90.  
    91.     // TRIS  0=output , 1=Input
    92.     TRISAbits.RA0=0;       // Pin 2
    93.     TRISAbits.RA1=0;       // Pin3
    94.     TRISBbits.RB0=0;       // Pin 33 (opp 8)
    95.     TRISBbits.RB1=0;       // Pin 34 (opp 7)
    96.  
    97.     // RB4 settings           Pin 37 (opp 4)
    98.     TRISBbits.RB4=1;       // Pin 37 (opp 4) as Input
    99.     INTCON2bits.RBPU = 0;  // 1= All PortB Pull Up Disabled
    100.                            // 0=1 all PortB Pull Up Enabled
    101.                            // Provided WPUB bit is set for the PIN
    102.     WPUBbits.WPUB4 = 1;    // Set the Pull Up for RB4
    103.  
    104.     while(1)
    105.         {
    106.       if (INTCONbits.IOCIE == 1 && INTCONbits.IOCIF == 1)
    107.        {
    108.         if (PORTBbits.RB4)
    109.             {
    110.             if (RB4_ON_OFF == 0)
    111.                 {
    112.                 LATAbits.LATA0 = 1;   // RB-1 to High
    113.                 delayzz();
    114.                 LATAbits.LATA1 = 1;   // RB-1 to High
    115.                 delayzz();
    116.                 LATBbits.LATB0 = 1;   // RB-1 to High
    117.                 delayzz();          
    118.                 LATBbits.LATB1 = 1;   // RB-1 to High
    119.                 delayzz();
    120.                 RB4_ON_OFF = 1;
    121.                 }
    122.             else
    123.                 {
    124.                 LATAbits.LATA0 = 0;   // RB-1 to Low
    125.                 delayzz();          
    126.                 LATAbits.LATA1 = 0;   // RB-1 to Low
    127.                 delayzz();
    128.                 LATBbits.LATB0 = 0;   // RB-0 to Low          
    129.                 delayzz();
    130.                 LATBbits.LATB1 = 0;   // RB-0 to Low
    131.                 delayzz();
    132.                 RB4_ON_OFF = 0;
    133.                 }
    134.             }
    135.         }
    136.     }
    137. }
    138.  
    139. /* THE END */
     
    Last edited by a moderator: Mar 21, 2016
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    First thing is to make this sort of test:
    if (INTCONbits.IOCIE == 1 && INTCONbits.IOCIF == 1)
    look like this:
    if ( (INTCONbits.IOCIE == 1) && (INTCONbits.IOCIF == 1) )

    && has higher precedence than == so the test will not evaluate like you want it to the way it's written.
    -----
    Do you mean you want
    !PORTBbits.RB4
    to evaluate to TRUE when RB4 is a logic low? I would use parenthesis as well i.e.
    !(PORTBbits.RB4)
    to ensure that there is no issue with how the ports.bits structure is defined OR write it like
    if (PORTBbits.RB4 == 0)

    Better yet, I would use macros to define the IO, at least the button i.e.

    #define BUTTON_PRESSED (PORTBbits.RB4==0)
    #define BUTTON_RELEASED (PORTBbits.RB4==1)

    Then use it like:

    if (BUTTON_PRESSED)
    do_something();

    Easier to read and modify if you need to.

    ------
    If you are using XC8, just include <xc.h>. You don't have to include the processor .h if you do and other necessary includes will be automatic.

    Good luck.
     
    Last edited: Mar 21, 2016
    djsfantasi likes this.
  3. Hoylegj

    Thread Starter New Member

    Mar 10, 2016
    16
    1
    Thank you John good ideas, some school boy errors there. :) This is my test project to understand how to make all the different PIC areas working before embarking on my real project.

    Thanks again will update once tried.
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    That is an excellent strategy. Let us know how you are getting along.
     
  5. Hoylegj

    Thread Starter New Member

    Mar 10, 2016
    16
    1
    // Basic Switch on RB4 now working :), Thanks JohnInTX for pointing my in the correct direction
    // Corrected
    // ANSEL bits for Analog/Digial inputs
    // "If" changed along the lines of JohnInTX recommendations
    // Added debug code using LED's to show the values
    // Still a bit scruffy but its a start
    // Next release, add the use of Timer0
    //
    Code (C):
    1.  
    2. /******************************************************************************/
    3. /* Files to Include                                                           */
    4. /******************************************************************************/
    5. #include <xc.h>
    6. // Written for PIC18F45K50
    7.  
    8. // CONFIG1L
    9. #pragma config PLLSEL = PLL4X   // PLL Selection (4x clock multiplier)
    10. #pragma config CFGPLLEN = OFF   // PLL Enable Configuration bit (PLL Disabled (firmware controlled))
    11. #pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
    12. #pragma config LS48MHZ = SYS24X4// Low Speed USB mode with 48 MHz system clock (System clock at 24 MHz, USB clock divider is set to 4)
    13.  
    14. // CONFIG1H
    15. #pragma config FOSC = INTOSCIO  // Oscillator Selection (Internal oscillator)
    16. #pragma config PCLKEN = ON      // Primary Oscillator Shutdown (Primary oscillator enabled)
    17. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
    18. #pragma config IESO = OFF       // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)
    19.  
    20. // CONFIG2L
    21. #pragma config nPWRTEN = OFF    // Power-up Timer Enable (Power up timer disabled)
    22. #pragma config BOREN = SBORDIS  // Brown-out Reset Enable (BOR enabled in hardware (SBOREN is ignored))
    23. #pragma config BORV = 190       // Brown-out Reset Voltage (BOR set to 1.9V nominal)
    24. #pragma config nLPBOR = OFF     // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)
    25.  
    26. // CONFIG2H
    27. #pragma config WDTEN = ON       // Watchdog Timer Enable bits (WDT enabled in hardware (SWDTEN ignored))
    28. #pragma config WDTPS = 32768    // Watchdog Timer Postscaler (1:32768)
    29.  
    30. // CONFIG3H
    31. #pragma config CCP2MX = RC1     // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    32. //#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
    33. #pragma config PBADEN = OFF     // PORTB<4:0> pins are configured as digital I/O on Reset)
    34. #pragma config T3CMX = RC0      // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
    35. #pragma config SDOMX = RB3      // SDO Output MUX bit (SDO function is on RB3)
    36. #pragma config MCLRE = ON       // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)
    37.  
    38. // CONFIG4L
    39. #pragma config STVREN = ON      // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
    40. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
    41. #pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
    42. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)
    43.  
    44. // CONFIG5L
    45. #pragma config CP0 = OFF        // Block 0 Code Protect (Block 0 is not code-protected)
    46. #pragma config CP1 = OFF        // Block 1 Code Protect (Block 1 is not code-protected)
    47. #pragma config CP2 = OFF        // Block 2 Code Protect (Block 2 is not code-protected)
    48. #pragma config CP3 = OFF        // Block 3 Code Protect (Block 3 is not code-protected)
    49.  
    50. // CONFIG5H
    51. #pragma config CPB = OFF        // Boot Block Code Protect (Boot block is not code-protected)
    52. #pragma config CPD = OFF        // Data EEPROM Code Protect (Data EEPROM is not code-protected)
    53.  
    54. // CONFIG6L
    55. #pragma config WRT0 = OFF       // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
    56. #pragma config WRT1 = OFF       // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
    57. #pragma config WRT2 = OFF       // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
    58. #pragma config WRT3 = OFF       // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)
    59.  
    60. // CONFIG6H
    61. #pragma config WRTC = OFF       // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
    62. #pragma config WRTB = OFF       // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
    63. #pragma config WRTD = OFF       // Data EEPROM Write Protect (Data EEPROM is not write-protected)
    64.  
    65. // CONFIG7L
    66. #pragma config EBTR0 = OFF      // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
    67. #pragma config EBTR1 = OFF      // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
    68. #pragma config EBTR2 = OFF      // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
    69. #pragma config EBTR3 = OFF      // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)
    70.  
    71. // CONFIG7H
    72. #pragma config EBTRB = OFF      // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)
    73.  
    74. /*===============*/
    75. /*=== Defines ===*/
    76. /*===============*/
    77. // #define debug
    78. #define BUTTON_PRESSED  (PORTBbits.RB4==0)
    79. #define BUTTON_RELEASED (PORTBbits.RB4==1)
    80.  
    81.  
    82. /*===============*/
    83. void delayzz(void)
    84. /*===============*/
    85. {   int i, j;
    86.     for(i=0;i<1000;i++)
    87.         {
    88.         for(j=0;j<2;j++)  {  }         /* Well its Just a Timer */
    89.         }
    90. }
    91.                      
    92.  
    93. /*=============*/
    94. void main(void)
    95. /*=============*/
    96. {  
    97.     int RB4_ON_OFF = 0;
    98.  
    99.     // ANSEL - Analog Input = 0,  Digital = 1
    100.     ANSELAbits.ANSA0=1;
    101.     ANSELAbits.ANSA1=1;
    102.  
    103.     // TRIS  0=output , 1=Input
    104.     TRISAbits.RA0=0;       // Pin 2
    105.     TRISAbits.RA1=0;       // Pin3
    106.     TRISBbits.RB0=0;       // Pin 33 (opp 8)
    107.     TRISBbits.RB1=0;       // Pin 34 (opp 7)
    108.  
    109.     // RB4 settings           Pin 37 (opp 4)
    110.     TRISBbits.RB4=1;       // Pin 37 (opp 4) as Input
    111.     INTCON2bits.RBPU = 0;  // 1= All PortB Pull Up Disabled
    112.                            // 0=1 all PortB Pull Up Enabled
    113.                            // Provided WPUB bit is set for the PIN
    114.     WPUBbits.WPUB4 = 1;    // Set the Pull Up for RB4
    115.  
    116.     // Init the LED's to Off
    117.     LATAbits.LATA0 = 0;
    118.     LATAbits.LATA1 = 0;
    119.     LATBbits.LATB0 = 0;
    120.     LATBbits.LATB1 = 0;
    121.  
    122.  
    123. #ifdef debug
    124.     LATAbits.LATA0 = 1;
    125.     delayzz();
    126.     LATAbits.LATA1 = 1;
    127.     delayzz();
    128.     LATBbits.LATB0 = 1;
    129.     delayzz();
    130.     LATBbits.LATB1 = 1;
    131.     delayzz();
    132.     LATAbits.LATA0 = 0;
    133.     delayzz();
    134.     LATAbits.LATA1 = 0;
    135.     delayzz();
    136.     LATBbits.LATB0 = 0;
    137.     delayzz();
    138.     LATBbits.LATB1 = 0;
    139.     delayzz();
    140.     delayzz();
    141.     delayzz();
    142. #endif
    143.  
    144.     while(1)
    145.         {
    146.  
    147. #ifdef debug
    148.     if (INTCONbits.IOCIE == 0) {
    149.         LATAbits.LATA0=0;}
    150.  
    151.     if (INTCONbits.IOCIE == 1) {
    152.         LATAbits.LATA0=1;}
    153.  
    154.     if (INTCONbits.IOCIF == 0) {
    155.         LATAbits.LATA1=0; }
    156.  
    157.     if (INTCONbits.IOCIF == 1) {
    158.         LATAbits.LATA1=1; }
    159.  
    160.     if (BUTTON_PRESSED) { // button pressed
    161.         LATBbits.LATB1=1; }
    162.  
    163.     if (BUTTON_RELEASED) { // button released
    164.         LATBbits.LATB1=0; }
    165.  
    166.     delayzz();
    167. #endif
    168.  
    169.     if ( (INTCONbits.IOCIE == 0) && (INTCONbits.IOCIF == 0) )
    170.     {
    171.         if (BUTTON_PRESSED) // button pressed
    172.             {
    173.             if (RB4_ON_OFF == 0)
    174.                 {
    175.                 LATAbits.LATA0 = 1;   // RB-1 to High
    176.                 delayzz();
    177.                 LATAbits.LATA1 = 1;   // RB-1 to High
    178.                 delayzz();
    179.                 LATBbits.LATB0 = 1;   // RB-1 to High
    180.                 delayzz();      
    181.                 LATBbits.LATB1 = 1;   // RB-1 to High
    182.                 delayzz();
    183.                 RB4_ON_OFF = 1;
    184.                 }
    185.             else
    186.                 {
    187.                 LATAbits.LATA0 = 0;   // RB-1 to Low
    188.                 delayzz();    
    189.                 LATAbits.LATA1 = 0;   // RB-1 to Low
    190.                 delayzz();
    191.                 LATBbits.LATB0 = 0;   // RB-0 to Low    
    192.                 delayzz();
    193.                 LATBbits.LATB1 = 0;   // RB-0 to Low
    194.                 delayzz();
    195.                 RB4_ON_OFF = 0;
    196.                 }
    197.             }
    198.         }
    199.     }
    200. }
    201.  
    202. /* End Main */
    Mod edit: add code tags
     
    Last edited by a moderator: Mar 22, 2016
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Good show!
    Sorry I missed the lack of ANSEL, that's a common problem I usually look for.

    You can post your code with code tags by clicking Insert->Code Tags from the toolbar in the edit window. If you have a small amount of code you can paste it in the window or just click Insert and move the resulting tags accordingly. If you inspect your previous post, you'll see the syntax.

    Have fun.
     
  7. dannyf

    Well-Known Member

    Sep 13, 2015
    1,775
    360
    The first routine of any of my code (running on a mcu) is to reset the mcu to a default state. For the PICs, that means turning off analog mode, and comparator mode.
     
Loading...