pic18f4520 interrupt

Thread Starter

rstevenson

Joined Apr 5, 2011
20
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!

Rich (BB code):
 #pragma interrupt intr_handler
void intr_handler(void) {    
if (PIR1bits.RCIF == 1) {
    RcBUF_ADD(RCREG);     // Add the byte into receiving buffer
    if ((opState==k_idle)&&(RcBUF_LENGTH() ==16))
        opState = k_commState;
        }
    }   /* If the microcontroller sent a byte */
    if (PIR1bits.TXIF == 1) {
        /* If there is nothing to send (the sending buffer is empty) */
        if (TxBUF_EMPTY()) {
/* Turn off sending interrupt */
            PIE1bits.TXIE = 0;
        } else {
/* Send the first byte in the sending buffer */
            TXREG = TxBUF_GET();
        }
    }
}
Rich (BB code):
void RcBUF_ADD(unsigned char byte) {
        rcbuf.data[rcbuf.tail] = byte;
        rcbuf.tail=((++rcbuf.tail) & e_Mask);
}
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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.)
 

Thread Starter

rstevenson

Joined Apr 5, 2011
20
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.
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 ? :
Rich (BB code):
#define RcBUF_LENGTH() do{ \
    ((rcbuf.head <= rcbuf.tail) ? (rcbuf.tail - rcbuf.head) : (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))); \
}while(0)
im getting an error. any ideas why?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
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:
Rich (BB code):
#define RcBUF_LENGTH()                            \
     (rcbuf.head <= rcbuf.tail) ?                 \
        (rcbuf.tail - rcbuf.head) :               \
        (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))
Then you can use that as:
Rich (BB code):
int BufLen = RcBUF_LENGTH();
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.
 

Thread Starter

rstevenson

Joined Apr 5, 2011
20
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"

Rich (BB code):
struct buffer rcbuf;

#define RcBUF_INIT() (rcbuf.head = rcbuf.tail = 0) 


#define RcBUF_LENGTH()                            \
     (rcbuf.head <= rcbuf.tail) ?                 \
        (rcbuf.tail - rcbuf.head) :               \
        (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))

#define RcBUF_ADD(c) (rcbuf.data[rcbuf.tail++ & e_Mask] = c)


#define RcBUF_GET() (rcbuf.data[rcbuf.head++ & e_Mask])
Rich (BB code):
/* Initialize buffer size */
#define e_BUF_SIZE 32
#define e_Mask e_BUF_SIZE - 1

/* Define the buffer structure */
struct buffer 
{
    char head;
    char tail;
    char data[e_BUF_SIZE];
};
I used a find in files in mplab and it can only find rcbuf defined once. any ideas?
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
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"

Rich (BB code):
struct buffer rcbuf;

#define RcBUF_INIT() (rcbuf.head = rcbuf.tail = 0) 


#define RcBUF_LENGTH()                            \
     (rcbuf.head <= rcbuf.tail) ?                 \
        (rcbuf.tail - rcbuf.head) :               \
        (e_BUF_SIZE - (rcbuf.head - rcbuf.tail))

#define RcBUF_ADD(c) (rcbuf.data[rcbuf.tail++ & e_Mask] = c)


#define RcBUF_GET() (rcbuf.data[rcbuf.head++ & e_Mask])
Rich (BB code):
/* Initialize buffer size */
#define e_BUF_SIZE 32
#define e_Mask e_BUF_SIZE - 1

/* Define the buffer structure */
struct buffer 
{
    char head;
    char tail;
    char data[e_BUF_SIZE];
};
I used a find in files in mplab and it can only find rcbuf defined once. any ideas?


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

Rich (BB code):
#ifndef MYHEADER_H
#define MYHEADER_H

          // Some header stuff
#endif
 
Top