How to read the timer 2 value into a variable in Keil C?

Status
Not open for further replies.

UroBoros

Advanced Member level 2
Joined
May 5, 2004
Messages
642
Helped
19
Reputation
38
Reaction score
8
Trophy points
1,298
Location
Cochin - India
Activity points
6,463
little endian keil

Hai,
In Timer2 ISR I have a unsigned intiger variable.
How I can I equate the lower byte of the variable to TL2 and upper byte to TH2.
so that I can treat the variable as timer value
I think keil has got a method.not sure.
Please suggest


Thanks for the reply
Sorry for the mistake. my question was totally confusing.
Actually on the external interrupt from port3.6,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.
I am a beginner in C.
Thanks again

Picstudent
 

Re: Keil declaration Help!

Hi,

try this:


unsigned int var, tmpHI, tmpLo;

tmpHi = var & 0xFF00;
tmpHi >>= 8;

tmpLo = var & 0x00FF;

TL2 = tmpLo;
TH2 = tmpHi;



Best Regards.


Mr.Cube
 

    UroBoros

    Points: 2
    Helpful Answer Positive Rating
Re: Keil declaration Help!

If I'm understand well, you have TH2 and TL2 and you want to make one integer variable from those two.


unsigned int timer2_val;

timer2_val = TH2*256 + TL2;

or

unsigned int tmp;

tmp = TH2 << 8;

timer2_val = tmp + TH2


Mr.Cube
 

    UroBoros

    Points: 2
    Helpful Answer Positive Rating
Re: Keil declaration Help!

Hi,

try this:

#define HIGHBYTE 0
#define LOWBYTE 1

union
{
unsigned int timer16;
unsigned char timer8[2];
}
timer;

timer.timer16=0x1234; // the 16 bit value
TH2=timer.timer8[HIGHBYTE]; // highbyte of the 16 bit value
TL2=timer.timer8[LOWBYTE]; // lowbyte of the 16 bit value

Please note if you are using this on a PIC HIGHBYTE has to be defined as 1 and LOWBYTE has to be defined as 0.

best regards
 

    UroBoros

    Points: 2
    Helpful Answer Positive Rating
Re: ke*il declaration Help!

Picstudent said:
Hai,
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.
Picstudent

if little endian order memory (Keil >4.0 or intel)

// define union
union WORDBYTE {
unsigned int two;
unsigned char one[2]; // one[0]=high one[1]=low
};

// declare variable

main()
{
union WORDBYTE timer2_val;
timer2_val.one[0] = TH2;
timer2_val.one[1] = TL2;

if(timer2_val.two >0xabcd) {
// your code here
}
}

if big endian order memory (Keil<4.0 or Motorola)
// define union
union WORDBYTE {
unsigned int two;
unsigned char one[2]; // one[0]=low one[1]=high
};

main()
{
union WORDBYTE timer2_val;
timer2_val.one[0] = TL2;
timer2_val.one[1] = TH2;

if(timer2_val.two >0xabcd) {
// your code here
}
}
 

    UroBoros

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…