0
\$\begingroup\$

I'm new to PIC programming. I made a program to get interrupt from RB pins. but it doesn't work. My code is as follows:

int flag=0;
unsigned short temp = 0;

void interrupt(void)
{   
    if(flag==2)
    flag=0 ;
    else
    flag++;   

    if (INTCON.RBIF==1)
    {
        flag=9;
        temp = PORTB;
        PORTB = temp;
        INTCON.RBIF = 0;
    }

    INTCON.INTF = 0;
}

int i,j,k,mina,minb,seca,secb,hra,hrb;
void main()
{
    IOCB=1111110;
    TRISA=0  ;
    TRISC=0  ;
    PORTB=0;
    ADCON0=0;
    TRISB=1;
    ANSELB=0;
    INTCON=1;
    INTCON.RBIE=1;
    INTCON.RBIE = 1;

    INTCON.RBIF = 0;

    INTCON.GIE = 1;
    INTCON.INTE = 1;
    INTCON.PEIE = 0;
    OPTION_REG.INTEDG = 1;
    while(1)
    {
        for(i=0;i<59;i++)
        for(j=0;j<59;j++)
        for(k=0;k<59;k++)
        {
            seca=k/10;
            secb=k%10;
            mina=j/10;
            minb=j%10;
            hra=i/10;
            hrb=i%10;

            if(flag==0)
            {
                PORTA=mask(seca);
                PORTC=mask(secb);
            }
            else if(flag==1)
            {
                PORTA=mask(mina);
                PORTC=mask(minb);
            }
            else if(flag==2)
            {
                PORTA=mask(hra);
                PORTC=mask(hrb);
            }

            Delay_ms(100);
        }
    }
}

All other sections are working, RB0 int is working too.. But the button from other inputs (RB4..RB7) are not working. The MCU I'm using is PIC16F722A.

\$\endgroup\$
  • 3
    \$\begingroup\$ PLEASE make the code somewhat readable. \$\endgroup\$ – Dzarda Oct 13 '14 at 10:02
  • 1
    \$\begingroup\$ Yes, agreed - learn to format your code with proper indents. It's impossible to follow the flow of it as it stands at the moment. \$\endgroup\$ – Majenko Oct 13 '14 at 10:05
  • \$\begingroup\$ For those too lazy to do it themselves: prettyprinter.de \$\endgroup\$ – Majenko Oct 13 '14 at 10:09
1
\$\begingroup\$

In your ISR you have this:

    flag=9;
    temp = PORTB;
    PORTB = temp;

Firstly, you set flag to 9, but you're only looking at if it's 0, 1 or 2 in your main loop. So that's never going to have any effect.

And secondly, why are you reading port B then writing the value directly back to port B? What are you hoping to achieve there?

Also, I don't know what compiler you're using, but in most compilers there is a requirement to have all global variables that are used in ISRs marked as "volatile" or the compiler may optimize them out.

\$\endgroup\$
  • \$\begingroup\$ Well when the flag=9 then the o/p wont mask any number, so i will know interrupt is working. i just added that to check if interrupt is working. The compiler iam using is mikroc \$\endgroup\$ – Habeeb Rahman Oct 13 '14 at 10:23
1
\$\begingroup\$

You've got TRISB=1; but it needs to be set to 1 for each each input bit. If you are using all the RB pins as inputs, TRISB = 0b11111111; if your compiler supports the 0b for binary, or 0xFF for hexadecimal which is always supported.

You've also got IOCB=1111110; which you presumably mean to be in a binary number format, IOCB bits need to be 1 for every RB pin that you want to enable interrupt on change. Should be IOCB = 0b11111111 if you want all RB pins as interrupt on change inputs.

Numbers without a leading zero are taken as decimal unless you have a prefix such as 0b or 0x. Beware that a leading zero on a constant means it is octal (base 8).

\$\endgroup\$
  • \$\begingroup\$ Tried that buddy. But still doesnt work. \$\endgroup\$ – Habeeb Rahman Oct 13 '14 at 10:25
0
\$\begingroup\$

I haven't used your particular compiler before, but what I suspect is the problem is that you haven't specified that your isr handler is actually your isr handler for your RB interrupts.

I'll use Mirochips XC16 as an exampl since its what I have on hand right now.

In one of the provided headers (interrupts.h specifically), it gives me a basic summary of the functions.

