Other Parts Discussed in Thread: MSP430FR5969
Hello community,
I just got myself a MSP430FR5969 Launchpad to get a feeling for the MSP430 µControllers. Now I'm playing around with it a little bit to see how everything works. I have started with switching on the LEDs, to reading buttons, combining everything and now I want to change the brightness of the LED via PWM.
#include <msp430.h>
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings
P1DIR |= BIT0; // Set P1.0 to output direction
P1OUT &= ~BIT0; // Switch LED off
P1DIR &= ~BIT1; // Set P1.1 as input
P1OUT |= BIT1; // Configure P1.1 for Pull-Up
P1REN |= BIT1; // Enable Pull Up of P1.1
P4DIR &= ~BIT5; // Set P4.5 as input
P4OUT |= BIT5; // Configure P4.5 for Pull-Up
P4REN |= BIT5; // Enable Pull Up of P4.5
TA0CCTL1 = OUTMOD_7; // Reset/Set Mode
TA0CTL = TASSEL_2 + MC_1 +TACLR ; // SMCLK / Upmode
TA0CCR0 = 100-1; // PWM Frequency 10 kHz
TA0CCR1 = 50; // 50% Duty Cycle
P1SEL0 |= BIT0; // PWM output to LED P1.0
P1SEL1 &= ~BIT0;
while(1)
{
if(!(P1IN & BIT1))
{
TA0CCR1 += 10;
}
else if(!(P4IN & BIT5))
{
TA0CCR1 -= 10;
}
}
}
The problem are the last view lines where I try to change the brightness of the LED by pushing a "Up" or "Down" button. After program start the PWM output will be enabled and is running just fine. As soon as I press a button and therefore update the TA0CCR1 register the PWM output will stop and I only measure a permanent high level on the LED.
When I change the last few line to this
while(1)
{
if(!(P1IN & BIT1))
{
TA0CCR1 = 90;
}
else if(!(P4IN & BIT5))
{
TA0CCR1 = 10;
}
}
the switching of the brightness works flawlessly. Bright -> Dark -> Bright -> Dark and so on. Trying to reduce the "step size" or working with a variable to change the CCR register does not help. The PWM will crash as soon as I try to set it dynamically. During debugging I can see that the register is updated correctly and not out of range. Still the PWM won't work anymore after the first button press. To make everything worse there are lots of examples in the net which are doing the same thing as I do and change the PWM duty cycle on the fly.
So I ask you what am I doing wrong?
Yours faithfully,
Christopher