EmbeddedRelated.com
Forums

electronic Chronometer with mplab

Started by Mich May 23, 2005
Hi
For school I have to program with mplab a electronic Chronometer
but the chronometer doesn't work
can some please look at the code

Thanks
Mich


; This program simulates a chronometer

	title "Chronometer"
	list p=16F870, r=hex, n=0
	__config 0x0C02
	#include	<p16F870.inc>

;************************************************************
; variables (equ statements)
;************************************************************

Tiental			equ 0x25 	; =10 sec 	pin RA0
Eenhede			equ 0x26 	; =1 sec	pin RA1
Tiende			equ 0x27 	; =0,1 sec	pin RA2
AantalOverFlow	equ 0x28 	; houdt bij hoeveel keer overflow is geweest
=> bij 5 een tiende bijtellen
W_TEMP			equ 0x29	; temp w register
STATUS_TEMP 	equ 0x30	; temp status register
Vlag			equ 0x31 	; 0 = timeroverflow , 1 = buttonchange, 2= press reset
SwitchChange	equ 0x01	; er is een switch change
SwitchState		equ 0x02 	; 1 bij running,0 bij stop
TimerOverFlow 	equ 0x03	; vlag voor timeroverflow

;************************************************************
; reset vector
;************************************************************

	ORG 0x00
	goto Start
	ORG 0x04
	goto Interrupt


;************************************************************
;program code starts here
;************************************************************

Start
	bsf		STATUS,RP0	; Bank 1 selecteren
	clrf 	TRISC		; Alle pinnekes PoortC output
	movlw 	0x03		; eerste 2 pins input van PORTB
						; RB0 is start / stop button
						; RB1 is reset => om alles te reseten
	movwf 	TRISB		; waarde in TRISB plaatsen

	movlw 	0x06
	movwf 	ADCON1
	movlw 	0x07		; eerste 3 pins are input
						; TRISA,0 tiental
						; TRISA,1 eenhede
						; TRISA,2 tiende
	movwf 	TRISA		; waarde in TRISA plaaten

	movlw 	0x47 		; option_reg juist zetten
	movwf 	OPTION_REG	; pull ups enabled, interupt op stijgende flank,
TMR0 kiezen als klok, werken met stijgende flank, nog eens prescalen
door delen door 256

	movlw 	0xB0		; INTCON juist zetten
	movwf 	INTCON		; setting all interrupts right

	bcf 	STATUS,RP0	; bank 0 selecteren

	movlw 	0xB0
	movwf 	INTCON		; setting all interrupts right

	movlw 	0x3B		; 3B
	movwf 	TMR0		; start te tellen van 59 tot 256 en we hebben 5
timeroverflows nodig voor 100ms
	clrf 	PORTC		; clear all output latches

	clrf 	Tiental			; tiental op nul zetten
	clrf 	Eenhede			; eenhede op nul zetten
	clrf 	Tiende			; tienden op nul zetten
	clrf 	AantalOverFlow	; Aantal overflows op nul zetten
	clrf 	Vlag			; Vlag register clearen
	goto 	Main			; deel waar we kijken of er interups zijn geweest of niet


Main
	call 	Display
	btfsc 	PORTB,1 				; kijken of de reset hoog staat
	goto 	NulMaken				; alles terug op nul zetten: tiental, eenhede,
tiende
	btfsc 	Vlag, SwitchChange		; kijken of er een verandering is van de
Start/Stop Switch
	Call 	ChangeSwitch			; er is een verandering
	btfss 	Vlag, SwitchState		; kijken of de chronometer aan het lopen is
	goto 	Main					; als de chronometer niet loopt => terug naar main
	btfss 	Vlag, TimerOverFlow		; kijken of er een TimerOverFlow is
	goto 	IncAantalOverFlow ;Main					; als er geen TimerOverFlow is terug
keren naar main
	goto 	IncAantalOverFlow		; als er een TimerOverFlow

; alles reseten
NulMaken
	clrf 	Tiental				; tiental clearen
	clrf 	Eenhede				; eenhede clearen
	clrf 	Tiende				; tiende clearen
	goto 	Main

; de Switchverandering aanpassen in Vlag
ChangeSwitch
	bcf		Vlag, SwitchChange
	btfsc 	Vlag, SwitchState		; kijken of bezig waren of niet: 1 bij
running,0 bij stop en deze veranderen
	goto 	LaagHoog
	goto 	HoogLaag

LaagHoog
	bcf 	Vlag, SwitchState		; de Vlag van SwitchState 0 maken => we zijn
gestopt met de chronometer
	return

HoogLaag
	bsf 	Vlag, SwitchState		; de Vlag van SwitchState 1 maken => we zijn
begonnen met de chronometer
	clrf 	AantalOverFlow			; de teller die het aantal overflows bij houdt
clearen
	return

IncAantalOverFlow
	bcf		Vlag, TimerOverFlow		; vlag terug laag zetten
	incf 	AantalOverFlow,1		; 1 optellen bij het aantal overflows
	movf 	AantalOverFlow,0		; naar werkgeheugen verplaatsen om te testen
of we al aan 5 overflows zitten
	sublw 	2     					; 5 van het werkgeheugen aftrekken
	btfss 	STATUS,Z 				;als we aan 5 overflows zaten is de zeroflag 1
	goto 	Main
	clrf 	AantalOverFlow    		;teller van het aantal overflows clearen
	goto 	IncTiende

IncTiende
	movf 	Tiende,0				; naar werkgeheugen verplaatsen om te testen of we
