SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By Theicaruseffect
#172978
Hello all. I'm trying to get PWM working on P1.0 but it's having issues where the light is just staying on, not clearing at the end of each pulse. Now I've enable CCIE and the ISR is being execuited, but I'm unable to
have any control over the PWM. I've looked into TAIV too, but I'm having no luck whatsoever.

Could somebody please point me in the right direction please?

Thanks
Code: Select all
#include <legacymsp430.h>

void setupPorts()
{
        P1DIR = 0xFF;
        P1OUT = 0;  
}
 
void setupPwm()
{
        //ACLK, UP MODE, Interrupt enabled, Divide by 2
        TACTL = TASSEL_1 | MC_1 | ID_1;
        TACCR0 = 128;
        TACCR1 = 12; //10 % duty cycle
        TACCTL1 = OUTMOD_7; //Reset and set
        TACCTL0 = CCIE; //Interrupt on compare
}
 
int main(void)
{
        WDTCTL = WDTPW | WDTHOLD;
        setupPorts();  
        setupPwm();  
        _BIS_SR(LPM0_bits + GIE);
        return 0;  
}
 
interrupt(TIMER0_A0_VECTOR) timera0_isr(void)
{

        P1OUT ^= BIT0; 
}

By UhClem
#173008
I can see a couple of obvious problems:

The timer is configured to use ACLK but ACLK is never configured.

P1.0 can source TACLK but not a timer output. (I am assuming that the device is a MSP430G2553 because you didn't say what you are using.) You can generate PWM on P1.2 and you do have it configured for that but you didn't use P1SEL to connect the timer to that pin. So no output.
By Theicaruseffect
#173009
Cheers for the reply. Yes, I'm using the msp430g2553. Are you saying that the only way I can configure a timer for PWM is by connecting it to the pin thats connected to the timer?

I'm not too sure if I'm correct, but surely I can control the outputs inside the ISR of a configured PWM timer? I understand that when I enable CCIE, an interrupt gets requested (when TAR reaches CCR1, based on a reset / set mode). I should be able to drive the pins inside the ISR when CCI gets execuited?

Thanks for your reply
By UhClem
#173012
Theicaruseffect wrote:Cheers for the reply. Yes, I'm using the msp430g2553. Are you saying that the only way I can configure a timer for PWM is by connecting it to the pin thats connected to the timer?
The device data sheet describes in great detail which functions are connected to which pins. If you want a timer output you must connect to a pin for which that is an option.
I'm not too sure if I'm correct, but surely I can control the outputs inside the ISR of a configured PWM timer? I understand that when I enable CCIE, an interrupt gets requested (when TAR reaches CCR1, based on a reset / set mode). I should be able to drive the pins inside the ISR when CCI gets execuited?
You would need two interrupts. For example you could the TACCR0 interrupt to reset the output and TACCR1 to set it. But you wouldn't want to do that at a very high rate because of the CPU cycles it would use.
By Theicaruseffect
#173013
"you would need two interrupts. For example you could the TACCR0 interrupt to reset the output and TACCR1 to set it."

I thought of this today! However, I really like to reduce to the CPU, so this option may not suffice.

I've read the datasheet. I will just hook up an led to the pin that's connected to the timer. On the texas launchpad, one of the two leds (the one connected to a timer) is connected to the SCL line for the i2c bus. I'm using this
bus to control the pwm's duty cycle. The texas launchpad's i2c slave doesn't work if the jumper is connected to P1.6 (which is also connected to a timer). I will just hook up an external LED to the pin connected to a different timer.

Thanks a dozen!!!!
By Theicaruseffect
#173308
Hello again. I'm still having trouble trying to generate a PWM on any arbitary pin on my texas instrument launchpad (msp430g2553). The light comes on, but I haven't got any control of the duty cycle. I really would like to be able to drive any pin on the microcontroller using PWM.
Code: Select all
#include <legacymsp430.h>

#define LED_RED BIT0
#define LED_GREEN BIT6

void pwm_config()
{
        P1DIR = 0xFF;
        P1OUT = 0x00;
        //ACLK, UP Mode, Timer interrupts
        TACTL = TASSEL_1 | MC_1 | TAIE;
        TACCR0 = 128; // PWM Period
        TACCTL0 = OUTMOD_7 | CCIE; //Reset / Set (see page 364 of the user guide)
        TACCR1 = 12; // 10% Duty cycle this doesn't work
}

interrupt(TIMER0_A0_VECTOR) timerccr0(void)
{
        P1OUT = LED_RED;
        //Should I be clearing any flags here?
}

interrupt(TIMER0_A1_VECTOR) timerccr1(void)
{
        P1OUT = 0;
}

int main(void)
{
        //Stop watchdog time
        WDTCTL = WDTPW + WDTHOLD;     // Kill watchdog timer
        pwm_config();
        _BIS_SR(CPUOFF + GIE);
        return 0;
}