Author Topic: PIC USART communication problem  (Read 21723 times)

0 Members and 1 Guest are viewing this topic.

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2252
  • Country: ca
Re: PIC USART communication problem
« Reply #25 on: March 19, 2014, 01:25:49 pm »
I agree with the above, you must check always for framing and other Rx errors.

I always first suspect the baud rate. Try setting BRG one step higher or lower, especially if you see framing errors. Try using a scope to measure the actual baud/bit rate coming out of each device and compare. You must be within 4% error (so each side no more than 2% error high/low from the desired rate). An error of just 5% doesn't sound like much, but consider there are ten bits in each frame (start, data*8, stop), so the tenth bit in each frame will be more 50% out of phase. Then one receiving side will likely see frequent errors. Often when the baud rate error is marginal, only one side sees framing errors.

Personal anecdote: I recently set up a terminal server (box with Ethernet and a bunch of network-accessible serial ports) and was getting strange errors talking to many devices. I scoped the signals to the baud rate and saw it was about 4 % low at any baud rate setting. The other device was maybe 1 % high. This caused errors. The fix was easy, just replace a 25 MHz oscillator with a (custom made) 26 MHz one, forcing the baud rate to be always 4% higher. It is now spot-on and can talk error free to everything I have plugged into it.

I just saw your post on Parity. Parity is most often not used. You can only use it if the other device does too, and it very very likely does not. There is an extra data bit sent as parity. For PICs, you need to calculate the bit based on your data, then set the (9th) bit in the appropriate register before writing to TXREG. On the receive side, you extract the parity (9th) bit before reading from RXREG, then calculate desired parity from the data bits and compare. Don't concern yourself with this as your device almost certainly does not use it.
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #26 on: March 19, 2014, 01:33:17 pm »
Your voltage on the GSM Tx PIN (PIC RX) looks suspect.  Are you trying to interface a 3.3V (or 2.8V?) device with your PIC at 5V?
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #27 on: March 19, 2014, 01:58:00 pm »
and tie RX to TX. Insert a long delay like 500ms in the main loop - don't set b in the main loop.

If you observe B1 blinking, your code is working.

Tying RX to TX doesn't tell you if the baud rate is what you intend, it will work even with non-standard rates.

To the OP:

There's absolutely no reason you can't use the internal oscillator (HFINTOSC) at 8 MHz for 9600 baud.  If you check the datasheet for the 16F887 for example, you will find that the internal oscillator is specified to +/- 2% over 0 C to 85 C.  This gives a maximum error of ((8.16MHz / (64*(12+1)))-9600)/9600 = 2.16%.  Assuming you are sitting at room temperature it will be much better.  Maximum allowable will be around 4.5% for 8bits+parity+stop bit.  Your issue is not about crystals, believe me.

 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC USART communication problem
« Reply #28 on: March 19, 2014, 02:03:22 pm »
18f4520 - has 4xPLL, other 2 chips don't have it.
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #29 on: March 19, 2014, 02:19:51 pm »
18f4520 - has 4xPLL, other 2 chips don't have it.

True, but why does that matter?  The 16F887 has 8 MHz without 4xPLL, and the 16F628A has 4 MHz.  No reason you need a 32 MHz oscillator to use a UART at 9600.
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC USART communication problem
« Reply #30 on: March 19, 2014, 03:05:07 pm »
18f4520 - has 4xPLL, other 2 chips don't have it.

True, but why does that matter?  The 16F887 has 8 MHz without 4xPLL, and the 16F628A has 4 MHz.  No reason you need a 32 MHz oscillator to use a UART at 9600.
I know that.  You know that.  The O/P might not know that.
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #31 on: March 19, 2014, 03:16:02 pm »
18f4520 - has 4xPLL, other 2 chips don't have it.

True, but why does that matter?  The 16F887 has 8 MHz without 4xPLL, and the 16F628A has 4 MHz.  No reason you need a 32 MHz oscillator to use a UART at 9600.
I know that.  You know that.  The O/P might not know that.

Good point!   ;D
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #32 on: March 19, 2014, 06:05:38 pm »
I have PLL disabled.

About 2.78 V on Rx pin - I actually started this project using Arduino Uno and it was a complete success. GSM was the same one I'm using with PIC, Sony Ericsson T610. I've read on arduino forums, that this GSM model should support 5 V Tx/Rx.

Quote from: Arduino forums
It seems as new Sony Ericsson phones need a TX/RX voltage of somewhere about 2,6V to get it working. If you use one of the listed older phones you should get it working with the 5V TX/RX from the Arduino.

T series :
T39, T68, T68i, T230, T250, T610, T630, T650i

I've never used Atmel chip by itself, so I don't know if there is a difference, but I measured voltages on Arduino again today and they are the same as with PICs.
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #33 on: March 19, 2014, 06:26:31 pm »
In your PIC 18F4520 datasheet, pg. 335, you will note that the spec for VIH says 0.8 VDD on Schmitt Trigger inputs.  Your UART RX pin is a Schmitt Trigger input.  That means you need 5V * 0.8 = 4V for the high level, so your GSM phone isn't driving it high enough.

Anyhow, even if someone on the Arduino forums talks about interfacing with phones at 2.6V with the (presumably) 5V Arduino, that doesn't mean that is really OK.  You shouldn't be driving the RX pin on the phone with 5V from your UART if it is using 2.6V logic.  Sometimes pins are tolerant, but you want to check that out for sure.  (My guess would be no, the phone isn't rated for 5V on it's RX pin).

Edit: Not intending to come across harshly.  The Arduino forums are filled with bad ideas from an EE perspective, so just keep that in mind.

Hope this helps you solve your problem.
« Last Edit: March 19, 2014, 06:31:57 pm by granz »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC USART communication problem
« Reply #34 on: March 19, 2014, 06:31:44 pm »
The voltage on rx pin reflects its duty cycle.
================================
https://dannyelectronics.wordpress.com/
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #35 on: March 19, 2014, 06:34:28 pm »
The voltage on rx pin reflects its duty cycle.

That is a good point, but I assume the OP measured the voltages when no data was being transmitted.  Yes?

Edit:  I re-read where you said you measured the voltages "during reception."  It's hard to know what you really measured.  Since you don't have a scope, check the level on the TX pin from the phone without any communication going on.

« Last Edit: March 19, 2014, 06:47:38 pm by granz »
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #36 on: March 19, 2014, 08:11:49 pm »
If I have my GSM hooked up to PIC, the TX voltage on the GSM is the same as if the GSM is disconnected from PIC - 2.78 V. RX voltage on the GSM is 5 V when hooked up and 0 V when disconnected from PIC.

I did observe the voltage when I have triggered the transmission and there was a slight (approx. 200 mV) voltage drop on GSM TX / PIC RX pin. But that probably doesn't tell you anything because multimeters refresh rate is to slow.
 

Offline qno

  • Frequent Contributor
  • **
  • Posts: 422
  • Country: nl
Re: PIC USART communication problem
« Reply #37 on: March 19, 2014, 08:27:31 pm »
Maybe try a Pull-up resistor?
Why spend money I don't have on things I don't need to impress people I don't like?
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #38 on: March 19, 2014, 08:47:59 pm »
If I have my GSM hooked up to PIC, the TX voltage on the GSM is the same as if the GSM is disconnected from PIC - 2.78 V. RX voltage on the GSM is 5 V when hooked up and 0 V when disconnected from PIC.

I did observe the voltage when I have triggered the transmission and there was a slight (approx. 200 mV) voltage drop on GSM TX / PIC RX pin. But that probably doesn't tell you anything because multimeters refresh rate is to slow.

Right.  So that 2.78V is definitely a problem for you, since the PIC requires the HIGH level on the RX pin to be > 4V.  The normal UART setup is both lines idle HIGH.  The receiver does just that, receives, and doesn't set the level on that line, it is set by the opposite TX output.  That is exactly why you see 2.78V on the TX pin on the phone and 0V on the RX pin, until you connect the PIC, which then drives that line to 5V.

You are getting your interrupt when the PIC RX line goes LOW the first time, but it never goes high enough again to be detected by the PIC, due to it's Schmitt Trigger input.

You need a level shifter.  You don't want to just pull that line high with a resistor to your 5V, because then you will be back-driving the output of the GSM phone and current will flow into the output.  A level shifter can be made simply with one transistor and resistor because of the low speed in this case.  The PIC has a bit which can be set/cleared to invert the UART logic.
 

jucole

  • Guest
Re: PIC USART communication problem
« Reply #39 on: March 19, 2014, 09:13:20 pm »
In my last PIC project I used a RS232 Bluetooth Module and just used a pullup.



 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #40 on: March 19, 2014, 09:59:33 pm »
In my last PIC project I used a RS232 Bluetooth Module and just used a pullup.

