Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic timer0 using external clock source

Status
Not open for further replies.

smm4k

Newbie level 4
Joined
May 28, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
hello guys am a newbie in pic as am used with avr. am doing a frequency counter project where the frequency is being generated externally. am using pic 16f72 porta4 as the clock source for timer0. please help me out on the prescaler calculation with external clock source, because all the information have gotten is for timer 0 internal clock source. am using mplab assembler. thanks
 

i'm guessing you don't need the prescaler for your frequency counting ?

to do this just assign the prescaler to the WDT, look at the OPTION register.

The TMR0 will count input pulses on RA4 in sync with internal clock - FOSC/4

look at the block diagram for TIMER0 in the datasheet
 

thanks, i have read the datasheet, however i will appreciate if one can send me a code example in assembly that can make me understand better.
 

Hi smm4k,
Try this:
Code:
	LIST P=16F72
	#INCLUDE <P16F72.INC>
	__CONFIG _BOREN_OFF & _BODEN_OFF & _CP_OFF & _PWRTEN_OFF & _WDTEN_OFF & _WDT_OFF & _XT_OSC

;Using PIC16F72 to run TMR0 from external source (RA4/T0CKI) and display on PORTB
;Every Time the input source goes from low to high, PORTB is incremented
;Compiler: MPASM (MPLAB IDE v8.30)
;Clock source: XT Osc (4MHz)
;Programmed by Tahmid

	ORG 0

MAIN
	BANKSEL	TRISB ;BANK1
	CLRF	TRISB
	MOVLW	0x10
	MOVWF	TRISA ;RA4 input
	MOVLW	0x28
	MOVWF	OPTION_REG ;TMR0 source is T0CKI, increment from low-to-high
	MOVLW	0x07
	MOVWF	ADCON1 ;Disable ADC
	BANKSEL	PORTB ;BANK0
	CLRF	PORTB ;Clear PORTB
	CLRF	TMR0 ;Reset Timer0
START
	MOVF	TMR0, W
	MOVWF	PORTB ;Read TMR0 and send value to PORTB
	GOTO	START ;Go again

END

Connect a frequency of 1Hz to RA4 and connect 8 LEDs to PORTB and observe the output.

Hope this helps.
Tahmid.
 

    V

    Points: 2
    Helpful Answer Positive Rating
thanks Tahmid, the code works ok, but timer0 keeps on incrementing and i need to capture a reading for a given time.this is how my code looks for 16f72...
list p=PIC16F72,r=dec

#DEFINE PAGE0 BCF H'03',5
#DEFINE PAGE1 BSF H'03',5

PCL EQU H'02' ;page 0, 1, 2, 3
PORTA EQU H'05' ;page 0
TRISA EQU H'05' ;page 1
PORTB EQU H'06' ;page 0
TRISB EQU H'06' ;page 1
PORTC EQU H'07' ;page 0
TRISC EQU H'07' ;page 1
TMR0 EQU H'01'
OPTION_R EQU H'81'
ADCON1 EQU H'9F'
INTCON EQU H'0B'
STATUS EQU H'03'

temp1 EQU H'1A'
temp2 EQU H'1B'
Bin EQU H'20'
Bin1 EQU H'21'
Ones EQU H'30'
Tens EQU H'31'
Hund EQU H'32'
Thou EQU H'33'
Tenk EQU H'34'
w EQU 0
f EQU 1

org 0
goto start
org 4
goto interrupt
start PAGE1
movlw B'00000000'
movwf TRISA ;PORTA as input
movlw B'00000000'
movwf TRISB ;PORTB as output
movwf TRISC ;PORTC as output
movlw B'00101000'
movwf OPTION_R
movlw B'00000111'
movwf ADCON1
PAGE0
clrf Bin1
clrf TMR0
bsf INTCON,7
bsf INTCON,5
main bcf INTCON,2
bsf PORTA,4 ;open gate
call condisp
bcf PORTA,4 ;close gate
movf TMR0,w
movwf Bin
call condisp
goto main


