Pic Project-2 RB0/INT

Discussion in 'Programmer's Corner' started by royalmadhu, Feb 5, 2011.

  1. royalmadhu

    Thread Starter New Member

    Feb 4, 2011
    6
    0
    hi i'm now making a pulse counter
    i'm supplying pulses externally to the pic 16f877a and i want to get the count to display..

    the displaying funtion is done..

    but i'm not sure how to handle the RB0/int

    this is my interrupt function

    void interrupt()
    {
    if(INTCON.INTF==1)
    {
    count9=count9+1;
    if(count9==999)
    {
    count9=0;
    }
    intcon.intf=0;
    }
    }


    void main()
    {
    int a,b,c,d,k,m,n;
    TRISB=0b11111111; //portb is configured as an input
    INTCON= 0b10110000;//i have enabled the tmr0 interrupt and RB0/int interrupt
    OPTION_REG= 0b11000010;//rising edge selected and the prescaleer selected for the tmr0
    TRISA=0b11111111 ;

    TRISC=0b00000000;
    TRISD=0b00000000;
    TRISE=0b111;


    ...
    ...
    ...
    }
     
  2. royalmadhu

    Thread Starter New Member

    Feb 4, 2011
    6
    0
    plzz help me
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    You might get a better response if you make your code more readable by using the code tags (#) in advanced editing mode (see below).
    It also helps to know which compiler you are using.
    Code ( (Unknown Language)):
    1. void interrupt()
    2. {
    3.     if(INTCON.INTF==1)
    4.     {
    5.         count9=count9+1;
    6.         if(count9==999)
    7.         {
    8.             count9=0;
    9.         }
    10.         intcon.intf=0;
    11.     }    
    12. }
    13.  
    14.  
    15. void main()
    16. {
    17.     int a,b,c,d,k,m,n;
    18.     TRISB=0b11111111; //portb is configured as an input
    19.     INTCON= 0b10110000;//i have enabled the tmr0 interrupt and RB0/int interrupt
    20.     OPTION_REG= 0b11000010;//rising edge selected and the prescaleer selected for the tmr0
    21.     TRISA=0b11111111 ;
    22.  
    23.     TRISC=0b00000000;
    24.     TRISD=0b00000000;
    25.     TRISE=0b111;
    26.  
    27.  
    28.     ...
    29.     ...
    30.     ...
    31. }
    As to your problem, I am unclear as what you are trying to do. Are you trying to count the number of pulses that you receive (in which case what is timer0 doing?) or are you trying to find the time interval between the pulses (in which case where is the code to check timer0?)

    Also you have enabled 2 interrupts (tmr0 and RB0/int) but are only checking for (RB0/int) in the ISR (interrupt service routine). This means that if you get a tmr0 interrupt (and you will!) then you will never get out of the ISR.
     
  4. royalmadhu

    Thread Starter New Member

    Feb 4, 2011
    6
    0

    Hey thanks for the reply

    my task is to count some pulses which are generated from an encoder....
    so i have enabled the rising edge bit in the option register...

    when the RBO pin receives a pulse it can generate an inturrupt and start the count right?
     
  5. royalmadhu

    Thread Starter New Member

    Feb 4, 2011
    6
    0
    and i have used the tmro interrupt and cleared that bit in the ISR in my code..i didn't put that here cos then it will be too long .sorry
     
  6. royalmadhu

    Thread Starter New Member

    Feb 4, 2011
    6
    0
    ANd i'm using mikroC as a compiler
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    1. You have posted to the wrong forum. You should have posted to the embedded forum.

    2. Please read the forum guidelines. People will help you when they have the time. This might take some time. Remember that not everyone is in your time zone.

    3. There is no need to repost your code unless there is an update. It only adds to confusion.

    4. Use the code block when posting code like you did in your first post.


    I don't see where you are attaching your interrupt vector to your interrupt routine.


    In C18 you do it like this

    Code ( (Unknown Language)):
    1.  
    2. #pragma code InterruptVectorHigh = 0x08
    3. void InterruptVectorHigh (void)
    4. {
    5.   _asm
    6.     goto InterruptServiceHigh //jump to interrupt routine
    7.   _endasm
    8. }
    9.  
    10. #pragma interrupt InterruptServiceHigh  // "interrupt" pragma also for high priority
    11. void InterruptServiceHigh(void)
    12. {
    13.     // Check to see what caused the interrupt
    14.     // (Necessary when more than 1 interrupt at a priority level)
    15.  
    16.     // Check for INT0 interrupt
    17.     if (INTCONbits.INT0IF)
    18.     {
    19.         // clear (reset) flag
    20.         INTCONbits.INT0IF = 0;
    21.  
    22.         // change directions
    23.         if (Direction == LEFT2RIGHT)
    24.         {
    25.             Direction = RIGHT2LEFT;     // change direction
    26.         }
    27.         else // (Direction == RIGHT2LEFT)
    28.         {
    29.             Direction = LEFT2RIGHT;     // change direction
    30.         }
    31.        
    32.     }
    33.  
    34.     // Check for another interrupt, examples:
    35.     // if (PIR1bits.TMR1IF)     // Timer 1
    36.     // if (PIR1bits.ADIF)       // ADC
    37.  
    38. }  // return from high-priority interrupt
    39.  
    40.  
    Search for PICkit™ 3 Debug Express PIC18F45K20 – MPLAB® C Lessons on the Microchip website. It explains how to do interrupts.

    But if all you want to do is count pulses then you probably want to use your CCP port. But I have not done this so I am not exactly sure how to do it. You could try searching on CCP count pulses.
     
  8. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    That's right, your interrupt code looks OK for counting pulsed on RB0.

    Also you can disregard what spinnaker said about attaching your interrupt vector. That only applies if you are using the Microchip C18 compiler.

    Spinnaker:
    There is no standard way that compilers handle interrupts and each compiler does it in its own fashion. Most either have reserved names for the ISR or add a tag to the ISR function definition that tells the compiler to treat the function as an ISR and automatically point the interrupt vector to it. Its only in C18 that you have to go through the rigmarole of manually pointing the processor interrupt vector to the ISR.
     
Loading...