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...
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...
Then if we look at the above segment of the timing loop I have changed a line from:
To this -
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:
has now become:
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!
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: