NJ Roadmap
New Member
This could have you guys dumbfounded like I am, or it could just be a simple answer!
Basically I have 2 PIC 18F4520s powered independently from two 12V-mains-sourced 5V regulators. I have the USART serial pins of the two PICs connected to each other along with a common ground line, so both the supplies have a common ground even though they are actually independent.
I basically have them talking to each other with PIC-1 waiting for PIC-2 to ask for data, and then PIC-1 transmitting data to PIC-2. This goes on in a loop just for testing purposes. I've got both PICs hooked up to LCDs so I know whats going on.
Now heres the peculiar part. I should theoretically be able to power up PIC-1 and then PIC-2 and immediately have PIC-2 asking PIC-1 for data, PIC-1 responding and the loop continuing. But I can't get that to happen. PIC-1 just waits for data even after PIC-2 has powered on and acknowledged that it has asked PIC-1 for data.
The ONLY way I can currently get around this is if I power on both PICs (in any order), reload the hex file for PIC-1 first while still having the 12V power connected, and then reload the hex file for PIC-2 using the same process. PIC-1 then correctly receives PIC-2's request and sends data back and the loop continues without a hitch.
I'm guessing this is something to do with either the fact that I've got common ground for 2 independent supplies, or something wrong with my code. I plan to eventually have wireless USART using Nigel's tutorials and inverting the signals as opposed to Manchester coding as the communication will be short-range, so the common ground issue will become irrelevant later. I have posted the important bits of my code below:
PIC-1 code (master):
PIC-2 code (slave):
Both PICs have the following serial initialisation:
Another question: I can currently send one character reliably, what if I want to send a string? I'm planning to send int's and float's from the slave to the master with the master periodically polling the slave, so I was thinking of using sprintf() to append them to a string and then send the string.
Any insights would be appreciated!
p.s. I'm using the Hi-Tech PICC18 compiler and PICKit2 to load HEX files.
Basically I have 2 PIC 18F4520s powered independently from two 12V-mains-sourced 5V regulators. I have the USART serial pins of the two PICs connected to each other along with a common ground line, so both the supplies have a common ground even though they are actually independent.
I basically have them talking to each other with PIC-1 waiting for PIC-2 to ask for data, and then PIC-1 transmitting data to PIC-2. This goes on in a loop just for testing purposes. I've got both PICs hooked up to LCDs so I know whats going on.
Now heres the peculiar part. I should theoretically be able to power up PIC-1 and then PIC-2 and immediately have PIC-2 asking PIC-1 for data, PIC-1 responding and the loop continuing. But I can't get that to happen. PIC-1 just waits for data even after PIC-2 has powered on and acknowledged that it has asked PIC-1 for data.
The ONLY way I can currently get around this is if I power on both PICs (in any order), reload the hex file for PIC-1 first while still having the 12V power connected, and then reload the hex file for PIC-2 using the same process. PIC-1 then correctly receives PIC-2's request and sends data back and the loop continues without a hitch.
I'm guessing this is something to do with either the fact that I've got common ground for 2 independent supplies, or something wrong with my code. I plan to eventually have wireless USART using Nigel's tutorials and inverting the signals as opposed to Manchester coding as the communication will be short-range, so the common ground issue will become irrelevant later. I have posted the important bits of my code below:
PIC-1 code (master):
while(1)
{
//approx 1 second delay before next sample and lcd update
for (n = 0; n <50; n++)
for( k= 0; k< twentyms; k++);
lcd_clr();
// initialise
TXSTA=0x26;
SPEN=1;
putch('?'); // Ask for data
LCDWrite('?', 1);
LCDWrite(' ', 1); //Acknowledge sent character to LCD
LCDWrite('s', 1);
LCDWrite('e', 1);
LCDWrite('n', 1);
LCDWrite('t', 1);
lcd_newline();
LCDWrite(getch(), 1); //Waiting...then write received character to LCD
LCDWrite(' ', 1); //Acknowledge receipt to LCD
LCDWrite('R', 1);
LCDWrite('c', 1);
LCDWrite('v', 1);
LCDWrite('d', 1);
}
PIC-2 code (slave):
while(1)
{
lcd_clr();
LCDWrite(getch(), 1); //Waiting...then write received character to LCD
LCDWrite(' ', 1);
LCDWrite('R', 1); //Acknowledge receipt to LCD
LCDWrite('c', 1);
LCDWrite('v', 1);
LCDWrite('d', 1);
//approx 1 second delay before sending character back
for (n = 0; n <50; n++)
for( k= 0; k< twentyms; k++);
// initialise
TXSTA=0x26;
SPEN=1;
putch('F');// transmit the character
lcd_newline();
LCDWrite('F', 1);
LCDWrite(' ', 1); //Acknowledge sent character to LCD
LCDWrite('s', 1);
LCDWrite('e', 1);
LCDWrite('n', 1);
LCDWrite('t', 1);
for (n = 0; n <50; n++)
for( k= 0; k< twentyms; k++);
}
Both PICs have the following serial initialisation:
/* Serial initialization */
void
init_comms(void)
{
SPBRG = DIVIDER;
TXSTA = (SPEED|NINE_BITS|0x20);
RCSTA = (NINE_BITS|0x90);
TRISC6=OUTPUT;
TRISC7=INPUT;
}
Another question: I can currently send one character reliably, what if I want to send a string? I'm planning to send int's and float's from the slave to the master with the master periodically polling the slave, so I was thinking of using sprintf() to append them to a string and then send the string.
Any insights would be appreciated!
p.s. I'm using the Hi-Tech PICC18 compiler and PICKit2 to load HEX files.
Last edited: