|
|
View previous topic :: View next topic |
Author |
Message |
zydis
Joined: 10 Aug 2015 Posts: 3
|
rs232 fault when receiving response |
Posted: Mon Aug 10, 2015 6:52 am |
|
|
Hi guys I need your help about rs232 communication, I try to communicate 2 pic18f67k22 using rs232 com. and max487 component.
1. I send the send something to slave obtain data that have
2. Slave can understand that is for reading
3. Then slave send response to master
4. Master should get the data but I cant get anything from slave side
All my code is below and can you tell me that where is the fault!
MASTER
Code: |
#include <18f67k22.h>
#INCLUDE <stdlib.h>
#use delay(clock=8000000)
#fuses INTRC_IO,NOWDT,PROTECT,NOPUT,NOMCLR
#fuses CCP2C1
#fuses SOSC_DIG
int8 indata_S1=0, indata_S2, veri[8];
int gonder=0,sayac=0;
//a[6],b[6],c[6],d[6],f[6],g[6];
#pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM=S_1, UART1)
#pragma use rs232(baud=9600, xmit=PIN_G1, rcv=PIN_G2, STREAM=S_2, UART2)
#INT_rda // for slave communication // I cant get the response here
void rda_interupt ()
{
disable_interrupts(int_rda); //
indata_S1=getc(S_1);
veri[sayac]=indata_S1; // store the indata in veri[]
if(indata_S1 == 'e') // if comming data is end
{
if(veri[sayac-1]=='b') // if coming id is b
{
if(veri[sayac-2]==10) // if data = 10
{
output_toggle(pin_e0); // I cant see the change here .
}
}
sayac=0;
}
sayac=sayac+1;
indata_S1=0;
delay_us(2);
enable_interrupts(int_rda);
}
#INT_rda2
void rda2_interupt ()
{
}
delay_us(2);
enable_interrupts(int_rda2);
}
void veriiste(char x)
{
output_high(pin_c5);
delay_ms(5);
putc(x,S_1); // mode write or read
delay_ms(5);
putc('b',S_1); // id which pic do you want to reach
delay_ms(5);
putc('e',S_1); // end bit
delay_ms(5);
output_low(pin_c5);
}
void main()
{
setup_psp(PSP_DISABLED); // PSP birimi devre dışı
setup_spi(SPI_SS_DISABLED); // SPI birimi devre dışı
enable_interrupts(GLOBAL);
output_low(pin_c5);
output_low(pin_g3);
enable_interrupts(int_rda);
enable_interrupts(int_rda2);
delay_ms(100);
while(1)
{
delay_ms(1000);
veriiste('r'); // send daha as read mode
delay_ms(1000);
veriiste('w'); // send daha as write mode
}
} |
SLAVE
Code: |
#include <18f67k22.h>
#INCLUDE <stdlib.h>
#use delay(clock=8000000)
#fuses INTRC_IO,NOWDT,PROTECT,NOPUT,NOMCLR
#fuses CCP2C1
#fuses SOSC_DIG
int8 indata,veri[10],sensor[6]={10,20,30,40,50,60};
int gonder=0,sayac=0;
#pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM = S_1)
////////////////////////////////////////
void verigonder() // I can send the data response here
{
output_high(pin_c5);
delay_ms(10);
putc(10,S_1); // send data
delay_ms(1);
putc('b',S_1); // send own id
delay_ms(1);
putc('e',S_1); // send end data
delay_ms(10);
output_low(pin_c5);
}
#INT_rda
void rda_interupt () // I can get the all data regular
{
disable_interrupts(int_rda); // haberleşeme kesmesi pasif edildi
indata=getc(S_1);
veri[sayac]=indata;
if(indata=='e') // if end comes
{
if(veri[sayac-1]=='b') // if id is matched that pic have
{
if(veri[sayac-2]=='r') // if coming data is read mode
{
output_high(pin_e0); // to see the behaviour of data
verigonder(); // if coming data says that it is for read, then send response
indata=0;
veri[sayac-1]=0;
veri[sayac-2]=0;
}
if(veri[sayac-2]=='w')
{
output_low(pin_e0);
indata=0;
veri[sayac-1]=0;
veri[sayac-2]=0;
}
}
sayac=0;
}
indata=0;
sayac=sayac+1;
delay_us(2);
enable_interrupts(int_rda);
}
////////////////////////////////////////////
////////////////////////////////////////
void main()
{
setup_psp(PSP_DISABLED); // PSP birimi devre dışı
setup_spi(SPI_SS_DISABLED); // SPI birimi devre dışı
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
enable_interrupts(GLOBAL);
enable_interrupts(int_rda);
output_low(pin_c5);
delay_ms(100);
while(TRUE)
{
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Aug 10, 2015 7:05 am |
|
|
First get rid of the pragma declarations.
These are used in some (most) versions of C to force a statement to execute 'before' other stuff. CCS accepts them, but does not use them, and needs things that must be done first to be placed first in the code.
Then there is a nasty problem with your array access. If you receive 'e', at the start of the message, it'll try to access veri[-1]. Uurgh. You need to think when using anything like this, and ensure that the indexes will always be inside the array.
Then get rid of the disable_interrupt (and enable) in int_rda. The _hardware_ disables all interrupts (in the code as used), when you are inside an interrupt handler.
Then get rid of the delay in the interrupt handler. This will cause interrupts to be disabled in all delays in the main code. Ugh.
Then there is a big problem with INT_RDA2. This potentially will hang the chip. If INT_RDA2 triggers, this will be called, and will never exit. Problem is because you do not read the character the interrupt cannot be cleared.
Then SPI_SS_DISABLED, _enables_ the SPI with slave select disabled. The syntax to disable SPI, is setup_spi(FALSE).
I gave up at this point.... |
|
|
|
|
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
|