#include <p16f887.inc> 

cblock	0x070
	tmr1high
	tmr1low
	counter
	maxsp
	endc

	org	0
	goto		main

	org	4
	clrf		PIR1
	bsf			INTCON,7
	bcf			T1CON,0
	btfss		maxsp,0
	goto		startint
	call		timerset
	goto		maxspeed			;Ha eléri a max sebességet
	
main
;Timer1 beállítása
	movlw		b'00111000'			;Pre=1:1, Enable 
	movwf		T1CON
	
;Interrupt beállítása
	movlw		b'11000000'
	movwf		INTCON				;Interrupt engedély
	banksel		PIE1
	movlw		b'00000001'
	movwf		PIE1				;TM1 interrupt engedély
	
;PORTD beállítása
	clrf		TRISD

;Belső szcillátor 8 MHz 	
	bsf			OSCCON,4
	bsf			OSCCON,5
	bsf			OSCCON,6
	
;Kezdőértékek beállítása
	movlw		b'11110000'			;Fordulatszám kezdő érték
	movwf		tmr1high
	movlw		b'00000000'
	movwf		tmr1low

	banksel		PORTD				;Az első fázis beállítása
	movlw		0x0E
	movwf		PORTD
	
	movlw		b'00000001'
	movwf		counter				;Segédszámláló fázisváltáshoz
	bcf			maxsp,0
	
startmain
	bsf			T1CON,0				;Timer1 indítása
	goto		stop

start
	incf		tmr1low
	incf		tmr1low
	incf		tmr1low
	incf		tmr1low
	btfsc		STATUS,2
	goto		tmr1h
	call		timerset
	retfie

tmr1h								;Ha tmr1low túlcsordul,
	incf		tmr1high			;akkor 1-el növeli tmr1high regisztert
	call		timerset
	retfie
	
maxspeed
	bsf			T1CON,0	
	bcf			STATUS,C
	rlf			counter,1
	btfsc		counter,1
	goto		one
	btfsc		counter,2
	goto		two
	btfsc		counter,3
	goto		three
	btfsc		counter,4
	goto		four
	btfsc		counter,5
	goto		five
	movlw		b'00000001'
	movwf		counter
	goto		six

startint
	call		timerset
	bsf			T1CON,0			
	bcf			STATUS,C			;Interruptok után PORTD beállítása
	rlf			counter,1
	btfsc		counter,1
	goto		one
	btfsc		counter,2
	goto		two
	btfsc		counter,3
	goto		three
	btfsc		counter,4
	goto		four
	btfsc		counter,5
	goto		five
	btfsc		counter,6
	goto		six
	movlw		b'00000001'
	movwf		counter				;Segédszámláló fázisváltáshoz
	
	goto		start

one
	movlw		0x0E
	movwf		PORTD
	nop
	nop
	nop
	nop
	nop
	retfie
two
	movlw		0x0B
	movwf		PORTD
	nop
	nop
	nop
	nop
	retfie
three
	movlw		0x23
	movwf		PORTD
	nop
	nop
	nop
	retfie
four
	movlw		0x32
	movwf		PORTD
	nop
	nop
	retfie
five
	movlw		0x38
	movwf		PORTD
	nop
	retfie
six
	movlw		0x2C
	movwf		PORTD
	retfie

timerset
	movf		tmr1low,0
	movwf		TMR1L
	movf		tmr1high,0
	movwf		TMR1H
	return	
		
stop
	movf		tmr1high,0
	addlw		b'00000010'			;Max speed beállítása
	btfsc		STATUS,2
	bsf			maxsp,0
	goto		stop

	end
