PIC Vietnam

Go Back   PIC Vietnam > Thành Viên

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

truongtulong truongtulong is offline

Nhập môn đệ tử

Visitor Messages

Showing Visitor Messages 1 to 1 of 1
  1. truongtulong
    02-11-2009 08:29 AM
    truongtulong
    Các bác ơi giúp em chương trình điều khiển động cơ AC 1 pha dùng DSPIC33FJ12MC202 với. Em viết chương trình rồi nhưng khi mô phỏng bằng Proteus nó ko chạy ra sóng.
    Code chương trình viết bằng C30 của em là :
    #include <p33fj12mc202.h>
    #include <pwm.h>
    #include <pwm12.h>
    #define Ts 830
    _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 int i=0;
    unsigned int k,j;
    unsigned int Int_k;
    unsigned int up=0.0008;
    unsigned int down=0.0008;
    unsigned int Voltage_Value1 = 0;
    unsigned int Voltage_Value2 = 0;
    float Float_k,temp,Voltage_Value;
    float F_req=0;
    float ADC_Value;
    float const SinValue[720]={0,0.0087,0.0175,0.0262,0.0349,0.0436,0.0523,0.06 1,0.0698,0.0785,0.0872,0.0958,0.1045,0.1132,0.1219 ,0.1305,
    0.1392,0.1478,0.1564,0.165,0.1736,0.1822,0.1908,0. 1994,0.2079,0.2164,0.225,0.2334,0.2419,0.2504,0.25 88,0.2672,0.2756,0.284,0.2924,
    0.3007,0.309,0.3173,0.3256,0.3338,0.342,0.3502,0.3 584,0.3665,0.3746,0.3827,0.3907,0.3987,0.4067,0.41 47,0.4226,0.4305,0.4384,0.4462,
    0.454,0.4617,0.4695,0.4772,0.4848,0.4924,0.5,0.507 5,0.515,0.5225,0.5299,0.5373,0.5446,0.5519,0.5592, 0.5664,0.5736,0.5807,0.5878,0.5948,
    0.6018,0.6088,0.6157,0.6225,0.6293,0.6361,0.6428,0 .6494,0.6561,0.6626,0.6691,0.6756,0.682,0.6884,0.6 947,0.7009,0.7071,0.7133,0.7193,
    0.7254,0.7314,0.7373,0.7431,0.749,0.7547,0.7604,0. 766,0.7716,0.7771,0.7826,0.788,0.7934,0.7986,0.803 9,0.809,0.8141,0.8192,0.8241,0.829,
    0.8339,0.8387,0.8434,0.848,0.8526,0.8572,0.8616,0. 866,0.8704,0.8746,0.8788,0.8829,0.887,0.891,0.8949 ,0.8988,0.9026,0.9063,0.91,0.9135,
    0.9171,0.9205,0.9239,0.9272,0.9304,0.9336,0.9367,0 .9397,0.9426,0.9455,0.9483,0.9511,0.9537,0.9563,0. 9588,0.9613,0.9636,0.9659,0.9681,
    0.9703,0.9724,0.9744,0.9763,0.9781,0.9799,0.9816,0 .9833,0.9848,0.9863,0.9877,0.989,0.9903,0.9914,0.9 925,0.9936,0.9945,0.9954,0.9962,
    0.9969,0.9976,0.9981,0.9986,0.999,0.9994,0.9997,0. 9998,1,1,1,0.9998,0.9997,0.9994,0.999,0.9986,0.998 1,0.9976,0.9969,0.9962,0.9954,
    0.9945,0.9936,0.9925,0.9914,0.9903,0.989,0.9877,0. 9863,0.9848,0.9833,0.9816,0.9799,0.9781,0.9763,0.9 744,0.9724,0.9703,0.9681,0.9659,
    0.9636,0.9613,0.9588,0.9563,0.9537,0.9511,0.9483,0 .9455,0.9426,0.9397,0.9367,0.9336,0.9304,0.9272,0. 9239,0.9205,0.9171,0.9135,0.91,
    0.9063,0.9026,0.8988,0.8949,0.891,0.887,0.8829,0.8 788,0.8746,0.8704,0.866,0.8616,0.8572,0.8526,0.848 ,0.8434,0.8387,0.8339,0.829,0.8241,
    0.8192,0.8141,0.809,0.8039,0.7986,0.7934,0.788,0.7 826,0.7771,0.7716,0.766,0.7604,0.7547,0.749,0.7431 ,0.7373,0.7314,0.7254,0.7193,0.7133,
    0.7071,0.7009,0.6947,0.6884,0.682,0.6756,0.6691,0. 6626,0.6561,0.6494,0.6428,0.6361,0.6293,0.6225,0.6 157,0.6088,0.6018,0.5948,0.5878,
    0.5807,0.5736,0.5664,0.5592,0.5519,0.5446,0.5373,0 .5299,0.5225,0.515,0.5075,0.5,0.4924,0.4848,0.4772 ,0.4695,0.4617,0.454,0.4462,0.4384,
    0.4305,0.4226,0.4147,0.4067,0.3987,0.3907,0.3827,0 .3746,0.3665,0.3584,0.3502,0.342,0.3338,0.3256,0.3 173,0.309,0.3007,0.2924,0.284,
    0.2756,0.2672,0.2588,0.2504,0.2419,0.2334,0.225,0. 2164,0.2079,0.1994,0.1908,0.1822,0.1736,0.165,0.15 64,0.1478,0.1392,0.1305,0.1219,
    0.1132,0.1045,0.0958,0.0872,0.0785,0.0698,0.061,0. 0523,0.0436,0.0349,0.0262,0.0175,0.0087,0,-0.0087,-0.0175,-0.0262,-0.0349,-0.0436,
    -0.0523,-0.061,-0.0698,-0.0785,-0.0872,-0.0958,-0.1045,-0.1132,-0.1219,-0.1305,-0.1392,-0.1478,-0.1564,-0.165,-0.1736,-0.1822,-0.1908,
    -0.1994,-0.2079,-0.2164,-0.225,-0.2334,-0.2419,-0.2504,-0.2588,-0.2672,-0.2756,-0.284,-0.2924,-0.3007,-0.309,-0.3173,-0.3256,-0.3338,
    -0.342,-0.3502,-0.3584,-0.3665,-0.3746,-0.3827,-0.3907,-0.3987,-0.4067,-0.4147,-0.4226,-0.4305,-0.4384,-0.4462,-0.454,-0.4617,-0.4695,
    -0.4772,-0.4848,-0.4924,-0.5,-0.5075,-0.515,-0.5225,-0.5299,-0.5373,-0.5446,-0.5519,-0.5592,-0.5664,-0.5736,-0.5807,-0.5878,-0.5948,
    -0.6018,-0.6088,-0.6157,-0.6225,-0.6293,-0.6361,-0.6428,-0.6494,-0.6561,-0.6626,-0.6691,-0.6756,-0.682,-0.6884,-0.6947,-0.7009,-0.7071,
    -0.7133,-0.7193,-0.7254,-0.7314,-0.7373,-0.7431,-0.749,-0.7547,-0.7604,-0.766,-0.7716,-0.7771,-0.7826,-0.788,-0.7934,-0.7986,-0.8039,
    -0.809,-0.8141,-0.8192,-0.8241,-0.829,-0.8339,-0.8387,-0.8434,-0.848,-0.8526,-0.8572,-0.8616,-0.866,-0.8704,-0.8746,-0.8788,-0.8829,
    -0.887,-0.891,-0.8949,-0.8988,-0.9026,-0.9063,-0.91,-0.9135,-0.9171,-0.9205,-0.9239,-0.9272,-0.9304,-0.9336,-0.9367,-0.9397,-0.9426,
    -0.9455,-0.9483,-0.9511,-0.9537,-0.9563,-0.9588,-0.9613,-0.9636,-0.9659,-0.9681,-0.9703,-0.9724,-0.9744,-0.9763,-0.9781,-0.9799,-0.9816,
    -0.9833,-0.9848,-0.9863,-0.9877,-0.989,-0.9903,-0.9914,-0.9925,-0.9936,-0.9945,-0.9954,-0.9962,-0.9969,-0.9976,-0.9981,-0.9986,-0.999,
    -0.9994,-0.9997,-0.9998,-1,-1,-1,-0.9998,-0.9997,-0.9994,-0.999,-0.9986,-0.9981,-0.9976,-0.9969,-0.9962,-0.9954,-0.9945,-0.9936,-0.9925,
    -0.9914,-0.9903,-0.989,-0.9877,-0.9863,-0.9848,-0.9833,-0.9816,-0.9799,-0.9781,-0.9763,-0.9744,-0.9724,-0.9703,-0.9681,-0.9659,-0.9636,
    -0.9613,-0.9588,-0.9563,-0.9537,-0.9511,-0.9483,-0.9455,-0.9426,-0.9397,-0.9367,-0.9336,-0.9304,-0.9272,-0.9239,-0.9205,-0.9171,-0.9135,
    -0.91,-0.9063,-0.9026,-0.8988,-0.8949,-0.891,-0.887,-0.8829,-0.8788,-0.8746,-0.8704,-0.866,-0.8616,-0.8572,-0.8526,-0.848,-0.8434,-0.8387,
    -0.8339,-0.829,-0.8241,-0.8192,-0.8141,-0.809,-0.8039,-0.7986,-0.7934,-0.788,-0.7826,-0.7771,-0.7716,-0.766,-0.7604,-0.7547,-0.749,
    -0.7431,-0.7373,-0.7314,-0.7254,-0.7193,-0.7133,-0.7071,-0.7009,-0.6947,-0.6884,-0.682,-0.6756,-0.6691,-0.6626,-0.6561,-0.6494,-0.6428,
    -0.6361,-0.6293,-0.6225,-0.6157,-0.6088,-0.6018,-0.5948,-0.5878,-0.5807,-0.5736,-0.5664,-0.5592,-0.5519,-0.5446,-0.5373,-0.5299,-0.5225,
    -0.515,-0.5075,-0.5,-0.4924,-0.4848,-0.4772,-0.4695,-0.4617,-0.454,-0.4462,-0.4384,-0.4305,-0.4226,-0.4147,-0.4067,-0.3987,-0.3907,
    -0.3827,-0.3746,-0.3665,-0.3584,-0.3502,-0.342,-0.3338,-0.3256,-0.3173,-0.309,-0.3007,-0.2924,-0.284,-0.2756,-0.2672,-0.2588,-0.2504,
    -0.2419,-0.2334,-0.225,-0.2164,-0.2079,-0.1994,-0.1908,-0.1822,-0.1736,-0.165,-0.1564,-0.1478,-0.1392,-0.1305,-0.1219,-0.1132,-0.1045,
    -0.0958,-0.0872,-0.0785,-0.0698,-0.061,-0.0523,-0.0436,-0.0349,-0.0262,-0.0175,-0.0087};
    void Init_PWM(void);
    void Init_ADC10(void);
    unsigned int Read_ADC(void);
    void Step_Calculate(void);
    void Voltage_Calculate(void);
    void Update_PDC(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_ADC10();
    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 = 6kHz
    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 Init_ADC10(void)
    {
    AD1PCFGL = 0xFFF0; //Cac chan khac la digital, chan AN0/RA0 la analog

    //Cau hinh thanh ghi AD1CON1
    AD1CON1bits.ADON=0;
    AD1CON1bits.ADSIDL=0; //Tiep tuc hoat dong o che do ko tai
    AD1CON1bits.AD12B=0; //Che do ADC-10bit
    AD1CON1bits.FORM=0b00; //Du lieu ra o che do unsigned integer (DOUT = 0000 00dd dddd dddd)
    AD1CON1bits.SSRC=0b011; //Chuyen doi ADC khi xay ra ngat PWM1
    AD1CON1bits.SIMSAM=0; //Mau nhieu kenh
    AD1CON1bits.ASAM=0; //Bat dau lay mau khi bit Sam dc thiet lap
    AD1CON1bits.SAMP=0; //Lay mau va giu roi khuyech dai
    AD1CON1bits.DONE=0; //ADC chuy?n d?i không b?t d?u ho?c trong quá trình


    AD1CON2 = 0x0000;
    AD1CHS0 = 0x0000; //Kenh 0 doc tin hieu giua AN0 va AVss
    AD1CSSL = 0x0000; //Khong quet cac ngo vao
    AD1CON3 = 0x0103; //Dung 1 TAD cho lay mau, dung clock he thong,
    //TAD = 4xTcy

    _ADON = 1; //Bat module ADC
    _ASAM = 1; //Khoi dong che do tu dong lay mau
    }
    void Update_PDC(void)
    {
    if(i>719)
    {
    i=i-720;
    Voltage_Value1 = Voltage_Value*SinValue[i];
    }
    else
    {
    Voltage_Value1 = Voltage_Value*SinValue[i];
    }
    j=h+359;
    if(j>719)
    {
    j=j-720;
    Voltage_Value2 = Voltage_Value*SinValue[j];
    }
    else
    {
    Voltage_Value2 = Voltage_Value*SinValue[j];
    }
    P1DC1 = 830 + Voltage_Value1;
    P1DC2 = 830 + Voltage_Value2;
    i=i+k;
    }
    //tinh gia tri buoc nhay
    void Step_Calculate(void)
    {
    Float_k=0.12*F_req;
    Int_k=Float_k;
    temp=Float_k-Int_k;
    if(temp>=0.5)
    {
    k=Float_k+0.5;
    }
    else
    {
    k=Int_k;
    }
    }
    //tinh dien ap khi f thay doi
    void Voltage_Calculate(void)
    {
    Voltage_Value=F_req/0.0601;
    if(Voltage_Value>=800)
    {
    Voltage_Value=800;
    }
    }
    //Doc gia tri chuyen doi ADC
    unsigned int Read_ADC(void)
    {
    while(_DONE == 0);//cho chuyen doi xong
    return ADC1BUF0;//tra gia tri
    }
    //ngat PWM
    void __attribute__((interrupt,auto_psv)) _MPWM1Interrupt(void)
    {
    _PWM1IF=0;
    Read_ADC();
    ADC_Value=Read_ADC()*0.0488;
    if(F_req>ADC_Value)
    {
    F_req=F_req-down;
    if(F_req<=ADC_Value)
    {
    F_req=ADC_Value;
    }
    }
    else
    {
    F_req=F_req+up;
    if(F_req>=ADC_Value)
    {
    F_req=ADC_Value;
    }
    }
    Step_Calculate();
    Voltage_Calculate();
    Update_PDC();
    }
    Mong các bác chỉ giúp

Statistics

Tổng số bai
Visitor Messages
General Information
  • Lần cuối: 21-05-2012 06:34 PM
  • Tham gia ngày: 18-03-2009
  • Ðã giới thiệu: 0

Múi giờ GMT. Hiện tại là 03:53 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam