(8051) Check if a single bit is set
Asked Answered
F

2

2

I'm writing a program for a 8051 microcontroller. In the first part of the program I do some calculations and based on the result, I either light the LED or not (using CLR P1.7, where P1.7 is the port the LED is attached to in the microcontroller).

In the next part of the program I want to retrieve the bit, perhaps store it somewhere, and use it in a if-jump instruction like JB. How can I do that?

Also, I've seen the instruction MOV C, P1.7 in a code sample. What's the C here?

Favata answered 9/6, 2010 at 15:53 Comment(0)
W
4

The C here is the 8051's carry flag - called that because it can be used to hold the "carry" when doing addition operations on multiple bytes.

It can also be used as a single-bit register - so (as here) where you want to move bits around, you can load it with a port value (such as P1.7) then store it somewhere else, for example:

MOV C, P1.7
MOV <bit-address>, C

Then later you can branch on it using:

JB <bit-address>, <label>
Waynewayolle answered 9/6, 2010 at 15:58 Comment(1)
Another trick used by the C51 compiler is using PUSH PSW to push the C flag (along with the rest of the status word), for example to store the previous interrupt state when using #pragma disable.Comedian
R
1

Some of the special function registers are also bit addressable. I believe its all the ones ending in 0 or 8. Don't have a reference in front of me but you can do something like setb r0.1. That way if you need the carry for something you dont have to worry about pushing it and using up space on your stack.

Reshape answered 2/7, 2010 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.