70 likes | 241 Views
輸出動作與輸入命令相同. 工作圖. //#include <stdio.h> // 呼叫 stdio( 標準輸入輸出 ) 指令巨集 //#define FCY 2000000 #include <p30F4011.h> // 呼叫 p30F3011(IC) 指令巨集 #include <timer.h> #include <math.h> unsigned int icount=0; double v1=0.0,v2=0.0,e_new=0.0,e_old=0.0,d_new=0.0,d_old=0.0,kp=1.0,ki=100.0;
E N D
//#include <stdio.h> //呼叫stdio(標準輸入輸出)指令巨集//#include <stdio.h> //呼叫stdio(標準輸入輸出)指令巨集 • //#define FCY 2000000 • #include <p30F4011.h> //呼叫p30F3011(IC)指令巨集 • #include <timer.h> • #include <math.h> • unsigned int icount=0; • double v1=0.0,v2=0.0,e_new=0.0,e_old=0.0,d_new=0.0,d_old=0.0,kp=1.0,ki=100.0; • _FWDT(WDT_OFF); • void PWM_Initial(void) • { • PTPER = 1250; //PWM = (10MHz / (2000)) = 5 KHz • IEC2bits.PWMIE = 0; //Disable PWM Interrupt • PTCON = 0xA008; // 0b 1010 0000 0000 0000 • PWMCON1 = 0x0777; // 0b 0000 0111 0111 0111 • PDC1=0; • OVDCON= 0x0100; //設定輸出腳位 • IPC9bits.PWMIP = 7; • }
void Init_Timer1( void ) • { • T1CON = 0; • IFS0bits.T1IF = 0; • IPC0bits.T1IP = 7; • IEC0bits.T1IE = 1; • PR1 = 5000; // 20MHz/8/5000=500Hz 中斷為2ms • T1CONbits.TCS = 0; • T1CONbits.TCKPS=1; //TCKPS<1:0>: Timer Input Clock Prescale Select bits • //11 = 1:256 prescale value • //10 = 1:64 prescale value • //01 = 1:8 prescale value • //00 = 1:1 prescale value • T1CONbits.TON=1; • } • void _ISR _ADCInterrupt(void) • { • IFS0bits.ADIF = 0 ; // 1 = Interrupt request has occurred • // 0 = Interrupt request has not occurred • while (!ADCON1bits.DONE ); // wait for end of conversion • // 1 = A/D conversion is done // 0 = A/D conversion • is NOT done • ADCON1bits.DONE = 0; • }
void ADC10_Initial(void) • { • ADPCFG = 0xfffc; // 0B 1111 1111 1111 1110 • // 1 = Analog input pin in Digital mode • // 0 = Analog input pin in Analog mode • ADCON1 = 0x00EE; // 0b 0000 0000 1110 1110 • ADCON2 = 0x043C; // 0b 0000 0100 0011 1100 • ADCON3 = 0x1F3F; // 0b 0001 1111 0011 1111 • ADCSSL = 0xFFFF; // no scan input selected ....... • ADCHS = 0x0000 ; • IEC0bits.ADIE = 1 ; // Disable AD interrupt • // IPC2bits.ADIP = 7 ; // Set Priority to 7 >> highest !! • ADCON1bits.ADON = 1; // turn ADC ON • }
void _ISR _T1Interrupt(void) • { • v1=ADCBUF0 & 0x3ff; • v2=ADCBUF1 & 0x3ff; • e_old=e_new; • d_old=d_new; • e_new=(v1-v2)*1.0; • d_new=d_old+kp*(e_new-e_old)+ki*0.001*e_new; • if(d_new>2500.0) • d_new=2500.0; • else if(d_new<0.0) • d_new=0.0; • PDC1=d_new; • IFS0bits.T1IF = 0; • } • int main(void) • { • TRISB = 0x0003; • ADC10_Initial( ); • Init_Timer1(); • PWM_Initial(); • while(1); • }
作業 • 改變輸出或輸入的倍率