|
|
View previous topic :: View next topic |
Author |
Message |
Labjac
Joined: 15 Dec 2011 Posts: 20
|
Max232 with Pic16F690 |
Posted: Tue Dec 20, 2011 8:58 am |
|
|
Hallo
I'm a bit new with the Pics and trying my first program and setup, but cannot get any data back from the Max232 chip. Please have a look at my program and wiring, I've checked the wiring but pretty sure something is wrong.
Code: |
#include <16F690.h>
void subrutine(int wagtyd);
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_B7,rcv=PIN_B5,bits=8)
#define In1 PIN_C0
#define Op1 PIN_C1
void subrutine(int wagtyd)
{
If (Input(In1) == 1 )
{
printf("Hello buddy");
output_high(Op1);
delay_ms(wagtyd);
output_low(Op1);
delay_ms(wagtyd);
}
}
void main()
{
set_tris_C(0B000001);
while(1)
{
subrutine(500);
}
}
|
Wiring according the the diagram in the link below:
http://sodoityourself.com/max232-serial-level-converter/
Any help will be appreciated.
Regards,
Labjac |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Tue Dec 20, 2011 10:28 am |
|
|
Here's a few things you need to know....
1) always add 'errors' to the use RS232(...) options.It'll keep the UART buffer from trapping out....
2) Get rid of the tris_C() code. Let the compiler handle the DDR requirements of the PIC.If YOU set it wrong, you'll lose a LOT of time wondering why some pin does seem to work right.
3) Press F11 in an open project to open up the onscreen CCS help file.Very handy reference, a must for beginners and us older guys who forget 'simple' stuff.
4)When the PIC is executing 'delay_ms(xxx)' it is ONLY doing the delay nothing else.Same as you 'twiddling your thumbs for 100 steamboats so you can't answer the ringing phone'.
5) Look in the 'examples' folder for ex_sisr.c to see how to handle serial communications using buffers and Interrupt Service Routines(ISR), that will allow the PIC to 'multitask' ( do more than one thing at a time ).
6) The internal oscillator is NOT very stable or accurate, so don't depend of a delay of say 100ms to be exactly 100 ms.Same hold true for serial communications.It may be fine at say 9600, but loses data at higher rates.
7) There's a simple 'hello World' echo program in the onscreen help FAQ( I think), use it to prove out the PIC, wiring, code,compiler,etc. It's a good starting point.
8)When cutting code copy the pervious version to a new file,make change to the new one,test,then copy it, make a new one, etc.... (test001.c,test002.c,test003.c) That way you'll have a 'history' of the changes as well as code to fall back on. Nothing is worse than making 3 or 4 changes,the program fails, and you spend HOURS trying to figure out which change was wrong....btdt.
9) use longer 'names' to actually self-describe' pins or variables.it'll help when you get into longer , more complicated code... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Dec 20, 2011 10:36 am |
|
|
First comment. What can an 'int' hold?. Not 500.....
Second comment. Where does the program expect anything 'back'?.
Third comment - remember the 1uF capacitors need at least 16v rating.
Forth comment - don't indent randomly. subrutine (though the word is subroutine), is reasonable, but main is foul.
Fifth comment - remember MCLR needs to be pulled high before the chip will run.
Sixth comment - don't fiddle with TRIS. The compiler will handle this for you.
Seventh comment - turn off the comparator.
Eight comment - get rid of your function prototype. These are needed when functions are declared after the main - your's isn't, and should be after the fuses etc..
Probably several more...
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Dec 20, 2011 12:22 pm |
|
|
Many versions of the max232 work well with low esr .1 uf caps - and 50 v rating is common for that part in an 805 or 1206 pkg.
The 50v rating is very low leakage / good 1/cv - |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Dec 21, 2011 6:24 am |
|
|
and....
if you use a MAX488 you do not need the caps at all. Also saves board space, fewer pins too !! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Dec 21, 2011 6:38 am |
|
|
But, you are then talking RS485/422, not 232.
Not much use if you want to talk using RS232 standards....
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Dec 21, 2011 6:59 am |
|
|
I've used the 488 for a long,long time hooked up to real RS232 devices and never had a problem,even equipment with real 1488 and 1489 drivers. Maybe 1100-1200 pieces or equipment in the field,some stuff I've never been back to in 8-10 years.Tech at Maxim told me it'd work.
Currently I've got an18F4550/MAX488 running at 115,200 ,zero problems...
I can't believe I'm that lucky. |
|
|
Lemosek
Joined: 15 Oct 2011 Posts: 36
|
|
Posted: Wed Dec 21, 2011 7:09 am |
|
|
Hello,
Can You put some example how to use max488 for RS232 ??
Best regards |
|
|
Labjac
Joined: 15 Dec 2011 Posts: 20
|
|
Posted: Wed Dec 21, 2011 7:53 am |
|
|
Hallo
Not really worried if it's 232/485/422 anything will work, I'm just trying to get the 232 for starters, the end goal will be to try and communicate with a PLC, but that's not the problem.
Thanks for all your help, I managed to get some data from the PIC into hyperterminal, but it very random every now and then some gabage slips passed and I get bad data, and I also don't get all the data, Please find snapshot below, I also changed the code to make a simpler to fault find, please have a look at what else I'm doing wrong, (Understand something about a delay after sending the message, not sure if mine is correct?
Code: |
#include <16F690.h>
#fuses intrc,nowdt,noprotect,put
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)
#define In1 PIN_C0
#define Op1 PIN_C1
void main()
{
while(1)
{
If (!Input(In1) == 1 )
{
//delay_ms(500);
Printf("Hallo");
delay_ms(500);
output_high(Op1);
delay_ms(1000);
output_low(Op1);
delay_ms(1000);
}
}
}
|
Hyperterminal snapshot:
HaþHaþHqHýÈþýÈHHaHaHaþHalHaHaHùHýHø
Please let me know if this looks like software or hardware issues? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Dec 21, 2011 8:09 am |
|
|
Have you seup Hyperterminal for 'no flow control'?
To prove Hyperterminal and PC are OK, connect pins 2 and 3 of the PC serial port together. Typing on the PC keyboard should be shown on the PC screen. This is simple 'loopback' test.
If that's OK..
then I'd suspect the 'MAX232' driver as the program seems OK( it's just sending 'Hello' out every 2 seconds.
To test the MAX232, disconnect the PIC pins to it and connect the xmt and rcv pins of the MAX232 together. Now using Hyperterminal type on the PC kbd, again the PC screen should echo what is typed. This process is called 'loopback'.
Be sure power is applied to the MAX232 though. |
|
|
Labjac
Joined: 15 Dec 2011 Posts: 20
|
|
Posted: Wed Dec 21, 2011 8:12 am |
|
|
Maybe also a good time now to mention that I'm using 50V 1uf polirized capacitors in the circuit, still need to get the paper type, can this cause any problems with the Max232 chip? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Dec 21, 2011 8:21 am |
|
|
It may be a frequency problem....
We have been carefully ignoring this, till you got something to display. If you look, your first two characters are correct, then the string goes screwy for a few characters (then the code pauses), and the first two characters are correct again.
Now, the internal RC oscillator can be 'borderline' for timing accuracy on RS232. Provided your supply is stable (is it?....), chip is at room temperature etc. etc., then it _should_ be good enough.
I'd say it doesn't look like a MAX232 issue, but a timing issue between the ends.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Dec 21, 2011 8:27 am |
|
|
I'd try is at say 300 baud,never met aPIC that couldn't run that 'slow'. |
|
|
Labjac
Joined: 15 Dec 2011 Posts: 20
|
|
Posted: Wed Dec 21, 2011 11:37 am |
|
|
Thanks for all the brainstorming, still got some issues, I tried dropping the baudrate to about 300 then is only sends blank spaces, I've tested using the loopback test, on serial side all good. (Hyperterminal testing the PC side), but when I remove the PIC wires and bridge the RX and XMIT wires nothing is happening on the PC.
I'm currently using 1uf electro caps, has anybody tried the max chip with 0.1uf paper caps, it's all I currently got and very tempted to give it a go.
Then also just want to confirm, I'm using the following pins for comms. (Ignoring all the caps).
Pic to Max chip. (Pic16F690 to Max232n).
Pin12(RB5) <- Pin12 (R1 out)
Pin10(RB7) -> Pin11 (T1 in)
Max to DB9
Pin13(R1 in) <- DB9 (Pin3)
Pin14(T1 out) -> DB9 (Pin2)
And Pin5 is ground at the same potential as the MAX and Pic controller.
I'm not sure where to start with the fault finding, maybe somebody also made a rookie mistake that I'm probably making now. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Dec 21, 2011 12:22 pm |
|
|
If you've jumpered MAX pins 11 to 12 and got nothing but garbage on the 'loopback through MAX232' test, then the MAX232 has a problem.
The 4 caps can be anything from 1 to 22mfd, but be sure of the polarity.
C1 +ve to pin 1, -ve to pin3
C2 +ve to pin 4,-ve to pin 5
C3 +ve to pin 2, -ve to gnd
C4, +ve to gnd, -ve to 6 !!! pin6 is the -ve V output.
If hooked up right(and working) you should get +9V on pin 2, -9V on pin 6.
If you get the right values(+-9 to +-11) is 'normal', I'd try pin 10 as the PIC xmt pin, 7 going to PC pin 2( that's the other transmitter section).You could also change wiring to use the other rcvr section.
The MAX232 is a fairly robust chip but miswiring could have blown part of it.16 goes to Vcc(+5) and 15 goes to ground.
Again try the 'loopback through MAX232' test now that you've proven Hyperterminal isn't a problem( or a busted PC serial port !)
If you're using a white breadboard, one strip of connections might be faulty.Older boards or using wire bigger than 22ga. can cause faults. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|