![]() |
|
Tài trợ cho PIC Vietnam |
dsPIC - Bộ điều khiển tín hiệu số 16-bit Theo dự kiến của Microchip, vào khoảng năm 2011 dsPIC sẽ có doanh số lớn hơn PIC |
|
Ðiều Chỉnh | Xếp Bài |
![]() |
#5 | |
Đệ tử 4 túi
Tham gia ngày: Jul 2008
Bài gửi: 61
: |
Trích:
chạy Ok đấy. anh e cùng tham khảo nhé. #if defined(__dsPIC33F__) #include "p33fxxxx.h" #elif defined(__PIC24H__) #include "p24hxxxx.h" #endif #include "adcDrv1.h" #include "tglPin.h" #define SAMP_BUFF_SIZE 8 // Size of the input buffer per analog input #define NUM_CHS2SCAN 4 // Number of channels enabled for channel scan /*================================================= ============================ ADC INITIALIZATION FOR CHANNEL SCAN ================================================== ===========================*/ void initAdc1(void) { AD1CON1bits.FORM = 3; // Data Output Format: Signed Fraction (Q15 format) AD1CON1bits.SSRC = 2; // Sample Clock Source: GP Timer starts conversion AD1CON1bits.ASAM = 1; // ADC Sample Control: Sampling begins immediately after conversion AD1CON1bits.AD12B = 0; // 10-bit ADC operation AD1CON2bits.CSCNA = 1; // Scan Input Selections for CH0+ during Sample A bit AD1CON2bits.CHPS = 0; // Converts CH0 AD1CON3bits.ADRC = 0; // ADC Clock is derived from Systems Clock AD1CON3bits.ADCS = 63; // ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*64 = 1.6us (625Khz) // ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us AD1CON2bits.SMPI = (NUM_CHS2SCAN-1); // 4 ADC Channel is scanned //AD1CSSH/AD1CSSL: A/D Input Scan Selection Register AD1CSSH = 0x0000; AD1CSSLbits.CSS4=1; // Enable AN4 for channel scan AD1CSSLbits.CSS5=1; // Enable AN5 for channel scan AD1CSSLbits.CSS10=1; // Enable AN10 for channel scan AD1CSSLbits.CSS13=1; // Enable AN13 for channel scan //AD1PCFGH/AD1PCFGL: Port Configuration Register AD1PCFGL=0xFFFF; AD1PCFGH=0xFFFF; AD1PCFGLbits.PCFG4 = 0; // AN4 as Analog Input AD1PCFGLbits.PCFG5 = 0; // AN5 as Analog Input AD1PCFGLbits.PCFG10 = 0; // AN10 as Analog Input AD1PCFGLbits.PCFG13 = 0; // AN13 as Analog Input IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit IEC0bits.AD1IE = 1; // Enable A/D interrupt AD1CON1bits.ADON = 1; // Turn on the A/D converter tglPinInit(); } /*================================================= ============================ Timer 3 is setup to time-out every 125 microseconds (8Khz Rate). As a result, the module will stop sampling and trigger a conversion on every Timer3 time-out, i.e., Ts=125us. ================================================== ===========================*/ void initTmr3() { TMR3 = 0x0000; PR3 = 4999; IFS0bits.T3IF = 0; IEC0bits.T3IE = 0; //Start Timer 3 T3CONbits.TON = 1; } /*================================================= ============================ ADC INTERRUPT SERVICE ROUTINE ================================================== ===========================*/ int ain3Buff[SAMP_BUFF_SIZE]; int ain4Buff[SAMP_BUFF_SIZE]; int ain10Buff[SAMP_BUFF_SIZE]; int ain13Buff[SAMP_BUFF_SIZE]; int scanCounter=0; int sampleCounter=0; void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void) { switch (scanCounter) { case 0: ain3Buff[sampleCounter]=ADC1BUF0; break; case 1: ain4Buff[sampleCounter]=ADC1BUF0; break; case 2: ain10Buff[sampleCounter]=ADC1BUF0; break; case 3: ain13Buff[sampleCounter]=ADC1BUF0; break; default: break; } scanCounter++; if(scanCounter==NUM_CHS2SCAN) { scanCounter=0; sampleCounter++; } if(sampleCounter==SAMP_BUFF_SIZE) sampleCounter=0; tglPin(); // Toggle RA6 IFS0bits.AD1IF = 0; // Clear the ADC1 Interrupt Flag } |
|
![]() |
![]() |
|
|