Hi,
Below is an example code that PWM's an LED continually up/down. Variable pwmDirection becomes +/-. You will see at the bottom of the code that if pwmDirection is <20 (as we are subtracting), the variable pwmDirection is changed to -pwmDirection. My question is if pwmDirection is already (-) and we make it (-) again how does it become (+)? The code is working as it should, im trying to learn why. Thanks in advance!
Code:
#include<msp430g2553.h>
int pwmDirection = 1;
void main(void){
Code:
WDTCTL = WDT_MDLY_32; // Watchdog timer ≈32ms
IE1 |= WDTIE; // enable Watchdog timer interrupts
P1DIR |= BIT6; // Green LED for output
P1SEL |= BIT6; // Green LED Pulse width modulation
TA0CCR0 = 1000; // PWM period
TA0CCR1 = 1; // PWM duty cycle, time cycle on vs. off, on 1/1000 initially
TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set -- high voltage below count and
// low voltage when past
TA0CTL = TASSEL_2 + MC_1; // Timer A control set to SMCLK clock TASSEL_2, 1MHz
// and count up mode MC_1
_BIS_SR(LPM0_bits + GIE); // Enter Low power mode 0
}
#pragma vector=WDT_VECTOR // Watchdog Timer interrupt service routine
__interrupt void watchdog_timer(void) {
TA0CCR1 += pwmDirection*20; // Increase/decrease duty cycle, on vs. off time
if( TA0CCR1 > 980 || TA0CCR1 < 20 ) // Pulse brighter (increasing TA0CCR1) or dimmer
pwmDirection = -pwmDirection;
}