|
|
View previous topic :: View next topic |
Author |
Message |
afede
Joined: 12 May 2008 Posts: 4
|
three pics communication |
Posted: Fri May 16, 2008 4:33 am |
|
|
Hi,
i want to communicate three pics.first pic will send data1 to second pic.second pic will receive data1,then will compare with data2 (second pic's own data) and after this comparison will send result to third pic.
first pic transmit code:
Code: |
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
#use rs232 (baud=600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
int16 tmrSayac = 0;
int1 portDurum = 0;
int32 ArabaVarZaman_ms = 0;
int32 ArabaYokZaman_ms = 0;
int1 arabaVar = true;
int16 arabaSayac = 0;
int16 KontrolSuresi = 0;
char data;
//***********************ANA PROGRAM********
void main()
{
enable_interrupts(int_RB);
enable_interrupts(int_timer1);
enable_interrupts(global);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
setup_timer_1(T1_internal | T1_div_by_8);
set_timer1(0);
set_tris_b(0x10);
while(1)
{
}
}
#int_RB
void RBkesme()
{
if(input(pin_b4))
{
arabaVar = true;
ArabaYokZaman_ms = ((tmrSayac * 524288)/1000 + (get_timer1()*8) / 1000);
set_timer1(0);
tmrSayac = 0;
arabaSayac++;
}
else
{
arabaVar = false;
ArabaVarZaman_ms = ((tmrSayac * 524288)/1000 + (get_timer1()*8) / 1000);
if(ArabaVarZaman_ms>=6000)
{putc('c');} //'d'=çok yoğun
if(ArabaVarZaman_ms>=3000 && ArabaVarZaman_ms<=6000)
{putc('b');}
if(ArabaVarZaman_ms<=3000)
{putc('a');}
set_timer1(0);
tmrSayac = 0;
}
}
#int_timer1
void tmr1Kesme()
{
tmrSayac++;
}
|
second pic transmit code:
Code: |
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
#use rs232 (baud=600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//*********************DEĞİŞKENLER*********************
int16 tmrSayac = 0;
int1 portDurum = 0;
int32 ArabaVarZaman_ms = 0;
int32 ArabaYokZaman_ms = 0;
int1 arabaVar = true;
int16 arabaSayac = 0;
int16 KontrolSuresi = 0;
char data;
char data_1;
char data_2;
char x;
void byte_yolla(data)
{
putc(0x55);
putc(0x55);
putc(0x55);
putc(0x55);
putc(0x55);
putc(0x00);
putc(0x00);
putc(0x00);
putc(0x00);
putc(0x00);
putc(0xff);
putc(0xff);
putc(0xff);
putc(0xff);
putc(0xff);
putc(data);
putc(data);
putc(data);
}
//***********************ANA PROGRAM********
void main()
{
enable_interrupts(int_RB);
enable_interrupts(int_timer1);
enable_interrupts(global);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
setup_timer_1(T1_internal | T1_div_by_8);
set_timer1(0);
set_tris_b(0x10);
while(1)
{
enable_interrupts(int_rda);
byte_yolla(data);
}
}
#int_RB
void RBkesme()
{
if(input(pin_b4))
{
arabaVar = true;
ArabaYokZaman_ms = ((tmrSayac * 524288)/1000 + (get_timer1()*8) / 1000);
set_timer1(0);
tmrSayac = 0;
arabaSayac++;
}
else
{
arabaVar = false;
ArabaVarZaman_ms = ((tmrSayac * 524288)/1000 + (get_timer1()*8) / 1000);
if(ArabaVarZaman_ms>=6000)
{
if(data_1=='a')
{data='i';}
if(data_1=='b')
{data='l';}
if(data_1=='c')
{data='o';}
}
if(ArabaVarZaman_ms>=3000 && ArabaVarZaman_ms<=6000)
{
if(data_1=='a')
{data='h';}
if(data_1=='b')
{data='k';}
if(data_1=='c')
{data='n';}
}
if(ArabaVarZaman_ms<=3000)
{
if(data_1=='a')
{data='g';}
if(data_1=='b')
{data='j';}
if(data_1=='c')
{data='m';}
}
set_timer1(0);
tmrSayac = 0;
}
}
#int_timer1
void tmr1Kesme()
{
tmrSayac++;
}
#int_rda
void rs232_kesme()
{
output_high(pin_c0);
x=getc();
if(x=='a')
{data_1='a';}
if(x=='b')
{data_1='b';}
if(x=='c')
{data_1='c';}
disable_interrupts(int_rda);
}
|
third pic receive code:
Code: |
#include <16F877A.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
#include <lcd.c>
#use rs232 (baud=600, xmit=PIN_C6, rcv=PIN_C7)
char x;
int say1=0;
int say2=0;
void read()
{ x=getc();}
void fivebyte1()
{
while(1)
{
if(getc()==0xff)
{say2++;
if(say2>=5)
{say2=0;
read();
break;}
}
else {say2 = 0;
break;}
}
}
void fivebyte0()
{
while(1)
{
if(getc()==0x00)
{ say1++;
if(say1>=5)
{say1=0;
fivebyte1();
break;}
}
else say1 = 0;
}
}
void main()
{
setup_timer_2(T2_DISABLED,0,1);
setup_timer_1(T1_DISABLED);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
lcd_init();
while(1)
{
fivebyte0();
switch(x)
{
case 'g':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL AKICI ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL AKICI ");
delay_ms(1000);
break;
case 'h':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL AKICI ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL YOGUN ");
delay_ms(1000);
break;
case 'i':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL AKICI ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL COK YOGUN ");
delay_ms(1000);
break;
case 'j':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL AKICI ");
delay_ms(1000);
break;
case 'k':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL YOGUN ");
delay_ms(1000);
break;
case 'l':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL COK YOGUN ");
delay_ms(1000);
break;
case 'm':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL COK YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL AKICI ");
delay_ms(1000);
break;
case 'n':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL COK YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL YOGUN ");
delay_ms(1000);
break;
case 'o':
lcd_gotoxy(1,1);
printf(lcd_putc, "1.YOL COK YOGUN ");
lcd_gotoxy(1,2);
printf(lcd_putc, "2.YOL COK YOGUN ");
delay_ms(1000);
break;
}
}
}
|
i know,codes are so long but i must do it
i tried comm. only between first and second pic and comm. only between second and third pic.it worked regularly.but three pics together can't communicate.i think i have problem in rs232 settings. must i add sth to #use rs232 () part ? for ex. stream...
if u help me,i'll be satisfied...i must solve this problem quickly...
thnx |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: three pics communication |
Posted: Fri May 16, 2008 5:47 am |
|
|
You don't need to define any other serial port in the second PIC. One serial port is OK if you only need to receive from the first PIC and send to the third PIC.
You said the 1->2 worked and 2->3 worked but 1->2->3 does not work. Try checking out the second PIC by using a terminal emulator, like hyperterminal, to replace the other two PICs. Then you will not be debugging three PICs at once. Divide and conquer.
Robert Scott
Real-Time Specialties |
|
|
afede
Joined: 12 May 2008 Posts: 4
|
|
Posted: Fri May 16, 2008 7:04 am |
|
|
Quote: |
Try checking out the second PIC by using a terminal emulator, like hyperterminal, to replace the other two PICs. Then you will not be debugging three PICs at once. Divide and conquer.
|
sory i cant understand how to check out with hyperterminal, and what does replacing other two pics mean? i dont know how to use hyperterminal... |
|
|
Indy
Joined: 16 May 2008 Posts: 24
|
|
Posted: Fri May 16, 2008 7:38 am |
|
|
afede wrote: | i dont know how to use hyperterminal... | HyperTerm
Quote: | and what does replacing other two pics mean? | - Remove PIC2.
- Insert PC with terminal program.
- Watch incoming data from PIC1 on PC
- Type data for PIC3 on PC
This way you can figure out which of the three systems is at error. Always when you don't see the cause of a problem try to divide the system into smaller sub-systems. Check which sub-system is at error and than again divide that sub-system into smaller units. Continue making smaller until you find the error.
A hint for your code: In PIC2 do not disable interupt int_rda in the interrupt handler. You do enable the interrupt again in the main loop but if function byte_yolla() is executing this takes too long and you are missing data. |
|
|
afede
Joined: 12 May 2008 Posts: 4
|
|
Posted: Fri May 16, 2008 8:00 am |
|
|
but pic2 is not only a bridge between pic1 and pic3 when rb4 changes pic2 produces other results and combine with data received from pic1, so when i remove pic2, system will be confused i think |
|
|
|
|
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
|