NPEDUCATIONS, Electronic circuits development, electronics tutorials, microcontroller tutorials and projects, advanced microcontroller (ARM) based tutorials and projects, Embedded c development, Embedded c for ARM cortex M, Intel IoT based projects, IoT projects, CC3200 launch pad projects, MSP430 Launchpad tutorials and projects, Tiva C launch pad tutorials and projects, 8051 tutorials and projects, sensor interfacing with microcontroller tutorials, data communications and networking tutorials, peripheral interfacing with microcontroller, led message scrolling display, arduino based tutorials and projects, intel galileo based tutorials and project, ADC interfacing tutorials, LED Blinking, LCD interfacing, Embedded System tutorials and projects, B.tech projects, M.tech projects, online Embedded C training

Generation of PWM signal using MSP430 Launch pad timers

Before going to generating PWM signal on MSP430 Launchpad, I would like to introduce some difference between the capture and compare modes of the Timer in MSP430G2553. We know that, there are two timers categories available In MSP430 family named as Timer_A and Timer_B. Both of these have got their importance based on its implementation.
  • In Timer_B, it is possible to program the bit length like 8-bit, 10-bit, 12-bit, 16-bit, which is available in Timer_A category.
  • Some Timers belongs to Timer_B have 7 CCR registers, but the Timer_A contains three capture/compare registers (CCR).
  • Timer_B contains double buffered capture/compare registers (CCR).
  • Remember MSP430G2553 contains two 16-Timers (Timer_A0 and Timer_A1) which belong to Timer_A category. 
Basically these timers may operate in two modes.
  •  Capture mode.
  •  Compare mode.
Whereas the counting modes for these two timers are
  • Up mode.
  •  Down mode.
  •  Up/down mode.
  •  Continuous mode.
Ok! Now we will look into capture mode and compare mode.

Capture mode: It is used to record/capture the time events for speed computations and time measurement. This will be very useful Robotics applications like Frequency measurement of a signal, motor speed calculations, degree of freedom in robotic arms, etc.,

Compare mode: It is used to generate Specific time periods which replaces the delay loops in software. By doing so, the timer allows CPU to sleep by consuming less power. It is also used to generate PWM signals which is more suitable in applications like MOTOR driving, LED display intensity control, etc.,

The Timer_A in MSP430G2553 contains mainly two sections as shown in the figure 1.1
  •  Timer Block
  •  Capture/compare block
Timer Block is mainly responsible for the basic configurations of the Timer like count mode selection, clock source selection, etc., for basic time intervals generation the user may simply concentrate on this block and can easily start his programming on timers. Here I am not going to discuss about the Timer block in details rather I will concentrate on Capture/compare block. I encouraged you read my tutorial  about “UNDERSTANDING TIMERS IN MSP430 LAUNCHPAD” .Capture/compare Block contains configuration settings for capture mode and compare mode operations. There are three capture/compare blocks (CCR0, CCR1, and CCR2) available in TIMER_A in MSP430G2553.
Figure 1.1 shows the basic Timer Block and three capture/compare blocks (CCR0, CCR1 and CCR2 respectively).
Capture mode:
  • If CAP = 1, capture mode is selected.
  • CCIxA and CCIxB are the input pins for capturing an event.
  •  CCISx bits are used to select one of the capture input pins.
  •  There are three types of triggering of capture input pins (raising edge, falling edge, both) and these can selected by using CMx bits.
  • If capture occurs, Timer value is copied into the TACCRx register and the interrupt flag CCIFG is set automatically.
NOTE: The input level signal can be read at any time via the CCI bit. Therefore the capture signal is asynchronous to the timer clock and cause race condition. Setting the SCS bit will sync the capture with the next timer clock.

Compare mode:
  • Each capture/compare block contains an output unit and it is used to generate output signals such as PWM signals.
  •  To select Compare mode, CAP should be ‘0’ (CAP = 0).
  • When the count value in the ‘TAR’ register reaches the value in a TACCRx, then, CCIFG          interrupt flag is set.
  • Internal signal EQUx = 1, the EQUx affects the output according to the output mode.
  • Each output unit has eight operating modes  as shown in the figure 1.2 that generate signals based     on the 
      EQU0 and EQUx signals and it can be selected by using OUTMODx.
Figure 1.2 shows that eight operating modes of output unit and its corresponding OUTMODx bits.
Figure 1.3 shows that the generation of different output signals by configuring the output unit in different modes and the timer operates in UP mode.

FIGURE 1.3 shows different output signals by configuring output modes in UP mode operation of the timer.
Now we will switch into the program to generate the PWM output on P1.6 pin of the MSP430 Launch pad, so that green LED will flash according to duty cycle provided in the program. Here the Timer0 is source from ACLK which is sourced from VLO (12 KHz) oscillator.

Step 1: Stop the watch dog timer, to avoid unnecessary resets of the microcontroller. We will discuss more 
            on watch dog timer later.

            WDTCTL = WDTPW + WDTHOLD; // stop watch dog timer
Step 2: make cpu to run with MCLK from DCO source. You can use any clock to drive cpu.

            BCSCTL1 = CALBC1_1MHZ;            
            DCOCTL = CALDCO_1MHZ;

Step 3:  BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is selected.

             BCSCTL3 |= LFXT1S_2;                     

Step 4:  Configure the MCLK and SMCLK by setting proper bits BCSCTL2. SELM_0 -- selects MCLK 
             source from DCOCLK, DIVM_3 --> MCLK = DCO/8,
             We should remember that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO  
             clock source" DIVS_3 --> SMCLK = DCO/8.

             BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;

Step 5:  Configure the GPIO port to blink the LED according to the PWM output signal. In MSP430G2553  
             port pins has multiple functionalities (like GPIO, UART, Timers, PWM, SPI, I2C, etc.,). To choose  
             a special functionality out of these many functionalities of a pin, there is a special function registers  
             P1SEL and P1SEL2 as shown in figure 1.4. Here we are using P1.6 (green LED) which has 
             “P1.6/TA0.1/UCB0SOMI/UCB0SCL/A6/CA6/TDI/TCLK” and we have to use P1.6 as 
             TA0.1. To do this, make the 6th bit of Port1 in P1SEL as ‘1’ and in P1SEL2 as ‘0’ as shown in 
             figure 1.4.
Figure 1.4 shows port pin multiplexing capability and its selection.

P1SEL2 |= ~BIT6;  
             P1SEL |= BIT6;                     // TA1 option is select, P1SEL2 and P1SEL combination is ‘01’
             Make the direction of the bit6 as output.
             P1DIR |= BIT6;                             // MAKE P1.6 (TA0.1) as output port

Step 6: Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - (TASSEL = 1) 

            and in up mode (MC = 1). TACLR bit clears the TAR register, clock divider and count direction.

           TACTL = TASSEL_1 + MC_1 + TACLR;

           Load the count 12000 in counter compare register to generate 1 second delay (12000 * T) = 12000  
           * (1/12000) = 1sec; T = 1/F = 1/12 kHz. This count define the PWM period (Ton + Toff).

               CCR0 = 12000; // PWM period.
               CCTL1 = OUTMOD_7; // RESET/SET
               CCR1 = 6000-1;   // 50% duty cycle (12000/6000 = 0.5).

              And the PWM waveform will generate depend upon the duty cycle as shown in the figure 1.5
Figure 1.5 shows the PWM waveform based on the duty cycle using CCR0 and CCR1 registers.
Step 7: use infinite loop or Lower power mode (LPM). We haven’t discussed about the low power mode, 
            so I use infinite loop even though it wastages the CPU time.
             While (1);

code:
#include 

void Config_TimerA(void);
void Config_GPIO(void);


void main(void)
{

	WDTCTL = WDTPW + WDTHOLD; // Stop the Watch dog timer

	BCSCTL1 = CALBC1_1MHZ;                    // make cpu to run with MCLK from DCO source
	DCOCTL = CALDCO_1MHZ;
	BCSCTL3 |= LFXT1S_2;                      // BCSCTL3 is used to low frequency crystal. LFXT1S_2 --> LFXT1 = VLO oscillator is selected

	//In the basic clock system by default OFIFG (oscillator-fault flag) bit is set and force the MCLK to use DCO as its source.
	//IFG1 &= ~OFIFG;                           // Clear OSCFault flag not used here

	BCSCTL2 |= SELM_0 + DIVM_3 + DIVS_3;      // SELM_0 -- selects MCLK source from DCOCLK, DIVM_3 --> MCLK = DCO/8,
	// We should remember that "Up on reset, MCLK and SMCLK are source from 1.1MHz DCO clock source"
	//DIVS_3 --> SMCLK = DCO/8
	Config_GPIO();
	Config_TimerA();

	while(1);

}

