//main.c
#include
#include "PWM.h"
#include "include.h"
#define uint unsigned int
/*----------------------------------------------------------------------------------------------
?* 功能:產(chǎn)生單路SPWM波
?* 輸入:無
?* 輸出:P1.4
?*/
void SPWM_1Way_Init(float x,float y)//x控制pwm周期,y控制占空比,y小于1且大于0
{???????????????????????????????????????????????? //X=freg為pwm小周期,y/200為正弦波幅值系數(shù)
??? P1SEL |=BIT4;
??? P1DIR |=BIT4;
??? P1DS |=BIT4; //全力驅(qū)動(dòng)
??? P1OUT &=~BIT4;
??? temp1=x;
??? temp1=temp1/100;//變大幅值
??? temp2=temp1*120;//放大倍數(shù)
??? 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;//選擇時(shí)鐘為SMCLK,UP模式
??? P2SEL |=BIT0;
??? P2DIR |=BIT0;
??? P2OUT &=~BIT0;
??? TA1CCR0 =freg1_4;
??? TA1CTL =TASSEL_2+MC_1+ID_0+TACLR;//+TACLR;//選擇時(shí)鐘為SMCLK(1.048576MHZ),UP模式
??? TA1CTL? |= TAIE;//開啟中斷
??? __enable_interrupt();//開啟總中斷
}
/*
?* 功能:輸出兩路SPWM波,相位可調(diào)
?* 輸入:無
?* 輸出: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;//放大倍數(shù)
??? 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 周期(50=100hz,250=20hz)
??? 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個(gè)點(diǎn):10KHZ
??? TA1CTL =TASSEL_2+MC_1+ID_0+TACLR;//選擇時(shí)鐘為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的單獨(dú)中斷會(huì)自動(dòng)清零,可TIMER的不會(huì),所以要軟件清零;!!
}
#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的單獨(dú)中斷會(huì)自動(dòng)清零,可TIMER的不會(huì),所以要軟件清零;??!
}
#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};
評論
查看更多