Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
Read, write and manipulate bits rather than bytes
I want to do somethinh really simple: read and write an old Eprom, 2708. The biggest challenge, I thought, wee the 4 different voltage supplies: 3 for normal operation (that's the way it worked in the past) and a fairly high (+/- 27V) programming voltage.
The LPC1768 generates the clock pulse, which is fed to a 4040-counter. These are the address lines for the Eprom. The biggest challenge now seems to be the data lines:
I need to reserve 8 pins on the LPC1768. For both input and output (DigitalInOut).. Easiest would be to use an 8-bit char. But how can I cycle along all 8 pins in a smart loop? And how can I manipulate individual bits?
Ideally, I would set up a char called Byte... Make some sort of a loop that goes along all 8 bits... And fills the char like Byte[n] = Bit_n, where Bit_n corresponds with a certain DigitalInOut pin...
For manipulating the bits, only using the shift >> and << seem to can be used?
Anyone any suggestions?
1 Answer
8 years, 10 months ago.
You have all of the normal c bit manipulation commands if you need them, ~ (bitwise invert), & (bitwise and), | (bitwise or) and ^ (bitwise xor), you can do just about anything with them.
to clear the 3rd bit:
x &= ~(1<<3) // x = x anded with the inverse of (0x01 shifted left 3 times)
To set the 5th and 7th bits high
// x = x ored with (0x01 shifted left 5 times) ored with (0x01 shifted left 7 times) x |= (1 << 5) | (1 << 7);
to check the 2nd bit is set
if (x & (1<<2)) { ... }
The above is exploiting the c feature that 0 is false, anything else is true. If you find that hard to follow then you can always do
if ( (x & (1<<2)) == (1<<2)) // if x anded with 0x02 == 0x02 { ... }
You my want to take a look at the BusIn, BusOut and BusInOut functions in the handbook, here https://developer.mbed.org/handbook/Homepage .
posted by David Fletcher 31 Jan 2016Wow, interesting, it was not in both (print) books I have about the the LPC1768. I also saw a PortInOut, which seems to do more or less the same; exactly what I needed. Individual pins can only be manipulated through <<, >>, AND, etc.?
As this did not seem possible:
myleds[1] = 0 to set the 2nd bit to 0.
BusOut myleds(p15, p16, p17, p18, p19, p20);
int main() { while(1) { for(int i=0; i<64; i++) { myleds = i; wait(0.35); } } }
posted by George DeB 31 Jan 2016Previous comment deleted!
You may have your leds wired incorrectly. I just tried it again and it works ok for me. Just try the example.
posted by David Fletcher 31 Jan 2016That code works fine indeed, my remark is about something like this, addressing an individual bit:
myleds[1] = 0; to set the 2nd bit to 0
That seems, unfortunately, not possible. Like a char could be an array of bits, like a string is an array of chars and where you can address an individual character in a string; that's what I meant. It would be handy to address a bit in a char through:
mychar[4] = 1;
posted by George DeB 31 Jan 2016So what is wrong with myleds = mychar[4];
mychar[n] can have its value manipulated by a logical operator.
posted by David Fletcher 31 Jan 2016