Realization and Adjustment of SPWM Wave on MSP430F5529[Copy link]
I won't go into details about the definition and generation of SPWM waves here. This article mainly talks about the implementation of SPWM waves on a single-chip microcomputer, and how to adjust the frequency and amplitude of the filtered sine wave. First, figure out how SPWM is generated on a single-chip microcomputer. For example, if you want to output a 100HZ SPWM sine wave with 100 points per cycle, the interrupt frequency of timer B is 100*100=10K, and the PWM frequency used for the carrier (timer A) should be >=10K (here I recommend a carrier of 1M, because the coordination of the timer will have an error of +-1, so the larger the carrier, the better the waveform of the filtered sine wave). Through software, you first get the 100-point sine wave sampling value, you can use timer A to generate 1MHZ PWM, and then use timer B to make a timer interrupt with a frequency of 10KHZ. Each time it is interrupted, a sampling value is assigned to the output channel of timer A: CCRx. Then it keeps looping, and outputs a wave every hundred interrupts. If the loop is continuous, the output will be continuous. Then the PWM output end, after filtering (external filter #include "SPWM.h" /*============================================================================ * Function: Generate single-channel SPWM wave * Input: None * Output: P1.4 * Note: The timer is also TA1.1; the carrier frequency is 126.26K; the default sine frequency is 100HZ, the amplitude is unknown*==========================================================================================*/ //Initialize 1 channel void SPWM_1Way_Init(void) { P1SEL |=BIT4; P1DIR |=BIT4; //P1DS |=BIT4; //Fully drive P1OUT &=~BIT4; spwm_i=0; TA0CCR0 =198; //Carrier 126.26KHZ TA0CCR3 =Lab1[spwm_i]; TA0CCTL3 =OUTMOD_7; TA0CTL =TASSEL_2+MC_1+TACLR; //Select SMCLK as clock, UP mode P2SEL |=BIT0; P2DIR |=BIT0; P2OUT &=~BIT0; TA1CCR0 =2499; //25000000/(100*100)=2500:100Hz, 100 points: 10KHZ (the time is not accurate, I made some compensation) TA1CTL =TASSEL_2+MC_1+TACLR; //Select SMCLK as clock, UP mode T1CTL |= TAIE; //Enable interrupt __enable_interrupt(); //Enable total interrupt } //Output 1 channel void SPWM_1Way_Set_Freq(unsigned int freq) { unsigned long freq_num; freq_num=250000/(freq)-1; TA1CCR0 =freq_num; } The program code is as follows:
MSP430F5529的SPWM_01.zip(65.89 KB, downloads: 59)