Building a stopwatch with PIC16F877a

Status
Not open for further replies.

ag501

New Member
hi all

im having problem with writing assembly language that will implement a stopwatch!!!
i have to design it so that it has a count to the base 7 and has max count of 66!
i also have to implement a delay of 1.38s for each count(i.e time taken for it to go from 1 value to the next) the clock is 20MHz and each instruction takes 200ns!

any help will be appreciated
 
Here's a code snippet that may help. It sets up timer 1 so that it overflows every 10mS.

Code:
		movlw	B'00000001'
		movwf	T1CON;		enable timer 1

		movlw	low(.50000)
		movwf	CCPR1L
		movlw	high(.50000)
		movwf	CCPR1H
		movlw	B'00001011';	enable special event trigger on CCP1
		movwf	CCP1CON;

The bit that gets set is PIR1,CCP1IF and you need to clear it afterwards. So your main code would be something like,
Code:
Wait		btfss	PIR1,CCP1IF
		goto	Wait
		bcf	PIR1,CCP1IF;	reset special event trigger bit
		incf	........etc

HTH

Mike.



Mike.
 
RE

hey

i have to design it that way because its part of the specification for my project!!!

that snippet you sent is that for the pic16f877a(20MHz clock,200ns intruction cycle)
 
blueroomelectronics said:
Why do you "have" to design it that way?

Because it's obviously a school assignment! - the stupid base 7 requirement is probably to stop them just copying something off the net?.
 
:-(

alrite

i think i have got the counter to count to the base7 but its the delay of 1.38s
im having problem with
 
ag501 said:
alrite

i think i have got the counter to count to the base7 but its the delay of 1.38s
im having problem with

Try the "delay code generator" on the PICList.
 
Just use the code I posted earlier. Test the bit and increment a counter, once it gets to 138 your 1.38s is up.

Mike.
 
Mike (Pommie) actually provided a workable 10 msec timer solution. The OP really just needs to add a counter that counts 138 of those timer 'ticks' before incrementing his digit counters. Gosh, how hard could it be?

Code:
;
;
Wait    btfss   PIR1,CCP1IF     ; 10 msec timeout?
        goto    Wait            ; no, branch and wait, else
        bcf     PIR1,CCP1IF     ; clear Timer 1 interrupt flag
        decfsz  Timer,F         ; has 1380 usecs elapsed?
        goto    Wait            ; no, branch, else
        movlw   d'138'          ; reset 1380 usec Timer var'
        movwf   Timer           ; 
;
;  increment digit counters here then jump back to the Wait sub'
;
 
Last edited:
Mike,

Your absolutely spot on except for the bit that is set. It is PIR1,CCP1IF that gets set rather than the timer1 interrupt flg.

Mike.
 
hi

hey all
what is PIR1,CCP1IF? are these variables set at the start of the prog?

is the very early ppost u put up is that the beginning of the 10msec delay?

thanks again

 
hey

hi
i was on the PICList website and looking at the delay code generator
... it uses an instruction similar to this goto $+1?
what does the $ sign mean?
 
ag501 said:
hi
i was on the PICList website and looking at the delay code generator
... it uses an instruction similar to this goto $+1?
what does the $ sign mean?

It's explained in the MPASM helpfile, and has been explained here numerous times - $ is the current address - so 'goto $+1' jumps to the following line, essentially a two instruction time NOP.
 
ag501 said:
hey all
what is PIR1,CCP1IF? are these variables set at the start of the prog?

is the very early ppost u put up is that the beginning of the 10msec delay?

thanks again

PIR1,CCP1IF is a bit that gets set very 10mS if you insert the code snippet that I posted earlier in your initialisation code.

Mike, K8LH posted example code to show how you would use this bit.

It sounds like you need to familiarise yourself with the pic opcodes. Having a read of the sticky and working your way through some of the tutorials would be a good way to learn.

Mike.
 

yeah im very new to assembly language...this is my 1st project
for the delay posted early...is it for the PIC16F877a?
from the PICList site (the delay code generator) i can just about follow but the snippet of code here is confusing me!!

thanks again...much appreciated
 
?????

how would i implement 3 pushbuttons...start,stop and reset the stopwatch?
any sites that help you do this?

thanks
 
?

prelimanry code ideas
this is wat i got so far

list p=16f877a
#include <pic16f877a.inc>
__config _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;unsure about declaring variables

;VALUE equ
ORG h'00' ;restart vector
GOTO MAIN

ORG h'05'

;base7 count sequence is 0,1,2,3,5,6,10,11,12,13,14,15,16,20
;base10 count 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

START
CLRF VALUE ;OUTPUT TO 7-SEG DISPLAY
LOOP1
movlw d'07'
movwf h'0E'

LOOP2
incf VALUE,F
decfsz h'0E',F
goto LOOP2

movlw d'04'
addwf VALUE,W
sublw d'66' ;max count of stopwatch is 66
btfss STATUS,Z
goto LOOP1



where would you put the delay of 1.38s between each count in?
 
delay code generator

; Delay = 1.38 seconds
; Clock frequency = 20 MHz

; Actual delay = 1.38 seconds = 6900000 cycles
; Error = -1.34974286176e-014 %

cblock
d1
d2
d3
endc

Delay
;6899996 cycles
movlw 0x71
movwf d1
movlw 0x0B
movwf d2
movlw 0x10
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0

;4 cycles (including call)
return

;this is the code i got from the code generator...what is
cblock
d1
d2
d3
endc?
is it the variables that are set at the start of prog?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…