PWM output of MSP430 library timer Ta

Source: Internet
Author: User

Timers are commonly used by single-chip microcomputer. They are used to generate precise timing or other functions. MSP430 timers can not only complete precise timing, but also generate PWM waveform output, and capture time value (when the rising or falling edge arrives ). A general PWM waveform is generated here.Program.

  1. Hardware introduction:

    The timera of MSP430 Series Single-Chip Microcomputer has complex structures and powerful functions. It is suitable for industrial control, such as digital motor control, ideal configuration of electric meters and hand-held instruments. It provides developers with more flexible options. When the PWM does not need to modify the duty cycle and time, timera can automatically output the PWM without the need to use the interrupt to maintain the PWM output.

    Msp430f16x and msp430f14x single-chip microcomputer internal contain two timer, Ta and TB; TA has three modules, CCR0-CCR2; TB contains CCR0-CCR67 module; the ccr0 module cannot completely output the PWM waveform (only three output modes are available); TA can output a complete two-way PWM waveform; TB can output six complete PWM waveforms.

    The PWM output of the timer has eight modes:

    Output Mode 0 output mode: the output signal outx is defined by the outx bit in cctlx, the control register of each capture/compare module, and is updated immediately after being written to this register. The final bit outx goes through.
    Output Mode: 1-Bit mode: When tar is equal to ccrx, the output signal is set to the specified position until the timer is reset or another output mode is selected.
    Output Mode 2 PWM flip/reset mode: when the output value of tar is equal to ccrx, It is flipped. When the tar value is equal to ccr0, It is reset.
    Output Mode 3 PWM/reset mode: when the output value in tar is equal to the value in ccrx, It is reset when the tar value is equal to the value in ccr0.
    Output Mode 4 flip mode: When the tar value of the output level is equal to ccrx, the flip mode is used, and the output period is twice the timer period.
    Output Mode 5 reset mode: reset when the output value in tar is equal to ccrx, and keep it low until another output mode is selected.
    Output Mode 6 PWM flip/placement mode: when the output level is equal to the value of tar, It is flipped, and when the tar value is equal to the value of ccr0, it is set.
    Output Mode 7pwm reset/reset mode: the output level is reset when the tar value is equal to ccrx, and when the tar value is equal to ccr0.

    Is the output waveform in the increase count mode (this program uses the increase mode 3 and 7 ):

    Count mode:

    Increase count mode
    The capture/compare register ccr0 is used as the cycle register of the timer_a increase count mode. Because the ccr0 is a 16-bit register, this mode applies to the continuous count condition with the scheduled cycle less than 65 536. The counter tar can increase the count to the value of ccr0. When the Count value is equal to the value of ccr0 (or the timer value is greater than the value of ccr0), the timer resets and starts counting again from 0.

    Continuous count mode
    The Continuous counting mode is usually used in the scheduled application that requires 65 536 clock cycles. After the timer starts counting from the current value to 0ffffh, it starts counting again from 0.

    Increase/decrease count mode
    In case of symmetric waveforms, you can often use the increase/decrease count mode. In this mode, the timer first increases the count to the value of ccr0, and then reduces the count to 0. The counting cycle is still defined by ccr0, which is twice the value of the ccr0 counter.

    Ta timers can be compared and captured in two ways; comparison can generate PWM waveforms and so on, capture can accurately measure the time; here the comparison output is used.

    There are so many hardware introductions. For more information, see msp430x1xx_family_users_guide (User Guide ).

  2. Program Implementation:

    This program is transplanted directly from msp430f42x, and can be used normally only after the port is changed. As a result, the 430 modules are common in different series and the registers are the same, but the external ports may not be the same.

    Program initialization: initializes the ta-related registers.

     Char Tapwminit ( Char CLK, Char Div, Char Mode1, Char Mode2) {tactl = 0; // Clear previous settings Tactl | = mc_1;// Set the timer TA to the increase count mode  Switch (CLK) // Select the clock source { Case  'A' : Case  'A' : Tactl | = tassel_1; Break ; // Aclk  Case  'S' : Case  'S' : Tactl | = tassel_2; Break ;// Smclk  Case  'E' : Tactl | = tassel_0; Break ; // External input (taclk)  Case  'E' : Tactl | = tassel_3; Break ; // External input (taclk inverse)  Default : Return (0 ); // Parameter error } Switch (Div)// Select the frequency division Coefficient { Case 1: tactl | = id_0; Break ; // 1  Case 2: tactl | = id_1; Break ; // 2  Case 4: tactl | = id_2; Break ; // 4  Case 8: tactl | = id_3; Break ; // 8  Default : Return (0 ); // Parameter error } Switch (Mode1) // Set the output mode of PWM channel 1. { Case  'P' : Case  'P' : // If it is set to high-level Mode Tacctl1 = outmod_7; // High-Level PWM output P1sel | = bit2; // Output from p1.2 (different MCU models may be different) P1dir | = bit2;// Output from p1.2 (different MCU models may be different)  Break ; Case  'N' : Case  'N' : // If it is set to low-level Mode Tacctl1 = outmod_3; // Low-level PWM output P1sel | = bit2; // Output from p1.2 (different MCU models may be different) P1dir | = bit2; // Output from p1.2 (different MCU models may be different)  Break ;Case  '0' : Case 0: // If disabled P1sel & = ~ Bit2; // P1.2 is restored to a common IO port  Break ; Default : Return (0 ); // Parameter error } Switch (Mode2) // Set the output mode of PWM channel 1. { Case 'P' : Case  'P' : // If it is set to high-level Mode Tacctl2 = outmod_7; // High-Level PWM output P1sel | = bit3; // Output from p1.3 (different MCU models may be different) P1dir | = bit3; // Output from p1.3 (different MCU models may be different)  Break ; Case  'N' : Case  'N' :// If it is set to low-level Mode Tacctl2 = outmod_3; // Low-level PWM output P1sel | = bit3; // Output from p1.3 (different MCU models may be different) P1dir | = bit3; // Output from p1.3 (different MCU models may be different)  Break ; Case  '0' : Case 0: // If disabled P1sel & = ~ Bit3; // P1.3 restore to a common IO port  Break ; Default : Return (0 ); // Parameter error } Return (1 );}

    It is mainly to set the tactl register so that the TA can work in the incremental mode and set the clock source and frequency division. The lx sets the corresponding output mode and enables the second function of the corresponding port.

    Set the cycle function: Set the cycle of the PWM waveform. The unit is the number of taclk cycles.

     
    VoidTapwmsetperiod (Unsigned intPeriod) {taccr0 = period ;}

    When the incremental mode is used, the TA counts to taccr0, and the cycle setting is completed when the ccr0 is set.

    Set duty cycle: set the time for the valid PWM output of TA.

    VoidTapwmsetduty (CharChannel,Unsigned intDuty ){Switch(Channel ){Case1: taccr1 = duty;Break;Case2: taccr2 = duty;Break;}}

    Set parameters for each route based on the parameters.

    Set the duty cycle. Set the duty cycle in the thousands ratio:

    * Entry parameter: Channel: The currently set channel number 1/2 percent: The kilobytes of PWM effective time ratio (0 ~ 1000) * exit parameter: none * Description: 1000 = 100.0% 500 = 50.0%, and so on * example: tapwmsetpermill (1,300) set the duty cycle of PWM channel 1 square wave to 30.0% tapwmsetpermill (2,825) set the duty cycle of PWM channel 2 square wave to 82.5% */VoidTapwmsetpermill (CharChannel,Unsigned intPercent ){Unsigned long intPeriod;Unsigned intDuty; period = taccr0; duty = period * percent/1000; tapwmsetduty (Channel, duty );}

    This function sets the effective time of the PWM output in a kilobytes. Convenience for program use.

    Many routines provided by Ti for timers are simple and clear. If you need other functions, You can compile the corresponding program based on the routine. There are so many program implementations. The following describes how to use this program.

  3. Example:

    Usage: add the c file to the project. The file contains the H header file. Then, you can use this function normally. For more information, see the sample project and Main. C.

    The main program is as follows:

      # include   "msp430x16x. H "  // 430 register header file   # include  " tapwm. H "  // ta PWM output library header file   void  main () { // stop watchdog timer to prevent Time out reset  wdtctl = wdtpw + wdthold; clkinit (); tapwminit ( 'A' , 1,  'P' ,  'P'  );  // initialize the timer TA into a PWM generator // The clock source = aclk; no frequency division; both Channel 1 and Channel 2 are set to the High Level mode.  tapwmsetperiod (500);  // The PWM square wave cycle of channel 1/2 is set to 500 clock cycles  tapwmsetduty (1,200);  // Channel 1 valid for 200 clock cycles  tapwmsetpermill (2,200);  // 2 channel 20.0%  lpm0 ;}

    This program calls the library to generate two-way PWM waveforms.

Ta's PWM output is here. If more PWM waves are needed, Tb can be used to generate 6 complete PWM waveforms; you can refer to this program to compile the TB waveform output program. If you have any shortcomings, Please comment and discuss them.

Attachment: Library

Author:Give me a drink

Source: http://Engin.cnblogs.com/

The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. repost the text and indicate the source. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.