Thread: Program for External Interrupt for PIC 16F877A

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    Program for External Interrupt for PIC 16F877A

    I am writing a program for a robot and I am having trouble with the program for the interrupt system. The robot uses a pair of bumpers that when pushed, sensors that are connected to port E bit 0 and 1 cause a signal the transitions form high to low. The code builds, but when executing the program the robot does not respond to the interrupt. I cannot seem to find what is wrong with the program and I'm hoping someone can help me!

    Code:
    #include<htc.h>
    #include<PICinit.h>
    #include<PICinit.c>
    
    
    void motorsOff ();
    void forwardMotion ();
    void leftPivot ();
    void rightPivot ();
    void backwardMotion ();
    void backwardLeft ();
    void backwardRight ();
    
    
    main(void)
    {
       
        motorspeed (1,10);
        motorspeed (2,10);
    
    
          configurePIC ();
          INTE =1;
          GIE= 1;
    
    
          while(1)
          {
              forwardMotion ();
              
          }
    
    
    }
    
    
    void motorsOff (){
    output_low ('C',0);
    output_low ('C', 5);
    output_low ('C',4);
    output_low ('C',3);
    return; } void forwardMotion (){
    output_low ('C',0);
    output_high ('C', 5);
    output_high ('C',4);
    output_low ('C',3);
    return; } void leftPivot (){
    output_low ('C',0);
    output_high ('C', 5);
    output_low ('C',4);
    output_high ('C',3);
    return; } void rightPivot (){
    output_high ('C',0);
    output_low ('C', 5);
    output_high ('C',4);
    output_low ('C',3);
    return; } void backwardMotion (){
    output_high ('C',0);
    output_low ('C', 5);
    output_low ('C',4);
    output_high ('C',3);
    return; } void backwardLeft (){
    output_high ('C',0);
    output_high('C', 5);
    output_low ('C',4);
    output_high ('C',3);
    return; } void backwardRight () {
    output_high ('C',0);
    output_high ('C', 5);
    output_high ('C',4);
    output_low ('C',3);
    return; } void interrupt isr (void) { if(save_data ()==0){ return; } pause(5); if((PORTB & 0x01)==1) {
    INTF = 0;
    restore_data ();
    return;
    } motorsOff (); pause (50); if((read_input('E',0))==0) //bit 0 port E is right bumper {
    backwardMotion ();
    pause (10);
    backwardRight ();
    pause (10);
    forwardMotion ();
    } else if((read_input('E',1))==0) //bit 1 port E is right bumper {
    backwardMotion();
    pause (10);
    backwardLeft ();
    pause (10);
    forwardMotion ();
    } else {
    backwardMotion();
    pause (20);
    backwardLeft ();
    pause (10);
    forwardMotion ();
    } if((PORTB & 0x01)==1) { INTF = 0; } restore_data (); return; }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would imagine you would tell if the motorsOff call happened, so I'm assuming that's not getting called.

    Notice that you have two early outs in your interrupt: if save_data returns 0 (presumably that's failure) or if PORT_B is on, whatever that represents; so you may want to check those things.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I'm afraid I have no personal experience with PIC, but have spent many a day bashing my head on disappearing interrupts on other cores. On ARM cores there are a lot of registers that give information abuot what interrupts are currently pending, or preempted, etc. I don't know if there's anything similar on PIC. Failingthat, I'd just try enabling every conceivable interrupt in case the designers connected it up in an odd way. Do interrupts from other sources get through ok? If they do, you might be able to figure out what's different.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you're reading PORTE pins and hoping to generate an interrupt, it won't work. According to the data sheet for your device, the external interrupt is generated on the PORTB bit 0. This will only work if you have hardware in your circuit that also feeds the output of the sensors to this pin.

    Your ISR seems to take this pin into account when checking for the interrupt, but I'm not sure how you expect a change on PORTE pins to trigger the interrupt in the first place. You need to be more clear on how you are expecting this to work.

    Also note that this interrupt defaults to a rising edge on that pin (see the INTEDG bit in the option register). It sounds like you want it to trigger on the falling edge, depending on how the signal is fed to PORTB bit 0. (This also might have been handled in the "configurePIC()" function, but that code was not provided.)

    However, once again, it seems that a rising edge on PORTB bit 0 is expected in the ISR - that pin is checked for logic high. I'm not seeing how you couple PORTB bit 0 and the PORTE pins. Also note that it is better to check the value of the interrupt flag itself rather than the current state of that pin.

    A few other points:

    The definitions of the "output_high" and "output_low" functions are not shown. If there are no pauses (e.g. NOP) between these changes of pins, you run the risk of encountering a "read-write-modify" bug.

    Your ISR should be as terse as possible. It is better to set a flag in the interrupt, and handle the resulting code in "main()". A state machine would be an ideal approach to this implementation, it would seem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Add a External Library in the C Program ?
    By Linked_List in forum C Programming
    Replies: 0
    Last Post: 10-06-2012, 02:42 PM
  2. Closing an External Program
    By magic.mike in forum Windows Programming
    Replies: 3
    Last Post: 08-04-2005, 01:27 AM
  3. External Program Communicating with IE (COM?)
    By Zeusbwr in forum C# Programming
    Replies: 0
    Last Post: 04-26-2005, 11:32 PM
  4. calling an external program
    By pshuster in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2004, 11:57 PM
  5. run an external program
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-27-2002, 08:09 AM

Tags for this Thread