Here are my first couple programs just to get it working while I read the data sheet. They both work on the 16F1827 with a 20mhz crystal. Use the other config1 line for internal clock.
Feel free to add any programs / subroutines to this thread.
Blinks 8 LEDs on PORTB in asm.
Reads ADC on AN0 and outputs the high 8 bits to LEDs on PORTB in asm.
Feel free to add any programs / subroutines to this thread.
Blinks 8 LEDs on PORTB in asm.
Code:
;Modified from Tutorial 1.5 - Nigel Goodwin 2002 tutorials from Electrotech online
list p=16f1827 ; list directive to define processor
#include <p16f1827.inc> ; processor specific variable definitions
;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
; __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF
cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
LEDPORT Equ PORTB ;set constant LEDPORT = 'PORTB'
LEDTRIS Equ TRISB ;set constant for TRIS register
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
; movlw 0x07
; movwf CMCON ;turn comparators off (make it like a 16F84)
; bsf STATUS, RP0 ;This doesn't work on 16F1827 use BANKSEL instead
BANKSEL TRISB
movlw b'00000000' ;set PortB all outputs
movwf LEDTRIS
; bcf STATUS, RP0 This doesn't work on 16F1827 use BANKSEL instead
BANKSEL PORTB
clrf LEDPORT ;set all outputs low
Loop
movlw b'10000000'
; movlw b'11111111'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'01000000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00100000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00010000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00001000'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000100'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000010'
movwf LEDPORT
call Delay ;this waits for a while!
movlw b'00000001'
movwf LEDPORT
call Delay ;this waits for a while!
goto Loop ;go back and do it again
Delay movlw d'250' ;d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end
Reads ADC on AN0 and outputs the high 8 bits to LEDs on PORTB in asm.
Code:
;Modified from Nigel Goodwin's tutorials from Electrotech online
list p=16f1827 ; list directive to define processor
#include <p16f1827.inc> ; processor specific variable definitions
ERRORLEVEL 0, -302 ;suppress bank selection messages
;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;_FOSC_HS is external crystal, 20 mhz because I couldn't get internal 8 mhz working
; __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF
cblock 0x20 ;start of general purpose registers
RESULTHI
RESULTLO
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
;This code block configures the ADC
;for polling, Vdd and Vss references, Frc
;clock and AN0 input.
;
;Conversion start & polling for completion
; are included.
;
BANKSEL ADCON1 ;
; movlw b'11110000' ;Right justify, Frc
movlw b'01110000' ;Left justify, Frc clock
; movlw b'00100000' ;Left justify, /32
MOVWF ADCON1 ;Vdd and Vss Vref
BANKSEL TRISB
movlw b'00000000' ;set PortB all outputs
movwf TRISB
BANKSEL TRISA ;
BSF TRISA,0 ;Set RA0 to input
BANKSEL ANSELA ;
BSF ANSELA,0 ;Set RA0 to analog
Loop
BANKSEL ADCON0 ;
movlw b'00000001' ;Select channel AN0
MOVWF ADCON0 ;Turn ADC On
;CALL SampleTime ;Acquisiton delay
call Delay50 ;this waits for a while!
BSF ADCON0,ADGO ;Start conversion
BTFSC ADCON0,ADGO ;Is conversion done?
GOTO $-1 ;No, test again
BANKSEL ADRESH ;
MOVF ADRESH,W ;Left justified, Read upper Byte
BANKSEL PORTB
movwf PORTB
MOVWF RESULTHI ;store in GPR space
BANKSEL ADRESL ;
MOVF ADRESL,W ;Read lower 8 bits
MOVWF RESULTLO ;Store in GPR space
goto Loop ;go back and do it again
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (20 MHz clock)
d0 movwf count1
d1 movlw 0xE7
movwf counta
movlw 0x04
movwf countb
Delay_0 decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
;end of Delay routines
end
Last edited by a moderator: