![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
Hi, I'm trying to send RS232 serial data from the PIC to the PC without a max232 between them. I developed a simple program for testing purposes, that sends about 50 words from the PIC to the PC in RS232 format, 9600 8 Odd 1. Firstly, I tried to use the USART from the 16F628A but it did not work. After looking here on this forum and googling, I concluded it would never work without a max232 since the voltage levels are different. Then, after googling a little more, I found out that modern PC serial ports can interpret 0v as -12v (logic 1) and 5v as +12v (logic 0). So, to send RS232 serial data from the PIC to the PC, I would need only to invert each and every bit so they are transmitted according to the voltages that can be understood by modern PCs. Then, I've written the following tutorial as a guideline: Code:
If I would like to send a 0xFF for example, the normal TTL bit sequence would be:
start LSB MSB Odd stop
0 1 1 1 1 1 1 1 1 1 1
Ok, then the above means in TTL voltages:
start LSB MSB Odd stop
0v 5v 5v 5v 5v 5v 5v 5v 5v 5v 5v
What you need to do is to invert each bit so the voltage levels become near the correct:
start LSB MSB Odd stop
1 0 0 0 0 0 0 0 0 0 0
In the above, you see how the PIC will need to send the data so the levels achieve the right ones for the RS232 receiver.
Now, below, the translation of the above logic states in voltages:
start LSB MSB Odd stop
5v 0v 0v 0v 0v 0v 0v 0v 0v 0v 0v
If we had a real RS232 emitter, we would be sending the original logic levels, like this:
start LSB MSB Odd stop
0 1 1 1 1 1 1 1 1 1 1
The difference is that in the RS232 devices, the logic levels above will translate into the below:
start LSB MSB Odd stop
12v -12v -12v -12v -12v -12v -12v -12v -12v -12v -12v
Hmmm, then now, we have concluded that we will be sending the below with the PIC:
start LSB MSB Odd stop
1 0 0 0 0 0 0 0 0 0 0
5v 0v 0v 0v 0v 0v 0v 0v 0v 0v 0v
And the above is just the accepted voltage levels for the same data in RS232. The right levels for RS232 would be:
start LSB MSB Odd stop
0 1 1 1 1 1 1 1 1 1 1
12v -12v -12v -12v -12v -12v -12v -12v -12v -12v -12v
We will make 0v corresponding to -12v and 5v corresponding to +12v.
I don't see what more I could do to put it to work... Does anybody have any tip? For those who might think it does not work, I've seen already a 12c509a sending bit bang rs232 directly to the PC serial port, without the MAX232. Thanks. Last edited by Mr.Anderson; 17th August 2006 at 06:29 PM. | |
| |
| | #2 |
|
Now just one question.... What's the maximum distance you can run your RS232 serial cable? One of the main problems with RS232 communication is the use of voltage signals vs. other protocols making use of current. (Hence the need for higher voltages) Hey, if it works for your application, good for you :cool: | |
| |
| | #3 |
|
If you are not using a MAX232 then your signal should be low when it is idle and high for a start bit. I can't tell if this is what you have. Mike. | |
| |
| | #4 |
|
You could always use a pair of transistors instead of a MAX232 for level conversion with the built-in serial peripheral. | |
| |
| | #5 | |
| Quote:
I've seen a 12c509a sending rs232 directly to the PC serial port through a 15 meters cable... Of course it was a shielded cable... So, I don't believe my problem is related to the cable itself... Maybe there are some fault on the software itself... My next test will be a hard coded 0xFF being sent to the PC in an endless loop... This way I can test if it is a software problem or not. Thanks. | ||
| |
| | #6 | |
| Quote:
As I understand, inverting is needed since RS232 logic levels are inverted if compared with the PIC voltages, giving +12V for logic 0 and -12V for logic 1. The PIC works with 0v for logic 0 and 5v for logic 1. So to have the nearest correct RS232 levels with a PIC, I need to have: 5v (representing +12v) for logic 0 = start bit 0v (representing -12v) for logic 1 = stop bit and iddle state This means I'll need to invert every bits before they are transmitted. Notice that I just would like to have some confirmation about what I've written. I need to know if it is correct so I can try to figure out my problem. If that is all correct, good, the tutorial will be available to others also. I'd like also to hear some tips and tricks regarding this, possible and common problems, whatever... Maybe there are something more to do, or I may have misunderstood something on the path. Thanks. | ||
| |
| | #7 | |
| Quote:
Thanks! Last edited by Mr.Anderson; 17th August 2006 at 06:30 PM. | ||
| |
| | #8 | |
| Quote:
However, you can't use the hardware USART as it requires the hardware inversion given by a MAX232 (or the two FET's shown above) - but it's trivial to do it in software - check my PIC tutorials for suitable software, it just needs minor changes to work in this way. | ||
| |
| | #9 |
|
All you need is pic basic pro manuel.pdf if you can find it, there is a sulution for you in serout section. You can do this in pic basic pro only one statement. according to me this is the most basic sulution.... to comminicate to a pc....
__________________ view my projects on www.otomatikmakine.com | |
| |
| | #10 |
|
Thanks Nigel! I forgot to mention that the communication needed is one way only, data flowing from the PIC to the PC. So, from your comments, I understand that I'm right in my tutorial and then there must be something wrong with my asm code. What I expected to do in my program: 1-Data comes in W from a data table and then it is saved in a register XMIT. 2-Then the parity is calculated, rrf'ing and xor'ing STATUS,C 3-Then, the data is transmitted, serially, going from start bit, then to data bits (LSB first), then to parity bit, then to stop bit and then to iddle. The inversion of each bit occurs at the same time they are being transmitted ( I rrf each bit and then invert each one before I put them on the digital output port. This should work, but it did not. Maybe is just some mistake on the software... I'll review it... Patan, I'll not use basic nor the USART hardware. I'm using asm and bit bang. Thanks anyway. | |
| |
| | #11 | |
| Quote:
| ||
| |
| | #12 |
|
Yeah, now it works!!! There was only one line of code lacking on my code... I am calculating the delay for 1 bit time using timer0 but I was forgetting to clean the T0IF bit after the fisrt delay... So only the first bit was delaying the right time... The remaining were not. My code is done, with different IFDEF's, I can compile it to work with parity or not, with bit bang or USART, with the max232 or not, at 4 or 8mhz, at different baud rates... The objective of this project is to have a kind of CRO, getting some samples of transitions delays between high and low (TTL) and sending the corresponding status and time to the computer through RS232. Of course, as I'm using a digital port for the measuring, I can measure times for transitions for HIGH and LOW in TTL levels or near them.... Of course, the program first gets 90 samples and then send all those samples to the computer. It would be impossible to send the data simultaneously to the computer due to the small amount of times I wanted to be able to measure. Each sample may last from 1us to 127us (this can be changed through the prescaler config). Thanks!!! Thanks! | |
| |
| | #13 |
|
Could you please post/send me the code? I was going to buy a MAX232, but if that works for me, then great ![]() Thanks! Hugo Ferreira | |
| |
| | #14 |
|
No sorry... But you can certainly do that yourself by looking at Nigel's tutorials. Try to understand also what you need the PIC to do... In short, for a 9600 baud, each bit should delay for 104us and all the bits need to be inverted logic if you compare them to what would be the right logic statuses for standard RS232. They need to be inverted since the voltage levels are not the same between RS232 and TTL levels. Notice the PIC can send data to the PC without the max232 (or the like) only with a software serial routine, not with the hardware USART. To use the hardware USART, you need to use the max232. | |
| |
| | #15 |
|
The transmit routine from my tutorial is VERY easy to alter the logic of, simply change the BCF's to BSF's, and the BSF's to BCF's (there are two of each).
| |
| |
|
| Tags |
| direct, max232, port, rs232, serial |
| Thread Tools | |
| Display Modes | |
| |