View previous topic :: View next topic |
Author |
Message |
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
Printf used in dsPIC33f |
Posted: Wed Nov 17, 2010 3:07 pm |
|
|
I used dsPIC33F want to print out:
Quote: |
0 CrLf
1 CrLf
2 CrLf
3 CrLf
4 CrLf
A CrLf
|
The program like this
Code: |
for(i=0;i<5;i++)
{
printf("%2x",i);
printf("\r\n");
}
printf("A");
|
But what I get from the PC are
Quote: |
70 70 0D 0A 70 71 0D 0A 70 72 0D 0A 70 73 0D 0A 70 74 0D 0A 81 |
Is there something I missed? I am appreciate any help in advance. |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
Re: Printf used in dsPIC33f |
Posted: Wed Nov 17, 2010 4:03 pm |
|
|
cchappyboy wrote: | I used dsPIC33F want to print out:
The program like this
|
what size is i? int8? int16?
what does your CCS manual say about the %x printf format specifier?
what effect does the '2' have upon this specifier?
jds-pic |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
Re: Printf used in dsPIC33f |
Posted: Wed Nov 17, 2010 4:08 pm |
|
|
cchappyboy wrote: |
70 70 0D 0A 70 71 0D 0A 70 72 0D 0A 70 73 0D 0A 70 74 0D 0A 81 |
I have highlighted the CR/LF above.
cchappyboy wrote: |
70 70 0D 0A 70 71 0D 0A 70 72 0D 0A 70 73 0D 0A 70 74 0D 0A 81 |
I have highlighted variable "i" above.
What program are you using on the PC end?
jds-pic |
|
|
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
|
Posted: Wed Nov 17, 2010 4:52 pm |
|
|
Thanks for your response.
I did some changes:
Code: |
#use rs232(baud=9600,xmit=pin_f5,rcv=pin_f4,bits=8,stop=1,parity=n)
unsigned int8 i=0;
for(i=0;i<5;i++)
{
printf("%d",i);
printf("\r\n");
}
printf("How are you?");
|
But this time it still not right.
It shows on Hyper terminal,
Quote: |
p
q
r
s
t
ˆÏ÷@ÁòÅ@ùÏõ
|
there should be
Quote: |
0
1
2
3
4
How are you? |
|
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Wed Nov 17, 2010 9:35 pm |
|
|
cchappyboy wrote: |
Code: |
unsigned int8 i=0;
...
printf("%d",i);
|
|
Check your manual... is "%d" used for signed int or unsigned int?
cchappyboy wrote: |
It shows on Hyper terminal |
Do not use Hyperterminal for debugging serial problems -- it stinks. So download and install TeraTerm or other (good) serial terminal program. Ensure that the bits/parity/stop settings are the same at both ends. I think this is the root cause of your problem.
jds-pic |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Nov 18, 2010 3:13 am |
|
|
jds-pic is correct, you should be using %u but it shouldn't matter in this case.
The output you show would imply an incorrect baud, bits, parity settings between your PC/term prog and the pic or a hardware/cable problem, you have got RS232 driver chip on your circuit, haven't you?
70 shown in your first post is hex 0x70 = 'p' = 0b01110000 it should be 0x30 = '0' = 00110000 this matches your latest output with using %d to display the decimal value rather than the hex.
%d converts the number 0 to the char '0' and when output hyperterm is displaying 'p'.
More importantly, show your whole program, not just these little snippets!
And yes Hyperterminal is crap but it should still work for this. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Nov 18, 2010 3:49 am |
|
|
Yes.
A simple look, says '70' (hex), is the letter 'p'.
The number '0', is '30', so what the PC is seeing is the signal being high for three bit times (at RS232 levels), when it is only meant to be seeing two high bit times.
Implies baud, is either 50% slow at the PIC, or 50% fast at the PC (in fact, depending on the PC's 'sampling strategy', anything over perhaps 30% slow would give this).
Start simple. Do a basic 1 second LED flash program, and with a stopwatch, count thirty flashes. 30seconds?. If not, the crystal is wrong,or one of the fuse settings, so the PIC is not running at the speed required.
Best Wishes |
|
|
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
|
Posted: Thu Nov 18, 2010 10:51 am |
|
|
Thanks you guys.
According your guys analysis, I try so many way to find out what is going on there. Finally I make a 1 second led flash program as Ttelmah said. I found it was 1.07 seconds other than 1 second. So I try use chips internal clock then it works properly. So i think I need to change the crystal.
Thanks again. |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Thu Nov 18, 2010 11:44 am |
|
|
cchappyboy wrote: |
I found it was 1.07 seconds other than 1 second. So I try use chips internal clock then it works properly. So i think I need to change the crystal. |
the clock variation that you document above (+7%) is outside the generally accepted RS232 frequency tolerance of +/-3%. some implementations will be more tolerant, some less so, but 3% is a good starting point.
see also
http://www.maxim-ic.com/app-notes/index.mvp/id/2141
jds-pic |
|
|
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
|
Posted: Thu Nov 18, 2010 12:02 pm |
|
|
Thanks JDS-PIC |
|
|
|