/******************************************************************************/
/* Interrupt Vector Options                                                   */
/******************************************************************************/
/*                                                                            */
/* Refer to the C30 (MPLAB C Compiler for PIC24F MCUs and dsPIC33F DSCs) User */
/* Guide for an up to date list of the available interrupt options.           */
/* Alternately these names can be pulled from the device linker scripts.      */
/*                                                                            */
/* PIC24F Primary Interrupt Vector Names:                                     */
/*                                                                            */
/* _INT0Interrupt      _IC4Interrupt                                          */
/* _IC1Interrupt       _IC5Interrupt                                          */
/* _OC1Interrupt       _IC6Interrupt                                          */
/* _T1Interrupt        _OC5Interrupt                                          */
/* _Interrupt4         _OC6Interrupt                                          */
/* _IC2Interrupt       _OC7Interrupt                                          */
/* _OC2Interrupt       _OC8Interrupt                                          */
/* _T2Interrupt        _PMPInterrupt                                          */
/* _T3Interrupt        _SI2C2Interrupt                                        */
/* _SPI1ErrInterrupt   _MI2C2Interrupt                                        */
/* _SPI1Interrupt      _INT3Interrupt                                         */
/* _U1RXInterrupt      _INT4Interrupt                                         */
/* _U1TXInterrupt      _RTCCInterrupt                                         */
/* _ADC1Interrupt      _U1ErrInterrupt                                        */
/* _SI2C1Interrupt     _U2ErrInterrupt                                        */
/* _MI2C1Interrupt     _CRCInterrupt                                          */
/* _CompInterrupt      _LVDInterrupt                                          */
/* _CNInterrupt        _CTMUInterrupt                                         */
/* _INT1Interrupt      _U3ErrInterrupt                                        */
/* _IC7Interrupt       _U3RXInterrupt                                         */
/* _IC8Interrupt       _U3TXInterrupt                                         */
/* _OC3Interrupt       _SI2C3Interrupt                                        */
/* _OC4Interrupt       _MI2C3Interrupt                                        */
/* _T4Interrupt        _U4ErrInterrupt                                        */
/* _T5Interrupt        _U4RXInterrupt                                         */
/* _INT2Interrupt      _U4TXInterrupt                                         */
/* _U2RXInterrupt      _SPI3ErrInterrupt                                      */
/* _U2TXInterrupt      _SPI3Interrupt                                         */
/* _SPI2ErrInterrupt   _OC9Interrupt                                          */
/* _SPI2Interrupt      _IC9Interrupt                                          */
/* _IC3Interrupt                                                              */
/*                                                                            */
/* PIC24H Primary Interrupt Vector Names:                                     */
/*                                                                            */
/* _INT0Interrupt      _SPI2Interrupt                                         */
/* _IC1Interrupt       _C1RxRdyInterrupt                                      */
/* _OC1Interrupt       _C1Interrupt                                           */
/* _T1Interrupt        _DMA3Interrupt                                         */
/* _DMA0Interrupt      _IC3Interrupt                                          */
/* _IC2Interrupt       _IC4Interrupt                                          */
/* _OC2Interrupt       _IC5Interrupt                                          */
/* _T2Interrupt        _IC6Interrupt                                          */
/* _T3Interrupt        _OC5Interrupt                                          */
/* _SPI1ErrInterrupt   _OC6Interrupt                                          */
/* _SPI1Interrupt      _OC7Interrupt                                          */
/* _U1RXInterrupt      _OC8Interrupt                                          */
/* _U1TXInterrupt      _DMA4Interrupt                                         */
/* _ADC1Interrupt      _T6Interrupt                                           */
/* _DMA1Interrupt      _T7Interrupt                                           */
/* _SI2C1Interrupt     _SI2C2Interrupt                                        */
/* _MI2C1Interrupt     _MI2C2Interrupt                                        */
/* _CNInterrupt        _T8Interrupt                                           */
/* _INT1Interrupt      _T9Interrupt                                           */
/* _ADC2Interrupt      _INT3Interrupt                                         */
/* _IC7Interrupt       _INT4Interrupt                                         */
/* _IC8Interrupt       _C2RxRdyInterrupt                                      */
/* _DMA2Interrupt      _C2Interrupt                                           */
/* _OC3Interrupt       _DCIErrInterrupt                                       */
/* _OC4Interrupt       _DCIInterrupt                                          */
/* _T4Interrupt        _U1ErrInterrupt                                        */
/* _T5Interrupt        _U2ErrInterrupt                                        */
/* _INT2Interrupt      _DMA6Interrupt                                         */
/* _U2RXInterrupt      _DMA7Interrupt                                         */
/* _U2TXInterrupt      _C1TxReqInterrupt                                      */
/* _SPI2ErrInterrupt   _C2TxReqInterrupt                                      */
/*                                                                            */
/* PIC24E Primary Interrupt Vector Names:                                     */
/*                                                                            */
/* __INT0Interrupt     __C1RxRdyInterrupt      __U3TXInterrupt                */
/* __IC1Interrupt      __C1Interrupt           __USB1Interrupt                */
/* __OC1Interrupt      __DMA3Interrupt         __U4ErrInterrupt               */
/* __T1Interrupt       __IC3Interrupt          __U4RXInterrupt                */
/* __DMA0Interrupt     __IC4Interrupt          __U4TXInterrupt                */
/* __IC2Interrupt      __IC5Interrupt          __SPI3ErrInterrupt             */
/* __OC2Interrupt      __IC6Interrupt          __SPI3Interrupt                */
/* __T2Interrupt       __OC5Interrupt          __OC9Interrupt                 */
/* __T3Interrupt       __OC6Interrupt          __IC9Interrupt                 */
/* __SPI1ErrInterrupt  __OC7Interrupt          __DMA8Interrupt                */
/* __SPI1Interrupt     __OC8Interrupt          __DMA9Interrupt                */
/* __U1RXInterrupt     __PMPInterrupt          __DMA10Interrupt               */
/* __U1TXInterrupt     __DMA4Interrupt         __DMA11Interrupt               */
/* __AD1Interrupt      __T6Interrupt           __SPI4ErrInterrupt             */
/* __DMA1Interrupt     __T7Interrupt           __SPI4Interrupt                */
/* __NVMInterrupt      __SI2C2Interrupt        __OC10Interrupt                */
/* __SI2C1Interrupt    __MI2C2Interrupt        __IC10Interrupt                */
/* __MI2C1Interrupt    __T8Interrupt           __OC11Interrupt                */
/* __CM1Interrupt      __T9Interrupt           __IC11Interrupt                */
/* __CNInterrupt       __INT3Interrupt         __OC12Interrupt                */
/* __INT1Interrupt     __INT4Interrupt         __IC12Interrupt                */
/* __AD2Interrupt      __C2RxRdyInterrupt      __DMA12Interrupt               */
/* __IC7Interrupt      __C2Interrupt           __DMA13Interrupt               */
/* __IC8Interrupt      __DMA5Interrupt         __DMA14Interrupt               */
/* __DMA2Interrupt     __RTCCInterrupt         __OC13Interrupt                */
/* __OC3Interrupt      __U1ErrInterrupt        __IC13Interrupt                */
/* __OC4Interrupt      __U2ErrInterrupt        __OC14Interrupt                */
/* __T4Interrupt       __CRCInterrupt          __IC14Interrupt                */
/* __T5Interrupt       __DMA6Interrupt         __OC15Interrupt                */
/* __INT2Interrupt     __DMA7Interrupt         __IC15Interrupt                */
/* __U2RXInterrupt     __C1TxReqInterrupt      __OC16Interrupt                */
/* __U2TXInterrupt     __C2TxReqInterrupt      __IC16Interrupt                */
/* __SPI2ErrInterrupt  __U3ErrInterrupt        __ICDInterrupt                 */
/* __SPI2Interrupt     __U3RXInterrupt                                        */
/*                                                                            */
/*                                                                            */
/* For alternate interrupt vector naming, simply add 'Alt' between the prim.  */
/* interrupt vector name '_' and the first character of the primary interrupt */
/* vector name.  There are no Alternate or 'Alt' vectors for the 24E family.  */
/*                                                                            */
/* For example, the vector name _ADC2Interrupt becomes _AltADC2Interrupt in   */
/* the alternate vector table.                                                */
/*                                                                            */
/* Example Syntax:                                                            */
/*                                                                            */
/* void __attribute__((interrupt,auto_psv)) <Vector Name>(void)               */
/* {                                                                          */
/*     <Clear Interrupt Flag>                                                 */
/* }                                                                          */
/*                                                                            */
/* For more comprehensive interrupt examples refer to the C30 (MPLAB C        */
/* Compiler for PIC24 MCUs and dsPIC DSCs) User Guide in the                  */
/* <compiler installation directory>/doc directory for the latest compiler    */
/* release.                                                                   */
/*                                                                            */
/******************************************************************************/
/* Interrupt Routines                                                         */
/******************************************************************************/

Notice the last little bit -

 /*  Example Syntax:                                                            */
                                                                                */
     void __attribute__((interrupt,auto_psv)) <Vector Name>(void)               */
     {                                                                          */
         <Clear Interrupt Flag>                                                 */
     }      

So if I wanted to use my Timer2 ISR (assuming I have set it up all the necessary registers), I would have something like the following

void __attribute__((__interrupt__, __auto_psv__)) _T2Interrupt(void)
{

   //do something here
    _T2IF = 0; //clear int flag
}

I think what you are misisng is the link for when an interrupt happens to where in your code its supposed to go, since the interrupts are vectored and each interrupt or a group of interrupts share a similar address.

Check your header files - you might find something in there.

Also, a quick google search of MicroC Interrupt example yeilded the following link

http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/interrupts.htm

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.