//main.c
#include
#include "PWM.h"
#include "include.h"
#define uint unsigned int
/*----------------------------------------------------------------------------------------------
* 功能:产生单路SPWM波
* 输入:无
* 输出:P1.4
*/
void SPWM_1Way_Init(float x,float y)//x控制pwm周期,y控制占空比,y小于1且大于0
{
P1SEL |=BIT4;
P1DIR |=BIT4;
P1DS |=BIT4; //全力驱动
P1OUT &=~BIT4;
temp1=x;
temp1=temp1/100;//变大幅值
temp2=temp1*120;//放大倍数
temp3=temp2;
freg1_4=(int)temp3;//pwm小周期
temp5=y;//控制占空比,y小于1
spwm_i=0;
temp4=Lab1[spwm_i]*temp1*temp5;
zhangkong1_4=(int)temp4;
temp7=temp1*temp5;
int i=0;
for(i=0;i<)
TA0CCR0 =freg1_4;//小PWM 周期
TA0CCR3 =zhangkong1_4;//小pwm占空比
TA0CCTL3 =OUTMOD_7;//Output mode 7(说明书TA1.1由P1.4输出pwm)
TA0CTL =TASSEL_2+MC_1+ID_0+TACLR;//+TACLR;//选择时钟为SMCLK,UP模式
P2SEL |=BIT0;
P2DIR |=BIT0;
P2OUT &=~BIT0;
TA1CCR0 =freg1_4;
TA1CTL =TASSEL_2+MC_1+ID_0+TACLR;//+TACLR;//选择时钟为SMCLK(1.048576MHZ),UP模式
TA1CTL |= TAIE;//开启中断
__enable_interrupt();//开启总中断
}
/*
* 功能:输出两路SPWM波,相位可调
* 输入:无
* 输出:P1.4,P1.5
*/
void SPWM_2Way_Init(float x,float y)
{
P1SEL |=BIT4+BIT5;
P1DIR |=BIT4+BIT5;
P1DS |=BIT4+BIT5;
P1OUT &=~BIT4+BIT5;
spwm_i=0;
spwm_j=28;//控制相位
temp1=x;
temp1=temp1/100;//变大幅值
temp2=temp1*120;//放大倍数
temp3=temp2;
freg1_4=(int)temp3;//pwm小周期
temp5=y;//控制占空比,y小于1
temp4=Lab1[spwm_i]*temp1*temp5;
zhangkong1_4=(int)temp4;
temp6=Lab1[spwm_j]*temp1*temp5;
zhangkong1_5=(int)temp6;
TA0CCR0 =freg1_4;//小PWM 周期
TA0CCR3 =zhangkong1_4;//小pwm占空比
TA0CCR4 =zhangkong1_5;//小pwm占空比
TA0CCTL3 =OUTMOD_7;
TA0CCTL4 =OUTMOD_7;
TA0CTL =TASSEL_2+MC_1+ID_0+TACLR;
temp7=temp1*temp5;
P2SEL |=BIT0;
P2DIR |=BIT0;
P2OUT &=~BIT0;
TA1CCR0 =freg1_4;//1.04857mhz/(100*100)=2500:100Hz,100个点:10KHZ
TA1CTL =TASSEL_2+MC_1+ID_0+TACLR;//选择时钟为SMCLK,UP模式
TA1CTL |= TAIE;//开启中断
__enable_interrupt();//开启总中断
}
/*
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
SPWM_2Way_Init(120,0.4);
}
#if 0
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TimerA1(void)
{
temp4=Lab1[spwm_i++]*temp1*temp5;
zhangkong1_4=(int)temp4;
TA0CCR3 =zhangkong1_4;
if(spwm_i==28)spwm_i=0;
TA1IV=0; //GPIO的单独中断会自动清零,可TIMER的不会,所以要软件清零;!!
}
#else
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TimerA1(void)
{
temp4=Lab1[spwm_i++]*temp7;
zhangkong1_4=(int)temp4;
TA0CCR3 =zhangkong1_4;
if(spwm_i==56)spwm_i=0;
temp6=Lab1[spwm_j++]*temp7;
zhangkong1_5=(int)temp6;
TA0CCR4 =zhangkong1_5;
if(spwm_j==56)spwm_j=0;
TA1IV=0; //GPIO的单独中断会自动清零,可TIMER的不会,所以要软件清零;!!
}
#endif
/*
* PWM.h
*/
#ifndef PWM_H_
#define PWM_H_
/*
* PWM.h
*/
#ifndef PWM_H_
#define PWM_H_
extern unsigned int spwm_i,spwm_j,load1,load2,load3,load4;
extern int zhangkong1_4;
extern int zhangkong1_5;
extern int freg1_4;
extern int freg1_5;
extern int rang1_4;
extern int rang1_5;
extern float temp1;
extern float temp2;
extern float temp3;
extern float temp4;
extern float temp5;
extern float temp6;
extern float temp7;
extern float temp8;
extern unsigned int Lab_base[];
extern float Lab1[];
extern void SPWM_1Way_Init(float x,float y);
extern void SPWM_2Way_Init(float x,float y);
#endif /* PWM_H_ */
#endif /* PWM_H_ */
/*
* include.h
*/
#include
#include"PWM.h"
unsigned int spwm_i,spwm_j,load1,load2,load3,load4;
unsigned int freg1_4;
unsigned int freg1_5;
unsigned int zhangkong1_4;
unsigned int zhangkong1_5;
unsigned int rang1_4;
unsigned int rang1_5;
float temp1;
float temp2;
float temp3;
float temp4;
float temp5;
float temp6;
float temp7;
float temp8;
unsigned int Lab_base[]={13,24,36,48,56,69,78,88,94,105,108,111,116,117,118,115,111,106,100,94,86,78,69,57,47,35,24,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
floa Lab1[]={13,24,36,48,56,69,78,88,94,105,108,111,116,117,118,115,111,106,100,94,86,78,69,57,47,35,24,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//{117,129,152,160,165,170,175,190,175,165,160,152,129,92,80,68,45,35,12,6,3,0,0,12,18,26,45,56,92,105};