SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By pmlonline
#107245
I'm using msp430-gcc. Does anyone have any code to tell how much time has passed? Preferably in microseconds or clock cycles, but milliseconds might be good enough.

Thanks,
Paul
By pmlonline
#107248
Why redo something when it's already been done? Science is about standing on the shoulders of others.
User avatar
By leon_heller
#107255
Just use a timer and write it yourself. You will learn more than by copying someone else's code. You could have had it working by now.
By pmlonline
#107257
This low level coding shouldn't be required by everyone to learn MCU's. Besides I'd rather learn by coding something that hasn't already been done-- more product for everything.

Anyhow, I have been reading about all of that, but it's far more difficult then you make it out. Quote, see bold text -->

+++
It's possbile, but not as easy as on the PC where the Pentium processor has an internal tick counter.

You'll need to set SMCLK or ACLK to the same clock frequency as MCLK (for ACLK it is only possible on newer devices), then set up a timer to be clocked by SMCLK or ACLK, enable overflow interrupt and write an ISR which counts the overflows. If you never disable interrupts for longer than ~65500 clock cycles, the overflow counter will hold the upper 16 bit (or 32 bits if you use a long int) and the timer counter register the lower 16 bits of the passed cpu ticks since starting the timer. It is a bit difficult, however, to retrieve the current value, as it is possible that an overflow occurs between collecting the two 16bit values (orbetween reading the upper and lower 16 bit of the long counter, if used).

Also, this approach consumes some processor time too, as every 65536 cpu cycles the ISR is entered and consumes 20-30 cycles for updating the overflow counter.

You can, however, use the dividers for SM/ACLK and/or timer to break down the count frequency by a factor of up to 64. This reduces the resolution of the tick counter by 64, but also reduces the cpu usage by the same factor. And maybe eliminates the need of using a long value for the overflow counter.
+++
User avatar
By leon_heller
#107262
You'll find lots of timer examples in slac163.zip, or the equivalent for the chip you are using. It's on the TI web site.
By pmlonline
#107263
Thanks. I found the zip file, but it's MSP430F21x2. I'll scan through it anyways.

I came across this code from another source. It uses BTCTL from basic_timer.h. The code only gets 1/2500 second resolution, but maybe it could be tweaked to get better resolution. Looks like there's an overflow issue in this code.

+++++++++++
int seconds;
int minutes;
int hours;
int days;
int ticks;
// This will drift and will require calibration – ok for testing though.
#define TICKS_PER_SECOND 2500
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ticks = seconds = minutes = hours = days = 0; // start clock at 0 run time
IE2 |= BTIE; // Enable BT interrupt
BTCTL = BTSSEL+BTIP2+BTIP1+BTIP0; // Divide System clock by 256
// The default system clock is approx 640kHz
// so this should give a tick rate of 2500Hz
_EINT(); // Enable interrupts
for (;;)
{
_BIS_SR(CPUOFF); // Enter LPM0
_NOP(); // Required only for C-spy
}
}
// Basic Timer interrupt service routine
interrupt[BASICTIMER_VECTOR] void basic_timer(void)
{
ticks++;
if (ticks >= TICKS_PER_SECOND) {
ticks = 0;
seconds++;
if (seconds > 59) {
seconds = 0;
minutes++;
if (minutes > 59) {
minutes = 0;
hours++;
if (hours > 23) {
hours = 0;
days++; // day of week, calender etc. not implemented.
}
}
}
}
}
+++++++++++
By pmlonline
#107264
Actually it seems the basic_timer.h is not for the MSP430x16x. So much for that code.
By pmlonline
#107271
In case this helps anyone, this code gets Timer A down to ~ 10 microseconds per tick -->

// Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD;

// Select SMCLK, clear TAR. MC1 starts Timer_A in continuous mode.
TACTL = TASSEL1 | TACLR | ID_3 | MC1;


There might be other code required to get it going. Simply read TA0R variable. When it hits 65535 it rolls over to 0. I'm sure there's a way to set an interrupt when it rolls over.