|
|
View previous topic :: View next topic |
Author |
Message |
khanh my
Joined: 01 Apr 2013 Posts: 13
|
uart multi slave (pic16f887) |
Posted: Mon Apr 01, 2013 11:19 am |
|
|
hello everybody!
I'm from vietnam, I not good English
I use uart communication connection to one master and two slave, the but two slave can not send data to the master
Thanks to everyone for checking my code
code master
Code: | #include <16f887.h>
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
int8 a, b, value[4],n,flag1=0, flag2=0;
#INT_RDA
void RDA_isr (void)
{
clear_interrupt(INT_RDA);
disable_interrupts(GLOBAL);
putc(1);
delay_ms(100);
putc('s');
delay_ms(100);
putc(2);
delay_ms(100);
putc('l');
delay_ms(100);
value[n]=getchar();
if(value[n]=='@')
{
flag1=1;
}
else if(value[n]=='#')
{
flag2=1;
}
else n++;
enable_interrupts(GLOBAL);
}
void main()
{
//set_tris_c(0xff);
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
delay_ms(500);
n=0;
while(true)
{
if(flag1==1)
{
flag1=0;
lcd_putcmd(line_1);
printf(lcd_putchar,"slave1 send%2u ",value[0]);
}
if(flag2==1)
{
flag2=0;
lcd_putcmd(line_1);
printf(lcd_putchar,"slave2 send%2u ",value[0]);
}
}
} |
code slave 1
Code: | #include <16f887.h>
#device adc=10
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
int8 a,value[8],n,flag;
#INT_RDA
void RDA_isr (void)
{
clear_interrupt(INT_RDA);
disable_interrupts(GLOBAL);
value[n]=getchar();
if(value[n]=='s')
{
putc(1);
delay_ms(100);
putc('@');
//delay_ms(100);
}
else n++;
enable_interrupts(GLOBAL);
}
void khoitaouart()
{
setup_uart(9600);
}
void main()
{
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
khoitaouart();
delay_us(1000);
set_timer0 (100);
n=0;
while(true)
{
lcd_putcmd(line_1);
printf(lcd_putchar,"master send%3u ",value[0]);
lcd_putcmd(line_2);
printf(lcd_putchar,"send master%2u ",1);
}
} |
code slave2
Code: | #include <16f887.h>
#device adc=10
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
#byte TMR0=0x01
int8 a,value[8],n, flag;
#INT_RDA
void RDA_isr (void)
{
clear_interrupt(INT_RDA);
disable_interrupts(GLOBAL);
value[n]=getchar();
if(value[n]=='l')
{
putc(2);
delay_ms(100);
putc('#');
delay_ms(100);
}
else n++;
enable_interrupts(GLOBAL);
}
void khoitaouart()
{
setup_uart(9600);
}
void main()
{
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
khoitaouart();
delay_us(1000);
set_timer0 (100);
n=0;
while(true)
{
lcd_putcmd(line_1);
printf(lcd_putchar,"master send%3u ",value[2]);
lcd_putcmd(line_2);
printf(lcd_putchar,"send master%2u ",2);
}
} |
thanks for everyone's help
Last edited by khanh my on Mon Apr 01, 2013 9:50 pm; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Apr 01, 2013 5:21 pm |
|
|
You need to help us to help you.
1) Reduce your code to the minimum possible.
2) Show us your hardware set up.
Mike
EDIT This should be in General discussion section NOT code library! |
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Mon Apr 01, 2013 9:48 pm |
|
|
Mike Walne wrote: | You need to help us to help you.
1) Reduce your code to the minimum possible.
2) Show us your hardware set up.
Mike
EDIT This should be in General discussion section NOT code library! |
Sorry for the confusion, thanks mod move this posts helped me
This code minimalist extreme, I want 2 slave to send data to the master, here is the second slave will send No. 1 and No. 2 on the master, simple as that |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 01, 2013 10:43 pm |
|
|
Can I ask a question ? Why do you put in lines like this (in bold, below) ?
I don't think there are any examples that ever say the lines in bold
are a good idea. There are many posts on this forum where people do
this, and we always tell them it's a mistake.
My question is, why did you put in the enable/disable global interrupts
and all the delay_ms(100) statements ? Who told you to do this ?
Quote: |
#INT_RDA
void RDA_isr (void)
{
clear_interrupt(INT_RDA);
disable_interrupts(GLOBAL);
putc(1);
delay_ms(100);
putc('s');
delay_ms(100);
putc(2);
delay_ms(100);
putc('l');
delay_ms(100);
value[n]=getchar();
if(value[n]=='@')
{
flag1=1;
}
else if(value[n]=='#')
{
flag2=1;
}
else n++;
enable_interrupts(GLOBAL);
} |
|
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Mon Apr 01, 2013 11:35 pm |
|
|
PCM programmer wrote: | Can I ask a question ? Why do you put in lines like this (in bold, below) ?
I don't think there are any examples that ever say the lines in bold
are a good idea. There are many posts on this forum where people do
this, and we always tell them it's a mistake.
My question is, why did you put in the enable/disable global interrupts
and all the delay_ms(100) statements ? Who told you to do this ?
Quote: |
#INT_RDA
void RDA_isr (void)
{
clear_interrupt(INT_RDA);
disable_interrupts(GLOBAL);
putc(1);
delay_ms(100);
putc('s');
delay_ms(100);
putc(2);
delay_ms(100);
putc('l');
delay_ms(100);
value[n]=getchar();
if(value[n]=='@')
{
flag1=1;
}
else if(value[n]=='#')
{
flag2=1;
}
else n++;
enable_interrupts(GLOBAL);
} |
|
when I delete the line in bold type, still can not solve the problem
data transport invalid when my delete Function delay_ms (100)
code master
Code: | #include <16f887.h>
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
int8 a, b, value[4],n,flag1=0, flag2=0;
#INT_RDA
void RDA_isr (void)
{
putc(1);
putc('s');
putc(2);
putc('l');
value[n]=getchar();
if(value[n]=='@')
{
flag1=1;
}
else if(value[n]=='#')
{
flag2=1;
}
else n++;
}
void main()
{
//set_tris_c(0xff);
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
delay_ms(500);
n=0;
while(true)
{
if(flag1==1)
{
flag1=0;
lcd_putcmd(line_1);
printf(lcd_putchar,"slave1 send%2u ",value[0]);
}
if(flag2==1)
{
flag2=0;
lcd_putcmd(line_1);
printf(lcd_putchar,"slave2 send%2u ",value[0]);
}
}
} |
code slave1
Code: | #include <16f887.h>
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
int8 a,value[8],n,flag;
#INT_RDA
void RDA_isr (void)
{
value[n]=getchar();
if(value[n]=='s')
{
putc(1);
putc('@');
}
else n++;
}
void khoitaouart()
{
setup_uart(9600);
}
void main()
{
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
khoitaouart();
delay_us(1000);
set_timer0 (100);
n=0;
while(true)
{
lcd_putcmd(line_1);
printf(lcd_putchar,"master send%3u ",value[0]);
lcd_putcmd(line_2);
printf(lcd_putchar,"send master%2u ",1);
}
} |
code slave 2
Code: | #include <16f887.h>
#fuses hs,nowdt,noput
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8)
#include <stdio.h>
#include <lcd_lib_4bit.c>
#byte TMR0=0x01
int8 a,value[8],n, flag;
#INT_RDA
void RDA_isr (void)
{
value[n]=getchar();
if(value[n]=='l')
{
putc(2);
putc('#');
}
else n++;
}
void khoitaouart()
{
setup_uart(9600);
}
void main()
{
set_tris_d(0x00);
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
khoitaouart();
delay_us(1000);
set_timer0 (100);
n=0;
while(true)
{
lcd_putcmd(line_1);
printf(lcd_putchar,"master send%3u ",value[2]);
lcd_putcmd(line_2);
printf(lcd_putchar,"send master%2u ",2);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Apr 02, 2013 12:38 am |
|
|
What you are not saying is how the connections are made?.
RS232, is a one to one connection. Only one 'master' and one 'slave'.
It can be partially used as a one to multi connection (with several devices listening to one line), but even this is out of spec. This won't allow any form of talk back though, since there cannot be more than one 'driver' on the bus.
To have 'master slave' comms, where the slave can reply, needs a bus like RS485, with an extra control signal from each device to say 'I want to talk' (the enable line), which turns on the transmitting buffer so the device can talk to the line. There is no sign of this in your code.
You need to re-think your hardware, before worrying about the software....
Best Wishes |
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Tue Apr 02, 2013 12:48 am |
|
|
Ttelmah wrote: | What you are not saying is how the connections are made?.
RS232, is a one to one connection. Only one 'master' and one 'slave'.
It can be partially used as a one to multi connection (with several devices listening to one line), but even this is out of spec. This won't allow any form of talk back though, since there cannot be more than one 'driver' on the bus.
To have 'master slave' comms, where the slave can reply, needs a bus like RS485, with an extra control signal from each device to say 'I want to talk' (the enable line), which turns on the transmitting buffer so the device can talk to the line. There is no sign of this in your code.
You need to re-think your hardware, before worrying about the software....
Best Wishes |
yes!I'm done rs485 connection, you can give me sample code for this? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Apr 02, 2013 1:01 am |
|
|
1) Search the forum.
2) Look in the examples. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 02, 2013 2:19 am |
|
|
Taken a long time for you to get round to telling us.
Quote: | yes!I'm done rs485 connection, you can give me sample code for this? |
And yes, CCS provides EX_RS485.C for you to try.
Mike |
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Tue Apr 02, 2013 6:49 am |
|
|
Mike Walne wrote: | Taken a long time for you to get round to telling us.
Quote: | yes!I'm done rs485 connection, you can give me sample code for this? |
And yes, CCS provides EX_RS485.C for you to try.
Mike |
in the ex_rs485.c example there is no mention of the slave or master??? who can help me complete the above example?
slave 1 sends 1 to master
slave 2 send 2 to the master
master two of them on screen display lcd
please!! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Apr 02, 2013 7:25 am |
|
|
Hi,
Could you please take a photo of your hardware, and then post it on-line for us to take a look at? Once posted, send us a link to the file location (eg. photobucket, etc.). I think that getting a look at your hardware will give us all the necessary information on how to proceed with this thread!
Thanks,
John |
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Tue Apr 02, 2013 8:34 am |
|
|
ezflyr wrote: | Hi,
Could you please take a photo of your hardware, and then post it on-line for us to take a look at? Once posted, send us a link to the file location (eg. photobucket, etc.). I think that getting a look at your hardware will give us all the necessary information on how to proceed with this thread!
Thanks,
John |
My hardware is
[/img][img]
by the way I asked if communication rs485 without using max485 or max487 or any one of anything else?
I just cross-connection 2 pins TX and RX[/img] |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Apr 02, 2013 8:57 am |
|
|
CCS provides both programs and schematic for 'multiPIC' setup in their Help files. Look in the FAQ or Q&A section.
You can connect only 2 PIC by jumpering TX1 to RC2, RC1 to TX2.In this configuration, if NOT using MAX232 or similar,you usually add 'invert' to the USE RS232(.....options....).
RS-485 is one well known, highly used 'standard' for interfacing several PICs in a network.As previously stated, CCS provides example programs for this as well.
hth
Jay |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Apr 02, 2013 9:22 am |
|
|
Hi,
Your picture file(s) seem to be missing. Can you check them and get back to us?
No, you need something like a MAX485 device to successfully implement RS-485 communications. These chips allow multi-drop communications over a pair of wires. You need to add 'Enable =' to your #use rs232 declaration, and you need to dedicate a pin on each PIC to control the direction of data on the RS485 bus.
John |
|
|
khanh my
Joined: 01 Apr 2013 Posts: 13
|
|
Posted: Tue Apr 02, 2013 9:22 am |
|
|
temtronic wrote: | CCS provides both programs and schematic for 'multiPIC' setup in their Help files. Look in the FAQ or Q&A section.
You can connect only 2 PIC by jumpering TX1 to RC2, RC1 to TX2.In this configuration, if NOT using MAX232 or similar,you usually add 'invert' to the USE RS232(.....options....).
RS-485 is one well known, highly used 'standard' for interfacing several PICs in a network.As previously stated, CCS provides example programs for this as well.
hth
Jay |
hardware as I have posted, you can use rs485 or uart? |
|
|
|
|
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
|