condisp swapf Bin,w
addwf Bin,w
andlw H'0F'
skpndc
addlw H'16'
skpndc
addlw H'06'
addlw H'06'
skpdc
addlw -H'06'
btfsc Bin,4
addlw H'15'+H'06'
skpdc
addlw -H'06'
movwf Ones
swapf Bin1,w
addwf Bin1,w
andlw H'0F'
skpndc
addlw H'16'
skpndc
addlw H'06'
addlw H'06'
skpdc
addlw -H'06'
btfsc Bin1,0
addlw H'05'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,4
addlw H'15'+H'06'
skpdc
addlw -H'06'
addlw H'06'
addwf Ones,w
skpdc
addlw -H'06'
movwf Ones
movwf Tens
swapf Tens,f
movlw H'0F'
andwf Ones,f
andwf Tens,f
rrf Bin1,w
andlw H'0F'
addlw H'06'
skpdc
addlw -H'06'
addlw H'06'
addwf Tens,w
skpdc
addlw -H'06'
btfsc Bin,5
addlw H'03'+H'06'
skpdc
addlw -H'06'
btfsc Bin,6
addlw H'06'+H'06'
skpdc
addlw -H'06'
btfsc Bin,7
addlw H'12'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,0
addlw H'25'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,5
addlw H'09'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,6
addlw H'08'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,7
addlw H'06'+H'06'
skpdc
addlw -H'06'
movwf Tens
swapf Tens,w
andlw H'0F'
btfsc Bin1,1
addlw H'05'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,5
addlw H'01'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,6
addlw H'03'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,7
addlw H'07'+H'06'
skpdc
addlw -H'06'
movwf Hund
swapf Hund,w
movwf Thou
movlw H'0F'
andwf Tens,f
andwf Hund,f
andwf Thou,f
rrf Bin1,w
movwf Tenk
rrf Tenk,w
andlw H'0F'
addlw H'06'
skpdc
addlw -H'06'
addlw H'06'
addwf Thou,w
skpdc
addlw -H'06'
btfsc Bin1,6
addlw H'16'+H'06'
skpdc
addlw -H'06'
btfsc Bin1,7
addlw H'32'+H'06'
skpdc
addlw -H'06'
movwf Thou
movwf Tenk
swapf Tenk,f
movlw H'0F'
andwf Thou,f
andwf Tenk,f
movf Thou,w ;Thou
call bcdlookup
call lookup
movwf PORTB
movlw B'00001000'
movwf PORTC
nop
nop
nop
nop
nop
clrf PORTB
clrf PORTC
movf Hund,w ;Hund
call bcdlookup
call lookup
movwf PORTB
movlw B'00000100'
movwf PORTC
nop
nop
nop
nop
nop
clrf PORTB
clrf PORTC
movf Tens,w ;Tens
call bcdlookup
call lookup
movwf PORTB
movlw B'00000010'
movwf PORTC
nop
nop
nop
nop
nop
clrf PORTB
clrf PORTC
movf Ones,w ;Ones
call bcdlookup
call lookup
movwf PORTB
movlw B'00000001'
movwf PORTC
nop
nop
nop
nop
nop
clrf PORTB
clrf PORTC
return


;lookup table 1(BCD)
bcdlookup addwf PCL,f
retlw H'0'
retlw H'01'
retlw H'02'
retlw H'03'
retlw H'04'
retlw H'05'
retlw H'06'
retlw H'07'
retlw H'08'
retlw H'09'
;lookup table 2(HEX)
lookup addwf PCL,f
retlw H'7E'
retlw H'30'
retlw H'6D'
retlw H'79'
retlw H'33'
retlw H'5B'
retlw H'5F'
retlw H'70'
retlw H'7F'
retlw H'73'



interrupt bcf INTCON,7
incf Bin1,f
bsf INTCON,7
bsf INTCON,5
bcf INTCON,2
retfie
end


any help will be appreciated.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top