Using as many internal peripherals as possible.


+ Reply to Thread
Results 1 to 12 of 12

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Using as many internal peripherals as possible.

    In my minimalist mania, I tried using interrupts. On pin RA3 hooked up 10k pullup and pushbutton to ground. On pin RA5 LED. First I tried the On Interrupt statement from PBP not work at all. Then I tried DT Instant interrupts, which worked flawlessly but the code size was a little big.
    Then I came up with this. Is it ok to use interrupts like this?
    I welcome comments so that I can learn from them.
    Code:
    #CONFIG
      __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
      __config _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_19 & _LVP_OFF
    #ENDCONFIG
    
    DEFINE INTHAND INT
    DEFINE NO_CLRWDT 1
    DEFINE OSC 4
    OSCCON = %01101010
    
    TRISA   =    %011000   ; RA4 input for battery measurement, RA3 input on change, rest output
    ANSELA  =    %0000     ; ADC Disable
    OPTION_REG = %10000111 ; Disable internal pull-ups, TMR0 On, prescaller to 256
    
    FVRCON  =    %10011000 ; Internal reference to 2048mV
    
    CM1CON0 =    %10100110 ; Comparator output to C1OUT, switch on LED if voltage is below ref. 2,048V
    CM1CON1 =    %00100001 ; C1IN1- pin is RA4, comparator voltage input, C1IN1+ positive input to FVR set to 2,048V
    
    LATA    =    %000001   ; init state of pin RA0 and RA1, LEDs connected to these pins
    
    T1CON = %00110001      ; TMR1 On, prescaller 1:8, Fosc/4
    INTCON = %11001000     ; Enable GIE, PEIE, IOCIE
    PIE1.0 = 1             ; Enable TMR1IE Owerflow interrupt 
    IOCAN.3 = 1            ; Enable RA3 negative edge OnChange interrupt 
    
    ; ------------  With TMR1 and interrupt, oscilator 4MHz ---------------
    Main:
    ; pause 10000 ; some stuff here
    goto main             
    
    ; ------------  INT Handler Routine ---------------
    asm
    INT:
    endasm
    IF PIR1.0  THEN LATA = LATA ^ %000011 : PIR1.0 = 0  ; Flash the LEDs on RA0 and RA1 alternately
    if IOCAF.3 then LATA = LATA ^ %100000 : IOCAF.3 = 0 ; Toggle LED on Pin RA5 if button on RA3 is pressed
    @ RETFIE
    It compiles only 58 words, and the main section is empty, ready for some other code.
    Last edited by louislouis; - 31st August 2023 at 21:13.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,392


    Did you find this post helpful? Yes | No

    Default Re: Using as many internal peripherals as possible.

    Is it ok to use interrupts like this?
    no , you may appear to get away with inserting pbp statements in an ASM interrupt but it is a delusion.
    pbp statements set many internal variables to enable complex statements to be evaluated , executing foreign statements from an isr will pollute this process. thats why dt-ints exists.


    use asm code in asm interrupts like this


    Code:
    #CONFIG  __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
      __config _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_19 & _LVP_OFF
    #ENDCONFIG
    
    
    DEFINE INTHAND myINT
    DEFINE NO_CLRWDT 1
    DEFINE OSC 4
    OSCCON = %01101010
    
    
    TRISA   =    %011000   ; RA4 input for battery measurement, RA3 input on change, rest output
    ANSELA  =    %0000     ; ADC Disable
    OPTION_REG = %10000111 ; Disable internal pull-ups, TMR0 On, prescaller to 256
    
    
    FVRCON  =    %10011000 ; Internal reference to 2048mV
    
    
    CM1CON0 =    %10100110 ; Comparator output to C1OUT, switch on LED if voltage is below ref. 2,048V
    CM1CON1 =    %00100001 ; C1IN1- pin is RA4, comparator voltage input, C1IN1+ positive input to FVR set to 2,048V
    
    
    LATA    =    %000001   ; init state of pin RA0 and RA1, LEDs connected to these pins
    
    
    T1CON = %00110001      ; TMR1 On, prescaller 1:8, Fosc/4
    INTCON = %11001000     ; Enable GIE, PEIE, IOCIE
    PIE1.0 = 1             ; Enable TMR1IE Owerflow interrupt 
    IOCAN.3 = 1            ; Enable RA3 negative edge OnChange interrupt 
    
    
    ; ------------  With TMR1 and interrupt, oscilator 4MHz ---------------
    Main:
    ; pause 10000 ; some stuff here
    goto main             
    
    
    ; ------------  INT Handler Routine ---------------
    asm
    myINT
     movlw 0
     banksel PIR1
     btfsc PIR1,0
     movlw 3
     bcf PIR1,0
     banksel IOCAF
     btfsc IOCAF,3
     iorlw 32
     bcf IOCAF,3
     banksel LATA
     xorwf LATA,f
     RETFIE
    endasm
    Warning I'm not a teacher

Similar Threads

  1. New cool PIC peripherals
    By HenrikOlsson in forum General
    Replies: 1
    Last Post: - 21st September 2016, 20:50
  2. Replies: 8
    Last Post: - 21st March 2015, 17:21
  3. Replies: 4
    Last Post: - 11th June 2014, 22:53
  4. Replies: 0
    Last Post: - 7th August 2008, 09:02
  5. Internal Oscillator
    By jhorsburgh in forum General
    Replies: 2
    Last Post: - 10th December 2007, 02:13

Members who have read this thread : 15

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts