Struggling with simple error arrhhhh!

Status
Not open for further replies.

olly_k

Member
Hey folks I have decided to give PICs another go after a long rest so treated myself to a PICKIT2 debug kit (real good price) and looked through the tutorials to recap - wow it is amazing what a combination of 3 years no pics (and only at basic level then) and changing device types can do to your head!!!

Anyway, I am using the tmr0 to create a 24hz strobe. ~37ms off ~4ms on. When I use the following code everything times perfectly...

Code:
ForeverLop:
	bsf	        STATUS,RP1
	movlw	D'110'
	movwf	TMR0
	bcf	        STATUS,RP1
ForeverLoop:
	btfss	        INTCON,T0IF    ; wait here until Timer0 rolls over
	goto	        ForeverLoop
	bcf	        INTCON,T0IF    ; flag must be cleared in software

	bsf	         PORTD,0


;ForeverLp:
	bsf	        STATUS,RP1
	movlw	D'240'
	movwf	TMR0
	bcf	        STATUS,RP1

ForeverLoo2:
	btfss	        INTCON,T0IF    ; wait here until Timer0 rolls over
	goto	        ForeverLoo2
	bcf	        INTCON,T0IF    ; flag must be cleared in software

	bcf	        PORTD,0


	goto	        ForeverLop


ok so next I wanted to have an option to implement slightly different timing, this time, 'off' for 50ms and 'on' for 5ms. I wanted to implement this with the push of a switch (connected to RB0 on the dev board).
Basically I seed two files with predetermined values (41ms default) as follows...


Code:
init:
	bcf	        PORTD,1		; this tells u to come back here from change routine
	movlw	D'110'		; this is the off time = 38ms
	movwf	fpsoff
	movlw	D'240'		; this is on time leaves 16 until carries over ~ 4ms
	movwf	fpson



Then if we look at the above segment of the timing loop I have changed a line from:

Code:
ForeverLop:
	bsf	        STATUS,RP1
	movlw	D'110'
	movwf	TMR0
	bcf	        STATUS,RP1

To this -

Code:
ForeverLop:
	bsf	        STATUS,RP1
	movf	        fps0ff,w            <<<<<< here is change
	movwf	TMR0
	bcf	        STATUS,RP1

The only other difference to the closed loop system is the end to allow breakout and change value in separate routine hence the original code:

Code:
ForeverLoo2:
	btfss	         INTCON,T0IF    ; wait here until Timer0 rolls over
	goto	         ForeverLoo2
	bcf	         INTCON,T0IF    ; flag must be cleared in software

	bcf	         PORTD,0


	goto	         ForeverLop

has now become:

Code:
ForeverLoo2:
	btfss	         INTCON,T0IF    ; wait here until Timer0 rolls over
	goto	         ForeverLoo2
	bcf	         INTCON,T0IF    ; flag must be cleared in software

	bcf	         PORTD,0

	btfsc	         PORTB,0           ; switch is tied to ground and weak pullups enabled!
	goto	         foreverlop
st:	btfss	         PORTB,0           ; this is simple debounce which does work!


.......code to change files fpson and fpsoff continues here before being returned to Foreverlop.

Ok so despite what I think is a fairly simple and straightforward change, has resulted in my timing values changing! When I scope portd I am getting a constant 25ms off with 4.5ms on phase as opposed to ~37/~4.2. I added a couple of test routines which involve reading registers fpsoff / on and displaying to portd which works fine. Also, when I switch 'speeds' in normal mode, led portd,1 illuminates to show that the fps registers have been updated with a new value, or at least the pic has been to that part of the code! However again timing values are not changing!

I just don't understand what is happening here - in my mind the only difference is the fact that I am not entering values directly to working through the code, but indirectly via reading registers to working, then writing straight to tmr0, but also the other difference being the small breakout routine at the end of the last tmr0 loop. If I was missing a rollover on tmr0 then surely the timings would increase but I have gone from 24hz to 32hz and don't know why!

I realise I might have gone about this inefficiently but personally I enjoy working such things out for myself so when I have accomplished a few more projects I will possibly look this up and start again!

