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

PIC18F4550 USART problem

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



Joined: 14 May 2013
Posts: 8

View user's profile Send private message

PIC18F4550 USART problem
PostPosted: Tue May 14, 2013 1:10 am     Reply with quote

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 Embarassed

thank you so much!!!! Laughing

best regards,
redlion
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 14, 2013 2:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 16, 2013 11:22 pm     Reply with quote

Thank you so much for your reply ckielstra Very Happy

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

View user's profile Send private message

PostPosted: Fri May 17, 2013 12:17 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 17, 2013 1:17 am     Reply with quote

it works!!!! Razz Razz Razz
thank you soooooooo much PCM programmer!!

really appreciate for your help and ckielstra as well Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri May 17, 2013 4:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 20, 2013 9:28 pm     Reply with quote

thank you Ttelmah for your explanation Very Happy
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