Example of a Program Written in the PIC Assembly Language


The following program integrates the 44-Pin Demo Board Lessons described in the 44-Pin Demo Board User's Guide by Microchip, 2007.

; Auteur : Richard St-Denis, Universite de Sherbrooke, 2013.

        LIST     P=18F45K20,F=INHX32   ; processor, little-endian
        #include <P18F45K20.INC>       ; include symbols

        CONFIG FOSC=INTIO7, PBADEN=OFF, DEBUG=OFF, WDTEN=OFF, PWRT=OFF
;
; Data definition
;
        CBLOCK 0x00C
               dcnt1 : 1       ; delay inner counter
               dcnt2 : 1       ; delay outer counter
               direction : 1   ; direction of LED rotation
               ledno : 1       ; LED number to turn on
               tIntcnt : 1     ; timer delay counter (interruption)
               W_tmp : 1       ; saving space for the current context
               STATUS_tmp : 1
        ENDC

        ORG     0x000          ; entry point on reset
        goto    main

        ORG     0x0008         ; address of interrupt request vector
        goto    ISR
;
; Configuration of the DELs
;
InitLEDs:
        clrf    TRISD          ; set I/O pins of port D for output
        clrf    LATD           ; turn off LEDs
        return
;
; Configuration of the push-button switch
;
InitSW:
        clrf    TRISB          ; set I/O pins of port B for output
        bsf     TRISB,RB0      ; set I/O pin number 0 of port B for input
        return
;
; Configuration of the analog-to-digital converter (ADC)
;
InitAnalog:
        clrf    TRISA          ; set I/O pins of port A for output
        bsf     TRISA,RA0      ; set I/O pin number 0 of port A for input
        clrf    ANSEL
        bsf     ANSEL,ANS0     ; configure port A as an analog device
        clrf    ADCON0         ; options : channel AN0
        bsf     ADCON0,6       ; A/D conversion : Fosc/8
        bsf     ADCON0,ADON    ; enable ADC
        clrf    ADCON1         ; options : left justified, Vss et Vdd
        return
;
; Configuration of the timer
;
InitTimer0:
        bsf     T0CON,TMR0ON   ; enable Timer0
        bsf     T0CON,T08BIT   ; 8 bits timer (0=16 bits; 1=8 bits)
        bcf     T0CON,T0CS     ; timer source: internal timer of the processor
        bcf     T0CON,PSA      ; select prescaler
        bsf     T0CON,T0PS2    ; set prescaler scale to 1:128 (110)
        bsf     T0CON,T0PS1
        bcf     T0CON,T0PS0

        bcf     INTCON,T0IF    ; clean the overflow flag
        bsf     INTCON,TMR0IE  ; enable Timer0 interrupts
        return
;
; Configuration of the LED display
;
InitDisplay:
        movlw   D'1'
        movwf   direction      ; left direction (0-7)
        movlw   0X80           ; select LED number 7
        movwf   ledno
        return
;
; Delay function implemented with two counters
;
DelayLoopC:
        movwf   dcnt2
dlc00:
        decfsz  dcnt1,f
        goto    dlc00
        decfsz  dcnt2,f
        goto    dlc00
        return
;
; Delay function implemented with a timer (interruption)
;
DelayLoopIntT:
        movlw   D'32'          ; for about 4 seconds
        movwf   tIntcnt
        clrf    TMR0           ; initialize the counter to 0
        bsf     INTCON,GIE     ; enable global interruption
dlit00:
        movf    tIntcnt,w
        bnz     dlit00
        bcf     INTCON,GIE     ; disable global interruption
        return
;
; Delay function implemented with a timer (interrogation)
;
DelayLoopT:
        movlw   D'32'          ; for about 4 seconds
        movwf   tIntcnt
dlt00:
        clrf    TMR0           ; initialize the counter to 0
dlt05:
        movf    TMR0,w
        xorlw   D'250'
        btfss   STATUS,Z
        goto    dlt05
        decfsz  tIntcnt,f
        goto    dlt00
        return
;
; Interrupt request subroutine
;
ISR:
        movwf   W_tmp          ; save current context
        movff   STATUS,STATUS_tmp

        btfsc   INTCON,T0IF    ; source of interruption != Timer0
        goto    Timer0_int
                               ; ignore other sources of interruption
        movf    STATUS_tmp,w   ; restore context
        movwf   STATUS
        swapf   W_tmp,f
        swapf   W_tmp,w
        return                 ; simple return, without enabling interruption

Timer0_int:
        decf    tIntcnt
        bcf     INTCON,T0IF    ; clean overflow flag

        movf    STATUS_tmp,w   ; restore context
        movwf   STATUS
        swapf   W_tmp,f
        swapf   W_tmp,w
        retfie                 ; return and enable global interruption
;
; Main program
;
main:
        call    InitLEDs
        call    InitSW
        call    InitAnalog
        call    InitTimer0
        call    InitDisplay
main00:
        btfsc   PORTB,RB0      ; wait for a push-button switch event
        goto    main00
        call    DelayLoopT     ; OR DelayLoopIntT (for testing interruption)
main05:
        bsf     ADCON0,GO      ; start ADC
main06:
        btfsc   ADCON0,GO_DONE ; ADC completed?
        goto    main06

        bcf     STATUS,C       ; clear the carry bit
        btfsc   direction,0
        goto    RotateLeft
RotateRight:
        rrcf    ledno,f
        btfsc   STATUS,C       ; End of a turn?
        bsf     ledno,7        ; set bit number 7
        goto    main10
RotateLeft:
        rlcf    ledno,f
        btfsc   STATUS,C       ; End of a turn?
        bsf     ledno,0        ; set bit number 0
main10:
        movf    ledno,w
        movwf   LATD           ; turn on a LED
        movf    ADRESH,w       ; delay with respect to the ADC
        addlw   1              ; adjust the conversion value
        call    DelayLoopC     ; waiting delay

        btfss   PORTB,RB0      ; test the push-button switch
        goto    main15
        goto    main05
main15:
        movlw   D'1'           ; change direction
        xorwf   direction,f
        goto    main05
        END


Under MPLAB X IDE v1.00, the following instructions allow for the creation of a .asm file.

  1. Select the File menu
  2. Select the New File... item
  3. Select the folder Assembler in the Choose File Type subwindow (step 1)
  4. Select the extension asm in the Name and Location subwindow (step 2)

Under MPLAB X IDE v1.00, the following instructions allow for the configuration of the PIC18F45K20 device.

  1. Click on the right button mouse under the project name
  2. Select the Set Configuration item in the menu
  3. Select the Customize... item in the submenu
  4. Select Conf: [default] in the Categories subwindow
    • In the Configuration subwindow, select PIC18F45K20 for Device
  5. Select PICkit 3 in the Categories subwindow
    • In the Memories to Program option, select Allow PICkit 3 to Select Memories for Auto select memories and ranges
    • In the Power option, click on the checkbox Power target circuit from PICkit 3 and select 3.25 for Voltage Level

Under MPLAB X IDE v1.00, the following instructions allow for the compilation, downloading and execution of the program.

  1. Select the Run menu
  2. Select the Clean and Build Project (project name) item
  3. Click on the icon Make and Program Device (Project name)
  4. In the warning dialog, click on OK button, if PIC18F45K20 appears in the message