Excellent tutorial Tabsoft.
Explains why I always had to "fine tune" after the fact when setting up a timer.
Excellent tutorial Tabsoft.
Explains why I always had to "fine tune" after the fact when setting up a timer.
Louie
Ok I was thinking where you said you made the PWM to measure on a scope,
so had my best go at that sitting here rather than scab the code
Probably not the most effective with memory or time, but I think the frequency will be correct.
and it’s probably also more like asm pseudo code at the moment.
I like the idea the PWM is almost free if you wanted it,Code:‘start of code somewhere: ‘ flipflip var byte ‘in ISR straight after Ticks is incremented ‘ @ btfsc flipflop ,00 ; 1/2 @ goto aaaa ; 2 @ goto bbbb ; 2 aaaa: ‘ @ bsf portb, 07 ; 1 @ goto overpwm ; 2 bbbb: ‘ @ nop ; 1 @ bcf portb, 07 ; 1 overpwm: ‘ @ comf flipflop ; 1 - but timing doesn’t matter here ‘instruction time 6 for flip state, 6 for flop state, ‘7 for the code to execute no matter the status
and there might be a situation you’d want the electrical frequency of the timer.
It might not have originally been obvious I was setting this up for a pic.. only works up to 20MHz though:
Though I have not needed to, it’s pointless unless you are varying the pic’s oscillator frequency,Code:'$FFFF - (((20 / 4) * 10000) - 8) = $3CB7 ‘might as well fix the constant val too constval = $FFFF - (((f/4)*10000-7)'
and also telling the pic program the new osc frequency, or figuring out some other way what frequency the pic is running at.
That PWM would indicate resolution error at runtime for different osc frequencies.
Last edited by Art; - 20th May 2015 at 16:02.
Very similar to what I did.
I used the Elapsed Timer to generate a 1KHz 50% duty cycle signal on PORTA.4.
I wanted to test the stability of the Timer1 interrupt period using the Elapsed Timer code we have been discussing.
In the ISR I used a call to the RELOAD_TIMER macro after setting the state of the pin.
That lets the Elapsed Timer code handle things and I could check the accuracy of the output.
I was lazy though and did not setup a variable to track the state of the pin.
I just read the port and flipped the pin.
This was a test and I really didn't mind if I hit the other pins on PORTA.
500ms Timer1 ISR:
Code:asm COMF PORTA, 0 ; 1 Tcy - Complement PORTA, Store in W ANDLW 10h ; 1 Tcy - Force all PORTA pins to 0 except pin 4 MOVWF PORTA ; 1 Tcy - Set the new value for PORTA RELOAD_TIMER ; Reload the timer endasm
Last edited by Tabsoft; - 20th May 2015 at 16:20. Reason: Additional Info
Regards,
TABSoft
There’s some fun to be had. It would be possible to rotate a byte with any value in it
and look at the value of a bit in that byte to get some more free frequency divisions.
Or if you had a 100Hz timer and wanted a 1Hz pulse, you could do similar to the timer code,
load a constant value to a byte, increment it, and watch for one of the more significant bits to be set.
Bookmarks