CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Program hanging when printf() used with RS 232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
heUAcyp



Joined: 16 Mar 2011
Posts: 33

View user's profile Send private message

Program hanging when printf() used with RS 232
PostPosted: Tue Mar 29, 2011 8:33 am     Reply with quote

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: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 10:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 10:39 am     Reply with quote

Thanks for the reply temtonic,

1. I include 'errors'

3. Hyperterminal did not have any flow control as you suggested.

I am using

UB232R
USB Mini-B FT232R Evaluation Module

http://www.ftdichip.com/Support/Documents/DataSheets/Modules/DS_UB232R.pdf

do I still need MAX232?

Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 11:18 am     Reply with quote

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: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 11:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 12:31 pm     Reply with quote

Quote:
I am using

UB232R
USB Mini-B FT232R Evaluation Module

http://www.ftdichip.com/Support/Documents/DataSheets/Modules/DS_UB232R.pdf

do I still need MAX232?

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

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 1:16 pm     Reply with quote

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

View user's profile Send private message

Thanks for replies
PostPosted: Tue Mar 29, 2011 2:35 pm     Reply with quote

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: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 2:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 2:50 pm     Reply with quote

@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

View user's profile Send private message

PostPosted: Tue Mar 29, 2011 3:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 6:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 30, 2011 7:54 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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