pic18f4520 interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by rstevenson, Nov 12, 2011.

  1. rstevenson

    Thread Starter New Member

    Apr 5, 2011
    21
    1
    im doing a project(a pacemaker) at school which is using a pic18f4520 and C18.

    so we are supposed to use interrupts for sending and receiving data. We are sending packets of data each packet has 16 bytes in it. Now right now I am calling a function outside of the interrupt to store each byte (receiving) into a buffer.

    This works, but i'm told the overhead to do this is huge and shouldnt be done at all.

    My problem is we are supposed to modularize our code so i have a buffer module and my main module and i dont know how to use the interrupt without doing a function call. Some people are using defines. Any ideas?
    I just want ideas and not anyone to do the actual code for me!

    Code ( (Unknown Language)):
    1.  #pragma interrupt intr_handler
    2. void intr_handler(void) {    
    3. if (PIR1bits.RCIF == 1) {
    4.     RcBUF_ADD(RCREG);     // Add the byte into receiving buffer
    5.     if ((opState==k_idle)&&(RcBUF_LENGTH() ==16))
    6.         opState = k_commState;
    7.         }
    8.     }   /* If the microcontroller sent a byte */
    9.     if (PIR1bits.TXIF == 1) {
    10.         /* If there is nothing to send (the sending buffer is empty) */
    11.         if (TxBUF_EMPTY()) {
    12. /* Turn off sending interrupt */
    13.             PIE1bits.TXIE = 0;
    14.         } else {
    15. /* Send the first byte in the sending buffer */
    16.             TXREG = TxBUF_GET();
    17.         }
    18.     }
    19. }
    Code ( (Unknown Language)):
    1. void RcBUF_ADD(unsigned char byte) {
    2.         rcbuf.data[rcbuf.tail] = byte;
    3.         rcbuf.tail=((++rcbuf.tail) & e_Mask);
    4. }
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    I really don't see the need for #define or a function here. The code on RcBUF_ADD is very simple and is not going to make your code in the interrupt that much harder to follow if it is in the main ISR.


    But if you want to do it, in this instance a #define would be slightly more efficient. It would have a few machine cycles for pushing and popping the stack, calling the JSR and the return.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Look up semaphores, fancy name for a variable to control ownership of the buffers.

    Everything can be linked from separate modules with #extern keywords.

    (Sorry, have a flu and not thinking all that clearly today.)
     
  4. rstevenson

    Thread Starter New Member

    Apr 5, 2011
    21
    1
    the point of this project is to have everything in modules. so as of right now I have a buffer module. Its not the fact that it would be harder to follow but that it should be separate from the main code so we could change the buffer implementation and the main code would still work. Could someone post an example of how to use a define to act like a function? I attempted but i kept getting syntax errors in mplab.

    actually i got a couple to work. i was just missing the \
    new problem: i have a condition in the macro and was trying to use the ? :
    Code ( (Unknown Language)):
    1. #define RcBUF_LENGTH() do{ \
    2.     ((rcbuf.head <= rcbuf.tail) ? (rcbuf.tail - rcbuf.head) : (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))); \
    3. }while(0)  
    im getting an error. any ideas why?
     
    Last edited: Nov 13, 2011
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    It would help if you told us what error you get. ;)

    You don't need the loop in the #define macro, it should be as simple as:
    Code ( (Unknown Language)):
    1. [FONT=Courier New]
    2. #define RcBUF_LENGTH()                            \
    3.      (rcbuf.head <= rcbuf.tail) ?                 \
    4.         (rcbuf.tail - rcbuf.head) :               \
    5.         (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))[/FONT]
    6.  
    Then you can use that as:
    Code ( (Unknown Language)):
    1. int BufLen = RcBUF_LENGTH();
    2.  
    Note: I don't follow what you are doing with this information. Also, all these variables must be defined in the module where the #define is used.
     
  6. rstevenson

    Thread Starter New Member

    Apr 5, 2011
    21
    1
    yes they are defined. and it was a syntax error. Thanks for your help! I'll see if that works!

    edit: i know its probably hard to see what im doing since i gave you only a small snippet of my code. sorry!

    new problem :p im getting a "symbol rcbuf has multiple definitions"

    Code ( (Unknown Language)):
    1.  
    2. struct buffer rcbuf;
    3.  
    4. #define RcBUF_INIT() (rcbuf.head = rcbuf.tail = 0)
    5.  
    6.  
    7. #define RcBUF_LENGTH()                            \
    8.      (rcbuf.head <= rcbuf.tail) ?                 \
    9.         (rcbuf.tail - rcbuf.head) :               \
    10.         (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))
    11.  
    12. #define RcBUF_ADD(c) (rcbuf.data[rcbuf.tail++ & e_Mask] = c)
    13.  
    14.  
    15. #define RcBUF_GET() (rcbuf.data[rcbuf.head++ & e_Mask])
    Code ( (Unknown Language)):
    1. /* Initialize buffer size */
    2. #define e_BUF_SIZE 32
    3. #define e_Mask e_BUF_SIZE - 1
    4.  
    5. /* Define the buffer structure */
    6. struct buffer
    7. {
    8.     char head;
    9.     char tail;
    10.     char data[e_BUF_SIZE];
    11. };
    I used a find in files in mplab and it can only find rcbuf defined once. any ideas?
     
    Last edited: Nov 13, 2011
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015


    Is it defined in a header (.h) file? If so it could effectively be defining more than once. You should really only define variables in a C file.

    Also you should set up a flag so the same header file does not get compiled more than once for the same C file

    For example in MyHeader.h

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #ifndef MYHEADER_H
    4. #define MYHEADER_H
    5.  
    6.           // Some header stuff
    7. #endif
    8.  
    9.  
     
  8. rstevenson

    Thread Starter New Member

    Apr 5, 2011
    21
    1
    oooohhhhhh Right!!! Thanks! i didnt think of that!
     
  9. rstevenson

    Thread Starter New Member

    Apr 5, 2011
    21
    1
    I cant seem to edit my last post but: Thanks to everyone who posted in this thread! You were very helpful! :D
     
Loading...