完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近在使用dsPIC33EP64GS504芯片,想用AN4采电压,但是设置无法进入AD中断,搞不清楚问题出在哪,请大神指导,代码如下
#include _FOSCSEL(FNOSC_FRC) _FOSC(FCKSM_CSECMD & OSCIOFNC_ON) _FWDT(WDTEN_OFF) _FICD(ICS_PGD2 & JTAGEN_OFF) _FDEVOPT(PWMLOCK_OFF) float dataAN4 = 0; float dataAN5 = 0; void InitClock(void); void InitCalibrateADC(void); void InitADC(void); void __attribute__((interrupt, no_auto_psv)) _ADCAN4Interrupt(void); void __attribute__((interrupt, no_auto_psv)) _ADCAN5Interrupt(void); void delay_s(unsigned int t); void InitClock(void) { // Configure Oscillator to operate the device at 68 MHz // Fosc = Fin*M/(N1*N2), Fcy = Fosc/2 // Fosc = 7.37*(76)/(2*2)= ~140Mhz for Fosc, Fcy = 70 MHz // Configure PLL prescaler, PLL postscaler, PLL divisor PLLFBD = 74; // M = PLLFBD + 2 CLKDIVbits.PLLPOST = 0; // N1 = 2 CLKDIVbits.PLLPRE = 0; // N2 = 2 __builtin_write_OSCCONH(0x01); // New Oscillator selection FRC w/ PLL __builtin_write_OSCCONL(0x01); // Enable Switch while(OSCCONbits.COSC != 0b001); // Wait for Osc. to switch to FRC w/ PLL while(OSCCONbits.LOCK != 1); // Wait for PLL to Lock // Setup the ADC and PWM clock for 120MHz // ((FRC * 16) / APSTSCLR ) = (7.37MHz * 16) / 1 = 117.9MHz ACLKCONbits.FRCSEL = 1; // FRC provides input for Auxiliary PLL (x16) ACLKCONbits.SELACLK = 1; // Aux Osc. provides clock source for PWM & ADC ACLKCONbits.APSTSCLR = 7; // Divide Auxiliary clock by 1 ACLKCONbits.ENAPLL = 1; // Enable Auxiliary PLL while(ACLKCONbits.APLLCK != 1); // Wait for Auxiliary PLL to Lock // __delay_us(50); // Errata#1 (reference:DS80000656A) } void InitCalibrateADC(void) { // Set initialization time to maximum ADCON5Hbits.WARMTIME = 15; // Turn on ADC module ADCON1Lbits.ADON = 1; // Turn on analog power for shared core ADCON5Lbits.SHRPWR = 1; // Wait when the shared core is ready for operation while(ADCON5Lbits.SHRRDY == 0); // Turn on digital power to enable triggers to the shared core ADCON3Hbits.SHREN = 1; // Enable calibration for the shared core ADCAL1Hbits.CSHREN = 1; // Single-ended input calibration ADCAL1Hbits.CSHRDIFF = 0; // Start calibration ADCAL1Hbits.CSHRRUN = 1; // Poll for the calibration end while(ADCAL1Hbits.CSHRRDY == 0); // Differential input calibration ADCAL1Hbits.CSHRDIFF = 1; // Start calibration ADCAL1Hbits.CSHRRUN = 1; // Poll for the calibration end while(ADCAL1Hbits.CSHRRDY == 0); // End the shared core calibration ADCAL1Hbits.CSHREN = 0; } void InitADC(void) { // Configure the I/O pins to be used as analog inputs. ANSELBbits.ANSB9 = 1; TRISBbits.TRISB9 = 1; // AN0/RA0 connected the dedicated core 0 //ANSELBbits.ANSB10 = 1; TRISBbits.TRISB10 = 1; // AN1/RA1 connected the dedicated core 1 // Configure the common ADC clock. ADCON3Hbits.CLKSEL = 2; ADCON3Hbits.CLKDIV = 0; // no clock divider (1:1) // Configure the cores? ADC clock. ADCON2Lbits.SHRADCS = 0; // clock divider (1:2) // Configure the ADC reference sources. ADCON3Lbits.REFSEL = 0; // AVdd as voltage reference // Configure the integer of fractional output format. ADCON1Hbits.SHRRES = 3; ADCON1Hbits.FORM = 0; // integer format // Select single-ended input configuration and unsigned output format. // ADMOD0Lbits.SIGN0 = 0; // AN0/RA0 // ADMOD0Lbits.DIFF0 = 0; // AN0/RA0 ADMOD0Lbits.SIGN4 = 0; // AN1/RA1 ADMOD0Lbits.DIFF4 = 0; // AN1/RA1 // ADMOD0Lbits.SIGN5 = 0; // AN1/RA1 // ADMOD0Lbits.DIFF5 = 0; // AN1/RA1 ADCON2Hbits.SHRSAMC = 0; _CNVRTCH = 1; _CNVCHSEL = 4; _SWLCTRG = 1; // Enable and calibrate the module. InitCalibrateADC(); // // Configure and enable ADC interrupts. //ADIELbits.IE0 = 1; // enable interrupt for AN0 //ADIELbits.IE1 = 1; // enable interrupt for AN1 //_ADCAN0IF = 0; // clear interrupt flag for AN0 //_ADCAN0IE = 1; // enable interrupt for AN0 _ADCAN4IF = 0; // clear interrupt flag for AN1 _ADCAN4IE = 1; // enable interrupt for AN1 // _ADCAN5IF = 0; // clear interrupt flag for AN1 // _ADCAN5IE = 1; // enable interrupt for AN1 _ADCAN4IP = 5; // enable interrupt for AN1 // _ADCAN5IP = 6; // Set same trigger source for all inputs to sample signals simultaneously. //ADTRIG0Lbits.TRGSRC0 = 13; // timer 2 for AN0 // ADTRIG1Lbits.TRGSRC5 = 13; // timer 2 for AN1 ADTRIG1Lbits.TRGSRC4 = 13; // timer 2 for AN1 _ADON = 1; // TIMER 2 INITIALIZATION (TIMER IS USED AS A TRIGGER SOURCE FOR ALL CHANNELS). T2CONbits.TCS = 0; // clock from peripheral clock T2CONbits.TCKPS = 0; // 1:1 prescale PR2 = 0x8000; // rollover every 0x8000 clocks T2CONbits.TON = 1; // start timer to generate ADC triggers } void delay_s(unsigned int t) { unsigned int i,j,m; for(i=0;i { for(j=0;j<1000;j++) { for(m=0;m<2000;m++); } } } int main() { InitClock(); // while(1); delay_s(5); TRISB = 0b0000011000000000;// in PORTB = 0b0000000010000000; //¼ÌµçÆ÷¶Ì· ANSELB = 0b0000011000000000; InitADC(); //_RB7 = 1; _IPL3 = 0; _IPL = 3; _GIE = 1; while(1); } // ADC AN1 ISR void __attribute__((interrupt, no_auto_psv)) _ADCAN4Interrupt(void) { dataAN4 = ADCBUF4; _RB7 = 0; while(1); _ADCAN4IF = 0; // clear interrupt flag } |
|
相关推荐
4个回答
|
|
备注是错的,本来按照例程改的 备注还没改
|
|
|
|
还有一个问题我想先采AN4再采AN5循环采应该怎么设置啊
|
|
|
|
这是包含AD与DA功能的开发板么?那里买的
|
|
|
|
只有小组成员才能发言,加入小组>>
求解外围电路实现的是4脚给持续低电平复位并正常工作,高电平不工作的原因
2093 浏览 1 评论
3643 浏览 3 评论
PIC1946程序有一个变量在运行过程中恢复初始值其他变量保持不变
2341 浏览 2 评论
2770 浏览 0 评论
PIC16F1825的RC5引脚,在主程序中操作无效,在中断中可以改变是为什么?
4046 浏览 5 评论
993浏览 0评论
用XC8编译PIC18F25K80时提示下面Error,求怎么解决这个问题
6375浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-13 01:53 , Processed in 0.694792 second(s), Total 91, Slave 74 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号