CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Using "Change on Port B" int for a serial input in

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
starfire151



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

Using "Change on Port B" int for a serial input in
PostPosted: Wed May 23, 2007 7:15 am     Reply with quote

I'm trying to set up a PIC18LF2525 port bit B4 for recv and bit B5 for xmit for a serial port interface. I set it up on the upper nibble of port B to use the interrupt-on-change feature. I have another serial interface using the hardware port of bits C6 and C7 so I can't use that port for this serial stream. The stream name for the serial input on pins B5 and B4 is "TERM". What I have done so far (with limited success) is something like the following:

Code:

short bFlag;    // flag to indicate a character byte on port B4 was seen
char ch;         // input character from port B change ISR
...

init bFlag = FALSE;
...

#INT_RB
void RB_isr(void)
{
    disable_interrupts(INT_RB);

    if(getch())
    {
        ch = fgetc(TERM);
        bFlag = TRUE;
    }
}
...

// in background loop:
...
    if(bFlag)
    {
        fprintf(TERM, "char was %c\r\n", ch);
        bFlag = FALSE;
        enable_interrupts(INT_RB);   // re-enable port B interrupts
    }
...


When I run the code, it appears the port B change interrupt routine is executed immediately and waits for a character. When I send an ASCII character to the pin B4 input, it sees it, loads it into the ch, sets the bFlag, and the background routine sees it and prints it successfully. Then everything locks up. No more characters are recognized on the port B4 input. I've also tried to clear_interrupt(INT_RB) before re-enabling the INT_RB interrupts but no change.

When I start the program up, I have a continuous data stream being sent to the hardware serial port of pins C6 and C7. This stream is not recognized until after I send a serial character to the port B4 pin. Once I send the character to the port B4 pin and it is printed, I start seeing the serial stream from the hardware port at C6 and C7.

Has anyone successfully used the change on Port B interrupt for a serial port interface in this way? Does anyone see what I'm doing wrong?

Any help appreciated... Thanks.

Dave
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed May 23, 2007 8:16 am     Reply with quote

Don't disable the interupt in the interupt service routine.

Just by being in the ISR the compiler turns them off for you,... ie normally you can't be interupted while in the ISR.

(you can also remove the enable)((since you never disable it))



also in other spec sheets I remember seeing that the port B IRQ is cleared when the port is read. try putting

int8 x;
x = input_b();

inside the ISR.


I think others have done this before. Try searching for working code
Ttelmah
Guest







PostPosted: Wed May 23, 2007 9:39 am     Reply with quote

In the 'main, before enabling the interrupt, read portB, and clear the interrupt. Remember that serial data idles 'high', so a change will almost certainly have already been 'seen' on the pin, soon after power-up.
The interrupt will trigger if _any_ of the upper pins change from the latched state. If you use one for output (as you do...), then the interrupt will trigger as soon as you start sending data, which will really stop things working...
Getch, will access the default stream (last one defined), not the 'term' stream, and you will effectively try to read a character from this, then one from 'term'. You need to use 'kbhit(TERM)', not 'getch', in your if statement.
The disable interrupts, will stop subsequent characters until after the print. May well result in data loss.
This is going to be very hard to make work. You would be better re-mapping something from port B0, B1, or B2, and using one oif these interrupts to handle this. Code for this has been published here in the past.

Best Wishes
starfire151



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

port b int-on-change for serial input
PostPosted: Wed May 23, 2007 10:11 am     Reply with quote

Thanks for the recommendations. I will try these when I can access to my hardware tonight.

I've tried searching the forum for previous (working) examples of someone trying to do this but my search has probably not been formatted quite correctly (over 4600 hits). Does someone have the link to the previously posted working code example of doing this operation?

Thanks.

Dave
starfire151



Joined: 01 Apr 2007
Posts: 195

View user's profile Send private message

serial port on port b change
PostPosted: Wed May 23, 2007 8:42 pm     Reply with quote

I tried the suggesstions and they worked perfectly!

I did a read on port B before enabling interrupts in the background first. According to the data sheet, the port change interrupt only occurs on pins B4 through B7 set as inputs. I defined only B4 as the serial input. I did a read on port B as the first step in the INT_PB routine. I checked for a complete character by using if(kbhit(TERM)){ } and assigned the character and set a flag. The background routine checked for the flag and, when seen, printed the character.

It worked interactively with the hardware serial port also active.

Thanks for your suggestions!
Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group