View previous topic :: View next topic |
Author |
Message |
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
Program hanging when printf() used with RS 232 |
Posted: Tue Mar 29, 2011 8:33 am |
|
|
Hi All,
I am using PIC 18F24J11, PicKit2 Programmer , Compiler Version 4.087.
I set up my circuit on a breadboard, everything works perfectly, but when I use printf() to communicate with hyperterminal program doesn't work at all.
I couldn't figure out the problem , please help.
My Code is:
Code: |
#include <18F24J11.h>
#device ADC=10
#include <STDIO.H>
#include <stdlib.h>
#include <math.h>
#include <STRING.H>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=4000000)
//#use delay(clock=internal);
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main()
{
setup_adc_ports(AN0) ;
setup_adc(ADC_CLOCK_INTERNAL) ;
set_adc_channel( 0 );
while(true)
{
if (read_adc() > 0x300)
output_low(PIN_A2);
else
output_high(PIN_A2);
//printf("%u\n\r",5) ;
}
} |
Basically I have an analogue detector circuit connected to AN0, outputs either 0V or 3V. When it goes to 0V LED connected to PIN A2 turns on, which is what I want.
Then I add :
Code: | printf("%u\n\r",5) ; |
or any printf function to the code. It builds without any errors or warnings. However, it doesn't sent anything to hyperterminal and even analogue detection doesn't work.
Basically nothing works when I add any printf().
I have checked hardware connections 100 times.
Please help me on this.
Cheers, |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Mar 29, 2011 10:15 am |
|
|
three quick items
1) always add 'errors' to the RS232(....) line
2) you need a MAX232 or equal chip between the PIC and the PC
3) be sure to setup Hyperterminal to NOT have any 'flow control'. |
|
|
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Mar 29, 2011 11:18 am |
|
|
Yes, you must use the MAX232 chip..This chip inverts the polarity of the signals to conform to the RS-232 standard.Others may say it's 'optional' BUT you must then use the 'invert' option in the RS232(...) command.The computers might be happy, but you could have problems.Not all PCs are built the same, some have correct RS232 chips, some cheat.
I suggest you jumper pins 2 and 3 of the DE-9 connector from your USB-RS232 module to see if your Hyperterminal program is running correctly. Do this without any conections to the PIC. This is commonly called a 'loopback' connection.
This will verify that the PC, hyperterminal and the USB<-> RS232 module are functioning correctly. Be aware that not all USB<->RS232 module will work correctly. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Mar 29, 2011 11:24 am |
|
|
Forgot to say what the loopback test does. Oops.
The PC screen should 'echo' every key pressed on the PC keyboard. Removing the jumper and it should stop.
In the Hyperterminal options, it does have a window for 'flow control'. You must chose the 'none' option. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 29, 2011 12:31 pm |
|
|
No, you don't need a MAX232. This FTDI module, as shown in the data
sheet link above, is a serial-to-USB converter. The USB side connects
to the PC. This is shown in the diagrams in Figure 1.1 on page 2 of the
data sheet. The serial side connects to a micro-controller (PIC).
The diagrams show a Laptop PC connected to the USB side.
The serial signals are at TTL levels (not RS232 levels). This is stated in
the FTDI data sheet on page 2:
Quote: |
1 Introduction
The module supports data transfer rates from 300 baud to 3 Mbaud
(RS422, RS485, RS232 and at TTL levels).
|
So the 18F24J11 running at 3.3v can connect to the FTDI module, which
can also be configured to have it's serial i/o signals use +3.3v levels.
(The other option is to use 5v levels. See page 6 of the FTDI data sheet). |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Mar 29, 2011 1:16 pm |
|
|
The topic title says the PIC is "hanging" when you use printf(). Are you sure it is hanging (ceasing to execute code) and not just failing to print? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
Thanks for replies |
Posted: Tue Mar 29, 2011 2:35 pm |
|
|
Yes SherpaDoug it is cheasing to execute. I have tested it with this simple code:
Code: | #include <18F24J11.h>
#include <STDIO.H>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void main()
{
while(true)
{
output_low(PIN_A2);
delay_ms(500) ;
output_high(PIN_A2);
delay_ms(500);
//printf("hello") ;
}
} |
When I write printf("hello") after output_high(), LED stayed on. When I write printf("hello") between output_low() and output_high() LED remains off. When I do not write printf() at all, LED flashes as expected.
I really could not figure this out.
@PCM programmer: yes I don't need a MAX232 and my connections are correct.
there might be a problem on my PC's MPLAB, I really don't know. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Mar 29, 2011 2:43 pm |
|
|
I assumed(oops) that the module was one of those $10 USB-RS232 convertors....I've downloaded the PDF and see it's a PCB for a uC.
It'd be nice to call it a USB<-> serial converter, NOT RS-232.Old guys like me figure it has 1488 and 1489 drivers in it....
However it does show that CTS* is used, so the uC must need to control it ,probably for 'buffer full' signalling ?
There may be some real notes in another document, but nothing in the 6 pages that I saw, except for the schematic.
Still a good test would be to do a loopback. |
|
|
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
|
Posted: Tue Mar 29, 2011 2:50 pm |
|
|
@temtronic
you are probably right , but more interesting thing is same code, with same uC on same board did work when programed on another PC with another programmer and with same compiler version. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 29, 2011 3:58 pm |
|
|
The hardware UART code doesn't work for the 18F24J11 for vs. 4.087
because it uses the wrong register addresses. It's a buggy version
for the 18F24J11. |
|
|
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
|
Posted: Wed Mar 30, 2011 6:02 am |
|
|
Thank you very much PCM programmer.
Is same valid for PIC18F25J11 ?
I have those available as well. Otherwise, I will change my PIC or the compiler. |
|
|
heUAcyp
Joined: 16 Mar 2011 Posts: 33
|
|
Posted: Wed Mar 30, 2011 7:54 am |
|
|
You are absolutely right PCM programmer. I have installed compiler v. 4.095 and it is working.
You are a legend, thank you very much man. |
|
|
|