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

Thread Starter

Hoylegj

Joined Mar 10, 2016
26
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

C:
/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/
#include<p18f45k50.h> 

// CONFIG1L
#pragma config PLLSEL = PLL4X   // PLL Selection (4x clock multiplier)
#pragma config CFGPLLEN = OFF   // PLL Enable Configuration bit (PLL Disabled (firmware controlled))
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
#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)

// CONFIG1H
#pragma config FOSC = INTOSCIO  // Oscillator Selection (Internal oscillator)
#pragma config PCLKEN = ON      // Primary Oscillator Shutdown (Primary oscillator enabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config nPWRTEN = OFF    // Power-up Timer Enable (Power up timer disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable (BOR enabled in hardware (SBOREN is ignored))
#pragma config BORV = 190       // Brown-out Reset Voltage (BOR set to 1.9V nominal)
#pragma config nLPBOR = OFF     // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)

// CONFIG2H
#pragma config WDTEN = ON       // Watchdog Timer Enable bits (WDT enabled in hardware (SWDTEN ignored))
#pragma config WDTPS = 32768    // Watchdog Timer Postscaler (1:32768)

// CONFIG3H
#pragma config CCP2MX = RC1     // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
//#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
#pragma config PBADEN = OFF     // PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config T3CMX = RC0      // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
#pragma config SDOMX = RB3      // SDO Output MUX bit (SDO function is on RB3)
#pragma config MCLRE = ON       // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)

// CONFIG5L
#pragma config CP0 = OFF        // Block 0 Code Protect (Block 0 is not code-protected)
#pragma config CP1 = OFF        // Block 1 Code Protect (Block 1 is not code-protected)
#pragma config CP2 = OFF        // Block 2 Code Protect (Block 2 is not code-protected)
#pragma config CP3 = OFF        // Block 3 Code Protect (Block 3 is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protect (Boot block is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protect (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protect (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)

/*  ***************  TIMER *************** */
        void delayzz(void)
        {   int i, j;
            for(i=0;i<250;i++)
            {
                for(j=0;j<2;j++)  {  }         /* Well its Just a Timer */
            }  
        }

/* ****************** MAIN ****************** */

void main(void)
{       
    int RB4_ON_OFF = 0; 
    // ANSEL - Analog Input = 1,  Digital = 0
    ANSELAbits.ANSA0=0; 
    ANSELAbits.ANSA1=0;

    // TRIS  0=output , 1=Input
    TRISAbits.RA0=0;       // Pin 2
    TRISAbits.RA1=0;       // Pin3
    TRISBbits.RB0=0;       // Pin 33 (opp 8)
    TRISBbits.RB1=0;       // Pin 34 (opp 7)

    // RB4 settings           Pin 37 (opp 4)
    TRISBbits.RB4=1;       // Pin 37 (opp 4) as Input
    INTCON2bits.RBPU = 0;  // 1= All PortB Pull Up Disabled
                           // 0=1 all PortB Pull Up Enabled
                           // Provided WPUB bit is set for the PIN
    WPUBbits.WPUB4 = 1;    // Set the Pull Up for RB4

    while(1) 
        {
      if (INTCONbits.IOCIE == 1 && INTCONbits.IOCIF == 1)
       {
        if (PORTBbits.RB4)
            {
            if (RB4_ON_OFF == 0)
                {
                LATAbits.LATA0 = 1;   // RB-1 to High
                delayzz();
                LATAbits.LATA1 = 1;   // RB-1 to High
                delayzz();
                LATBbits.LATB0 = 1;   // RB-1 to High
                delayzz();           
                LATBbits.LATB1 = 1;   // RB-1 to High
                delayzz();
                RB4_ON_OFF = 1;
                }
            else
                {
                LATAbits.LATA0 = 0;   // RB-1 to Low
                delayzz();          
                LATAbits.LATA1 = 0;   // RB-1 to Low
                delayzz();
                LATBbits.LATB0 = 0;   // RB-0 to Low          
                delayzz();
                LATBbits.LATB1 = 0;   // RB-0 to Low
                delayzz(); 
                RB4_ON_OFF = 0;
                }
            }
        }
    }
}

/* THE END */
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
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:

Thread Starter

Hoylegj

Joined Mar 10, 2016
26
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.
 

Thread Starter

Hoylegj

Joined Mar 10, 2016
26
// 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
//
C:
/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/
#include <xc.h>
// Written for PIC18F45K50

// CONFIG1L
#pragma config PLLSEL = PLL4X   // PLL Selection (4x clock multiplier)
#pragma config CFGPLLEN = OFF   // PLL Enable Configuration bit (PLL Disabled (firmware controlled))
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
#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)

// CONFIG1H
#pragma config FOSC = INTOSCIO  // Oscillator Selection (Internal oscillator)
#pragma config PCLKEN = ON      // Primary Oscillator Shutdown (Primary oscillator enabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config nPWRTEN = OFF    // Power-up Timer Enable (Power up timer disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable (BOR enabled in hardware (SBOREN is ignored))
#pragma config BORV = 190       // Brown-out Reset Voltage (BOR set to 1.9V nominal)
#pragma config nLPBOR = OFF     // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)

