|
|
View previous topic :: View next topic |
Author |
Message |
redlion
Joined: 14 May 2013 Posts: 8
|
PIC18F4550 USART problem |
Posted: Tue May 14, 2013 1:10 am |
|
|
hi everyone,
I am trying to write a very simple code to display "A" using the pickit2 uart tool.
here is the code:
Code: | #include <18F4550.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,CPUDIV1
#use delay(clock=20000000)
#use RS232(baud=9600,xmit=PIN_B7,rcv=PIN_B6,PARITY=N,BITS=8,STOP=1)
void main()
{
printf("A");
}
|
but the result I get is in "?" instead of "A"
what's wrong with my code?
ps: Im a newbie
thank you so much!!!!
best regards,
redlion |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 14, 2013 2:24 am |
|
|
Always post your compiler version!
Most likely your problem is a classic error for people starting with embedded programming. In your PIC processor there is no operating system running, so what do you think will happen when the main function is finished?
Answer: the program is finished and the compiler inserts a sleep instruction here.
Result: the processor hardware goes into sleep mode but the (relative) slow hardware UART was still busy in the background and is now shut down before before the last character is fully sent out.
The most popular solution is to ensure the main function never ends by adding a forever while loop at the end of main(): Code: | #include <18F4550.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,CPUDIV1
#use delay(clock=20000000)
#use RS232(baud=9600,xmit=PIN_B7,rcv=PIN_B6,PARITY=N,BITS=8,STOP=1)
void main()
{
printf("A");
while(1); // loop forever
} |
|
|
|
redlion
Joined: 14 May 2013 Posts: 8
|
|
Posted: Thu May 16, 2013 11:22 pm |
|
|
Thank you so much for your reply ckielstra
Actually i am using the ccs c compiler demo version currently.
I have added the while loop in my coding, but the problem is still there.
Here is my updated source code
Code: |
#include <18F4550.h>
#fuses hs,NOWDT,put,brownout,nolvp,PLL1,CPUDIV1
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B6,STREAM=PC)
void main()
{
int i;
printf("start");
for(i=0;i<=10;i++)
{
printf("%u\r\n",i);
delay_ms(100);
}
while(1);
}
|
The output from pickit2 uart tool is
?????
0
1
2
3
4
5
6
7
8
9
10
Which means i can display the number correctly but not for the character.
My external resonator is 20MHz.
What is the problem for my updated coding??
Thank you so much!
best regards,
redlion |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 17, 2013 12:17 am |
|
|
Quote: | Which means i can display the number correctly but not for the character. |
That's not conclusive.
It could also be that an initial delay is needed before the PIC sends
anything to the Pickit 2. Try adding the delay statement shown in
bold below. It must be at the start of main(). See if it now works:
Quote: |
#include <18F4550.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,CPUDIV1
#use delay(clock=20M)
#use RS232(baud=9600,xmit=PIN_B7,rcv=PIN_B6,PARITY=N,BITS=8,STOP=1)
void main()
{
delay_ms(500);
printf("Start");
while(1); // loop forever
} |
|
|
|
redlion
Joined: 14 May 2013 Posts: 8
|
|
Posted: Fri May 17, 2013 1:17 am |
|
|
it works!!!!
thank you soooooooo much PCM programmer!!
really appreciate for your help and ckielstra as well |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri May 17, 2013 4:20 am |
|
|
Key thing here is it takes time for the capacitors on the charge pump used in the RS232 drivers to charge up. There are similar problems with LCD's, where again you must wait for a significant time after the PIC starts, before things will work. Teaches you something about how fast the PIC actually boots....
Best Wishes |
|
|
redlion
Joined: 14 May 2013 Posts: 8
|
|
Posted: Mon May 20, 2013 9:28 pm |
|
|
thank you Ttelmah for your explanation |
|
|
|
|
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
|