• PicŪ Basic


  • ADC interval with Special Event Trigger

    This article is in response to a user asking "Is it possible to use the Adin command in a timer based interrupt routine?"
    This solution was provided by forum member Barak and uses the internal Timer3 interrupt flag CCP2IF. There is no software inside the interrupt, instead the software waits in a loop for CCP2IF to be set and then takes the required action. This approach provides a fixed interval and avoids having a large amount of code inside an interrupt routine and any need to save registers.

    Here is Barak's solution.
    You can generate ADC with fixed interval with the Special Event Trigger. Example for 18FXXK20 running at 64MHz with 200uS interval:

    Code:
    SpecialEvent_setup: 
    CCP2CON = %00001011 ' set for trigger special event ADC
    T3CON = %10111000 ' set timer 3 as clock source for CCP2 and prescale 1:8
    Symbol TMR3ON = T3CON.0 ' Timer 3 enable on bit
    Symbol CCP2IF = PIR2.0 ' CCP2 interrupt flag bit
    Symbol CCP2Reg = CCPR2.Word ' alias for CCP2 registers
    CCP2Reg = $190 ' load CCP2 registers with 400 for 200uS sampling/Fs=5KHz 
     
    ADC_Setup: 
    ADCON0 = %00001100 ' CHS<3:0> 0011 for AN3
    ADCON1 = %00000000 ' VSS -ref and VDD +ref. 
    ADCON2 = %00010110 ' Bit 7=0 left justify, bit 5-3 = ACQT 4 TAD, bits 2-0 = Fosc/64
    Symbol ADON = ADCON0.0 ' ADC enable bit
    Symbol GO_DONE = ADCON0.1 ' ADC status bit cleared after acq 
     
    Process:
    While 1 = 1
    GoSub ADCEvent ' wait for the next CCP2 event
    ' Process here
    ' you have less than 200uS to calculate and process 
    ' before going back to the top of the loop to wait 
    ' for the next ADC Event 
    Wend
     
    ADCEvent: 
    While CCP2IF = 0 : Wend ' Poll the CCP2 interrupt flag
    CCP2IF = 0 ' clear the flag for next time
    While GO_DONE = 1 : Wend ' Poll the GO_DONE flag for completion of conversion
    Samp = ADRESH ' load 8 MSB from AN3 conversion to variable
    Return
  • Recent Activity

    towlerg-21522

    FineLineIDE

    Thread Starter: normnet

    Coming soon a new IDE: FineLineIDE! Features include:<O:p</O:p Bracket lines linking If-Endif, For-Next etc.<O:p</O:p Tabbed explorer so...

    towlerg Today, 16:49 Go to last post
    evoortman-388

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    evoortman Today, 11:13 Go to last post
    amod-29593

    Multiple functions by a single switch

    Thread Starter: amod

    Hi, How can I use multiple function by a single switch.I want to use 4 functions by a single switch.

    amod Today, 17:15 Go to last post
    towlerg-21522

    FUSES for PIC18F47K20

    Thread Starter: gtv_pic

    000 Good morning I am using the PIC18F47K20 and in the help of the proton I did not locate the FUSES for said micro. Where can I locate...

    towlerg Today, 14:52 Go to last post