Thursday 16 July 2020

Pulse Width Modulation using MSP430 Timer (Launchpad)

Video based Tutorial for PWM generation using MSP430 is covered on my YouTube channel:

Please Subscribe to the Channel!

Code used for generation of PWM:

#include <msp430g2553.h>

void main(void)
{
 WDTCTL = WDTPW + WDTHOLD;  // Stop WDT
 P1DIR |= BIT6;             // P1.6 set for output
 P1SEL |= BIT6;             // select TA0.1 output signal
 CCR0 = 1000-1;             // PWM Time Period/ frequency (1 KHz)
 CCTL1 = OUTMOD_7;          // reset/set mode 7 for output signal
 CCR1 = 750-1;                // PWM Duty cycle is 75%
 TACTL = TASSEL_2 + MC_1;   // SMCLK and Up Mode
 _BIS_SR(LPM0_bits);        // Enter LPM0
}


You can change the value in CCR1 and get different Ton/ Duty cycles. This will cause the LED to appear brighter or dimmer (higher the value or closer it is to CCR0 value, the LED will appear brighter).
Time period or frequency of the PWM signal can be changed by selecting different clock frequency or by dividing the clock frequency and by changing the CCR0 value.

Monday 6 July 2020

MSP430 Timer in Compare Mode - Part 1 (Up Mode)


Video based Tutorial for Timer in Compare mode is available on My YouTube Channel: https://youtu.be/Drisf7VQI60

Please Subscribe to the Channel!

Code:


#include <msp430g2553.h>

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P1DIR |= 0x40;                            // P1.6 output
  CCTL0 = CCIE;                             // CCR0 interrupt enabled
  CCR0 = 65000;
  TACTL = TASSEL_2 + MC_1;                  // SMCLK, upmode

  _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt
}

// Timer A0 interrupt service routine  - CCR0 vector
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
  P1OUT ^= 0x40;                            // Toggle P1.6
}