View previous topic :: View next topic |
Author |
Message |
jhasan
Joined: 03 Jun 2006 Posts: 14
|
Problems with printf() and getc() |
Posted: Thu Jan 08, 2009 10:14 pm |
|
|
I am using PIC18F2331 and using basic printf() and getc() functions to communicate between PIC and PC. But I am having problems.
When I use printf() using the code below I get unknown characters in the Hyperterminals. This seems to be a very simple problem but I don't know what I am doing incorrectly.
Attached is my code:
Code: |
#include <18F2331.h>
#include <String.h>
#include <Stdlib.h>
#include <stdio.h>
//#include <math.h>
#use delay (clock=20000000)
#fuses HS,NOLVP,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, stream=SETUP, ERRORS)
void main() {
int8 command;
while(1)
{
printf("Hi\n\r");
delay_ms(1000);
}
} |
Thanks in advance |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
IceMetal
Joined: 20 Nov 2008 Posts: 79 Location: white Plains, NY
|
|
Posted: Thu Jan 08, 2009 11:22 pm |
|
|
You will get unknown characters, if you are not using a max232 circuit, are you going directly to the RS232 port? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jan 09, 2009 1:30 pm |
|
|
Likely culprits are 1) Baud rate 2) signal level and 3) noise.
If the unknown characters are repeatable it is not #3. If they are not repeatable look closely at your ground connections.
How well do you know your baud rate? Are you just using the number printed on the crystal case? Use a counter, scope, or program a LED to flash every 60 seconds and time it with a watch.
IceMetal is right about the MAX232 circuit. Are you aware that the signal on the PIC pin will be inverted relative to what a PC expects on its RS232 pin? The PIC assumes there will be an inverting driver chip. PCM's link will probably tell you all about that. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 12:08 pm |
|
|
I got the getc() function to work properly. But still am having problem with printf().
I have connected the max232 chip with pic as recommended by some of you. Also, if I use putchar() it works fine.
The interesting thing is if I used say printf("t") it works fine then. I am having trouble when I am trying to print a whole string for example if I use printf("Hello\n\r") then the printf() won't work. It will give me rubbish on the hyperterminal.
So,is there something special I have to do when I want to print a string using printf(). I am using pic18f2431 and setup the fuse as below:
Code: | #fuses HS,NOWDT,NOPUT,NOPROTECT,NOLVP,MCLR,BORV27,HPOL_HIGH,LPOL_HIGH,T1STANDARD
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stream=SETUP, ERRORS) |
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 1:50 pm |
|
|
Quote: | I am using pic18f2431 |
Now you have changed your PIC. You were using 18F2331 in your first
post.
1. Post your current test program. (Similar to the program in your first post).
2. Post a link to your schematic.
3. Post if this project is being done in hardware, or is it in Proteus ? |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 2:48 pm |
|
|
1)Attached is my code:
Code: | #include <18F2431.h>
#device ADC=10
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#use delay (clock=20000000)
#fuses HS,NOLVP,NOWDT,NOPROTECT,NOPUT,BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
#byte port_c=0x82
void main() {
set_tris_c(0xFF);
port_c=0;
while(1)
{
printf("HELLO");
}
} |
2)Attached is the link to how I hooked up MAX232:
http://www.geocities.com/SiliconValley/Network/3656/c2c/rs232x.gif
Also I want to mention the pin 11 of Max232 is connected to pin 17 of my pic18f2431 and pin 12 of Max232 is connected to pin 18 of my pic18f2431.
3)The project is done using hardware not proteus.
Then the rs232 is connected via the serial cable to the pc. I am using the serial communication interface that comes with the CCS compiler to communicate between the pc and pic. It is called "Serial Input/Output monitor or siow". It works better than hyperterminal.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 3:01 pm |
|
|
Quote: | #include <18F2431.h>
#device ADC=10
#include <string.h>
#include <stdio.h>
#include <stdlib.h> |
The lines shown in bold are not needed for this test program.
Quote: | #use delay (clock=20000000)
#fuses HS,NOLVP,NOWDT,NOPROTECT,NOPUT,BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
#byte port_c=0x82 |
The Port C address is wrong.
Look in this section of the 18F2431 data sheet to see the correct address:
Quote: | 5.9.2 SPECIAL FUNCTION REGISTERS |
http://ww1.microchip.com/downloads/en/DeviceDoc/39616C.pdf
Quote: |
set_tris_c(0xFF);
port_c=0;
|
These lines are not needed for this test program. |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 3:49 pm |
|
|
I just tried according to your suggestions but same problem.This is what I get in siow
lÿelhllollllhhhlelhlleÿllelhoelleeoeolelllehhlÿlllhholÿllhlhehlhllhlllllhheelÿllÁ¬Pþ
óÿÿÿÿÿÿÿlllollhlllhhlelllhhlhlelllhlhhlhllolehhoellelhhlhlhhhohlhhllhlhelellleehhlll
ohllhhleloolhlelolhhelhhellllehhhelelellllhelhhÿohleohoÿoelllolelhhhhhÿlhlelhoehl¯P
ÿÿÿÿÿÿÿÿhollehlhlhhehlhohhllž«ÿÿÿÿÿÿÿÿÿellhlolllhlllllhhlehhlelehlehehhellllhllehol
It should say "hello" not the rubbish I am getting.It just seems annoying or weird.I tried in hyperterminal as well and I am getting the same thing.
Thanks for all your help but do you have any more suggestions for me.It seems to be a simple problem not that complicated. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 4:13 pm |
|
|
Your posted code shows "HELLO" in upper case. Does the program
that you just tested have upper case or lower case letters ?
Change the program so it only sends the text one time. What do you
see in the terminal ? Example:
Code: | void main() {
printf("HELLO");
while(1);
} |
|
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 5:11 pm |
|
|
I have tried using lower case 'hello'.
I have tried the code you send me.But it still no luck.
I went back and checked getc(), and putchar().It works fine.Also printf("k") also works but printf() will not work when I put a whole word like printf("hello") won't work.Weird!!!!!!!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 5:17 pm |
|
|
As a test, try doing it with a delay between each character.
Also post your compiler version. It's given at the top of the .LST file.
It will be a number in this format: x.xxx
Example:
Code: |
void main()
{
printf("H");
delay_ms(1);
printf("e");
delay_ms(1);
printf("l");
delay_ms(1);
printf("l");
delay_ms(1);
printf("o");
while(1);
}
|
|
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 6:26 pm |
|
|
Attached is the code
Code: | #include <18F2431.h>
#use delay (clock=20000000)
#fuses HS,NOLVP,NOWDT,NOPROTECT,NOPUT,BROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8)
void main()
{
printf("H");
delay_ms(100);
printf("e");
delay_ms(100);
printf("l");
delay_ms(100);
printf("l");
delay_ms(100);
printf("o");
delay_ms(100);
while(1);
} |
It gives me "Hello" in the siow window as expected.
The compiler version of my PIC C compiler is "CCS PCH C Compiler, Version 3.219, 23007 ".
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 6:33 pm |
|
|
Go to the Configuration menu in the SIOW window, and then go to the
sub-menu "Set Port Options". The "Com Port Options" window will pop up.
Post the settings that it shows. |
|
|
jhasan
Joined: 03 Jun 2006 Posts: 14
|
|
Posted: Tue Jan 13, 2009 6:41 pm |
|
|
In the SIOW window:
ComPorts=Direct to COM1 [It is connected to COM1 of my PC]
Baud rates=9600
Parity=None
Data bits=8
Stop bits=1
Software Flow Control=None
Hardware Flow Control=I have not selected any of the options.So, all are unchecked. |
|
|
|