Hello, All!
I'm studying the 8051 microcontroller and exploring now sample code I've found. I'm using: - Keil C compiler v6.21 - assembler compiler v6.20c - simulation DLL v2.20b
There sre some items I can't understand. Anyway, the code I included to this message, is doing the following:
1) initialize Timer 2 2) calculate the number of timer increments 3) define 16-bit reload value 4) split reload 16-bit value onto two 8-bit values.... etc......... #define TICK_MS 30 #define OSC_FREQ (12000000UL) #define OSC_PER_INST (12)
void Init_Timer2(const tByte TICK_MS) { tLong Inc, Reload_long; tWord Reload_16; tByte Reload_08H, Reload_08L;
// Timer 2 is configured as a 16-bit timer, // which is automatically reloaded when it overflows T2CON = 0x04; // Load Timer 2 control register
// Number of timer increments required (max 65536) Inc = ((tLong)TICK_MS * (OSC_FREQ/1000)) / (tLong)OSC_PER_INST;
// 16-bit reload value Reload_16 = (tWord) (65536UL - Inc); // Reload_long = (tLong) (65536UL - Inc);
// 8-bit reload values (High & Low) Reload_08H = (tByte)(Reload_16 / 256); Reload_08L = (tByte)(Reload_16 % 256);
// Used for manually checking timing (in simulator) //P2 = Reload_08H; //P3 = Reload_08L;
TH2 = Reload_08H; // Load Timer 2 high byte RCAP2H = Reload_08H; // Load Timer 2 reload capt. reg. high byte TL2 = Reload_08L; // Load Timer 2 low byte RCAP2L = Reload_08L; // Load Timer 2 reload capt. reg. low byte
// Timer 2 interrupt is enabled, and ISR will be called // whenever the timer overflows. ET2 = 1;
// Start Timer 2 running TR2 = 1;
EA = 1; // Globally enable interrupts }
When I'm doing tracing of the code, I find that the value of Reload_16 is set to 0x00D0 (in hex), but it should be:
Inc = ( 30 * (12000000/1000) ) / 12 = 30000, that's OK, clear... Reload_16 = 65536-30000 = 35536, 0x8AD0 in hex
But how?
And then Reload_08H and Reload_08L appeared to be: 0xD0 and 0xD0, so I still don't understand....
I'm sorry if my question is too silly, so please direct me onto some useful resource. Thanks in advance!
With best regards, Roman Mashak. E-mail: snipped-for-privacy@tusur.ru