Actually on the external interrupt from port,I want to read the timer 2 value into a variable.(TH2 and TL2). suppose I declare a variable unsigned int timer2_val; How can I move TL2 and TH2 into lower and upper nibble of timer2_val, so that I can treat Timer2 value as a single variable. Weather keil has any declaration for assigning the lower byte or higher byte of a unsigned int to any hardware register or something like that. I am a beginner in C. Cygnalroy
timer2_val = (TH2*256)+TL2 or timer2_val = (TH2<<8)|TL2 You didn't realy mean 'Nibble' as that's 4 bits.
hi,
timer2_val = (TH2*256)+TL2 or timer2_val = (TH2<<8)|TL2
This is pure, standard 'C' - nothing specifically to do with Keil. See this thread - the principles involved are the same: http://www.keil.com/forum/docs/thread5275.asp Note that using pointers or unions may be more efficient, but is non-portable - since it relies upon the specific compiler's byte ordering. Using shifts (or multiplications) is portable, but may be less efficient.
The SFR keyword can't be used within a struct or union. Nor can you do something like
timer2_val = *(unsigned int*)&TH2;
sfr16 T2 = 0xcc;
union word_byte { unsigned int word; unsigned char byte[2]; }; y.byte[0] = gTH1; y.byte[1] = gTL1; x = TIMER_1_RELOAD + y.word;
x = ((unsigned int)gTH1 << 8 ) + gTL1 + TIMER_1_RELOAD;
Well, but just remember that you should stop the timer before read these registers otherwise you may obtain wrong values. It is because reading of integer value is split into two reading of bytes (chars) due 8051 8-bit architecture. And if timer run then there is a chance that TL2 and TH2 registers change their values between these two readings. Sage advice!