Not really I'm controlling an R2R array and I need bit addressability. But never the less the question was 'is the code correct', not as to whether it is overkill or not.
@dhunt A define won't work as I need to be able to change ports at will while running.
I have played with class in MSE2003 in a console application, which was educational on the line of core dependancies and have gotten alot of different outputs other than what I wanted. And as for class overkill, possibly, but what would I learn in the process? It is as much an excercise in bit shift mathmatics as anything else. The goal was to iterate the port bits of the last port write and alter the bit pattern based on the comparison with the port value and a measured value.
Say the measured value equated to B110101110, conversion begins the port is set to B10000000.
Port is compared to value, in this case value is greater than Port so compare is 1, Port isn't great enough yet so I add B01000000 to Port making B11000000
Comparison is still 1 so now B00100000 is added, Port now B11100000, Comparison is now 0 as Port exceeded value, so I have to strip the right 6 bits and replace with 0's and then add B00011111 to port.
I now srtip 5 bits and do a similar process now only with 5 bits. This continues up to and including the LSB. The end goal is a short hand method to achieve successive aproximation without going through each bit count consecutively until value match. The end goal if the class will be to do the bit stripping. In my dabblings I discovered I was shifting the wrong direction first. Oddly in the process of explaining it here what I want to do is a whole lot simpler than I had been making it. A Right shift will dump the lesser bits up to and including the bit number coinciding to number of shifts, and the return shift will leave 0's in those locations.
I think the macro is fairly flexible. For example you can change its use at run time like this:
#define PortBit(port,bit) (((port) >> (bit)) & 1)
volatile uint8_t *myport = &PORTB;
int count = 0;
loop()
{
uint8_t mybit;
mybit = PortBit(*myport, 2);
if (mybit == 0) {
// some action when the bit is 0
} else {
// some other action when the bit is 1
}
count++;
if (count == 10) {
// Switch to PORTC
myport = &PORTC;
} else if (count == 20) {
// Switch back to PORTB
myport = &PORTB;
count = 0;
}
}
Could you describe the flexibility you need for your project? Maybe some pseudo code to explain it?