Hi Stew,
Based on the PIC 16F 684.
Please tell us if we are “teaching grandma to suck eggs” and there are many solutions to a problem – this is my version.
Have you considered a device frequency (Fosc) of 4MHz (4Mhz crystal).
If you look at the data sheet Figure 5-1, the Timer can use the internal clock which is Fosc/4 which then increments the Timer every microsecond ( 1us) if Fosc is 4Mhz.
Incrementing Timers by 1us becomes very useful when working with the servos, which use the 1 to 2ms PWM signal (i.e. 1000 to 2000us).
When programing, we will be using 3 bases decimal, binary and Hexadecimal – therefore d, b or h after a number stops any confusion.
This solution is to give the potentiometer complete mechanical movement a range of 0 to 0.127ms.
Now, if we want a 0 to 0.1ms delay, we need to load a number between 0 and 100d into the timer (assuming a 4MHz frequency – 1us clock). The Timers actually count up to zero (the overflow condition) and then issue an interrupt to say that they’ve arrived at zero.
Now Timer0 is an 8 bit register, and therefore can represent a number of the range 0 to 255d (it is very useful to have a calculator than can switch between decimal, binary and Hex). Since the Timer counts up to zero, we must initially set it to 155d for a 100us delay (see Paragraph 5.1.1 note about the two instruction delay if we want to be really accurate). You will notice that the Timer is set to the equation “255d minus delay”.
The Timer0 then increments every clock pulse to 255d and then overflows to zero and issues the interrupt.
To reiterate what Mike has said…
The analogue input value, as you can see from Fig. 9.3, arrives at two registers ADRESH and ADRESL containing the highest 8 bits of the analogue value in the first register and the lowest 2 bits in the second register – if ADFM is set to zero.
We just have to “right shift” the register once to get a value of 0 – 127d, which can go into the Timer equation above. Set bit 8 of ADH to zero in case the carry from bit 0 is 1.
First move the value of ADRESH into your own registers, say, ADH. This avoids the possibility of ADRESH changing during your conversion.
Little register sketches appear at this stage!
Now in ADH you have the value to be subtracted from 255d before it sets the Timer.
Referring to the instruction set Table 13.2 – then are plenty of logical operators like “RRF” right rotate and “BTFSS”, which can be used to reduce the 10bit analogue value into a 7 bit value that can be used in the Timer equation. You can also get your teeth into “SUBWF” for the subtraction process.
I hope this points you in the right direction. It can be a bit (excuse pun!) of a challenge to start with, but each instruction mastered is like adding another spanner to the tool kit!
Ian