PDA

View Full Version : Serial Interrupts and latency



barkerben
- 27th November 2004, 17:02
Hi,

I'm trying to use the USART module in my PIC16F876. I have built a pan/tilt camera stand using stepper motors, and controlled by 2 5804 stepper crivers. The idea is to use 2 PIC's as interfaces to the PC. I tell the PIC's what speed/direction I want over the serial link, and they set the neccesary control lines and output a pulse train to run the motors.

I was planning to use USART interruts on receive - when serial data arrived, the ISR would be called, read the data, and then get back to running the motors. The prblem is that if
I am outputting a pulse chain at a low frequency, it may be a while before the ISR is serviced, by which time the data has already arrived. As I understand it, this is not a problem as long as the size of the data is not > the size of the buffer?

Another option is to send one 'garbage' byte to trigger the interrupt, wait a second, then proceed. In thisapplication, quick response is not that important - but getting the correct data is!

There is also the issue of how to address 2 PIC's seperately, although I suspect this will be easier than solving the above. Does anyone have any ideas / experience?


Sorry for the enourmous post....


Cheers,


Ben

barkerben
- 27th November 2004, 22:39
Hmm.... If the receive buffer is large enough, there may not be a problem. All I need t send is:

PIC ID
Mode
Rate

So 3 bytes would be more than enough. Having trouble deciphering the datasheets though. Anyone know how large the buffer is?

Also, presumably I need to reset the buffer after each read. Can I do this in Basic? Am I talking gibberish....


Cheers,

Ben

barkerben
- 28th November 2004, 01:37
All I need to be able to do is specify the following:

Motor ID
Motor Mode
Paramter (speed or # steps depending on mode)

It seems the PIC has a 2 byte buffer. I could use one byte
for passing the parameter, and the first byte to specify the various possible combinations of ID/Mode.

If the PC only ever transmits 2 bytes at a time, and each time the PIC reads it clears the buffer, I should be in the clear.

However, it all depends on how PICBasic works. I assume that if 2 bytes arrive, triggering the interrupt, then when the service routine reads from the serial input, it will first empty the buffer, and, if data is still coming in, read directly from the serial Pin? If the data has already arrived, then presumably it will just read the 2 bytes from the buffer, at which point I need to somehow reset the buffer, act on the received data.

If I know I'm going to be receiving 2 bytes of data, presumably the syntax is:


HSERIN byte1, byte2

where byte1 and byte2 are predefined variables?

barkerben
- 28th November 2004, 17:33
Ok - I've come up with a possible solution.

If the serial buffer is indeed two bytes, then I will triggger an interrupt when the buffer is full, at which point both PIC's check to see if the 2 byte command is relevant to them, and act accordingly. Originally I was planning to interrupt when serial transmission began, but problem there is that with two pics, there would be an interrupt issued for both bytes, but only one PIC would still be in the ISR, so it would get bit messy.

This all relies on the size of the buffer being 2 bytes - The data sheet looks worryingly like only 1 byte, but I really can't decipher it. Help would be very much appreciated!


Ben