NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » How do/can you store boolean values in registers?
March 01, 2011 by BStory |
I've been searching and thinking about this for a while. Research tells me that a boolean value is 1 byte. If that byte = 0 then it is false, if byte != 0 then it's true. Example 0b00101011,0b10000001, and 0b00000001 all compute to true or 1. 0b00000000 computes to false or 0. My question... Could you store 8 boolean values into 1 byte/storage register/whatever? This way you could shrink 8 bytes down to 1 byte or 1 byte down to 1 bit. Are there registers in the 168 for general storage that can be directly accessed? Would declaring uint8_t x assign x to it's own register in memory? I guess I'm assuming that memory is made up of 8 bit chunks. If so how could you check or change the individual bit configuration of x? and please let me know if any of this info = 0... err, I mean is false. |
---|---|
March 01, 2011 by Ralphxyz |
"Are there registers in the 168 for general storage that can be directly accessed?" Ahem, there is this wonderful resource called a specsheet. Weren't you the guy that said he had the Registers pined to the wall? "Are there registers in the 168 for general storage that can be directly accessed?" Try looking for GPIOR (page 25). 7.5.1 General Purpose I/O Registers The ATmega48A/48PA/88A/88PA/168A/168PA/328/328P contains three General Purpose I/O Registers. These registers can be used for storing any information, and they are particularly use- ful for storing global variables and Status Flags. General Purpose I/O Registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI, CBI, SBIS, and SBIC instructions. Maybe you could/would write-up a sketch of how you use the GPIO registers. Ralph |
March 01, 2011 by bretm |
You can do something like this to use the GPIORs (or any other byte) as boolean value storage. I'm typing this without access to the compiler so there may be some slight syntax problems, but it should be close.
I also use this to define my "pin" constants:
You can also do this to define sets of multiple pins, e.g. the four data pins for the LCD display:
The WriteByte function in lcd.c could be written along these lines:
It's ugly but the compiler actually generates really efficient code for it. |
March 01, 2011 by bretm |
Important change: in order to use registers, you have to add "volatile", like so:
|
March 02, 2011 by BStory |
Thanks for the replies. I think that cleared up a lot for me. Looking back at my post it does sorta look like I had a bunch of questions but it was really just 1 that I wasn't sure how to ask. I've been reading up on pointers and I'm thinking they might be the answer. bretm: Thanks for the examples. They really help a lot. It looks like pointers are a pretty big necessity for direct memory access. It makes sense I think. I never really understood the use of pointers. Also typedef looks very useful. I'll definitely be looking more into that. Learning how to write efficient code is my ultimate goal here. You were spot on with your examples. Ralph: I'm embarrassed that my question made me sound so hypocritical. I don't have all the registers on my wall. I'm just the guy with page 88, 89, and the pin-out on my wall. Though I guess that makes me guilty of knowing what a datasheet is. But thanks for mentioning it in case I didn't :D. I started to read through the AVR Memories section but it looked like it was just EEPROM stuff. Thanks for pointing me to 7.5.1. That looks right. I should have kept reading. I'll definitely be looking into it and try to post something about it when I figure it all out(best way to learn).. Specifically this part: "Purpose I/O Registers within the address range 0x00 - 0x1F are directly bit-accessible using the SBI, CBI, SBIS, and SBIC instructions". Looks like good stuff. |
March 02, 2011 by Ralphxyz |
Well like I said you are forgiven as long as you post your final working code. bretm's sample code, as usual really helps. I would love to understand the SBI, CBI SBIS and SBIC instructions. Well I sota kinda do a little but a nice simple explanation would be great. Page 532 -535 has a nice table you could pin to your wall, it has the memory reference and a page reference for all of the Registers. That is from the big specsheet it's also in the back of the smaller specsheets as well I believe. Ralph |
March 02, 2011 by bretm |
CBI, SBI, SBIS, SBIC aren't something that you would generally worry about when using C to program the AVR, but knowing about them lets you squeeze a few more bytes out of your program memory. Normally, setting or clearing a bit in a memory location involves loading the byte from that location (unless it's already sitting around in an Rxx register), modifying it, and writing it back out. This takes three instructions (LD for load, ORI or ANDI to change bits, and ST for store) taking around 10 bytes, and 5 clock cycles. If the memory location is in the first 32 bytes (0x00 to 0x1F) and you only need to set or clear a single bit, the compiler uses a shortcut instruction, either CBI to clear a bit, or SBI to set a bit. This takes only a single 2-byte instruction and 2 clock cycles. You would think that that address range would be crammed full of the most important registers, but it's actually mostly wasted space. All the DDRx, PINx, and PORTx registers are there, the general-purpose register GPIOR0 is there, some timer and EEPROM stuff, but it's mostly empty, reserved space. GPIOR1 and GPIOR2 are outside that range. (SBIS tells the microcontroller to skip the next machine instruction if a specific bit is set, and SBIC tells it to skip the next instruction if the bit is not set.) So really GPIOR0 is the only general-purpose place where you can use SBI and CBI, so if you have 8 or fewer bits that you modify or test like crazy all throughout your program, each time you use it you can save a handful of bytes of program memory if you map those bits to GPIOR0. |
Please log in to post a reply.
Did you know that talking to the microcontroller over the USB/Serial link is easy under Windows, Linux, and OS X? Learn more...
|