Switch on a LED: why doesn’t this code work

cfreescaleledmicrocontrollerswitches

I am using DEMO9S08AW60E and using Codewarrior(Version: 5.9) with Processor Expert.

PTFDD = 0x00;  // to activate/intialize the port F and D   
PTDDD = 0x00;

if ((PTDD_PTDD2 |= 0x02))
{
    PTFD_PTFD0 |=0x01;
} 
else
{
    PTFD_PTFD0 &=0x01;
}

Link for DEMO9S08AW60E.

According to the schematic of the board, Switch 4 is connected to PortD/Pin2. And the set of LED are connected to the PortF/Pin(0-7). The idea is to switch the LED on when the button is pressed: I have used a pull up resistor with the help of PROCESSOR EXPERT. Logically, I feel my code is fine but there doesn't seem to be any effect.

I would like to know where I am going wrong and hope you will able to help.

Best Answer

if ((PTDD_PTDD2 |= 0x02))

This is equal to:

PTDD_PTDD2 |= 0x02;
if (1)

This does probably not what you want either:

PTFD_PTFD0 &=0x01;

The resulting value of PTFD_PTFD0 is 0x00 or 0x01, depending on the value of least sigificant Bit.

It looks like you wanted:

PTFD_PTFD0 &= ~ 0x01;

This sets the LSB to zero.