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;
Really, the *nicest* solution I think would be the ability to compile .asm functions inline.... if that was possible. Then I could do whatever I want! hehe I do quite like the DJNZ instruction, and I also like that while(--x){ generates a djnz instruction in the compiled code. I found the RRC instruction useful in two situations : 1) An interrupt that triggers on an external clock signal and shifts in a bit of data. 2) A tight loop that shifts in a byte of data from a dataflash. the reason why I like RRC is that the code that Drew wrote can be expressed as such (Note that this loop is not guaranteed to terminate, in the same way as Drew's)
do { CY = PortPin RRC // Shifts the portpin into ACC, ACC flows off into carry } while (CY);
RxCurrentBit = _DPRX0; if (!RxLastBit){ //Code which evaluates incoming byte //.. } RxLastBit = CurrentData & 0x01; CurrentData = (CurrentData >> 1); if (RxCurrentBit){ CurrentData = (CurrentData | 0x80); }
RxLastBit = CurrentData & 0x01; 0044 7800 R MOV R0,#LOW CurrentData 0046 E2 MOVX A,@R0 0047 FF MOV R7,A 0048 13 RRC A 0049 9200 R MOV RxLastBit,C CurrentData = (CurrentData >> 1); 004B EF MOV A,R7 004C C3 CLR C 004D 13 RRC A 004E F2 MOVX @R0,A if (RxCurrentBit){ 004F 300004 R JNB RxCurrentBit,?C0028 CurrentData = (CurrentData | 0x80); 0052 E2 MOVX A,@R0 0053 4480 ORL A,#080H 0055 F2 MOVX @R0,A
0047 FF MOV R7,A 0048 13 RRC A 0049 9200 R MOV RxLastBit,C
CY = RxCurrentBit; ACC = CurrentData; CODE RRC; (*Rotate accumulator 1 bit right through carry flag (CY -> ACC(7..0) -> CY)*) CurrentData = ACC; RxLastBit = CY;
would be the ability to compile .asm functions inline Whaddayamean "would be"? This ability exists. Guess what: it's even documented. And supported by the IDE. The fact that you don't appear to know that strongly suggests you complain publically before bothering to actually break open the documentation. That's a bad habit. Inline assembly does have some drawbacks, but those are largely inevitable, given the way Keil tools are designed.
hi, the reason why I like RRC is that the code that Drew wrote can be expressed as such
CY = PortPin;
would be the ability to compile .asm functions inline Whaddayamean "would be"? This ability exists. In a way so clumsy that I agree "this function does not exist" Erik
If I did use CY = Portpin, I would check the compiled assembly for side-affects. As for the inline assembly compiling thing, I will look into that more. Thanks for your help everyone ! -Tom