View previous topic :: View next topic |
Author |
Message |
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
send integer between two pic rs232! |
Posted: Tue May 15, 2012 4:30 am |
|
|
Hello everyone !
i'm working on send integer between two pic16f877a, i know that is a simple topic and the codes are easy, but still don't work so i decided to ask you maybe there is something missing!!!
here are the codes,and thanks 4 reading:
transmitt pic:
#include <16F877a.h>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1)
#include<lcd420_b.c>
void main() {
int valor=0;
lcd_init();
while(1){
for (valor=0;valor<=10;valor++) {
PUTC(valor);
delay_us(20);
printf(lcd_putc,"\frecibiendo=%d\n",valor);//display the virtual sent value yo lcd
delay_ms(100);
}
}
}
reveicer pic:
#include <16F877a.h>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1)
#include<lcd420_b.c>
int valor=0;
#int_RDA
RDA_isr()
{
if(kbhit()){
valor=getc();
}
}
void main()
{
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(1) { lcd_gotoxy(1,1);
printf(lcd_putc,"receiver=%d\n",valor);
}
} |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue May 15, 2012 5:06 am |
|
|
? What DOESN'T 'work' !
The transmitter program or the receiver program ?
Have you confirmed that the LCD and LCD driver functions correctly(aka does 'Hello World' program work?).
Do you have a MAX232 or equal on both PICs UARTs pins ?
You must add 'errors' to the use rs232(...) lines. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 15, 2012 5:18 am |
|
|
Several remarks:
1) When posting code please use the 'code' tags around your program; select your program text and press the code button. This ensures the formatting is preserved and makes for easier reading.
2) Always add the 'ERRORS' keyword to the #use RS232 line. The hardware UART will stop normal operation when the receiver buffer overflows until the error condition is cleared. The ERRORS keyword will have the compiler add code to remove the error condition on each call to getc.
3) Calling kbhit inside the RDA_isr is useless. The interrupt will only fire when data is ready for reading, so calling kbhit will always return TRUE.
4) Why do you want to use interrupts? Your code will be a lot easier without them.
My suggestion is to make the receiver program simpler by removing the interrupt code:
Code: | #include <16F877a.h>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1, ERRORS)
#include<lcd420_b.c>
int valor=0;
void main()
{
lcd_init();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
lcd_gotoxy(1,1);
printf(lcd_putc, "Start");
while(1) {
valor=getc(); // Will block until a character is received.
lcd_gotoxy(1,1);
printf(lcd_putc, "receiver=%d\n", valor);
}
} |
If this doesn't work then:
- Post what you see happening; do you see the texts change at both the transmitter and receiver?
- Explain your connection between the PICs, what components and how long are the wires? Do you have a common ground wire?
- Post your compiler version number. |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Tue May 15, 2012 5:52 am |
|
|
temtronic: The lcd works. I tried it alone and it's perfect, and yeah I added errors without results!!
ckielstra: After I select the program where is the code button!!
I make code without the rs interrupt but the same results. I just connect the tx pin (c6) of the transmitter to the (c7) of the receiver and of course the common ground wire. The transmitter pic shows me that the value of the xx is changing like i want (from 0 to 10). While in the receiver code i put the word "hello" before the getc() and the lcd on pic receiver shows me just "Hello" like it never receive data!!!!
(Sorry for this but I don't find the code button!!!)
while(1) { printf(lcd_putc, "\fHello");
delay_ms(100);
valor=getc(); // Will block until a character is received.
lcd_gotoxy(1,1);
printf(lcd_putc, "receiver=%d\n", valor);
delay_ms(100);
} |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Tue May 15, 2012 8:23 am |
|
|
There is a list of button such as: Quote, Code, List, List==, Img under the subject bar when you make a post.
In my opinion, first you should try to communicate with PC, to check whether your pic really transmit out something, and also for you to learn more about RS232. If you already done that, then try to transmit from your PC to your pic, so you learn how to write code to receive something. After that, you would have more ideas on how to communicate between two pics.
Also, you could try this in the receive pic:
Code: | while (true)
{
c = getc();
lcd_gotoxy(1,1);
printf(lcd_putc,"%c",c);
} |
Hope it helps. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue May 15, 2012 9:55 am |
|
|
Your serial connection is wrong if I read your reply right.
You MUST use some kind of 'inverter' IC, typically a MAX232 nowadays, in order to effect the RS232 'standard'.
Hooking up your transmitter PIC to a PC running a terminal program will quickly show what's going on.... |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Tue May 15, 2012 10:17 am |
|
|
hoangkhuong: I make few months ago... connection between pic and pc and was successful. I'm gonna retest it on it why not!!!
temtronic: Should I have to use max232 between two pic for serial communication ? I know that max232 is just for ttl devices and computer!!!?? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue May 15, 2012 2:09 pm |
|
|
The Max232 type chips invert on the transmit and invert again on the receive. Going from a TTL PIC to another TTL PIC you get 2 inversions, so you can do without BOTH of them. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed May 16, 2012 2:13 am |
|
|
It depends on how far you are going.
Two PIC's talking directly to one another, will work fine without any buffer chips, _provided_ the units are close together. However go further, and add noise, and buffering becomes needed. What buffering is down to you. Ideally probably, use a lower voltage signalling standard like RS485 - buffers are smaller than 232 buffers, give longer transmission distances, at higher speeds, and using a differential 'pair' gives noise immunity better than RS232. Or for lower cost, RS423, to avoid needing extra wires. The choices are almost infinite.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed May 16, 2012 6:05 am |
|
|
Ttelmah is correct. I was assuming the PICs are close and share a good common ground. Standards like RS232 and RS485 are there for good reasons. Which to use and why is a lengthy subject. But if you have two PICs on your desk running off of the same power supply, a direct TTL connection should work fine. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 16, 2012 7:36 am |
|
|
Summarizing:
TTL to TTL communication is common practice but lengths should be limited and depend on environmental noise, cable quality and baudrate. 19200 baud over 3m length is a working figure, faster possible on shorter lengths. Longer lengths are possible but advice is to use one of the other mentioned standards (RS-232, RS-485, etc.).
When either side of the communication is using RS-232 voltages then the other side needs an RS-232 driver too, for example a MAX232 chip. When connecting a PIC to your PC you always need an RS232 driver.
Back to the original topic:
You really have to find out where the problem is. Is the transmitter really sending _correct_ data? Or is the receiver not receiving?
For tips on how to debug this see the CCS manual chapter 'Why is the RS-232 not working right?', page 339. |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Fri May 18, 2012 8:04 am |
|
|
The distance that I want is short (few cm)... now:
I make 2 circuits pic hardware and for each one I tried if it works or not. So I sent an integer and receive it (connecting the tx to rx directly of each pic together) and i realized that the two pic send and receive because the two works alone. Now when I connect them (tx to rx directly)...it doesn't work even if I make the same code to the two (code that I tested before and worked). Now I don't know what to try else!!!
P.S: I read that getc() and putc() send an 8 bit integer data.. and if you want to send 16 bit you have to put (long_data) in the #users232. So why sending 16 bit and receiving 16 bit works without that I write long_data on #rs232?!??!? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri May 18, 2012 8:09 am |
|
|
You need a ground wire between the boards, as well as the data line...
Best Wishes |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Fri May 18, 2012 8:18 am |
|
|
yeah i know that...so I grounded the two board and the voltage of the pic is the same ..so the first pic is 5.01 as the second... and now i realized that it always receive data.. so it likes the if(kbhit()) is always true (i put the kbhit()) inside the while(1) and not in the isr(). |
|
|
karimpain
Joined: 15 Feb 2012 Posts: 39 Location: italia
|
|
Posted: Sat May 19, 2012 10:29 am |
|
|
Nothing!!! I don't know what's wrong, like I said each pic can receive data from itself (so the code and the hardware are right) but can't communicate each other!!! (I'm going try all the baudrate). |
|
|
|