homfray
Joined: 19 Nov 2003 Posts: 45 Location: Oxford
|
Communcation between PIC's using wireless RS232 |
Posted: Wed Aug 10, 2005 3:46 am |
|
|
http://www.ccsinfo.com/forum/viewtopic.php?t=23886
Transmitter Code: Code: | // INCLUDE FILES
#include <18F8621.h>
#device *=16 ADC=8
#include <STRING.H>
#include <STDIO.H>
#include <STDLIB.H>
#define Fosc 40000000
//-------------------------------------------------------------------------------------
// DEFINES
#define WireTX PIN_C6
#define WireRX PIN_C7
//-------------------------------------------------------------------------------------
// COMPILER DIRECTIVES and HARDWARE CONFIGURATION
#use delay(clock = Fosc)
#fuses EC_IO // EC Oscillator with RA6 configured as DIO
#fuses NOOSCSEN // Oscillator System Clock Switch Disabled
#fuses NODEBUG // No Background Debugger
#fuses NOLVP // Low Voltage ICSP Disabled
#fuses NOPROTECT // No Code Protect
#fuses NOWDT // No onboard watchdog
#fuses PUT // Power Up Timer Enabled
#fuses BROWNOUT // Brown Out Reset enabled
#use rs232(baud=9600, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)
void main()
{
while(1)
{
fprintf(Wireless, "%c", 0xBA); // LAM - something for the RX's USART to "lock onto"
fprintf(Wireless, "%c", 0xBE); // LAM - something for the RX's USART to "lock onto"
fprintf(Wireless, "%c", 0xFA); // LAM - something for the RX's USART to "lock onto"
fprintf(Wireless, "%c", 0xCE); // LAM - something for the RX's USART to "lock onto"
fprintf(Wireless,"Dave Nice\r"); // I'm going to get the RX code to look for "Dave " as the ID...
// ...and "Rocks" as the command/data
delay_ms(5000);
}
} |
Reciever Code: Code: | #include <18F8621.h>
#device *=16 ADC=8
#define Fosc 40000000
#define WireTX PIN_C6
#define WireRX PIN_C7
#define ConsTX PIN_G1
#define ConsRX PIN_G2
#use delay(clock = Fosc,RESTART_WDT)
#fuses EC_IO, BROWNOUT, BORV20, PUT, STVREN, NOLVP
#use rs232(baud=9600, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)
#use rs232(baud=9600, xmit=ConsTX, rcv=ConsRX, ERRORS, STREAM=Console) //Setup RS232
#define RX_BUFFER_SIZE 80
#define TX_BUFFER_SIZE 80
int8 rx_wr_index = 0, tx_rd_index = 0, tx_wr_index = 0, tx_counter = 0, received = 0;
int8 lock_state = 0, rxd, i, valid_data_count;
unsigned int8 rx_buffer[RX_BUFFER_SIZE + 1], tx_buffer[TX_BUFFER_SIZE + 1];
int1 data_avail = FALSE, got_id = FALSE;
#int_RDA
void RDA_isr(void) {
rx_buffer[rx_wr_index] = getc();
rxd = rx_buffer[rx_wr_index]; // this just makes it easier typing-wise later on
rx_wr_index++;
if (rx_wr_index > RX_BUFFER_SIZE) {
rx_wr_index = 0;
}
// now look for unique ID: "Dave "
if (rxd == 'D' && lock_state == 0) {
lock_state++;
}
else if (rxd == 'a' && lock_state == 1) {
lock_state++;
}
else if (rxd == 'v' && lock_state == 2) {
lock_state++;
}
else if (rxd == 'e' && lock_state == 3) {
lock_state++;
}
else if (rxd == ' ' && lock_state == 4) { // got the entire string "Dave ", in that order
lock_state = 0; // reset our "combination lock"
got_id = TRUE;
valid_data_count = 0xff; // get ready to count the number of data bytes - we know we have to expect 5 (Rocks)
// also going to reset the buffer write index back to 0, so that I know where my valid data will be
rx_wr_index = 0;
}
else { // we didn't receive "Dave ", so reset the lock back to the beginning
lock_state = 0;
}
if (got_id && ++valid_data_count == 5) {
data_avail = TRUE;
got_id = FALSE;
}
}
#int_TBE
void TBE_isr(void) {
if (tx_counter != 0) {
putc(tx_buffer[tx_rd_index]);
if (++tx_rd_index > TX_BUFFER_SIZE) {
tx_rd_index = 0;
}
tx_counter--;
if (tx_counter == 0) {
disable_interrupts(INT_TBE);
}
}
}
void bputc(int c) {
int restart = 0;
while (tx_counter > (TX_BUFFER_SIZE - 1));
if (tx_counter == 0) {
restart = 1;
}
tx_buffer[tx_wr_index++] = c;
if (tx_wr_index > TX_BUFFER_SIZE) {
tx_wr_index = 0;
}
tx_counter++;
if (restart == 1) {
enable_interrupts(INT_TBE);
}
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_ON);
setup_timer_0(RTCC_INTERNAL|RTCC_OFF|RTCC_8_bit);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
enable_interrupts(INT_RDA);
//enable_interrupts(INT_TBE);
enable_interrupts(global);
set_tris_e(0);
fprintf(Console,"working");
while (TRUE) {
restart_wdt();
if (data_avail) {
data_avail = FALSE;
fprintf(Console,"\r\n\r\nData is now available\r\nData: ");
for (i = 0; i < 5; i++) {
fprintf(Console,"%c",rx_buffer[i]);
}
}
}
} |
_________________ Nice!!! |
|