PDA

View Full Version : Strange SerOut Problem



masosi
- 17th April 2007, 11:56
Gday,

I have been programming the 16F628A to control servo's, with instructions being received from a PC. I had mostly everything in working order, and then everything started to deteriorate.

I had SerIn and SerOut in almost perfect working order, with the only problem being the accuracy of all characters. Upon more testing, the quality of the transmissions from the PIC to the PC deteriated. For example, i was receiving the words "Declared Variables" from the PIC without any problems. Then I started receiving "Declared Var******", with the asterisks being random characters. I have now changed this to the alphabet for testing purposes.

As of now, the PC does not acknoweledge any communications from the PIC, as if the serial cable is unplugged.

I have stripped the program down to the basics, as shown below.



PORTA = 0
PORTB = 0
CMCON = 7 ' Set comparators off

include "modedefs.bas"

i var byte
light var PORTB.0

'Startup routine
high light
for i = 0 to 10
toggle light
pause 100
next i
low light

pause 1000
high light
serout PORTA.1,N300,["AbcdefghijklmnopqrstuvwxyZ",10] 'Output lots of data slowly
serout PORTA.1,N300,["AbcdefghijklmnopqrstuvwxyZ",10]
serout PORTA.1,N300,["AbcdefghijklmnopqrstuvwxyZ",10]
serout PORTA.1,N300,["AbcdefghijklmnopqrstuvwxyZ",10]
low light
pause 500

end


The LED flashes as expected, and the LED lights while the PIC is meant to be sending data to the serial port, slowly. Hyperterminal does not receive anything, and neither does my control program written in VB6.

PORTA.1 is connected to the RX pin of the PC's serial port, ground is connected to the PC's serial port too. I have checked this many times, changed pins, changed cables, changed ports and even changed PIC's.

I cannot see any problems with the code above, or the hardware used. The configuration bits i've set are 3F18.

What I can't work out is why the SerOut command WAS working initially, but is not working anymore.

paul borgmeier
- 17th April 2007, 12:06
The configuration bits i've set are 3F18

The internal oscillator of the 628A is not accurate enough for reliable asynchronous transmission - switch to a crystal or resonator and all should be well

masosi
- 17th April 2007, 12:12
Ok, I will try that when I can get my hands on a Resonator.

What i can't understand is that it DID work, and then deteriorated. And even with a new chip, it doesnt work at all anymore.

If anyone has any other suggestions in the meantime, please let me know.
Thanks in advance!

skimask
- 17th April 2007, 12:12
Something else probably changed, input voltage, temperature, shocked the PICs a bit, seasonal changes, humidity, whatever. Apparently, it was running close enough to 4mhz to keep the timing right to work at 300baud. Now the internal oscillator isn't running at 4mhz.
The fact that you said it DID work just fine, then it would work for a few characters before it started sending random characters, and now it doesn't work at all suggests that the framing of the characters is getting farther and farther off the longer you are transmitting.

Try using SEROUT2, with a mode # of 3332 (which is optimal). Then try shifting 3332 up and down a bit and see what happens. Eventually you'll hit on a range of numbers that does seem to work. Take the middle one and use it.

