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

Use of RRC (Rotate right through carry) in Keil

The 8051 family of microcontrollers has a very useful assembly instruction RRC. This instruction rotates carry into the MSB of the given register, shifts that register right by 1, and carry becomes equal to what used to be the LSB of the register.

  bit NewCarry;
  unsigned char Data;

  NewCarry = Data & 0x01;
  Data = (Data >> 1);
  if (Carry){
    Data = (Data | 0x80);
  }
  Carry = NewCarry;
Basically it does the above, but all in one instruction. I was wondering what the easiest way to include this in my code was. What I want to do is to read a microcontroller pin into carry, shift it into a data byte, and then use the bit that shifts off as an exit condition. Is there an easy way to include a single assembly instruction in Keil? Perhaps a Intrinsic function?

Thanks

-Tom

Parents
  • hi,

    the reason why I like RRC is that the code that Drew wrote can be expressed as such

    do {
      CY = PortPin
      RRC // Shifts the portpin into ACC, ACC flows off into carry
    } while (CY);
    No, it cannot be used such way. You should not access registers and condition flags (including carry) directly from C. Under C, they all are subject of changes by compiler. When you write
      CY = PortPin;
    
    then it does not mean that at next line the carry flag still keeps its value. Compiler may use registers and flags for own purposes at each line of C program and even between lines.

    In short, in C you have got variables and lost registers. If you need to manipulate with registers/flags then you should switch to ASM.

    Regards,
    Oleg

Reply
  • hi,

    the reason why I like RRC is that the code that Drew wrote can be expressed as such

    do {
      CY = PortPin
      RRC // Shifts the portpin into ACC, ACC flows off into carry
    } while (CY);
    No, it cannot be used such way. You should not access registers and condition flags (including carry) directly from C. Under C, they all are subject of changes by compiler. When you write
      CY = PortPin;
    
    then it does not mean that at next line the carry flag still keeps its value. Compiler may use registers and flags for own purposes at each line of C program and even between lines.

    In short, in C you have got variables and lost registers. If you need to manipulate with registers/flags then you should switch to ASM.

    Regards,
    Oleg

Children