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 support@ccsinfo.com

18F26K20: Serial baud rate wrong, maybe OSC too

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



Joined: 14 Mar 2006
Posts: 45

View user's profile Send private message

18F26K20: Serial baud rate wrong, maybe OSC too
PostPosted: Fri Jun 12, 2009 2:29 am     Reply with quote

I have started software design and debugging of a new project and a new (for me) PIC... the 18F26K20.

I am using the ICD-U40 and I can confirm that I am able to d/l code, single step and run a program. I am using PCWH 4.093. I am attempting to use the normal serial port for communications. Unfortunately the baud rate is wrong. For example, if I send a U (0x55) I can measure the lows and highs are about 400us long which is about 2500 baud, instead of the expected 2400.

And the above is at OSC frequency of 8mhz Exclamation
Datasheet says (see below) I should be able to use 1mhz Exclamation Exclamation

I would like to run at 9600 baud, but believe I am either coding something wrong, or the part or compiler is not properly responding to my coding. Per the datasheet, it is supposed to be possible. I am using the internal oscillator.
Quote:
TABLE 18-5: BAUD RATES FOR ASYNCHRONOUS MODES (CONTINUED)
SYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
FOSC = 1.000 MHz actual rate 9615 %error 0.16 SPBRGH (decimal) 25

Another piece of the puzzle is that, when stopped, the debugging window displays the phrase Ready MCU at 1.00 Mhz.
I am unsure, but I think that frequency should change when I change the OCS value and recompile. It does not.

I have in the main.c
Code:
void main()
{
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF);

Code:
   WHILE (1)
   {
   fputc('+',CONSOLE);
   }

in the man.h file
Code:
#include <18F26K20.h>
#device ICD=TRUE

#use delay(clock=8000000)

#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=CONSOLE,errors)


Any suggestions/corrections appreciated, thanks in advance.

Leef_me
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 12, 2009 12:14 pm     Reply with quote

Try a simple test program like this:
Code:
#include <18F26K20.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)

//=========================================
void main()
{
printf("Hello World\n\r");

while(1);
}

Also, your posted code has no fuses. The fuses are important.
They tell the compiler how to setup the oscillator registers.
If you have INTRC_IO fuse, the compiler knows that you want to use
the internal oscillator. It also looks at the #use delay() frequency.
With that information, it programs the OSCCON register correctly
in the start-up code.

Let the compiler do it for you.
Leef_me



Joined: 14 Mar 2006
Posts: 45

View user's profile Send private message

PostPosted: Fri Jun 12, 2009 4:12 pm     Reply with quote

Thanks PCM, the sample code you provided works fine. Very Happy

I added in the putch 'U' loop, and measured the high and low pulse widths at approx 104us each,
which is the 9615 baud claimed in the datasheet Exclamation

PCM programmer wrote:
If you have INTRC_IO fuse, the compiler knows that you want to use the internal oscillator. It also looks at the #use delay() frequency. With that information, it programs the OSCCON register correctly in the start-up code.

Let the compiler do it for you.


I read in the thread "PIC18F2620 Oscillator configuration"
http://www.ccsinfo.com/forum/viewtopic.php?t=39199
Your comment to Simões, ___ Posted: Wed Jun 10, 2009 2:26 pm
Quote:
If you have a modern version of the compiler, and if everything is
working correctly with it, then you don't even need a setup_oscillator()
statement. The compiler will look at your fuses and the #use delay()
frequency and it will set up everything correctly.

The thing is that the PIC Wizard put the "setup_oscillator() " in my code, I just changed the OSC_?? value. Rolling Eyes

Thanks again.
Leef_me
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 12, 2009 4:17 pm     Reply with quote

You don't need to use everything that the Wizard puts in. It puts in a lot
of lines that disable peripheral modules that are already disabled upon
power-up of the PIC.
Leef_me



Joined: 14 Mar 2006
Posts: 45

View user's profile Send private message

PostPosted: Fri Jun 12, 2009 6:28 pm     Reply with quote

Ah yes, now I remember that from times past.

BTW, thanks as well for your patience.
You've probably given similar advice several times before.

And for the next several fellow users who forget to come back and say Eureka & thanks:

Thanks in advance! Smile

Leef_me
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