al aan negen tiende zitten
	sublw 	9
	btfsc	STATUS,Z				; als we op negen zitten is de zeroflag 1
	goto 	IncEenhede				; indien ja => eenhede + 1 en de tiende clearen
	incf 	Tiende,1				; zoniet bij tiende 1 optellen
	goto 	Main					; en terug keren naar Main

IncEenhede
	clrf 	Tiende					; de tiende op nul zetten
	movf 	Eenhede,0				; naar werkgeheugen verplaatsen om te testen of we
al aan negen tiende zitten
	sublw 	9
	btfsc 	STATUS,Z				; als we op negen zitten is de zeroflag 1
	goto 	IncTiental				; indien ja => Tiental + 1 en de Eenhede clearen
	incf 	Eenhede,1				; zoniet bij tiende 1 optellen
	goto 	Main					; en terug keren naar Main

IncTiental
	clrf 	Eenhede
	movf 	Tiental,0				; naar werkgeheugen verplaatsen om te testen of we
al aan negen tiende zitten
	sublw 	9
	btfss 	STATUS,Z				; als we op negen zitten is de zeroflag 1
	goto 	EindeChrono				; indien ja => Tiental clearen
	incf 	Tiental,1				; zoniet bij tiende 1 optellen
	goto 	Main

EindeChrono
	clrf 	Tiental
	goto 	Main


;***********************************************************************
; alles van het interupt gedeelte
; eerst context saving
; dan kijken vanwaar interupt
; dan juist handelen	=> timeroverflow 	=> bijtellen indien nodig
;						=> Switch			=> starten/stoppen
;***********************************************************************
Interrupt
 	;context saving
	movwf 	W_TEMP
	SWAPF 	STATUS,W
	CLRF 	STATUS
 	MOVWF 	STATUS_TEMP

	; kijken vanwaar de interrupt
	bsf 	STATUS,RP0 			; Bank 1 selecteren
	btfsc 	INTCON,INTF 		; kijken of de interrupt van de Switch kwam
	call 	InterSwitch
	btfsc 	INTCON,T0IF			; kijken of de interrupt van de timer kwam
	call 	InterTimer


	bcf 	STATUS,RP0 			; Bank 0 selecteren

	; context terug plaatsen
	SWAPF 	STATUS_TEMP,W
	MOVWF 	STATUS
	SWAPF 	W_TEMP,F
	SWAPF 	W_TEMP,W

	; terug keren
	retfie

InterSwitch
	bcf 	STATUS,RP0 			; Bank 0 is selected
	bsf 	Vlag, SwitchChange
	bsf 	STATUS,RP0 			; Bank 1 is selected
	bcf 	INTCON,INTF			; vlag van de Switch terug op nul zetten
	return

InterTimer
	movlw 	0xF0
	movwf 	TMR0 				; tellen van 59 tot 255
	bcf 	STATUS,RP0 			; Bank 0 is selected
	bsf 	Vlag, TimerOverFlow
	bsf 	STATUS,RP0 			; Bank 1 is selected
	bcf 	INTCON,T0IF			; vlag van de Timer terug op nul zetten
	return

;************************************************************
;einde interupt gedeelte
;************************************************************

;************************************************************
; alles van Display Chronometer
; keuze van wat op de display
; eerst kijken naar tiende dan eenhede en dan tientallen
; niets gekozen => toon de tiende
;************************************************************
Display
   	btfsc 	PORTA,2 			; kijken naar tientallen
	goto 	Toontiental	;
	btfsc 	PORTA,1 			; kijken naar eenhede
	goto 	Tooneenhede	;
	goto 	Toontiende			; als niets geselecteerd => toon tiende

Toontiental
	movf 	Tiental,0
	call 	Tabel
	goto 	Toon

Tooneenhede
	movf 	Eenhede,0
	call 	Tabel
	goto 	Toon

Toontiende
	movf 	Tiende,0
	call 	Tabel
	goto 	Toon

Toon
	movwf 	PORTC
	return

Tabel
	addwf 	PCL		;volgorde van plaatsing: dp g f e d c b a BEST NOG IS
NAKIJKEN MET DOBBEL
	retlw	0x40	;display 0
	retlw	0x79	;display 1
	retlw	0x24	;display 2
	retlw	0x30	;display 3
	retlw	0x19	;display 4
	retlw	0x12	;display 5
	retlw	0x02	;display 6
	retlw	0x78	;display 7
	retlw	0x00	;display 8
	retlw	0x10	;display 9
    return          ;einde tabel-subroutine
;************************************************************
;einde Display van Chronometer
;************************************************************

	END ;einde programma

Mich wrote:
> Hi > For school I have to program with mplab a electronic Chronometer > but the chronometer doesn't work > can some please look at the code >
Just a recommendation in all well-meaning. Please take on the habit to write your comments and variable names in english. It's not my mother-tongue either but over the years it has helped me so many times when I had to send sample code to different support departments. Encrypted comments won't matter if a company choose to move the development to some other country, so it would not even fall into the category for job-insurance. Regarding your code I have not looked at it due to the above reason. It would simply be much too much work to understand it with the encrypted comments. Just about the same thing as reading uncommented disassembly. /Leif
Thanx for the tip

but it works now
forgot to switch banks somewhere
stupid mistake

Hi, another potential problem is your lookup table. As it is it can end
up anywhere in memory, when you add more code it gets pushed further
down program memory untill maybe across a page boundary, then your code
will fail. Then you have the problem of looking for the (non existent)
fault in the "new" piece of code. Small tables are best placed at the
front of the code where they stay put, large tables at the end of the
memory fixed with an org statement.