Why doesn't this code work? All that I want is portb to toggle when I press a button.
main
trisb=0
trisa=0xff
while true
if ra0<>0 then
portb = not portb
end if
wend .end
I'm not sure what that is; is it pseudocode?
Anyway, you need to trigger on the CHANGE from RA0 == 0 to RA0 == 1. As written, as long as RA0 == 1 then your PORTB will toggle every time through the loop.
Here's an example in C:
int main(void)
{
unsigned char bButtonValue = 0;
TRISA = 0xFF;
TRISB = 0x00;
while (1)
{
if (RA0 != bButtonValue)
{
bButtonValue = RA0;
if (bButtonValue == 1)
{
PORTB= ~PORTB;
}
}
}
}
Keep in mind that for a real app, you would want to debounce the switch input (make sure it's stable for a few samples before triggering the change event.