|
|
View previous topic :: View next topic |
Author |
Message |
Stefanos Dris Guest
|
RS232 to i2c |
Posted: Mon Jun 30, 2003 3:36 pm |
|
|
My goal is to convert between RS232 to i2c messages and vice versa using a PIC (I am only concerned with RS232 to i2c at the moment). I don't have my hardware yet, and hence I can't test my code. Nevertheless, I am thinking about the implementation and would like some ideas to get me started...
Looking through the various threads, I wrote the code below. It uses the serial interrupt to grab an incoming RS232 byte, sets a flag to indicate reception of the byte, then goes back to main to send it via i2c.
I am guessing this would not be good enough to deal with multiple incoming bytes (i.e. it would miss some of them) - Would it make sense to use a while(kbhit) statement in the ISR (so as to receive more than one byte at a time if they are available in the UART buffer) and store these in my own buffer?
Then, when I go back to main to send these via i2c, do I need to disable the RS232 interrupt so the i2c send does not get corrupted?
Any ideas are welcome - Thanks in advance.
----------------------------------------------------------
#include <16C63.h> // demo limited to this device
#fuses HS, NOWDT, NOPROTECT, BROWNOUT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
byte data;
char char_rcvd = FALSE;
#int_RDA
void serial_isr() {
if(kbhit()) {
// Maybe use while(kbhit()) and store bytes into data array
data = getc();
char_rcvd = TRUE;
}
}
void main() {
enable_interrupts(global);
enable_interrupts(INT_RDA);
while(TRUE) {
// Maybe disable RDA interrupt ??
if(char_rcvd == TRUE) {
char_rcvd = FALSE;
i2c_start();
i2c_write(0xa0); // address of slave + read/write bit
i2c_write(data); // send data
i2c_stop();
}
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515662 |
|
|
Stefanos Dris Guest
|
Re: RS232 to i2c |
Posted: Mon Jun 30, 2003 3:44 pm |
|
|
Just saw the following suggestion by PICProgrammer in an earlier thread:
"In the int_rda routine, you should
just receive one char, put it into a circular buffer, and exit.
Each time a new char comes into the hardware UART, you will
get an interrupt. Each interrupt will put the new char into
the circular buffer. That's all it does. "
Hence I take it that I only need to receive one character at a time in the INT_RDA, but I do need to use a buffer to store it in. But what happens if a character is received when I am trying to send data via i2c (e.g. after issuing the START condition on the i2c bus)? Will this not corrupt the i2c transfer?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515663 |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
Re: RS232 to i2c |
Posted: Mon Jun 30, 2003 6:12 pm |
|
|
:=Just saw the following suggestion by PICProgrammer in an earlier thread:
:=
:="In the int_rda routine, you should
:=just receive one char, put it into a circular buffer, and exit.
:=Each time a new char comes into the hardware UART, you will
:=get an interrupt. Each interrupt will put the new char into
:=the circular buffer. That's all it does. "
:=
:=Hence I take it that I only need to receive one character at a time in the INT_RDA, but I do need to use a buffer to store it in. But what happens if a character is received when I am trying to send data via i2c (e.g. after issuing the START condition on the i2c bus)? Will this not corrupt the i2c transfer?
Don't think so. All START does is pull SDA down while SCL is high. When a byte, address or data, is to be sent, once the SSPSR register is loaded with the byte the hardware clocks it out without processor control. If INT_RDA happens, then loading of SSPSR will be just delayed.
Regards
Kenny
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515668 |
|
|
Stefanos Dris Guest
|
Re: RS232 to i2c |
Posted: Tue Jul 01, 2003 2:38 pm |
|
|
<font face="Courier New" size=-1>:=:=Just saw the following suggestion by PICProgrammer in an earlier thread:
:=:=
:=:="In the int_rda routine, you should
:=:=just receive one char, put it into a circular buffer, and exit.
:=:=Each time a new char comes into the hardware UART, you will
:=:=get an interrupt. Each interrupt will put the new char into
:=:=the circular buffer. That's all it does. "
:=:=
:=:=Hence I take it that I only need to receive one character at a time in the INT_RDA, but I do need to use a buffer to store it in. But what happens if a character is received when I am trying to send data via i2c (e.g. after issuing the START condition on the i2c bus)? Will this not corrupt the i2c transfer?
:=
:=Don't think so. All START does is pull SDA down while SCL is high. When a byte, address or data, is to be sent, once the SSPSR register is loaded with the byte the hardware clocks it out without processor control. If INT_RDA happens, then loading of SSPSR will be just delayed.
:=
:=Regards
:=Kenny
Thanks Kenny. I will make changes based on PICProgrammer's code and try it out. </font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515693 |
|
|
|
|
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
|