PIC16F628A TMR0 block diagram.
Fig. 1 TMR0 block diagram.


Operate PIC16F84A TMR0 from RA4-T0CKI Pulse Input

by Lewis Loflin


SN74HC14 based square wave generator with differentiator circuit.
SN74HC14 based square wave generator with differentiator circuit.


The next section requires a square wave generator or source. This used the above setup one can build. See Simple Schmitt Trigger SN74HC14 Square Wave Generator

Connect either A or B to RA4 pin 3 on the PIC16F84A. With a frequency counter (a function on better digital volt meters) measure the input frequency.

This as a time base for TMR0. Adjust the input for 60Hz. With a TMR0 preload value of 225 the output will be a 1Hz square wave on LED RB1. Play with differing values of prelaod value and input frequency.


;============================================
;   CLOCK: External 4MHz (instruction execution time: 1usec)
;	This is a basic start template for PIC16F84A
;	Includes delay routines 
;	Program name: Operate PIC16F84A TMR0 from RA4/T0CKI Pulse Input
;	Test external 60Hz pulse input and 7414 pulse generator
;	Date: 6/22/2024
;	Author: Lewis Loflin
;	LEDS connected to GRD RB1-RB3. Switch inputs RA0-RA2			
;============================================

 	list      p=16F84A      
	#include <p16F84A.inc>  
	errorlevel  -302        
 
  	__CONFIG        _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC 

;===========================================

; define 

LED1 EQU 1
LED2 EQU 2
LED3 EQU 3

;*****[ Data Storage Reg's ]*****
	CBLOCK  0x0C ; Assign each reg. from Bank 0 RAM area.
	CNT1
	CNT2
	CNT3
	CNT4
	FLAG
	w_temp       ; variable used for context saving W
	status_temp  ; variable used for context saving	STATUS	
	ENDC         ; Conclude Bank 0 RAM assignments.

;*******************************************
	ORG     0x000             ; processor reset vector
	goto    setup              ; go to beginning of program
	
	ORG     0x004             ; interrupt vector location
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register

	; isr code can go here or be located as a call subroutine elsewhere

	movf    status_temp,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
      
    BSF	STATUS,RP0   ;Jump to bank 1
	; BANKSEL TRISA	
	MOVLW	B'00011111'		;Config Port A, RA0 -> RA4 are inputs
	MOVWF	TRISA			;Set I/O configuration for PORTA
	MOVLW	B'11110001'		;Config Port B, RB1-3 OUTPUT, RB0, RB4-7 INPUT
	MOVWF	TRISB			;Set I/O configuration for PORTB

	CLRF OPTION_REG 
	; setup TMR0 ext. clock source
	; bit 5 source select 1 = Transition on RA4/T0CKI pin
	BSF OPTION_REG, T0CS 
	; 0 = Increment on low-to-high transition on RA4/T0CKI pin
	BCF OPTION_REG, T0SE 
	BSF OPTION_REG, PSA ; 1 = Prescaler is bypassed
	BCF	STATUS,RP0		    ;Jump back to bank 0 of PIC.
	; BANKSEL INTCON
	CLRF INTCON
	CLRF	PORTA			;Clear all I/O's of PORTA
	CLRF	PORTB			;Clear all I/O's of PORTB     

goto main

main

	; INTCON bit 2 T0IF: TMR0 Overflow Interrupt Flag bit
	; must be cleared in software
	; 60Hz input no prescale
	; because of the XOR toggle LED is divide by 2
	; use TMR0 preload of 225 for 1 Hz pulse on LED
	BCF INTCON, 2 ; CLR TMR0 Flag
	MOVLW D'225' ; overflow after 60 counts = 1 Sec.
	MOVWF TMR0 ; Load TMR0
	BTFSS INTCON, 2 ; Wait for bit set
	GOTO $-1
	; count / 2
	MOVLW 0x02
	XORWF PORTB, F

goto main
	
end

PIC16F84A Test Circuit



Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.