Trưởng lão PIC bang
Tham gia ngày: Feb 2006
Nơi Cư Ngụ: Tp. HCM, Việt Nam
Bài gửi: 3,025
:
|
Trích:
Nguyên văn bởi JohnnyNguyen
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 ạ
|
Công thức mà tôi đã nêu dùng để liên hệ giá trị duty cycle với giá trị của hai thanh ghi PxDCy và PxTPER. Còn vấn đề bạn đang thắc mắc ở đây lại là thuật toán điều khiển (bạn chọn quy luật điều chỉnh duty cycle để đạt ý đồ điều khiển).
Do đó, bạn nên hỏi tác giả của ví dụ đó về hệ số hs.
Thân,
thay đổi nội dung bởi: namqn, 30-08-2009 lúc 06:27 PM.
Lý do: viết thiếu từ "cycle"
|