??? 08/05/07 03:59 Read: times |
#142778 - Problem with using Timer 1 of AT89S52 in C |
I am learning C51 and am writing a C function to control RC servos with AT89S52. The pulses that control the servo shaft are generated by Timer 1.
The function: SetServoPo(X); where 0 <= X <= 180, in degree, sends a control pulse to the servo. The segment of the program that assigns values to TH1 and TL1 is: float tmp; unsigned char TH,TL; -------------------- tmp = 65536 - (10 * AxisAngle + 460); //with a 12MHz crystal TH = tmp / 256; //Derive the upper 8-bit from "tmp" TL = tmp - TH * 256; //Derive the lower 8-bit from "tmp" -------------------- and the program worked fine with a 12MHz crystal. After that I modified the program to work with a 22.1184MHz crystal, so the segment above was replaced with: -------------------- tmp = 65536 - (10 * AxisAngle + 460) * 22118400 / 12000000; TH = tmp / 256; TL = tmp - TH * 256; -------------------- ...and the servos started trembling fiercely (which indicated Timer 1 was refilled with random values). It happened to work correctly with this segment: -------------------- tmp = 65536 - (10 * AxisAngle + 460) * 221184 / 120000; //that is, by removing the two zeros TH = tmp / 256; TL = tmp - TH * 256; -------------------- Since I used to program in assembly I have no idea what was going wrong with the code in the past few days. I have no access to the Keil C51 software after school but I don't think the difference of the EVAL and the FULL versions caused the problem because the code size is within the 2k limit. Any guidance will be appreciated. Stanley |