Home > pic18f, Uncategorized > pic18f assembly example 2 – digital output high-low

pic18f assembly example 2 – digital output high-low

 
;-----------------------------------------------------------------------------------------------------------------------------
; This assembly code uses a timer to set LATD2 high for 50ms then low for 50ms, repeatedly
;
; by David Dorran (https://dadorran.wordpress.com) May 2014
;------------------------------------------------------------------------------------------------------------------------------

; TRY TO DO THE FOLLOWING IN ORDER TO DEVELOP YOUR PROGRAMMING SKILLS:
;    1. Change the on-off rate from 10Hz to 100 Hz
;    2. Change the on-off rate from 10Hz to 1.25Hz by changing the timer prescaler  

;configure the  assembler directive 'list' so as to set processor to 18f4620 and set the radix used for data expressions to decimal (can be HEX|DEC|OCT)
    list p=18f4620, r=DEC
    #include <p18f4620.inc>
; configure the micro so that the watchdog timer is off, low-voltage programming is off, master clear is off and the clock works off the internal oscillator
    config WDT=OFF, LVP=OFF, MCLRE=OFF, OSC=INTIO67
;The org directive tells the compiler where to position the code in memory
    org 0x0000 ;The following code will be programmed in reset address location i.e. This is where the micro jumps to on reset
    goto Main ;Jump to Main immediately after a reset
 
;--------------------------------------------------------------------------
; Main Program
;--------------------------------------------------------------------------

    org 0x0100 
Main
    ; configure LATD0-LATD3 as outputs and RD4-RD7 as inputs
    movlw 0xf0
    movwf TRISD 
    clrf LATD
    bsf LATD,2 ; turn on LATD2
    bsf LATD,3 ; turn on LATD3
    
    ; Set clock frequency (section 2 of the PIC18F4620 Data Sheet)
    ; Set Fosc = 8MHz, which gives Tcy = 0.5us
    movlw B'01110000'
    movwf OSCCON 
    
    ;------- Set up timer0 to increment every 1us
    ;set up 1:2 prescaler so that TMR0 SFR is incremented every 2 Tcy  i.e. 1us
    bcf T0CON,0
    bcf T0CON,1
    bcf T0CON,2
    bcf T0CON, T0CS; use internal instruction cycle clock
    bcf T0CON, T08BIT; use 16 bit mode
    bcf T0CON, PSA; turn on prescaler
    
    ; setup so that timer0 overflow is triggered quickly
    ; after the first trigger then set it up so that it is triggered at the rate you want
    movlw 0xFF
    movwf TMR0L
    movlw 0xFF
    movwf TMR0H

    bcf INTCON, TMR0IF; reset timer overflow flag
    BSF T0CON, TMR0ON; turn on timer 0    
        
loop  
    ; check if the timer0 overflow flag has been set
    BTFSS INTCON, TMR0IF
    goto loop
    bcf INTCON, TMR0IF; reset timer overflow flag
    ;Could set up an interrupt to be generated whenever TMR0 goes from FFFFh to 0000h i.e. on overflow.
    ;The timer is incremented every 1us.
    ;Would like timer overflow flag triggered every 50ms so will set timer to 0xffff - 50000 = 0x3CAF
    movlw 0xAF
    movwf TMR0L
    movlw 0x3C
    movwf TMR0H
    
    ;invert LATD2
    movlw 0x4
    XORWF LATD,F

    goto loop   
    END
Categories: pic18f, Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: