Tuesday 9 December 2014

does PIC require MAX232 for uart?


Sorry for the stupid question but, I'm interfacing a PIC18f46k20 to an Atlas pH sensor module. Both are running on 3.3v. Must I use a max232 between or can the pic talk 232 directly to the module?


The reason I'm asking is it seems in the datasheet (pic) that the baud rate generator doesn't generate the 38400 required by the sensor board so I'm thinking the interface chip (max232) would correct for that somehow? Using the formula on page 249, I'm running at the default internal 1MHz and I come up with a negative value for SPBRGH:SPBRG. Or does the autobaud account for this?



Answer



All a MAX232 provides is level translation.


"Traditional" RS-232 uses high-voltages to do it's signaling, generally ~+10V to indicate a logical "0", and ~-10V to indicate a logic "1" (though the spec technically says anything > 3V = 0, and < -3V = 1. In real-world applications, you may see a range of signaling levels represented as "RS232").


However, most modern devices do not have the facilities onboard to generate or handle these (relatively) high voltages. As such, things like your PIC will use 0V and VCC for their signalling levels, representing logical "0" and "1" respectively.


What the MAX-232 does is convert an input level of 0V to an output of ~+10V, and an input of VCC to ~-10V. That's all it does. It will not do any baud-rate conversion at all.





In your case, the datasheet for the Atlas Scientific PH sensor states:



The baud rate is: 38400, 8 bits, no parity, with one stop bit.
The voltage swing 0-VCC, not +/- 12 volts
If standard voltage level RS232 is desired, connect an RS232 converter such as a MAX232.



So you don't need a MAX232, as you don't want or need the ~+-10V signalling levels. Assuming you are using the same power supply for the pH sensor and your MCU, you can simply connect the two devices straight together.


As an aside, I would reccomend putting a 1K resistor between the pH sensor's serial output and your MCUs serial input. This way, if you accidentally set the serial input as an output pin, it will not cause the two device's outputs to fight each other, and potentially damage one of them.




You will either need to change the baud rate of the Atlas sensor, or calculate your own baud-rate generator values for the PIC to make it run at 38400 baud you need.



If you cannot achieve the desired baud-rate with your system clock, you have to add an external crystal or resonator, and increase the system clock so you can.


As an aside, I think you're doing something wrong with your math if you're getting negative numbers for SPBRGH:SPBRG.


That said, 38400 baud is pretty high for a 1 Mhz system clock. You're going to be right at the edge of the top-end which you can run the EUSART at.


From the data sheet:


enter image description here


Assuming:



  • SYNC = 0

  • BRG16 = 1

  • BRGH = 1



n = value of SPBRGH, SPBRG register pair $$ BaudRate = \frac{F_{OSC}}{4*(n+1)} $$ $$ BaudRate * 4*(n+1) = F_{OSC} $$ $$ (n+1) = \frac{F_{OSC}}{4*BaudRate} $$ $$ (n+1) = \frac{1,000,000}{4*38400} $$ $$ (n+1) = \frac{1,000,000}{153,800} $$ $$ (n+1) = 6.51041666666667 $$ $$ n = 5.51041666666667 $$


so the closest available SPBRGH, SPBRG value would be 6 or 5.


Then, we calculate the baud rate error, which will likely be very high, since we're down at such small SPBRGH, SPBRG values.


$$ BaudRate = \frac{F_{OSC}}{4*(n+1)} $$ $$ BaudRate _{SPBRGH=6} = \frac{1e6}{4*(6+1)}, BaudRate _{SPBRGH=5} = \frac{1e6}{4*(5+1)} $$ $$ BaudRate _{SPBRGH=6}= 35714 , BaudRate _{SPBRGH=5} = 41666 $$


So the two availalbe baud rates are:



  • SPBRGH=6: 35714, -6.9 % Error

  • SPBRGH=5: 41666, 8.5% Error



Both are too far from your target baud rate to work, so you have to change your system clock rate.


No comments:

Post a Comment

arduino - Can I use TI&#39;s cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...