View previous topic :: View next topic |
Author |
Message |
angel
Joined: 19 Oct 2004 Posts: 40
|
problems with USART and printf!!! |
Posted: Tue Oct 19, 2004 4:51 am |
|
|
Hi
I send a message from my computer to te PIC and the PIC send the same message to my computer. The problem is that the answer from the PIC is sometimes nothing, sometimes half message and sometimes the answer is good. Around 50 % good answers, 25 % half answers and 25 % no answer. I use a PIC 16F872, 73A or 76 and the behaviour is always the same.
Between my PIC and my PC I use a MAX232 CPE. It works correctly.
I tested these thansmition with a quarz 4Mhz and 20 Mhz. With 4 Mhz there wasn't answer for any transmission speed. The best score was using a quartz of 20 Mz with a speed of 38400. Does anyone know what happen?
Maybe my source of voltage is not strong!!! it seems that my PIC haven't force to make sometimes the answer!! I use 5 V from the port USB.
Thanks for your answer |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Oct 19, 2004 6:25 am |
|
|
Code?????? |
|
|
angel
Joined: 19 Oct 2004 Posts: 40
|
code |
Posted: Thu Oct 21, 2004 5:19 am |
|
|
Hi
The code is simple:
#include <16F873A.H>
#fuses HS,NOWDT,NOPROTECT
#use fast_io(C)
#use delay(clock=20000000)
#define RS232_T67 PIN_C6 // (output) RS232 serial transmit
#define RS232_R67 PIN_C7 // (input) RS232 serial receive
#use rs232(baud=38400, xmit=RS232_T67, rcv=RS232_R67,errors)
void main() {
char message[5];
set_tris_c(0b10000010);
while(1) {
if (kbhit()) {
gets(message);
printf("%s hello world\n",message);
delay_ms(10); // Allow UART to finish?????????????
}
}
/////////////////////////////////////////////////////
Example :
I send from my computer the message '111'
and I receive '111 hello world' 50% of times.
111 hel 25% . This message change......
Nothing 25% |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Thu Oct 21, 2004 6:17 am |
|
|
Are you using a manual terminal program on the PC like Hyperterm or are you using your own application?
If you are using an application try adding a carriage return character by changing the printf statement to Code: | printf("%s hello world\r\n",message); |
You do not need the delay statement.
Have you meansured the + (pin 2) and - (pin 6) voltages at the MAX232 chip? From +5v they should be about +9.5 and -9.5 volts respectively. You mentioned you are picking up power from the USB connector - I assume it is from the same PC. Have you measured this voltage?
Disconnect the serial cable to the PC but leave the USB installed so you circuit is powered up. With the serial cable disconnected measure the voltage between you 0 volt rail and pin 5 (ground) of the DB9 serial cable from the PC to make sure it is 0V.
If you have not already done so, then if you implement this type of cicuit in the future where you power your target from the PC, instead of connecting the RS232 pin 5 to ground directly on the target, use a 10 ohm resistor to prevent you forming a (relatively) low impedance ground loop. |
|
|
Ttelmah Guest
|
|
Posted: Thu Oct 21, 2004 7:46 am |
|
|
The first thing to realise, is that 'kbhit', only means one character has been received. Hence what happens, is that as soon as you type '1', the gets command is called. Now this then waits receiving characters, till it sees a 'carriage return' code. The kbhit is therefore basically pointless. The next question, is how your terminal program is setup. Many will be set to send 'line feed/carriage return', when the 'enter' key is pressed. On this basis, your '111' code, would be received as:
'1'
'1'
'1'
LF
CR
0x0
The gets function, appends a zero to the string as a terminator. If this is what is happening, then your input storage area for 'message', is too small (since this will need six characters to store). It may be as simple as this buffer overflowing. Try with a bit more space (say 8 characters).
Best Wishes |
|
|
angel
Joined: 19 Oct 2004 Posts: 40
|
I found the solution |
Posted: Wed Oct 27, 2004 6:55 am |
|
|
I found the solution.
I changed an electrolytics capacitor and it works correctly.
Thanks for your help |
|
|
densimitre
Joined: 21 Dec 2004 Posts: 45
|
Re: I found the solution |
Posted: Sun Apr 17, 2005 9:05 pm |
|
|
angel wrote: | I found the solution.
I changed an electrolytics capacitor and it works correctly.
Thanks for your help |
Angel,
Cual es el valor del capacitor que cambiaste?
Modificaste tu codigo?
Gracias |
|
|
angel
Joined: 19 Oct 2004 Posts: 40
|
solution |
Posted: Mon Apr 18, 2005 9:31 am |
|
|
hello
I found a solution changing an electrolitics capacitor but it was by chance.
The problem was in my earth. It was a bad earth.
So conecting my hand to my earth my problems disapeared!!!
If you have the same problem check your system working with your hand over your earth. |
|
|
|