This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Keil Variable declaration help

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

Parents
  • The SFR keyword can't be used within a struct or union.

    Nor can you do something like

    timer2_val = *(unsigned int*)&TH2;
    
    because TH2 must be a direct byte transfer otherwise the data will come out of idata, and it won't compile.

    On the cypress FX2 TH2 and TL2 are at 0xcd and 0xcc, which can be declared as:
    sfr16 T2 = 0xcc;
    
    This is straight out of the cx51 manual under sfr16, page 100.

    However, Timer 0 and Timer 1 are not as conveniant and this will work:
    union word_byte {
        unsigned int word;
        unsigned char byte[2];
    };
        y.byte[0] = gTH1;
        y.byte[1] = gTL1;
        x = TIMER_1_RELOAD + y.word;
    
    which will compile a little tighter than
    x = ((unsigned int)gTH1 << 8 ) + gTL1 + TIMER_1_RELOAD;
    

    :-)
    Frodak

Reply
  • The SFR keyword can't be used within a struct or union.

    Nor can you do something like

    timer2_val = *(unsigned int*)&TH2;
    
    because TH2 must be a direct byte transfer otherwise the data will come out of idata, and it won't compile.

    On the cypress FX2 TH2 and TL2 are at 0xcd and 0xcc, which can be declared as:
    sfr16 T2 = 0xcc;
    
    This is straight out of the cx51 manual under sfr16, page 100.

    However, Timer 0 and Timer 1 are not as conveniant and this will work:
    union word_byte {
        unsigned int word;
        unsigned char byte[2];
    };
        y.byte[0] = gTH1;
        y.byte[1] = gTL1;
        x = TIMER_1_RELOAD + y.word;
    
    which will compile a little tighter than
    x = ((unsigned int)gTH1 << 8 ) + gTL1 + TIMER_1_RELOAD;
    

    :-)
    Frodak

Children
No data