My advice (as well as others I'm sure)...use an external crystal.

mackrackit
- 17th April 2007, 13:43
Have you checked the PC port or the signal from the cable?

masosi
- 17th April 2007, 21:18
I'm using COM1 to send/receive data to the PIC, and i'm also using COM1 to program the PIC (using only one program at a time of course). So there's nothing wrong with the COM port because it programs the PIC fine.

I've tried another computer also, with no different results.

masosi
- 18th April 2007, 23:32
I have PORTA.1 connected directly (with a resistor) to the RX pin on the serial port COM1 (pin 2 if I remember correctly) and using Hyperterminal for the time being.

Do I use 'Driven True' (T300) or 'Driven Inverted' (N300)?

skimask, you suggested Driven True. Is this right for my setup?

Thank again.

paul borgmeier
- 19th April 2007, 00:11
If your pic is directly connected to the PC, you need inverted (idle low)

masosi
- 19th April 2007, 00:12
Thanks Paul.

masosi
- 20th April 2007, 23:49
Try using SEROUT2, with a mode # of 3332 (which is optimal). Then try shifting 3332 up and down a bit and see what happens. Eventually you'll hit on a range of numbers that does seem to work. Take the middle one and use it.

I have tried SerOut2 with the following modes: 3322, 3327, 3332, 3337, 3342 and 3347 (+5 increments).
And then again with the following modes (inverted): 19706, 19711, 19716, 19721, 19726 and 19731.

I would have thought I'd get something on the PC, but still nothing. Is this range too big? or too small? If this doesn't work, the is the resonator really going to fix all this?

skimask
- 21st April 2007, 01:01
I have tried SerOut2 with the following modes: 3322, 3327, 3332, 3337, 3342 and 3347 (+5 increments).
And then again with the following modes (inverted): 19706, 19711, 19716, 19721, 19726 and 19731.

I would have thought I'd get something on the PC, but still nothing. Is this range too big? or too small? If this doesn't work, the is the resonator really going to fix all this?

Probably....but the main thing would be to put some sort of counter on it, 'scope, freq-meter, anything and write a program to see how fast the clock is really going.

Failing that, write a program that counts up in seconds. Let it run for a few hours, compare it with a stopwatch and see how far off it is. Maybe it's actually so far out in the weeds that you can't even get into the ball park.

What happens if you write a program to toggle an LED on and off once per second? Does it actually toggle once per second?

Did you smoke the PC's serial port? Have the right COM1 port selected, etc.?
Simple little things...we all know how it goes...but ya never know..could be something stupid that's tripping you up...

masosi
- 21st April 2007, 01:52
I'll try those ideas soon, thanks.

Another thought...
I'm running the 16F628A at 5.0V DC. After looking at the electrical information, it seems that I should be running it at 3.5V. This would run the internal oscillator at a more precise speed. Is this correct?

Please make sure I'm getting this right.
5V supply... 3.5V drop across the PIC... Voltage drop of 1.5V... PIC runs at 150mA
R = V / I
R = 1.5 / .15
R = 10 ohms (1/4 watt)

skimask
- 21st April 2007, 01:53
I'll try those ideas soon, thanks.
Another thought...
I'm running the 16F628A at 5.0V DC. After looking at the electrical information, it seems that I should be running it at 3.5V. This would run the internal oscillator at a more precise speed. Is this correct?
Please make sure I'm getting this right.
5V supply... 3.5V drop across the PIC... Voltage drop of 1.5V... PIC runs at 150mA
R = V / I
R = 1.5 / .15
R = 10 ohms (1/4 watt)

What electrical information is telling you to run it at 3.5v?
If you're talking about parameter F13, that's for 'jitter' not 'accuracy'.

masosi
- 21st April 2007, 02:02
This graph...
Correct?

skimask
- 21st April 2007, 02:03
This graph...
Correct?

That -1% @ 5v isn't going to kill your serial.
Also, it's say 'TYPICAL'...doesn't really mean 'yours'.

masosi
- 21st April 2007, 02:08
Ah... well thats another desperate idea gone...

I'll try the timing trials.

masosi
- 21st April 2007, 06:50
I've tried the circuit with the Crystal (4MHz), everything seems to be the same...

Attached is the circuit diagram, could someone please check it to see what I am doing wrong please???

The LED blinks if the serial port is not connected to the computer.
The LED doesn't blink if the serial port IS connected to the computer, so I'm thinking its a grounding problem. Any suggestions?

This is the code that I am testing with:



PORTA = 0
PORTB = 0
CMCON = 7 'Set 16F628A Comparators off

Include "modedefs.bas"
light var PORTB.0
Low light

Start:
Toggle light
SerOut PORTA.1,N300,["Pulse",10,13]
Pause 100
goto Start

High light
end

skimask
- 21st April 2007, 06:57
I've tried the circuit with the Crystal (4MHz), everything seems to be the same...

Attached is the circuit diagram, could someone please check it to see what I am doing wrong please???

The LED blinks if the serial port is not connected to the computer.
The LED doesn't blink if the serial port IS connected to the computer, so I'm thinking its a grounding problem. Any suggestions?

This is the code that I am testing with:



PORTA = 0
PORTB = 0
CMCON = 7 'Set 16F628A Comparators off

Include "modedefs.bas"
light var PORTB.0
Low light

Start:
Toggle light
SerOut PORTA.1,N300,["Pulse",10,13]
Pause 100
goto Start

High light
end


Ya, there's a few things wrong with that.
Check your little green book...page 131, 136, 139, 144. You might find your problem/answer there.

paul borgmeier
- 21st April 2007, 07:11
You show VSS = -5V on your sketch. It should be VSS = 0V

ALso, If you have nothing between your +5.0V power supply and your PIC, then you should measure +5.0V between VDD and VSS on your PIC (not 3.5V).

EDIT: it sounds like a grounding problem just like you suspected

masosi
- 21st April 2007, 07:13
I've tried with and without a resistor on the Rx and Tx Pin, there's no difference.

Does it make sense that the serial port seems to be sucking the life out of the circuit...?

masosi
- 21st April 2007, 07:20
You show VSS = -5V on your sketch. It should be VSS = 0V

ALso, If you have nothing between your +5.0V power supply and your PIC, then you should measure +5.0V between VDD and VSS on your PIC (not 3.5V).

EDIT: it sounds like a grounding problem just like you suspected

Well by -5V, I mean the negative side of the power supply.
This is the power supply I'm using (pretty cheap and nasty, but I thought it would do the job (it did for a while)).
http://www.jaycar.com.au/productView.asp?ID=MP3144&CATID=&keywords=switchmode+power&SPECIAL=&form=KEYWORD&ProdCodeOnly=&Keyword1=&Keyword2=&pageNumber=&priceMin=&priceMax=&SUBCATID= - Model MP-3144 (the first one listed).
...I don't know what switchmode means...

skimask
- 21st April 2007, 07:21
I've tried with and without a resistor on the Rx and Tx Pin, there's no difference.

Does it make sense that the serial port seems to be sucking the life out of the circuit...?

No, but it makes sense that without the resistor, you probably smoked all of your ports.

But...back to the timing thing. The LED blinks, ok... Back to basics...
Do you have an LCD? Make a counter, one second, power up the PIC, start your stopwatch. Wait for 100 seconds. If your watch and the PIC/LCD match up after 100 seconds, you can be reasonably sure you're within 1-2% of the 4Mhz clock speed. The longer you go, the more accurately you can count it up.

skimask
- 21st April 2007, 07:23
Well by -5V, I mean the negative side of the power supply.
This is the power supply I'm using (pretty cheap and nasty, but I thought it would do the job (it did for a while)).
http://www.jaycar.com.au/productView.asp?ID=MP3144&CATID=&keywords=switchmode+power&SPECIAL=&form=KEYWORD&ProdCodeOnly=&Keyword1=&Keyword2=&pageNumber=&priceMin=&priceMax=&SUBCATID= - Model MP-3144 (the first one listed).
...I don't know what switchmode means...

Switchmode...hmmmm...maybe there's not enough load on the circuit to turn on the regulator in the PSU, which is sometimes needed for those things. I'd try a battery pack next, even a 9v battery with a 7805 will do for awhile.

P.S. - quick lesson in PSU's

Linear power supply - you put in 10v, you want to draw 5v @ 1 amp. The regulator dumps 5v @ 1 amp overboard as heat and saves the other 5v for your output. In this case, it's only 50% efficient.

Switchmode - you put in 10v, you want to draw 5v @ 1 amp. The 'switcher' basically turns a MOSFET on and off really fast (sometimes as high as a few Mhz) thru an inductor to keep an output cap charged up to 5v. Sometimes these types can hit about 98% efficiency, very little wasted heat. Drawbacks are that you have to draw a little bit to keep the regulator regulating and they introduce a little bit of noise on the output (a very little bit, usually not enough to mess with anything).

masosi
- 21st April 2007, 07:34
No, but it makes sense that without the resistor, you probably smoked all of your ports.
I'm using the same COM port to program the PIC using JDM2 serial programmer and WinPic - there are no errors there, so the COM port should be fine!?


But...back to the timing thing. The LED blinks, ok... Back to basics...
Do you have an LCD? Make a counter, one second, power up the PIC, start your stopwatch. Wait for 100 seconds. If your watch and the PIC/LCD match up after 100 seconds, you can be reasonably sure you're within 1-2% of the 4Mhz clock speed. The longer you go, the more accurately you can count it up.
I just did a Pause command of 2mins, timing it showed up as 2mins 2secs. Is this too far out?

masosi
- 21st April 2007, 07:38
Switchmode...hmmmm...maybe there's not enough load on the circuit to turn on the regulator in the PSU, which is sometimes needed for those things.

So what do you suggest for a permanant setup for this kind of thing? What type of wall adapter power supply? (Not battery).

skimask
- 21st April 2007, 07:40
I'm using the same COM port to program the PIC using JDM2 serial programmer and WinPic - there are no errors there, so the COM port should be fine!?

But all of your PICs might not be! Or at least none of the ports you've hooked up to. Maybe not just PortA.1, but all of PortA, heck for that matter, maybe the whole chip. I don't know why...just maybe...


I just did a Pause command of 2mins, timing it showed up as 2mins 2secs. Is this too far out?

Nah, I'd chalk that amount of error up to you not clicking quite right on time, maybe a couple ms here and there in the program, etc.etc., ya know, all those things adding up. I'd say your clock is good within say 1-3%, far close enough for 300 baud, and probably up to 2400 baud.

masosi
- 21st April 2007, 07:43
But all of your PICs might not be! Or at least none of the ports you've hooked up to. Maybe not just PortA.1, but all of PortA, heck for that matter, maybe the whole chip. I don't know why...just maybe...
Ok, I'll get my hands on another chip and give that a go.


Nah, I'd chalk that amount of error up to you not clicking quite right on time, maybe a couple ms here and there in the program, etc.etc., ya know, all those things adding up. I'd say your clock is good within say 1-3%, far close enough for 300 baud, and probably up to 2400 baud.
Well thats good news!

skimask
- 21st April 2007, 07:44
Ok, I'll get my hands on another chip and give that a go.


Well, what I really meant was to give PortB a try.

masosi
- 21st April 2007, 07:46
Well, what I really meant was to give PortB a try.

Ok, will do shortly.

Another thought: Is it a good thing or a bad thing that this power supply doesn't have a ground (earth) to mains??
DC shouldn't have earth, should it?

skimask
- 21st April 2007, 07:47
Ok, will do shortly.

Another thought: Is it a good thing or a bad thing that this power supply doesn't have a ground (earth) to mains??
DC shouldn't have earth, should it?

You mean a ground lug, like the 3rd pin? If so, that's a chassis ground. If something shorts out inside the case of the PSU, the juice returns through that line. I wouldn't worry about it too much in a wall wart type power supply.

masosi
- 21st April 2007, 07:49
You mean a ground lug, like the 3rd pin? If so, that's a chassis ground. If something shorts out inside the case of the PSU, the juice returns through that line. I wouldn't worry about it too much in a wall wart type power supply.

Very good, thanks.

masosi
- 21st April 2007, 07:50
Would it be worth trying a 350W Computer PSU?
Are it's outputs (5VDC & 12VDC) regulated?

skimask
- 21st April 2007, 07:56
Would it be worth trying a 350W Computer PSU?
Are it's outputs (5VDC & 12VDC) regulated?

More trouble than it's worth. Get a NiMH battery pack and a charger. Smaller, more controllable, etc. And those computer PSU's DO require a minimum load and you have to do some trickery to them to get them to work.

masosi
- 21st April 2007, 08:00
More trouble than it's worth. Get a NiMH battery pack and a charger. Smaller, more controllable, etc. And those computer PSU's DO require a minimum load and you have to do some trickery to them to get them to work.

Ok, thanks again.


I can see a LED flash on PORTA.1 and now PORTB.5 when it does SerOut, so I guess the ports are fine.
So if its not timing, not the programming, not the chip, not the COM port and not Hyperterminal - I DON'T KNOW WHAT TO DO!!!

paul borgmeier
- 21st April 2007, 08:43
1) Are you sure you have pin 2 correct and pin 5 correct (you are using a 9 pin connector that is straight thru?) see http://www.airborn.com.au/rs232.html

