PIC24fJ triggering an ADC by timer - Page 1
Forum section: electronics / general

author: TbayBoy
author_rank: Contributor
timestamp: September 02, 2025, 07:11:51 pm
content: I've been away for awhile but I will try to be clear.Device is PIC24fj64gb2002.the device is running fine 8Mhz internal osc, but I haven't been able to get the ADC to convert a sample and throw an interrupt.Timers 4 & 5 and combined into a 32bit timer set to interrupt every 15ms.Code://-----------------------------------------------------------------------------// Timer 4&5 peripheral//-----------------------------------------------------------------------------void TMR32_Init() {// Initializes the Timer4 and Timer5 peripherals for class B and class C// concatenated to a 32bit synchronous timing. Class B forms LSW Class C// MSW in 32bit. Class B timer registers control the timer. Class C timer// controls the interrupt. TMR4 class B, TMR5 class C.// Initializes the Timer1 peripheral as a Class A timer with//a system interrupt triggerunsigned int TMR32comp = 0; // a comparison trigger.// TMR32comp = (FOSC/2) / (1/(interrupt/1000))TMR32comp = (8000000/2) / (1/0.015); // 60,000 15msT5CON=0x00; // Stop timer 5T4CON=0x00; // stop timer 4TMR5=0x00; // clear timer 5TMR4=0x00; // clear timer 4PR5=0; // set period registerPR4=TMR32comp; // set period registerIPC7bits.T5IP=4; // timer 5 interrupt priorityIFS1bits.T5IF=0; // clear interrupt flagIEC1bits.T5IE=1; // enable interruptT4CONbits.T32=1; // 32bit timerT4CONbits.TON=1; // turn on timer 4return;}void _ISR_PSV _T5Interrupt() {// interrupt service for timer 5// interrupt also triggers the ADC to start a conversionIEC1bits.T5IE=0; // disable interruptIFS1bits.T5IF=0; // clear interrupt flag// test pulseif (LATBbits.LATB10) {LATBbits.LATB10=0;} else LATBbits.LATB10=1;IEC1bits.T5IE=1; // enable interruptreturn;}//-----------------------------------------------------------------------------// END Timer 4&5 peripheral//-----------------------------------------------------------------------------the timer does interrupt every 15ms verified with a scope. (I arbitrarily set this value and open to suggestions)the ADCCode://-----------------------------------------------------------------------------// ADC1 peripheral//-----------------------------------------------------------------------------// to convert an analog signal to a digital format. ADC configured to:// idle when CPU idle, unsigned integer output, VRef+ VRef- from VDD,VSS// interrupt on completion, single 16 bit buffer, system clock used, Timer 5// triggers conversionvoid ADC1_Init() {AD1PCFGbits.PCFG0=0; // PORT A0 analog inputAD1CON1bits.ADON=0; // turn off ADCAD1CON1bits.ADSIDL=1; // stop in idle modeAD1CON1bits.FORM=0x00; // unsigned integer resultAD1CON1bits.SSRC=0b011; // 32 bit timer 4&5 provide triggerAD1CON1bits.ASAM=1; // sample immediately after conversionAD1CON2bits.VCFG=0; // V+- reference AVDD and AVSS busAD1CON2bits.SMPI=0; // interrupt at EACH conversionAD1CON2bits.BUFM=0; // result buffer is 16bitsAD1CON3bits.ADRC=0; // system clock provides conversion clockAD1CON3bits.SAMC=6; // Auto sample timer in TADAD1CON3bits.ADCS=6; // Conversion clock period in TCY//AD1CHS // for multiplexing (ignored here)//AD1CSSL // for multiplexing (ignored here)IFS0bits.AD1IF=0; // clear interrupt flagIEC0bits.AD1IE=1; // Interrupt enabledreturn;}void _ISR_PSV _ADC1Interrupt() {// interrupt service for ADC1IEC0bits.AD1IE = 0; // interrupt disabledIFS0bits.AD1IF = 0; // clear the interrupt flagOC1R= (((ADC1BUF0*65536)/1024));// rescale for PWMIEC0bits.AD1IE = 1; // interrupt enabledAD1CON1bits.ADON=1; // turn on ADCreturn;}//-----------------------------------------------------------------------------// end ADC1 peripheral//-----------------------------------------------------------------------------I verified that there is an analog voltage on AN0 by multimeterI'm probably missing something simple but don't quite see it.Thanks in advance for your advice

author: TbayBoy
author_rank: Contributor
timestamp: September 03, 2025, 02:14:07 pm
content: I solved my problem. The Family Resource manual and the ADC documents don't matchsee attached

author: ahsrabrifat
author_rank: Regular Contributor
timestamp: September 15, 2025, 02:28:43 pm
content: Quote from: TbayBoy on September 02, 2025, 07:11:51 pmYou never configure AD1CHS.Default channel is 0, but it’s better to be explicit:Code:AD1CHSbits.CH0SA = 0; // AN0 is inputAD1CHSbits.CH0NA = 0; // Vref- is negative input

author: TbayBoy
author_rank: Contributor
timestamp: September 16, 2025, 03:58:03 pm
content: I'm not multiplexing the ADC.