Oh PWM will possibly be my next route but would like to understand what is wrong here first!
 
Last edited:
so know one is able to help?

Well I have worked out it is something to do with transferring the value from a file reg to working then to tmr0 because when I rewrite the program with completely separate routines for the speeds (maintaining near identical code) with literal values written straight to working it operates as expected...

But what is it that is preventing the above method from working? Am I writing to tmr0 correctly?
 

hi,
As Nigel says use the '#' sign to enclose your code in order to retain its formatting.

Also post the FULL listing, whats the PIC type.?

Code:
ForeverLop: 
	bsf	STATUS,RP1
	movlw	D'110'
	movwf	TMR0
	bcf	STATUS,RP1
ForeverLoop: 
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	ForeverLoop
	bcf	INTCON,T0IF	; flag must be cleared in software

	bsf	PORTD,0


;ForeverLp: 
	bsf	STATUS,RP1
	movlw	D'240'
	movwf	TMR0
	bcf	STATUS,RP1
ForeverLoo2: 
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	ForeverLoo2
	bcf	INTCON,T0IF	; flag must be cleared in software

	bcf	PORTD,0


	goto	ForeverLop

************************************************** ************************************* 

ok	so	next I wanted to have an option to implement slightly different timing, this time, 'off' for 50ms and 'on' for 5ms. I wanted to implement this with the push of a switch (connected to RB0 on the dev board).
Basically I	seed two files with predetermined values (41ms default) as follows...

************************************************** ************************************* 

init:
	bcf	PORTD,1		; this tells u to come back here from change routine
	movlw	D'110'		; this is the off time = 38ms
	movwf	fpsoff
	movlw	D'240'		; this is on time leaves 16 until carries over ~ 4ms
	movwf	fpson

************************************************** ************************************* 

Then	if	we look at the above segment of the timing loop I have changed a line from:

************************************************** ************************************* 

ForeverLop: 
	bsf	STATUS,RP1
	movlw	D'110'
	movwf	TMR0
	bcf	STATUS,RP1

************************************************** ************************************* 

To	this	-

************************************************** ************************************* 

ForeverLop: 
	bsf	STATUS,RP1
	movf	fps0ff,w <<<<<< here is change
	movwf	TMR0
	bcf	STATUS,RP1

************************************************** ************************************* 

The	only	other difference to the closed loop system is the end to allow breakout and change value in separate routine hence the original code:

************************************************** ************************************* 

ForeverLoo2: 
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	ForeverLoo2
	bcf	INTCON,T0IF	; flag must be cleared in software

	bcf	PORTD,0


	goto	ForeverLop

************************************************** ************************************* 

has	now	become:

************************************************** ************************************* 

ForeverLoo2: 
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	ForeverLoo2
	bcf	INTCON,T0IF	; flag must be cleared in software

	bcf	PORTD,0

	btfsc	PORTB,0		; switch is tied to ground and weak pullups enabled!
	goto	foreverlop
st:	btfss	PORTB,0		; this is simple debounce which does work!
 
To be fair I cannot find a code button anywhere but have amended things manually now so hope that makes things a little easier...
regards the code button when I go to advanced mode I get no options at all!

As you posted code without 'code' tags it's too jumbled to bother trying to read, so most people won't bother.
 
hi,
As Nigel says use the '#' sign to enclose your code in order to retain its formatting.

Also post the FULL listing, whats the PIC type.?


[/CODE]

It is a 16f887 as supplied with the pickit2. I will transfer to a smaller pic when I have sorted the basic problem out. That will be the next challenge I didn't really want to supply the rest of code partly because the error seems to lie with the writing of code to tmr0 coming from a register and not literal write to working if that makes sense?
 
Last edited:

hi,
Look at this image.
BTW: if you post the FULL listing we can simulate the program.

 
Last edited:
hi,
Look at this image.
BTW: if you post the FULL listing we can simulate the program.

View attachment 39368

ok thanks Eric here goes....


Code:
#include <p16F887.inc>
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V



	cblock     0x20