2) Try it without TX connected

3) A must - add a decoupling capacitor across VDD and VSS (10 uF should do or anything close to this)

4) You do have pin 5 connected to your PIC ground?

5) try another power supply that can be regulated (or 3 AA batteries in series)

6) Do you have access to an oscilloscope? see what is coming out of PortA.1 and pin 2 on the cable?

7) Check your hyperterminal settings to make sure you have 300 selected and the parity off

8) you have been using a crystal like in your sketch?

report back

masosi
- 21st April 2007, 09:06
1) Are you sure you have pin 2 correct and pin 5 correct (you are using a 9 pin connector that is straight thru?) see http://www.airborn.com.au/rs232.html
Correct



2) Try it without TX connected
Will do again - didn't make a difference last time



3) A must - add a decoupling capacitor across VDD and VSS (10 uF should do or anything close to this)

Will do



4) You do have pin 5 connected to your PIC ground?

Yes



5) try another power supply that can be regulated (or 3 AA batteries in series)
Will do



6) Do you have access to an oscilloscope? see what is coming out of PortA.1 and pin 2 on the cable?
I don't have one unfortunately - an LED flashes like mad though...



7) Check your hyperterminal settings to make sure you have 300 selected and the parity off
Have checked that many times!



8) you have been using a crystal like in your sketch?

4MHz with capacitors as drawn

masosi
- 21st April 2007, 12:58
My computer won't talk to a serial modem anymore... and it won't echo its own data from Tx to Rx either...
That explains it!

Then a new question pops up... How was the serial JDM2 programmer verifying the PIC's contents? (I know the PIC was being programmed successfully too!)

Anyway, I'll get my hands on a new set of serial ports tomorrow :)

Skimask and Paul, thankyou so much for your help so far! I've learnt alot even though the ideas havn't solved my problem! Thanks again!

mackrackit
- 21st April 2007, 13:44
In post 5 I asked about the PC port, and you said that the same thing happened on another computer. ?????

As far as the programmer software reading back correctly, all I can say is it happens. I have a PICKIT1 that will read back errors if I use the software that microchip supplied, but mplab does not. Both software work. I would guess in this case the "lesser" software does not have something correct in its receiving side, like a checksum miss calc.

Again, check the signal with a meter. TX will show low (Maybe even a negative value) when idle and will spike anywhere from 2.5 to 25 V+ at transmit. If you have a machine less than 10 years old or using a usb/serial adpt, you will most likely see 2.5 to maybe 6.

masosi
- 21st April 2007, 13:48
In post 5 I asked about the PC port, and you said that the same thing happened on another computer. ?????
I know! I checked it and it was fine, and the other computer behaved the same... I must have stuffed that up! (Not sure how though!)

masosi
- 23rd April 2007, 06:06
The new serial ports let everything behave as it should!
I wish I knew the port was dead 2 weeks ago!!!

Thanks again everyone, very good help :)

Regards,
Tom.