void Config_GPIO(void)
{
	P1DIR |= BIT6;                             // MAKE P1.0  and P1.2 (TA0.1) as output port
	P1SEL |= BIT6; // TA1 option
}

void Config_TimerA(void)
{
	// Configure the TimerA to operate with ACLK which is source from VLO (12KHz) - (TASSEL = 1) and in up mode (MC = 1)
	TACTL = TASSEL_1 + MC_1 + TACLR;
	CCR0 = 1000-1; // PWM Period
	CCTL1 = OUTMOD_7;// Reset/set
	CCR1 = 200-1; // 50% Duty cycle
}

11 comments :

  1. Dear Sir,

    I am not able to see the the images(its not showing).

    ReplyDelete
    Replies
    1. try to refresh the page or send me the mail ID i will send you the doc!

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. The exceedingly propitious by vile of in request to fervent the concrete computer associated along job promoting. Retain pertaining to tell this share.travel nurses

    ReplyDelete
  4. Output is not toggling on pin1.6 after running program. It remain on "while(1)" statement. TACCR0 and TACCR1 registers values also not decreasing.

    ReplyDelete
  5. Once i run the code TAR register is not incrementing which compare the value with TACCR0 register. How to know the clock is active and TAR is getting the clock or not

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. I am running the program on simulator. TAR register is not increment, so never reach to desire value set in TACCR0 register and execution remains on while(1) loop. Is there any setting require in simulator?

    ReplyDelete
  8. These are so adorable! My husband's birthday is coming up in a few weeks and I need some unique gift ideas for him. I don't have a ton of money to spend but I want to get him something that will last. Let me know if you have any recommendations.sacramento excel training class

    ReplyDelete
  9. First of all i appreciate the author for taking the effort to share an article about a very good topic. This article throws light on the new film : In Between Black and White!!!. What surprises me is that my favorite star Zara Phillips. who is a singer, song writer, actor and author, is also acting in this.orange county excel training class

    ReplyDelete
  10. Hey I am so grateful I found your site, I really found you
    by error, while I was browsing on Digg for something else, Anyhow I
    am here now and would just like to say thank you for a incredible post and a all round enjoyable blog wdi reports

    ReplyDelete

Thanks for visiting NPEDUCATIONS. We will contact you soon

11 comments :

Mallappa T said...

Dear Sir,

I am not able to see the the images(its not showing).

Lovakiranvarma Myla said...

try to refresh the page or send me the mail ID i will send you the doc!

dishwasher repairs perth said...
This comment has been removed by a blog administrator.
travel nurses said...

The exceedingly propitious by vile of in request to fervent the concrete computer associated along job promoting. Retain pertaining to tell this share.travel nurses

Anonymous said...

Output is not toggling on pin1.6 after running program. It remain on "while(1)" statement. TACCR0 and TACCR1 registers values also not decreasing.

Anonymous said...

Once i run the code TAR register is not incrementing which compare the value with TACCR0 register. How to know the clock is active and TAR is getting the clock or not

black and white bow tie said...
This comment has been removed by a blog administrator.
Anonymous said...

I am running the program on simulator. TAR register is not increment, so never reach to desire value set in TACCR0 register and execution remains on while(1) loop. Is there any setting require in simulator?

sacramento excel training class said...

These are so adorable! My husband's birthday is coming up in a few weeks and I need some unique gift ideas for him. I don't have a ton of money to spend but I want to get him something that will last. Let me know if you have any recommendations.sacramento excel training class

orange county excel training class said...

First of all i appreciate the author for taking the effort to share an article about a very good topic. This article throws light on the new film : In Between Black and White!!!. What surprises me is that my favorite star Zara Phillips. who is a singer, song writer, actor and author, is also acting in this.orange county excel training class

wdi reports said...

Hey I am so grateful I found your site, I really found you
by error, while I was browsing on Digg for something else, Anyhow I
am here now and would just like to say thank you for a incredible post and a all round enjoyable blog wdi reports

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail or like our Facebook page.
Related Posts Plugin for WordPress, Blogger...

 
Powered by Blogger