crushneck
Joined: 27 Nov 2005 Posts: 29
|
Question on Pic-Pic master/slave |
Posted: Wed Mar 15, 2006 1:32 pm |
|
|
Hi everyone.
Ive been able to communicate between the two pics but i did not know how to get the the input send from the master PIC. Maybe some amendments need to be done from my slave codes but i did not know the directive to get it work. Here's my codes with some ammendments made by me.
Watchout for some comments i put.
Code: | //master
#include<18f452.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
#define RS485_ID 0x10 // The device's RS485 master address
#define RS485_USE_EXT_INT FALSE
#include<RS485.c>
#include <lCD420.c>
int buffer[40];
int next_in = 0;
int next_out = 0;
//#INT_RDA
void serial_isr()
{
int t;
buffer[next_in] = fgetc(RS485);
t = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=t; // Buffer full !!
}
void main(void)
{
int j;
int data_received[32];
lcd_init();
// enable_interrupts(INT_RDA);
// enable_interrupts(GLOBAL);
rs485_init();
while(1)
{
rs485_wait_for_bus(FALSE);
if(rs485_send_message(0x11, 1, 3))
{
lcd_putc("\fsend 1");// This is the data send. the slave should receive this msg. delay_ms(100);
}
delay_ms(5);
if(rs485_get_message(data_received, TRUE))
{
for(j=0; j<3; ++j)// I do not know what this for, but i think its the basic directive to make the code work?Anyway got it from some forums.
data_received[j] = buffer[j];
data_received[0] = buffer[0];
data_received[1] = buffer[1];
data_received[2] = buffer[2];
data_received[3] = buffer[3];
printf(lcd_putc,"\f%d",data_received[j]);// I really did not understand this part.Is this the correct way?
}
}
} |
heres the slave
Code: | //slave
#include<18f452.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_B4, stream=RS485)
#define RS485_ID 0x11 // The device's RS485 slave address or ID
#define RS485_USE_EXT_INT FALSE // Select asynchronous serial interrupt
#include<RS485.c>
#include <lCD420.c>
int buffer[40];
int next_in = 0;
int next_out = 0;
//#INT_RDA
void serial_isr()
{
int t;
buffer[next_in] = fgetc(RS485);
t = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=t; // Buffer full !!
}
void main(void)
{
int j;
int data_received[32];
lcd_init();
// enable_interrupts(INT_RDA);
// enable_interrupts(GLOBAL);
rs485_init();
while(1)
{
if(rs485_get_message(data_received, TRUE))
{
for(j=0; j<3; ++j)
data_received[j] = buffer[j];
data_received[0] = buffer[0];
data_received[1] = buffer[1];
data_received[2] = buffer[2];
data_received[3] = buffer[3];
printf(lcd_putc,"\f%d",data_received[j]);
}
rs485_wait_for_bus(FALSE);
if(rs485_send_message(0x10, 1, 3))
{
//lcd_putc("\fgot it");
printf(lcd_putc,"\f%d",data_received[0]);// What should i put here so i get the correct msg send from the master?
delay_ms(100);
}
delay_ms(5);
}
} |
I hope u can reply me as soon as possible. Thank u. |
|