Display
ledoff
ledon

	endc


;Display	EQU	H'0020'
;fpsoff		EQU	H'0021'
;fpson		EQU	H'0022'






	org 0
	bsf	STATUS,RP0	; Bank 1
	movlw	b'00000111'	; configure Timer0.  Sourced from the Processor clock;
	movwf	OPTION_REG	; Maximum Prescaler
	clrf	TRISD		; Make PortD all output
	movlw	B'00000001'	; make RB0 input
	movwf	TRISB		; 
	movlw	B'00000001'	; weak pullup rb0
	movwf	WPUB		;	

	bsf	STATUS,RP1	; select Register Bank 3
	movlw	D'0'		; figure to program portb function
	movwf	ANSELH		; PortB pins are digitial (important as RB0 is switch)
	bcf	STATUS,RP1	; back to Register Bank 0
	bcf	STATUS,RP0	; Bank 0

	clrf	PORTD
	clrf	Display


	btfsc	PORTB,0
	goto	init
	bsf	Display,0


init:
	bcf	PORTD,1		; this clears 2nd speed indicator
	movlw	D'110'		; pre-program for 24fps - this is the off time = 38ms
	movwf	ledoff
	movlw	D'240'		; this is on time (256 - 16) until carries over ~ 4ms
	movwf	ledon



	btfsc	PORTB,0		; Go into test? If input high then goto start>

	goto	$+5		; FOR SOME REASON I GET PREVIOUSLY UNDEINED LABEL WHEN I [GOTO START] HERE WHY????

	btfss	PORTB,0		; basic debounce a little tempramental. will sub for a delay when finished!
	goto	$-1
	btfsc	Display,0	; used to indicate test mode for later use.
	goto	test




Start:
	bsf	STATUS,RP1	;prepare to write to timer0!
	movf	ledoff,w	;get value
	movwf	TMR0		;write to tmr0
	bcf	STATUS,RP1	;come back to earth :)
offloop:
	btfss	INTCON,T0IF	; wait here until Timer0 rolls over
	goto	offloop
	bcf	INTCON,T0IF	; flag must be cleared in software

	bsf	PORTD,0



	bsf	STATUS,RP1	;see above
	movf	ledon,w
	movwf	TMR0
	bcf	STATUS,RP1
onloop:
	btfss	INTCON,T0IF    ; wait here until Timer0 rolls over
	goto	onloop
	bcf	INTCON,T0IF    ; flag must be cleared in software

	bcf	PORTD,0


	btfsc	PORTB,0		; test if user wants to change to alternate timing....

	goto	Start		; no then repeat

st:	btfss	PORTB,0		; temporary debaounce again
	goto	st
	btfsc	PORTD,1		; have we already been here i.e. are we on 55ms strobe? yes = reset!
	goto	init		; start again and reset
	bsf	PORTD,1
	movlw	D'61'
	movwf	ledoff
	movlw	D'234'
	movwf	ledon

	goto	Start

;				;following section for quick debug purposes it is rough but seems to do the job!


test:	
	btfss	PORTB,0
	goto	$-1
	movf	ledoff,w
	movwf	PORTD

	btfsc	PORTB,0
	goto	$-1

	btfss	PORTB,0
	goto	$-1

	movwf	TMR0
	movf	TMR0,w
	movwf	PORTD
	btfsc	PORTB,0
	goto	$-1

	btfss	PORTB,0
	goto	$-1
	movf	ledon,w
	movwf	PORTD
	btfsc	PORTB,0
	goto	$-1
	goto	init
	
     
	end


**BTW this is what my screen looks like!
 

Attachments

  • temp..jpg
    23 KB · Views: 151
lol some things gotta make you laugh there is me sorting out a rather upset 2 month old son and the bit of code flashed before my eyes and it was obvious I was trying to access memory register in the wrong bank! Now where is that facepalm picture....
 
This is just about right... lol
**broken link removed** **broken link removed** **broken link removed**
 

Attachments

  • doh..jpg
    26.7 KB · Views: 151
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…