// CONFIG2H
#pragma config WDTEN = ON       // Watchdog Timer Enable bits (WDT enabled in hardware (SWDTEN ignored))
#pragma config WDTPS = 32768    // Watchdog Timer Postscaler (1:32768)

// CONFIG3H
#pragma config CCP2MX = RC1     // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
//#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
#pragma config PBADEN = OFF     // PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config T3CMX = RC0      // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
#pragma config SDOMX = RB3      // SDO Output MUX bit (SDO function is on RB3)
#pragma config MCLRE = ON       // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)

// CONFIG5L
#pragma config CP0 = OFF        // Block 0 Code Protect (Block 0 is not code-protected)
#pragma config CP1 = OFF        // Block 1 Code Protect (Block 1 is not code-protected)
#pragma config CP2 = OFF        // Block 2 Code Protect (Block 2 is not code-protected)
#pragma config CP3 = OFF        // Block 3 Code Protect (Block 3 is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protect (Boot block is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protect (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protect (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)

/*===============*/
/*=== Defines ===*/
/*===============*/
// #define debug
#define BUTTON_PRESSED  (PORTBbits.RB4==0)
#define BUTTON_RELEASED (PORTBbits.RB4==1)


/*===============*/
void delayzz(void)
/*===============*/
{   int i, j;
    for(i=0;i<1000;i++)
        {
        for(j=0;j<2;j++)  {  }         /* Well its Just a Timer */
        }
}
                      

/*=============*/
void main(void)
/*=============*/
{  
    int RB4_ON_OFF = 0;

    // ANSEL - Analog Input = 0,  Digital = 1
    ANSELAbits.ANSA0=1;
    ANSELAbits.ANSA1=1;

    // TRIS  0=output , 1=Input
    TRISAbits.RA0=0;       // Pin 2
    TRISAbits.RA1=0;       // Pin3
    TRISBbits.RB0=0;       // Pin 33 (opp 8)
    TRISBbits.RB1=0;       // Pin 34 (opp 7)

    // RB4 settings           Pin 37 (opp 4)
    TRISBbits.RB4=1;       // Pin 37 (opp 4) as Input
    INTCON2bits.RBPU = 0;  // 1= All PortB Pull Up Disabled
                           // 0=1 all PortB Pull Up Enabled
                           // Provided WPUB bit is set for the PIN
    WPUBbits.WPUB4 = 1;    // Set the Pull Up for RB4

    // Init the LED's to Off
    LATAbits.LATA0 = 0;
    LATAbits.LATA1 = 0;
    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;


#ifdef debug
    LATAbits.LATA0 = 1;
    delayzz();
    LATAbits.LATA1 = 1;
    delayzz();
    LATBbits.LATB0 = 1;
    delayzz();
    LATBbits.LATB1 = 1;
    delayzz();
    LATAbits.LATA0 = 0;
    delayzz();
    LATAbits.LATA1 = 0;
    delayzz();
    LATBbits.LATB0 = 0;
    delayzz();
    LATBbits.LATB1 = 0;
    delayzz();
    delayzz();
    delayzz();
#endif

    while(1)
        {
  
#ifdef debug
    if (INTCONbits.IOCIE == 0) {
        LATAbits.LATA0=0;}

    if (INTCONbits.IOCIE == 1) {
        LATAbits.LATA0=1;}

    if (INTCONbits.IOCIF == 0) {
        LATAbits.LATA1=0; }

    if (INTCONbits.IOCIF == 1) {
        LATAbits.LATA1=1; }

    if (BUTTON_PRESSED) { // button pressed
        LATBbits.LATB1=1; }

    if (BUTTON_RELEASED) { // button released
        LATBbits.LATB1=0; }

    delayzz();
#endif

    if ( (INTCONbits.IOCIE == 0) && (INTCONbits.IOCIF == 0) )
    {
        if (BUTTON_PRESSED) // button pressed
            {
            if (RB4_ON_OFF == 0)
                {
                LATAbits.LATA0 = 1;   // RB-1 to High
                delayzz();
                LATAbits.LATA1 = 1;   // RB-1 to High
                delayzz();
                LATBbits.LATB0 = 1;   // RB-1 to High
                delayzz();      
                LATBbits.LATB1 = 1;   // RB-1 to High
                delayzz();
                RB4_ON_OFF = 1;
                }
            else
                {
                LATAbits.LATA0 = 0;   // RB-1 to Low
                delayzz();     
                LATAbits.LATA1 = 0;   // RB-1 to Low
                delayzz();
                LATBbits.LATB0 = 0;   // RB-0 to Low     
                delayzz();
                LATBbits.LATB1 = 0;   // RB-0 to Low
                delayzz();
                RB4_ON_OFF = 0;
                }
            }
        }
    }
}

/* End Main */
Mod edit: add code tags
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
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.
 
Top