Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
// simple PIC 12F675 4MHz TMR1 flash LED
flash_loop
movlw 0xFF ; load number of loops
movwf count
btfss TMR1H,7 ; wait for TMR1
goto $-1
bcf TMR1H,7
decfsz count,f ; count loops
goto $-4
bsf GPIO,0 ; then turn LED on
btfss TMR1H,7 ; wait for TMR1
goto $-1
bcf TMR1H,7
decfsz count,f ; count loops
goto $-4
bcf GPIO,0 ; then turn LED off
goto flash_loop
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode
;
;******************************************************************************
; processor defined ;
list p=12F675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here
LIST b=5, n=97, t=ON, st=OFF
; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================
STATUS equ 03h
TRISA equ 85h
PORTA equ 05h
w_temp equ 20h
w_status equ 21h
;==============================================================================
; Code here
org 0x000 ; Set program memory base at reset vector 0x000 reset
goto setup ; set up ints and port stuff
org 0x004 ; Interrupt vector, int handler code comes next.
;==============================================================================
; INTERRUPT HANDLER (runs this code each timer0 interrupt)
;******************************************************************************
int_handler ; first we preserve w and status register
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf w_status ; save off contents of STATUS register
movlw b'00010000' ; mask for bit 3
xorwf PORTA,f ; toggle PORTA,bit3 (toggle the led)
BCF INTCON,T0IF ; reset the tmr0 interrupt flag
movf w_status,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;******************************************************************************
; SETUP (runs this only once at startup)
;******************************************************************************
setup ; goto label
;******************************************************************************
; Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
movlw b'00000001' ; Used to set up timer
; x------- ; 7, not applicable
; -x------ ; 6, 0=>timer is on / 1=>timer is on if T1G is low
; --x----- ; 5 prescale selection
; ---x---- ; 4 11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
; ----x--- ; 3 1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
; -----x-- ; 2 1 = Do not synchronize external clock input / 0 = Synchronize external clock input
; ------x- ; 1 1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
; -------x ; 0 1 = Enables Timer1 / 0 = Stops Timer1
movwf T1CON ; load data into OPTION_REG
;******************************************************************************
; Timer1 --- PIE1 — PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
bsf STATUS,5 ; shift to bank1 control registers
movlw b'00000001' ; Used to enable interrupts only bit0 is used for timer 1
; -------x ; 0 1 = Enables Timer1 interrupt
movwf T1CON ; load data into OPTION_REG
bcf STATUS,5 ; shift to bank0 working registers
;******************************************************************************
; Timer1 --- TMR1H --- 0Fh and TMR1L --- 0Eh
;******************************************************************************
movlw 22h ; Counter value ready in Working reg
movwf TMR1H ; value loaded to counters high register
movlw 22h ; Counter value ready in Working reg
movwf TMR1H ; value loaded to counters low register
;******************************************************************************
; Port setup
;******************************************************************************
clrf PORTA ; port is Cleared no output yet
movlw b'11001111' ; only bit 4 snd 5 are set as output rest is indput
bsf STATUS,5 ; shift to bank1 control registers
movwf TRISA ; send mask to porta
bcf STATUS,5 ; shift to bank0 working registers
goto main ; start main program
;******************************************************************************
; MAIN (main program loop)
;******************************************************************************
main ; Main program rest til next interupt
main_loop ;
nop ; in this program main do nothing
goto main_loop ; keep running the main code.
;==============================================================================
end ; no code after this point.
;==============================================================================
; ZERO-ERROR ONE SECOND TIMER
; (Roman Black 2001, public domain, use it as you like)
;
; INTERRUPT VERSION
;
; for PIC 16F84 at 4 MHz (or most PICs)
; (this code has been assembled with MPLAB and hardware tested)
; (for best viewing set TABS=5 in MPLAB editor)
;
; Note! See text: Zero-error 1 second timing algorithm
;
; Generates an event every second (or other period) from any PIC
; with any clock frequency.
;
; This version uses the timer0 overflow interrupt.
; Code can be adapted for different clock speeds, period lengths
; and accuracy levels.
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode
;
;******************************************************************************
; processor defined ;
list p=12F675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here
LIST b=5, n=97, t=ON, st=OFF
; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================
STATUS equ 03h
TRIS_GP equ 85h
PORT_GP equ 05h
w_temp equ 20h
w_status equ 21h
;==============================================================================
; Code here
org 0x000 ; Set program memory base at reset vector 0x000 reset
goto setup ; set up ints and port stuff
org 0x004 ; Interrupt vector, int handler code comes next.
;==============================================================================
; INTERRUPT HANDLER (runs this code each timer1 interrupt)
;******************************************************************************
int_handler ; first we preserve w and status register
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf w_status ; save off contents of STATUS register
movlw b'00010000' ; mask for bit 3
xorwf PORT_GP,f ; toggle PORTA,bit3 (toggle the led)
BCF PIR1,TMR1IF ; Cleared timer1 interrupt register overflowed
movf w_status,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;******************************************************************************
; SETUP (runs this only once at startup)
;******************************************************************************
setup ; goto label
;******************************************************************************
; Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
movlw b'00000001' ; Used to set up timer
; x------- ; 7, not applicable
; -x------ ; 6, 0=>timer is on / 1=>timer is on if T1G is low
; --x----- ; 5 prescale selection
; ---x---- ; 4 11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
; ----x--- ; 3 1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
; -----x-- ; 2 1 = Do not synchronize external clock input / 0 = Synchronize external clock input
; ------x- ; 1 1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
; -------x ; 0 1 = Enables Timer1 / 0 = Stops Timer1
movwf T1CON ; load data into OPTION_REG
;******************************************************************************
; Timer1 --- PIE1 — PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
bsf STATUS,5 ; shift to bank1 control registers
movlw b'00000001' ; Used to enable interrupts only bit0 is used for timer 1
; -------x ; 0 1 = Enables Timer1 interrupt
movwf PIE1 ; load data into OPTION_REG
bcf STATUS,5 ; shift to bank0 working registers
;******************************************************************************
; Timer1 --- TMR1H --- 0Fh and TMR1L --- 0Eh
;******************************************************************************
movlw 22h ; Counter value ready in Working reg
movwf TMR1H ; value loaded to counters high register
movlw 22h ; Counter value ready in Working reg
movwf TMR1L ; value loaded to counters low register
;******************************************************************************
; Port setup
;******************************************************************************
clrf PORT_GP ; port is Cleared no output yet
movlw b'11001111' ; only bit 4 snd 5 are set as output rest is indput
bsf STATUS,5 ; shift to bank1 control registers
movwf TRIS_GP ; send mask to porta
bcf STATUS,5 ; shift to bank0 working registers
;******************************************************************************
; Global interupt enable
;******************************************************************************
movlw b'10000000' ; only bit 4 snd 5 are set as output rest is indput
; 1------- ; GIE: Global Interrupt Enable bit --- 1
; -1------ ; PEIE: Peripheral Interrupt Enable bit --- 1
movwf TRIS_GP ; send mask to porta
goto main ; start main program
;******************************************************************************
; MAIN (main program loop)
;******************************************************************************
main ; Main program rest til next interupt
main_loop ;
nop ; in this program main do nothing
goto main_loop ; keep running the main code.
;==============================================================================
end ; no code after this point.
;==============================================================================
;******************************************************************************
; test for using timer1 on a PICkit1 flashing a single diode
;
;******************************************************************************
; processor defined ;
list p=12F675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
;======================================================================================================
; MPLAB stuff here
LIST b=5, n=97, t=ON, st=OFF
; absolute listing tabs=5, lines=97, trim long lines=ON, symbol table=OFF
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
;==============================================================================
; Code here
org 0x000 ; Set program memory base at reset vector 0x000 reset
goto setup ; set up ints and port stuff
org 0x004 ; Interrupt vector, int handler code comes next.
;==============================================================================
; INTERRUPT HANDLER (runs this code each timer1 interrupt)
;******************************************************************************
int_handler ; first we preserve w and status register
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf w_status ; save off contents of STATUS register
movlw b'00010000' ; mask for bit 4
xorwf PORT_GP,f ; toggle PORTGP,bit3 (toggle the led)
BCF PIR1,TMR1IF ; Cleared timer1 interrupt register overflowed
movf w_status,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;******************************************************************************
; SETUP (runs this only once at startup)
;******************************************************************************
setup ; goto label
;******************************************************************************
; Timer1 --- TIMER1 CONTROL REGISTER address 10h
;******************************************************************************
movlw b'00110001' ; Used to set up timer
; x------- ; 7, not applicable
; -x------ ; 6, 0=>timer is on / 1=>timer is on if T1G is low
; --x----- ; 5 prescale selection
; ---x---- ; 4 11 = 1:8 / 10 = 1:4 / 01 = 1:2 / 00 = 1:1 Prescale Value
; ----x--- ; 3 1 = LP oscillator is enabled for Timer1 clock / 0 = LP oscillator is off
; -----x-- ; 2 1 = Do not synchronize external clock input / 0 = Synchronize external clock input
; ------x- ; 1 1 = External clock from T1OSO/T1CKI pin (on the rising edge)/0 = Internal clock (FOSC/4)
; -------x ; 0 1 = Enables Timer1 / 0 = Stops Timer1
movwf T1CON ; load data into OPTION_REG
;******************************************************************************
; Timer1 --- PIE1 PERIPHERAL INTERRUPT ENABLE REGISTER 1 --- 8Ch
;******************************************************************************
bsf STATUS,5 ; shift to bank1 control registers
movlw b'00000001' ; Used to enable interrupts only bit0 is used for timer 1
; -------x ; 0 1 = Enables Timer1 interrupt
movwf PIE1 ; load data into OPTION_REG
bcf STATUS,5 ; shift to bank0 working registers
;******************************************************************************
; Timer1 --- TMR1H --- 0Fh and TMR1L --- 0Eh
;******************************************************************************
movlw 0h ; Just clear TMR1 and let it roll each flash
movwf TMR1H ;
movlw 0h ;
movwf TMR1L ;
;******************************************************************************
; Port setup
;******************************************************************************
clrf PORT_GP ; port is Cleared no output yet
movlw b'00000000' ; GP4 = output to LED
bsf STATUS,5 ; shift to bank1 control registers
movwf TRIS_GP ; send mask to porta
bcf STATUS,5 ; shift to bank0 working registers
;******************************************************************************
; INTCON Global interupt enable
;******************************************************************************
movlw b'11000000' ; only bit 4 snd 5 are set as output rest is indput
; 1------- ; GIE: Global Interrupt Enable bit --- 1
; -1------ ; PEIE: Peripheral Interrupt Enable bit --- 1
movwf INTCON ;
goto main ; start main program
;******************************************************************************
; MAIN (main program loop)
;******************************************************************************
main ; Main program rest til next interupt
main_loop ;
nop ; in this program main does nothing
goto main_loop ; keep running the main code.
;==============================================================================
end ; no code after this point.
;==============================================================================