Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Thu Mar 18, 2004 6:17 pm |
|
|
Here's some bits and pieces to get you started:
Code: |
// Interrupt flags
#bit TXIF = 0x0C.4
#bit RCIF = 0x0C.5
#bit RX9D = 0x18.0
#bit OERR = 0x18.1
#bit FERR = 0x18.2
#bit ADEN = 0x18.3
#bit CREN = 0x18.4
#bit SREN = 0x18.5
#bit RX9 = 0x18.6
#bit SPEN = 0x18.7
#bit TX9D = 0x98.0
#bit TRMT = 0x98.1
#bit BRGH = 0x98.2
// bit 0x98.3 is undefined in 16F627
#bit SYNC = 0x98.4
#bit TXEN = 0x98.5
#bit TX9 = 0x98.6
#bit CSRC = 0x98.7
// Interrupt enables
#bit TXIE = 0x8C.4
#bit RCIE = 0x8C.5
#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte SPBRG = 0x99
void put_serial(int tx_data)
{
while(!TXIF)
{
restart_wdt();
}
TXREG = tx_data;
return;
}
int get_serial()
{
while(!RCIF)
{
restart_wdt();
}
return RCREG;
}
void TransmitData(void)
{
int j = 0;
SYNC = TRUE;
CREN = FALSE;
CSRC = TRUE;
do
{
put_serial(TransmitBuffer.Data[j++]); // Send the data then increment j
} while ((TransmitBuffer.Data[j-1] != 0x0D) && (j < BUFFERLENGTH));
}
main() // extracted from main
{
// set comm to sync master transmit
SPBRG = 0x19;
SYNC = TRUE;
CREN = FALSE;
SPEN = TRUE;
CSRC = TRUE;
TXEN = TRUE;
// Construct a message and send
your code goes here
TransmitData();
// wait for transmit to complete
// then loop for multiple commands
// first wait for the transmit interrupt flag (TXREG is empty)
while (1)
{
while (!TXIF)
{
restart_wdt();
}
// then wait for the TSR register to empty
while (!TRMT)
{
restart_wdt();
}
// set comm to sync slave receive
SYNC = TRUE;
CSRC = FALSE;
CREN = TRUE;
ReceiveBuffer.Index = 0;
do
{
// Wait for data to be received
while (!RCIF)
{
restart_wdt();
}
receiveddata = get_serial();
_other code_
} while ((receiveddata != 0x0D) &&
(Index < BufferLength));
}
|
The above is NOT fully functional, it is just the main stuff you would need to get started with sync serial setup. The original program ping-ponged messages back and forth, switching between master and slave.
You could set this up with true interrupts, but for my application, it was just waiting for a message, then did something based on the message received.
Hope this helps. |
|