|
|
View previous topic :: View next topic |
Author |
Message |
Futterama
Joined: 17 Oct 2005 Posts: 98
|
RS232 problem, first char is garbage |
Posted: Wed Nov 02, 2005 1:46 am |
|
|
Hello forum,
Now I'm testing the RS232 capability of the CSS compiler.
My problem is that when sending one char repeately, the first char is garbage.
Another problem is that I can't send a string with printf.
My code:
.h file:
Code: | #include <16F88.h>
#device adc=8
#fuses NOWDT,INTRC_IO, NOPUT, NOMCLR, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT, NOFCMEN, NOIESO
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B5,bits=9,invert) |
.c file:
Code: | #include "myfile.h"
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
while(1)
{
putc('A');
delay_ms(300);
}
} |
The first char sent is always superscript 3.
Then I tried with printf:
Code: | while(1)
{
printf("A");
delay_ms(300);
} |
But this gives me the exact same output.
If I try to send a string with printf:
Code: | while(1)
{
printf("ABCDEFG");
delay_ms(300);
} |
Nothing is recieved when sending a string with printf.
What am I doing wrong?
Thanks.
Regards,
Futterama |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 02, 2005 3:32 am |
|
|
I'd imagine the gabage, is because the line is floating as an input when the chip wakes up, and only switches to being 'driven', when the RS232 message starts.
I'd suggest adding:
Code: |
output_low(PIN_B3);
//since you are using 'invert', the idle state is low
delay_ms(2);
|
After the 'setup_vref' line.
The printf, as far as I can see, should work!.
Do you really want 9bit comms?. Remember you should set the value for the ninth bit in the 'RS232_ERRORS' variable, before sending the data, otherwise you are 'relying' on this having been cleared by the compiler. Personally, I'd probably suggest using the 'long_data' directive in the use RS232 line, and working only through getc/putc, and prepare string data with sprintf, if you want 9bit comms.
What compiler version are you using?.
Best Wishes |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Wed Nov 02, 2005 5:59 am |
|
|
Code: | output_low(PIN_B3);
delay_ms(2); |
This code works, it eliminates the garbage char. Actually I did try this without the 2ms delay, and that didn't help.
Regarding the 9bit, I thought it was a way to tell the compiler to add a stopbit. There is no visible difference between using 9bit and 8bit.
I only want to send 8bit data with 1 stopbit.
I'm sending the data to Hyper Terminal for testing, later it will be sent to a Qbasic program running on an old laptop for data collection.
Now, I want to send 4 digits to the laptop and it seems like I need some 40ms delay between each putc to get all the chars. It's acceptable cause it's only sent a few times per minute. But why?
Edit: If i set it to 8bit data, I don't need the 40ms delay anymore
Everything I need seems to work OK. Even printf works after changing to 8bit data. DOH me
Thanks for your help Ttelmah. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Nov 02, 2005 7:35 am |
|
|
If you are using an RS232 converter, remember that they have a built-in charge pump
power supply to generate +V and -V step-up voltages.
Your code is transmiting the first character after a few miliseconds after power up and
the charge pump didn�t reach it�s expected value.
Work around: add a delay at the begginning of your code previous to the main().
Also in your directives you are using NOPUT (??)
Humberto |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Wed Nov 02, 2005 7:59 am |
|
|
I'm not using any converter, and NOPUT was generated by the wizard, I don't know what it does...
Edit: Oh, NOPUT, Power Up Timer, this one delays program execution so the supply will have time to stabalize, right? |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Nov 02, 2005 8:12 am |
|
|
Quote: |
I'm sending the data to Hyper Terminal for testing
|
Quote: |
I'm not using any converter
|
Ok, let us know how do you connect the PIC Tx Rx pins to the PC Com.
Humberto |
|
|
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Wed Nov 02, 2005 8:30 am |
|
|
Humberto wrote: | Ok, let us know how do you connect the PIC Tx Rx pins to the PC Com. |
this should be connected the right way, because only the first character is garbage. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Nov 02, 2005 8:48 am |
|
|
Quote: |
this should be connected the right way, because only the first character is garbage
|
Nop. I�m not talking if it was connected in the wrong way or not, I�m talking
regarding the voltage converter needed to connect a microcontroller (TTL levels)
to a PC Com (RS232 levels).
Humberto |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
RS232 vs asynch serial (again) |
Posted: Wed Nov 02, 2005 6:13 pm |
|
|
see here for my concerns regarding "RS232": http://www.ccsinfo.com/forum/viewtopic.php?t=25018
Then: If you are not using a level changer (eg MAX232) chip, this explains why you are using 'invert' in your #use and it is still working. The level changer chips normally invert for you and this is generally a 'standard', but... without one, the PC is driving -12V into your PIC pin for a logic high, which will be bending the body diodes in the pic and probably shifting ground negative for your whole circuit. A real no-no! Also, for logic low, (+12V), your VDD rail will be having current pushed into it by the PC.
Do you have a resistor and diode on your RX line? Resistor to limit the current and diode to clip out any negative swing to prevent what I have described above? I think CCS mention this as a minimalist way of interfacing a PIC directly to a PC, but it is a real [censored] way of doing it and would never be used in practise. I view this as just a 'quick tryout'! (I think some 'build your own' Nokia dataleads a few years back did this!)
For transmitting back to the PC, the data received by the PC could be undefined because it is seeing transitions from 0 to +5v. I'm not sure what the actual RS232 standard thresholds are (you could look them up) but I think the PC would be seeing either no transitions, or just the odd few. I think RS232 actually looks for zero crossings +ve to -ve with some hysteresis. This is the whole point of the level shifting. It can be transferred over a long(ish) distance and volt-drop doesn't matter within reason because it is looking at two polarities, rather than set thresholds.
Anyway, I'm going on a bit here. I think what I have mentioned will be causing problems with your data, not the PIC or the code! |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Wed Nov 02, 2005 6:21 pm |
|
|
Also, I have found hyperterminal the most unreliable bit of comms software ever written! I think it has problems managing its buffers properly. I have had serious problems sending raw data files to a PIC before using hyperterminal! large chunks of data were missing and I actually blamed my receive ISR/buffer code, simply because mine was being prototyped and hyperterminal was released!
I found a bit of freeware to send the files to my PIC, and it worked very reliably and 10 times faster!!!!
I would use the 'SIOW' test program bundled with CCS. It even has indicators for all the handshake and modem lines so you can see what the port is doing. I think CCS have produced a winner there!
Having said this, try actually talking to the PC in its own language (eg RS232) first, then possibly blame hyperterminal!
Neil. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Thu Nov 03, 2005 12:13 am |
|
|
Well, actually I connect the COM port with the PIC the very wrong way, no diodes and no resistors (I know all about the serial line voltages and so on, I was just lazy and it still works).
Thank you all for your replies. I'll make sure I use a RS232 converter from now on to reduce the risk of errors.
Regards,
Futterama |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Thu Nov 03, 2005 3:23 am |
|
|
Futterama, you cannot afford to be 'lazy' with situations like this, especially if you know all about them! If you do things this way, you can expect to have problems! |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Nov 03, 2005 11:10 am |
|
|
Quote: |
Well, actually I connect the COM port with the PIC the very wrong way, no diodes and no resistors (I know all about the serial line voltages and so on, I was just lazy and it still works).
|
I must admit: I�m tired to crash against a wall with comments like this.
I will ovoid a beginners inquires that do not include:
1) Compiler version. (3.xxx format)
2) Full header.
3) Short and compilable code that demostrate the problem.
4) Short and concise hardware description.
Humberto |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Thu Nov 03, 2005 1:54 pm |
|
|
neil wrote: | Also, I have found hyperterminal the most unreliable bit of comms software ever written! |
AMEN!
every time i read a post starting with "I am using Hyperterminal..." i know that there is an RS232 debugging novice at the wheel. you simply can't do anything useful with Hyperterminal and moreover half the time it is flat out lying to you.
TeraTerm is a million times better. plus you can ssh with it as well.
http://www.tunl.duke.edu/~canon/Help/ttssh.html
http://www.ayera.com/teraterm/
jds-pic |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Thu Nov 03, 2005 6:15 pm |
|
|
Ohhhh, I'm glad you said that!
I'm not a professional programmer by any stretch, and my immediate reaction is to blame my code and/or methods, when (serial) things go wrong, so this is reassuring!
I'll have a serious look at teraterm, now you mention it. I have got by with other freeware, so far, but it's always good to get other suggestions!
Maybe some of us should have more faith in our code than windows gives us!
Its good to exchange these sort of views on here!
Cheers,
Neil. |
|
|
|
|
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
|