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

Changing Baud rates - SOLVED
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Aug 08, 2012 8:20 am     Reply with quote

I already mentioned, that using your own method to set UART speeds can be reasonable. Thanks for explaining about possible cases.
Quote:
Problem is that with switched #use delay statements, it can first be difficult to know which one is in force for a particular code flow. They do not work quite as you might expect regarding which one is 'in force'.

The scheme is simple, but indeed inflexible. Just position of code.

I agree that dynamical clock switching may suggest a different method, e.g. global flag.
Gabriel



Joined: 03 Aug 2009
Posts: 1085
Location: Panama

View user's profile Send private message

PostPosted: Wed Aug 08, 2012 9:28 am     Reply with quote

controling the flow of code in _my_particular application _should_ be fairly simple....

i only have 2 functions that use diferent bauds and a total of 3 main(as in not support functions) functions ...

my use of delays is minimal ... maybe once?... so it should be ok...

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Aug 08, 2012 6:23 pm     Reply with quote

After some consideration, i think the way the compiler time/frequency based intrinsics work is not so well suited to the problem of changing master clock and baud rate together, let alone independently Very Happy

I would try to to this by asserting
ONE use delay statement for a 16mhz oscillator - and then to resolve the baud and timing issues like this:

Set a with a flag called MHZ16 when at 16mhz
and zero when at 4 mhz.

I don't do delays - preferring a timer tick rate that i control
for all delays.

When running at 16mhz - my 'tic' timer runs at div_256
but is set to div_64 when 4 mhz is switched to and is running.

A single delay function call for my delays then work in both speeds.

I don't use any ccs delay with greater span than delay_cycles().

I call my own delaytix(); function which is made
internally consistent by changing the prescalar divisor when clocks change.

but now ;-((
Since only constants are allowed for setbaud
my set baud becomes

If( mhz16 ) setupt_usart( real_16mhz_115200_whatever_baud)
else setup_usart( 38400_baud) // divisor for 9600 post compile as if it is 16mhz

Does this work?
Gabriel



Joined: 03 Aug 2009
Posts: 1085
Location: Panama

View user's profile Send private message

PostPosted: Thu Aug 09, 2012 6:28 am     Reply with quote

yeah... i think that would solve my problem!
ill go for a 20M crystal though....(dont have any 16 in my lab) should be the same exept its div/5

its sort of a hack...but seems legit.

ill build my own delay function as you did (unless you want to post it ;)) using tmr 0. (tmr 1 is my RTC so no go on that)

in any case... ill get back to this during the weekend
Code:
While(mother_in_law == in_town) coding_time--;


and i shall let you know my developements...

thank you all for your help!
G
_________________
CCS PCM 5.078 & CCS PCH 5.093
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Aug 09, 2012 7:08 am     Reply with quote

Quote:

should be the same exept its div/5


timers only have powers of 2 for adjustment
hence the need to use 16 mhz Very Happy Very Happy

Code:

#bit  T0IF    = 0xFF2.2  // 18F family timer 0 flagreg

void tix1delay(unsigned int8 mytix){ // or a 16 bit count if you like
      if (!mytix) return;  // trap call of 0
      set_timer0(0); // zero flag
       T0IF=0;
     DO {
        while (!T0IF) {
           // cycles free for the taking here
           // if (early_exit_condition) return;
            // prematurely
        };
        T0IF=0;
        --mtix;
      } while (mytix);
}
Gabriel



Joined: 03 Aug 2009
Posts: 1085
Location: Panama

View user's profile Send private message

PostPosted: Thu Aug 09, 2012 1:34 pm     Reply with quote

5 internet points for you!

thanks for the TMR0 function!

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Aug 09, 2012 2:46 pm     Reply with quote

Be warned : with my timer presets math - each "tick" is 16.384 msecs.

Other ratio-metric timer divisors can make shorter delays if need be.

Scaling the timer0 divisor LOWER for an extra divide by 16 gives a pretty decent 1 msec tick - so long as precise timing is not too critical.

ie timer divisor of 16 and 1 respectively for 16mhz and 4 mhz.

I tend to not need exact 1 msec increments but you might.
Gabriel



Joined: 03 Aug 2009
Posts: 1085
Location: Panama

View user's profile Send private message

PostPosted: Tue Sep 18, 2012 9:13 pm     Reply with quote

Hi, sorry to dig up my old thread...

I just wanted to let you all know i got it working perfectly...

Today is the first time i actually have been able to code since i last replied on this thread...

Setup_Uart(); did the trick... i can now print at 115200 and 9600 and switch frequencies.... just have to remember 48000 is 9600....

Smile

and code looks alot cleaner...

I just wanted to thank all of you that contributed!

thanks!

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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