Originally Posted by markun
Well, the actual code is quite easy (let me know if you spot any mistake I made)
Code:
bool backlight_on = true;
//Set backlight pin to output and enable
int oldval = PCON0;
PCON0 = ((oldval & ~(3 << 4)) | (1 << 4));
PDAT0 |= (1 << 2);
//Set PLAY to input
oldval = PCON1;
PCON1 = ((oldval & ~(0xf << 16)) | (0 << 16));
//toggle backlight on PLAY
while(true)
{
// Wait for play to be pressed
while(!(PDAT1 & (1 << 4)))
{
}
if (backlight_on)
PDAT0 &= ~(1 << 2);
else
PDAT0 |= (1 << 2);
backlight_on = !backlight_on;
// Wait for play to be released
while(PDAT1 & (1 << 4))
{
}
}
but I'll post the binary here when it works
Hello markun,
Thanks for letting us look at a little bit of source. As for input, I don't know whether or not your mapping is correct, but based on what you've done, I believe you could replace:
Code:
if (backlight_on)
PDAT0 &= ~(1 << 2);
else
PDAT0 |= (1 << 2);
with an exclusive-OR to toggle the bit on and off, ie:
PDAT0 ^= (1 << 2);
You could also replace all complements followed by AND, with an exclusive-OR, ie:
replace this (& ~) with this (^).
That should produce equivalent code, a bit more succintly. Also, I don't think you strictly need to have a seperate 'backlight_on' variable, unless you'd prefer one, of course.
- I'm Speechless.