EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Timer B question

Started by kelsonbatista April 20, 2011
Hi All

I need to implement 2 timers (B), one running each second and another 1KHz, but I am not getting the second working properly.

Source:
MCLK ==> 8MHz
ACLK ==> 32KHz

Timer B Setup:
TBCTL = TBSSEL_1 + ID_0 + MC_1 + TBIE + TBCLR;
TBCCR0 = 32768;
TBCCTL0 |= CCIE;
TBCCR1 = 32;
TBCCTL1 |= CCIE;

The TBCCR0 is ok, interrupt for 1Hz.
But TBCCR1 is not giving me 1 KHz, it looks like 1 Hz too. :s

Can anybody tell me what I am doing wrong?
Thanks
Kelson

Beginning Microcontrollers with the MSP430

Are you sure your ISR are correct ? CCR0 and CCR1 use a different vector
For a start you have not chosen your timer allocation wisely from the
information given. If you have a timer B then you have a timer A. In
this case I would set timer A to source from ACLK and timer b to source
from MCLK. then set timer A up pretty much as you have for your 1 second
timebase. next set Timer B up for continuous running, using the count to
mode inhibits timer flexibility quite a bit. Then set CCRB0 to 8000 and
set the interrupt. If you need to set pins you can use the built in pin
control, else simply handle this in the ISR. the reason your code isn't
working is because you need to keep resetting the CCRA1 register at
every interrupt, ie CCRA1 += 32, otherwise you only get what you're
seeing, a single interrupt after the timer has rolled over and clocked
back around to 32.

Cheers

Al
which chip are you employing?
Not related to your specific question, but on some other processors you need
to count to 32768-1, because 0 counts. That is, the timer counts from 0 to
32767 to get 32768 distinct pulses, not 1 to 32768. I don't have the data
sheet in front of me to decode your mode, but something to verify.

Devin
> Are you sure your ISR are correct ? CCR0 and CCR1 use a different vector

Yes, the ISR's are corret.

> For a start you have not chosen your timer allocation wisely from the
> information given. If you have a timer B then you have a timer A. In
> this case I would set timer A to source from ACLK and timer b to source
> from MCLK. then set timer A up pretty much as you have for your 1 second
> timebase. next set Timer B up for continuous running, using the count to
> mode inhibits timer flexibility quite a bit. Then set CCRB0 to 8000 and
> set the interrupt. If you need to set pins you can use the built in pin
> control, else simply handle this in the ISR. the reason your code isn't
> working is because you need to keep resetting the CCRA1 register at
> every interrupt, ie CCRA1 += 32, otherwise you only get what you're
> seeing, a single interrupt after the timer has rolled over and clocked
> back around to 32.

Hi Al

I cannot use Timer A because it's already running for another application, so only Timer B is available to generate both frequencies.

Regarding the reset, I think is really something related to it. I did some tests like you said, but it generates 1KHz (TIMERB1) just at the begining and then TIMERB1 stops when 1Hz (TIMERB0) goes to second cycle. Is there any other idea to keep running in a infinite loop?

> which chip are you employing?

Hi

I am using the MSP430FG4618.
The whole question is ok, only what I need to know is how to run different frequencies from the same timer.
> Not related to your specific question, but on some other processors you need
> to count to 32768-1, because 0 counts. That is, the timer counts from 0 to
> 32767 to get 32768 distinct pulses, not 1 to 32768. I don't have the data
> sheet in front of me to decode your mode, but something to verify.

Hi Devin

In fact I heard something about this but I did some tests running only the 1Hz (32768) frequency and looks ok, so I do not think this is the problem. I just need to run different frequencies from the same timer.
Timer A has multiple channels, are you using them all? Are you using the
Timer A overflow ISR as well? There are 4 different very flexible timing
functions you can grab from Timer A. Anyway the principal is the same.
Your 1kHz doesn't work because you are NOT refreshing the compare
register. Also, since you choose to use the 'Count to' mode to generate
your 1Hz time base you have also forgotten that after 32768 is reached
in the 1kHz compare register it will never see the next value becasue
the count overflows and resets to 0. that is only one of the reasons I
hate using the count to mode in most instances. I would simply set the
timer to continous mode, set a constant OneHz and another kHz. Make
OneHz equal to 32768 and khz equal to 32, in each relevant ISR simply do:_



using constants lets you fiddle around with other values and change the
frequency painlessly.

If you insist on using the count to mode then change khz_ISR to:_



Al

Memfault Beyond the Launch