View previous topic :: View next topic |
Author |
Message |
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
PIC18F46K20 UART don´t work properly |
Posted: Tue Sep 17, 2013 6:10 am |
|
|
Hi!,
I´m writing code for a PIC18F46K20, my CCS version is V5.1 & the code is this
Code: |
#include <18F46K20.h>
#fuses NOWDT,XT,NOPROTECT,MCLR
#use delay(clock= 12000000)
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
long digito = 0;
long resultado;
long Cubo(long num);
void main()
{
digito = 4;
resultado = Cubo(digito);
printf("El numero es %lu\r", digito);
printf("El cubo del numero es: %lu", resultado);
}
long Cubo(long num)
{
return (num * num * num);
}
|
The problem is when printing on screen (Hyperterminal), the output string is
"El numero es 4 El cubo del numero es"
when the correct string should be
"El numero es 4"
"El cubo del numero es 64"
I think the problem lies in the configuration of the oscillator, and try using the internal oscillator at 8MHz but the problem persists.
Someone can help me to verify where is the error?
thanks in advance |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Sep 17, 2013 6:23 am |
|
|
Hi,
If your code is printing *anything* correctly then it's not the oscillator!!
The problem is most likely very simple! The CCS compiler inserts a 'Sleep' instruction immediately following the end of 'Main'. If this
instruction is executed before printing is complete, any serial communications in progress will be stopped.
Try modifying your code as follows:
Code: |
void main()
{
digito = 4;
resultado = Cubo(digito);
printf("El numero es %lu\r", digito);
printf("El cubo del numero es: %lu", resultado);
while(1); //<------- Add this line of code!!!
}
|
Please let us know if this solved your problem!
John |
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 6:43 am |
|
|
H!,
ezflyr thanks for the help, the string now is shown complete but in the same line, this have a carriage return \r on end line
Code: |
{
delay_ms(5000);
digito = 4;
resultado = Cubo(digito);
printf("El numero es %lu\r", digito); //<-------------return carriage \r
printf("El cubo del numero es: %lu", resultado);
//while(1);
delay_ms(5000); //<-----also work
}
|
what will be the reason why this happens?
Other question is which is the difference in while(1) or delay?
thanks!!! |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Sep 17, 2013 7:02 am |
|
|
Hi,
The 'carriage return' ('\r') simply sends the cursor to the beginning of the current line. To print additional lines of text, you need both a 'carriage return'
and a 'line feed'. Change your code like this:
Code: |
printf("El numero es %lu\n\r", digito); // <-------- added '\n'
|
The delay is simply keeping the PIC occupied long enough to finish printing before going to sleep, but it still will go to sleep once the end of Main() is
reached..... With the 'While(1);', the code simply loops forever, and never reaches the sleep instruction!
If you look at the .lst file (in the directory where your project files are located), you'll see the extra code that the compiler is adding by default....
John |
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 7:17 am |
|
|
hi!
I add \ n \ r but still the problem
In Proteus work fine but in physic still the problem
thanks |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Sep 17, 2013 7:35 am |
|
|
Hi,
Post your exact code that shows the problem!
Oh, one thing, you show a 'space' between the '\' character and the 'n' character, and you can't do that. If your code really does that, remove the space.
John
Last edited by ezflyr on Tue Sep 17, 2013 9:24 am; edited 1 time in total |
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 7:43 am |
|
|
Hi!
This is the complete code
Code: |
#include <18F46K20.h>
#fuses NOWDT,XT,NOPROTECT,MCLR
#use delay(clock= 12000000)
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
long digito = 0;
long resultado;
long Cubo(long num);
void main()
{
delay_ms(5000);
digito = 4;
resultado = Cubo(digito);
printf("El numero es %lu\n\r", digito);
printf("El cubo del numero es: %lu", resultado);
while(1);
}
long Cubo(long num)
{
return (num * num * num);
} |
There is no space between \ and n
thanks |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Sep 17, 2013 8:23 am |
|
|
Hi,
When I run your code, here is what I see on the Hyperterminal screen:
Code: |
El numero es 4
El cubo del numero es: 64
|
What do you see, and what were you expecting??
John |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 17, 2013 1:28 pm |
|
|
Quote: |
In Proteus work fine but in physic still the problem
#include <18F46K20.h>
#fuses NOWDT,XT,NOPROTECT,MCLR
#use delay(clock= 12000000)
|
Proteus doesn't care about oscillator fuses. The real PIC does care.
XT does not work with 12 MHz. You must use the HS fuse. |
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 1:32 pm |
|
|
Hi!
which hyperterminal you are using?
I expecting this
Code: |
El numero es 4
El cubo del numero es: 64
|
My problem is that the string don´t jump to the new line
This is an image of my result
thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 17, 2013 1:47 pm |
|
|
At the top of your screenshot it says bluntly, a newline is 0D 0A,
and the Release Notes for the Pickit 2 UART Tool say the following:
Quote: |
- A New Line is displayed when both a Carriage Return
and Line Feed are received. Individually, they will
display as a box character.
|
But your code below is not doing this. It's doing 0A 0D, which is the
reverse order:
Quote: | printf("El numero es %lu\n\r", digito); |
It could be that the PicKit 2 UART Tool wants to see the exact byte
order that is specified. Change it to this and see what happens:
Quote: |
printf("El numero es %lu\r\n", digito); |
|
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 1:51 pm |
|
|
PCM programmer wrote: | Quote: |
In Proteus work fine but in physic still the problem
#include <18F46K20.h>
#fuses NOWDT,XT,NOPROTECT,MCLR
#use delay(clock= 12000000)
|
Proteus doesn't care about oscillator fuses. The real PIC does care.
XT does not work with 12 MHz. You must use the HS fuse. |
Hi!
I change the fuse but the problem still |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 17, 2013 1:56 pm |
|
|
Did you read my 2nd post, just after your screenshot post ? |
|
|
christianjar
Joined: 17 Sep 2013 Posts: 8 Location: México
|
|
Posted: Tue Sep 17, 2013 2:02 pm |
|
|
PCM programmer wrote: | Did you read my 2nd post, just after your screenshot post ? |
sorry, I was writing my post when I get the notification.
thanks ezflyr & PCM programmer!, the program now works correctly! |
|
|
|