Designing stopwatch with 16f887

Status
Not open for further replies.

matlark

New Member
Hello,
I am trying to make a stopwatch that reads up to 99.99 (1 min 39 secs.99hundredths). When the button is pressed it starts and upon the second button push it displays the time elapsed on the 8 leds in a line in BCD. It first displays up to 99 then blank then whats after the decimal. I know I need to utilize either timer0 or timer1 possibly even use the OSCCON register to slow down the internal clock. I am having trouble getting started. This is for a school project so I am not asking for the code but does anyone have any ideas that can help me? I am having trouble getting started.

thanks
 
The internal osc is too inaccurate for a stopwatch, use a crystal instead. Since you're trying for a 10ms timebase find a crystal that divides perfectly into 10ms (after prescaling etc) Timer 2 is a good choice as it can do some pretty handy clock division.
 
Is there a way I can post the below routine(display,rotation,Displaytime)into my main? The way it is set up calls display from main and I need to integrate it into the main.
thanks



Display:
btfss Rotate,0
goto DisplayTime
goto Rotation

Rotation:

call Delay
rlf LED,f ; rotates bit to left.
btfsc STATUS,C ; did it rotate out of the display
bsf LED,0 ; yes, put it into bit 0
movf LED,w
movwf PORTD
retlw 0

DisplayTime:

movlw 0x00
movwf PORTD
call Delay
swapf TenSeconds,w
addwf Seconds,w
movwf PORTD
call Delay
swapf Hundreds,w
addwf Tens,w
movwf PORTD
call Delay
movlw 0x00
movwf PORTD
retlw 0



;Program Start---------------------------------------------------------
Start
call Init

Main

call Display
goto Main

end
 
The below code is for utilizing TMR2, I declared the variables hundreds,tens,seconds, and tenseconds. When each variable is declared does that mark that register address as with the result of TMR2? I am not sure if I explained it right but I am trying to make a stopwatch on the 16f887 board.
thanks


Stopwatch:
bcf PIR1,TMR2IF ;Clears interrupt flag
incf Hundreds,f ;Increment hundreds by one, subtract 10 from hundr
movlw .10 ;when zero flag is set clear hundreds and skip to Tens.
subwf Hundreds,w ;if zero flag in STATUS Z not reached skiptoEXITISR.
btfss STATUS,Z
goto ExitISR
clrf Hundreds

incf Tens,f ;after hundreds have been incremented and zero flagset in
movlw .10 ;STATUS z, skip to Tens which are incremented in similar subwf Tens,w
btfss STATUS,Z
goto ExitISR
clrf Tens

incf Seconds,f
movlw .10
subwf Seconds,w
btfss STATUS,Z
goto ExitISR
clrf Seconds
incf TenSeconds,f
movlw .10
subwf TenSeconds,w
btfss STATUS,Z
goto ExitISR
movlw 0xFF
movwf PORTD
goto $-1
 
Here's a nice tip. When you post code here, be sure to put it inside Code tags. Easiest way is to click the # in the menu just before pasting your code. Alternately, you can type in
Code:
 before your code and [/co[COLOR="Black"]de][/COLOR] after your code. This forces the web-site to keep your formatting so people can actually read your code. When you don't do this, it loses all indentation and turns into the unreadable lump in your original post.

It should look like this:
[CODE]Stopwatch:
	bcf	PIR1,TMR2IF	;Clears interrupt flag
	incf	Hundreds,f	;Increment hundreds by one, subtract 10 from hundr
	movlw	.10		;when zero flag is set clear hundreds and skip to Tens.
	subwf	Hundreds,w	;if zero flag in STATUS Z not reached skiptoEXITISR.
	btfss	STATUS,Z
	goto	ExitISR
	clrf	Hundreds

	incf	Tens,f		;after hundreds have been incremented and zero flagset in
	movlw	.10		;STATUS z, skip to Tens which are incremented in similar subwf Tens,w
	btfss	STATUS,Z
	goto	ExitISR
	clrf	Tens

	incf	Seconds,f
	movlw	.10
	subwf	Seconds,w
	btfss	STATUS,Z
	goto	ExitISR
	clrf	Seconds
	incf	TenSeconds,f
	movlw	.10
	subwf	TenSeconds,w
	btfss	STATUS,Z
	goto	ExitISR
	movlw	0xFF
	movwf	PORTD
	goto	$-1
 
hi futz,
When you don't do this, it loses all indentation and turns into the unreadable lump in your original post.

Have you seen Pommies codetidy, nice little program reformats lumps into readable and pasteable code..
 
I've seen it mentioned. Been meaning to try it.

hi,
Just before the infamous finger, Mike was fixing a small bug.
The latest version has a convert from upper case to lower case option.

The original version works OK, no case change.
 
hi,
Just before the infamous finger, Mike was fixing a small bug.

Mike managed to fix the small bug within the 15 minute edit window.

I just wish I could edit the first post and add the fixed version. Oh well.

The infamous finger has now started to contribute the odd keypress to late night postings.

Mike.
 

hi Mike,
Thanks, I have re-downloaded,, will give it a go.

Are you going to release the VB source.?
Reason for asking is, I reduce the tabs sizes, so that the tidied listing is confined to left side of the screen when running simulations.

Regards
 
I'll post the source tomorrow. I thought about making the tab sizes settable and will probably add that in the morning as well. If you find any problems with the new version then let me know and I'll fix them as well.

Mike.
 
I also find myself writing in SF for small code snippets, its formatting works great for code tags (tabs don't jumble up with space's etc)



Just on the project, have you heard of a DS1307? It uses a 32.768Khz external crystal, and depending on your circuit layout, its by far more accurate from my experience.

Its got some great built in features that make things like this easy, like leap year compensation until the year 2100, and battery powered backup mode so it utilizes minuscule current while doing so
**broken link removed**
**broken link removed**
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…