View Single Post
Old 30-08-2009, 03:02 PM   #37
JohnnyNguyen
Đệ tử 3 túi
 
Tham gia ngày: Jul 2009
Bài gửi: 46
:
em cũng hiểu công thức đó.nhưng trong 1 ví dụ có viêt như sau:
Code:
#include <p33fj12mc202.h>
#include <pwm.h>
#include <pwm12.h>
#define Ts 1249
#define hs 0.5
_FOSCSEL(FNOSC_PRIPLL&IESO_OFF);//dung dao dong ngoai co bo PLL
_FOSC(FCKSM_CSDCMD&OSCIOFNC_OFF&POSCMD_XT);//chon che do XT
_FWDT(FWDTEN_OFF);//tat watchdog
_FPOR(PWMPIN_ON&HPOL_ON&LPOL_ON);//cac chan ra dieu khien boi PORT luc reset,deu co the cao
unsigned char i=0;
unsigned char j=0;
unsigned int const sin_table[40]={0,98,195,291,386,478,567,653,734,811,883,950,1010,1065,1113,1154,1188,1214,1234,1245,1249,
								  1245,1234,1214,1188,1154,1113,1065,1010,950,883,811,734,653,567,478,386,291,195,98};
void Init_PWM(void);
int main(void)
{ PLLFBD = 41;
  _PLLPOST = 0;
  _PLLPRE = 0;
  while (_COSC !=0b011);//dung bo PLL tao Fosc=80MHz,cho cho dao dong on dinh
  while (_LOCK !=1);//cho PLL bat dau khoa pha
  TRISB=0X0000;
  LATB=0X0000;
  Init_PWM();
while(1);
}
void Init_PWM(void)
{
P1TCONbits.PTMOD = 0b10;//continuous up/down cout mode
P1TCONbits.PTCKPS = 0b01;//prescale 4Tcy
P1TCONbits.PTOPS = 0b0000;//postscale 1:1
P1TPER = Ts;//Fpwn = 4kHz
PWM1CON1bits.PMOD1=0;//complementary mode
PWM1CON1bits.PMOD2=0;
PWM1CON1bits.PEN2H=1;//enable for PWM output
PWM1CON1bits.PEN1H=1;
PWM1CON1bits.PEN2L=1;
PWM1CON1bits.PEN1L=1;
PWM1CON2bits.IUE=0;
PWM2CON2bits.UDIS=0;//update from duty cycle and period buffer registers are enabled
P1DTCON1bits.DTAPS=0b00;//Dead time prescale 1:1
P1DTCON1bits.DTA = 40;//deadtime = 1um
//P1DTCON1bits.DTBPS=0b00;
//P1DTCON1bits.DTB = 0;
P1DTCON2bits.DTS2A=0;//Unit A selected for PWM active transitions
P1DTCON2bits.DTS1A=0;
P1DTCON2bits.DTS1I=0;//Unit B selected for PWM inactive transitions
P1DTCON2bits.DTS2I=0;
P1OVDCONbits.POVD2H=1;//PWM I/O pin controlled by PWM generator
P1OVDCONbits.POVD1H=1;
P1OVDCONbits.POVD2L=1;
P1OVDCONbits.POVD1L=1;
P1DC1=0;
P1DC2=0;
_PWM1IF=0;
_PWM1IE=1;
P1TCONbits.PTEN=1;
}
void __attribute__((interrupt,auto_psv)) _MPWM1Interrupt(void)
{
_PWM1IF=0;
if (i==0) 
{ P1DC1 = (unsigned int)(Ts - sin_table[j])*hs;
  P1DC2 = (unsigned int)(Ts + sin_table[j])*hs;
  j++;
  if (j==40) {j=0;i=1;};
}
else
{ P1DC1 = (unsigned int)(Ts + sin_table[j])*hs;
  P1DC2 = (unsigned int)(Ts - sin_table[j])*hs;
  j++;
  if (j==40) {j=0;i=0;};
}
}
em thắc mắc cái công thức tính PDC ở đây.hệ số hs=0,5 tính ra trc như thế nào ạ

thay đổi nội dung bởi: namqn, 30-08-2009 lúc 04:55 PM. Lý do: định dạng code
JohnnyNguyen vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn