This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553: How to use this timer class with a button?

Part Number: MSP430G2553

Hello! I am currently taking a class in programming embedded systems and we recently got this device to do our labs/tasks on.

This week's task is to toggle the red light on and off using the Button (BIT3) and also to write the code in seperate files using header files and classes. 

Anyway, I have got the functonality to work, as in button press --> on, press again --> off. My problem is, I don't understand how to use this timer.c class that we were given. I suppose it's meant to be used in a way so that the debounce problem is fixed?

Here is the code for timer.c

#include "project.h"
#include "led.h"

#include "timer.h"

#define DIVIDER 8
#define TMRC (DCO_FREQ / DIVIDER / 1000 * TIMER_INTERVAL)

static tWord tickCount=0;

void Timer_Init() {

        BCSCTL1 = CALBC1_16MHZ;
        DCOCTL = CALDCO_16MHZ;
        BCSCTL2 = 0x0 ; // MCLK clock source DCOCLK, MCLK divider 1, SMCLK source DCOCLK, SMCLK divider 1

        // Set up interrupts and timer 0

        // Enable interrupts on timer
        CCTL0 = CCIE;

        // Use clock SMCLK, UP counting, divided of 8
        TACTL = TASSEL_2 + MC_1 + ID_3;
       
        // Set compare value
        CCR0 = TMRC;
}

__attribute__((interrupt(TIMER0_A0_VECTOR))) void Timer_A(void) { 
timer_run(); 
} 

tWord getTick() {return tickCount;}

timer_run() {
	tickCount++;
	Led_Update();
}

tWord == unsigned int

#define TMRC (DCO_FREQ / DIVIDER / 1000 * TIMER_INTERVAL) <-- In this lie, nothing is defined and I don't even understand what DCO_FREQ is? TIMER_INTERVAL I think has to be set by the main function which I actually do.

Led_Update is written by me:

void Led_Update() {
	if(status==LED_STAT_ON) {
		RED_LED_PORT |= RED_LED_PIN;
	}
	else {
		RED_LED_PORT &= ~RED_LED_PIN;
	}
}

The status variable changes on button press.

Can anyone give me some tips on how to use the timer class? I also obviously use __enable_interrupt(); in my main class. I have noticed the Timer_A interrupt method runs quite frequently if I manually set CCR0 = 10000; for ex....

Thanks!

  • Matheos Mattsson said:
    #define TMRC (DCO_FREQ / DIVIDER / 1000 * TIMER_INTERVAL) <-- In this lie, nothing is defined and I don't even understand what DCO_FREQ is? TIMER_INTERVAL I think has to be set by the main function which I actually do.

    "DCO_FREQ" should be defined in one of your header files and should be equal to the frequency of the DCO, which sources the system clocks. Seeing you're using 16MHz calibration factors for setting up the DCO in your Timer_Init() function, I would assume it equals approximately 16000000. Search your header files to be sure. I would recommend moving the DCO configuration code outside of your Timer_Init() function and into a system-level init function. You can reference our code examples to make sure the DCO is set up correctly.

    Regarding the purpose of the timer, I'm not really sure of it's intended use here. Debouncing is possible, but I've also seen where you can use the timer to blink the LED at a frequency indicated by how fast you push the button. The faster you push it, the faster the LED will blink. Again, if you have questions about how to use a timer, you can reference our code examples.

    Regards,

    James

  • DCO_FREQ is NOT defined in any of my header files (not even if u are refering to msp430.h). The header file for the timer is as follows
    #ifndef _TIMER_H
    #define _TIMER_H

    #include "project.h"

    void Timer_Init();

    void timer_run();

    tWord getTick();

    #endif
  • Matheos Mattsson said:
    DCO_FREQ is NOT defined in any of my header files (not even if u are refering to msp430.h).

    It must be defined somewhere. What does TMRC equal? You're using it to set CCR0 in your code. If you're using CCS, try holding down the "Ctrl" key, take the mouse cursor, and click the "DCO_FREQ" define. It should take you to the file where it's defined. If not, you can highlight it and search for its source or definition in CCS. Have you figured out how you're supposed to use the timer?

    Regards,

    James

**Attention** This is a public forum