Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16f628A - RS232 direct to PC serial port (without max232)

Status
Not open for further replies.

Mr.Anderson

New Member
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:
[FONT="Courier New"]
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.
[/FONT]

After writing the above tutorial for myself, I tried to put it in practice with bit bang routines. Unfortunatelly it is not working. I'm doing the right thing with the timing and the logic levels. My PIC is sending logic 0 as 5v and logic 1 as 0v. Each bit is 104us long. Parity is Odd. The PIC is setup with a 8Mhz crystal. Everything was checked on the MP-Lab debugger...
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:
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:
 
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.
 
You could always use a pair of transistors instead of a MAX232 for level conversion with the built-in serial peripheral.

rs-232-circuit-for-16f627a-jpg.7924
 
SPDCHK said:
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:
The cable that I'm using is 1.5 meters long from a dead mouse... I suppose it is a short cable right?
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.
 
Pommie said:
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.
Yes, as I've stated in my tutorial, I'm inverting all the bits before they get out the PIC. So, as you said, the RS232 standard specifies a logic low (+12v) for the start bit and logic high (-12v) for both the stop bit and iddle state. On the PIC, the levels not like the RS232.
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.
 
Mike said:
You could always use a pair of transistors instead of a MAX232 for level conversion with the built-in serial peripheral.
Thanks, Mike... But I would like to reduce the components to minimal... Currently, I'm using a serial cable from a dead mouse and the PIC itself... If a PIC can talk directly to the PC, I'll do that... I just need to know if I'm doing the right things... My problems may be related to a wrong concept, a hardware problem or just a simple bug in the software on the PIC.... I need to know what may be wrong so I can try to solve it...

Thanks!
 
Last edited:
Mr.Anderson said:
Thanks, Mike... But I would like to reduce the components to minimal... Currently, I'm using a serial cable from a dead mouse and the PIC itself... If a PIC can talk directly to the PC, I'll do that... I just need to know if I'm doing the right things... My problems may be related to a wrong concept, a hardware problem or just a simple bug in the software on the PIC.... I need to know what may be wrong so I can try to solve it...

The minimum hardware you can use is just ONE resistor, this goes from TX on the PC to the input pin of the PIC and uses the protection diodes in the PIC to clip the +/-12V (the resistor is just there to limit the current), the ouput pin of the PIC can go directly to RX on the PC - this is what a BASIC STAMP uses.

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.
 
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....
 
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.
 
Mr.Anderson said:
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...

Check my tutorial software, it originally came (years and years ago) from an old Parallax application note. Why are you using parity?, there seems no point to it, and I can't remember the last time I saw parity being used on RS232?.
 
woohooo!

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!
 
Could you please post/send me the code? I was going to buy a MAX232, but if that works for me, then great :D

Thanks!

Hugo Ferreira
 
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.
 
Mr.Anderson said:
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.

I understand this. But I can't understand how the serial communication between the PIC and the PC can be reliable with 0 and +5 V levels. Does your circuit work properly with those levels?
Thanks
 
eng1 said:
I understand this. But I can't understand how the serial communication between the PIC and the PC can be reliable with 0 and +5 V levels. Does your circuit work properly with those levels?

It's a perfectly valid method, and has been used for many years - it's how the BASIC STAMP works, and was the subject of a Parallax application note even before the STAMP was produced.

Not that I'm suggesting that Parallax came up with the idea, I suspect it's far older than that!.
 
This stuff was just not too mentioned until now. I've found some information but not found all in one place. My tutorial expects to explain what was not clear to me before and help others.
 
not nice

mr anderson: too bad you asked for help and people helped you...and when someone ask you for help you refuse to help...don't give up all your code but just the part that mimics the USART with software
 
I have projects on this page that sends and receives serial between a PIC 18F1320 and PC serial port, using just 2 resistors. All source code is included (in MikroC), and it uses a very simple nifty bit-bang system with the PIC timer that can be done on any PIC;
Connect JuneBug to serial port and Sharp GP2 sensor
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top