Hello Mauricio,
I presume your code does not work because it will not compile. The problem is that you are attempting to used floating point variables. The HC908JK1 simply does not have sufficient memory resources to accommodate floating point calculations.
You will need to utilize integer arithmetic for your calculations. Since the variables Modulo and Duty ultimately need to become integer values for writing to TIM module registers, this is not a drawback.
Assuming the TIM clock freqency remains at 2.5 MHz, as is likely to be the case, the modulo value can be pre-calculated at compile time for an overflow period of 20 ms, and this constant value (49999) written directly to the TMOD register.
The value written to TCH0 would then vary between the following limits -
0.65ms -> 1625
1.30ms -> 3250
1.95ms -> 4875
For example, if Dureza were to be expressed as an integer value 0 -> 100, the following integer calculation would be required -
unsigned int Dureza = 50; /* Initialize to servo mid-point */
TCH0 = Dureza * 325 / 10 + 1625;
Regards,
Mac
Message Edited by bigmac on
2007-08-20 02:19 PM