That diagram is showing 5V TTL to 5V CMOS.  The issue in that case is that TTL is based on BJTs, where CMOS is, well, MOSFETs.  Those systems are both at 5V.  Not the same thing here.  Yes, you can sometime get away with a pull-up, but it's not the right way to do it.  If you use simply a pull-up, you have current flowing through the body diode on the output transistor of the transmitter (GSM phone).  If your project with the Bluetooth module is also at 3.3V you are doing the same thing in that case.  This is bad idea.

If you don't believe me, have a look at this discussion on the TI forums:

http://e2e.ti.com/support/microcontrollers/msp430/f/166/t/60581.aspx

To the OP: why not just run your PIC at 3.3V (or 2.7V?), which will avoid these issues.
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #41 on: March 19, 2014, 10:12:29 pm »
I'm gonna try both - 3.3 V circuit and level shifter. Maybe even a pullup, just for fun. :)

By the way, I really appreciate everyone's help.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC USART communication problem
« Reply #42 on: March 20, 2014, 12:04:08 am »
I would try the pull up first.
================================
https://dannyelectronics.wordpress.com/
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #43 on: March 20, 2014, 09:39:12 am »
I'm looking at a Voltage - Frequency graph on page 324.

Can I even run this chip from 3.3 V?
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: PIC USART communication problem
« Reply #44 on: March 20, 2014, 10:44:41 am »
Wicket... I'm looking at a Voltage - Frequency graph on page 324.
Can I even run this chip from 3.3 V?

PIC18LF2420/2520/4420/4520 - INDUSTRIAL
Yes but only if clocked at 10MHz or lower...
The other chip variants don't seem to be so accommodating
Don't ask a question if you aren't willing to listen to the answer.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC USART communication problem
« Reply #45 on: March 20, 2014, 11:00:33 am »
Quote
Can I even run this chip from 3.3 V?

Put in a couple of diodes on the 5v supply line and you will find.

Yes, it will - there is considerable room for tolerance for the specs.
================================
https://dannyelectronics.wordpress.com/
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #46 on: March 20, 2014, 12:32:26 pm »
AMEN! It's finally working! You were right, I replaced my 5 V regulator with 3.3 V one and it started blinking exactly as it should. 2,78 V was clearly not enough for logical high @ 5 V. I first tried it using 3.6864 MHz crystal, but it even works with internal osc. @ 4 MHz.

Oh and just so you know, I also tried using pullup @ 5 V, but it didn't help. I can finally move on now, it took me 10 days to get this right, jesus...

Anyway, thanks to everyone!  :clap:
 

Offline granz

  • Regular Contributor
  • *
  • Posts: 136
  • Country: us
  • 6.62606957
Re: PIC USART communication problem
« Reply #47 on: March 20, 2014, 07:58:18 pm »
AMEN! It's finally working! You were right, I replaced my 5 V regulator with 3.3 V one and it started blinking exactly as it should. 2,78 V was clearly not enough for logical high @ 5 V. I first tried it using 3.6864 MHz crystal, but it even works with internal osc. @ 4 MHz.

Oh and just so you know, I also tried using pullup @ 5 V, but it didn't help. I can finally move on now, it took me 10 days to get this right, jesus...

Anyway, thanks to everyone!  :clap:

Glad to hear you got it working!
 

Offline Skimask

  • Super Contributor
  • ***
  • Posts: 1433
  • Country: us
Re: PIC USART communication problem
« Reply #48 on: March 20, 2014, 08:03:37 pm »
AMEN! It's finally working! You were right, I replaced my 5 V regulator with 3.3 V one and it started blinking exactly as it should. 2,78 V was clearly not enough for logical high @ 5 V. I first tried it using 3.6864 MHz crystal, but it even works with internal osc. @ 4 MHz.

just so you know, I also tried using pullup @ 5 V, but it didn't help. I can finally move on now, it took me 10 days to get this right, jesus...

Anyway, thanks to everyone!  :clap:
meh...don't feel too bad.  You aren't the first one to get bit by this sort of thing.  (eg. ST vs TTL vs CMOS inputs/outputs, etc.)
I didn't take it apart.
I turned it on.

The only stupid question is, well, most of them...

Save a fuse...Blow an electrician.
 

Offline wicketTopic starter

  • Contributor
  • Posts: 35
Re: PIC USART communication problem
« Reply #49 on: March 21, 2014, 07:22:18 am »
What would be the best way to boost up the 2.78 V signal from GSM? My entire circuit is based on 5 V (relays, sensors, other chips) and it's kinda awkward for me to use 3.3 V now. Can I use the level shifter just for this signal? Are this things fast enough though?
« Last Edit: March 21, 2014, 07:25:29 am by wicket »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf