Duda al programar pic16f887 en mikrobasic

Hola compañeros del foro.
tengo una aplicacion domestica con un pic 16f887 pero me surgen algunos contratiempos...
quiero activar y desactivar 4 puertos (rd0-rd3) con 4 pulsadores (rb0-rb3) respectivamente.
Con la aplicacion que tengo hecha solo puedo encender los puertos pero no apagarlos..Me dice alguien donde esta el fallo??
La configuracion del pic es:
HS_OSC
WDT_OFF
LVP_OFF
Código:
dim oldstate, oldstate1, oldstate2, oldstate3 as BYTE
main:

ANSEL = 0
ANSELH = 0
TRISB = 0xFF
TRISD = 0x00
PORTD = 0

while true

      if (Button(PORTB, 0, 10, 1)) then
         oldstate=1
      end if
      if (oldstate and Button(PORTB, 0, 10, 0)) then
         PORTD.0 = NOT PORTD.0
         oldstate = 0
      end if

      if (Button(PORTB, 1, 1, 1)) then
         oldstate1=1
      end if
      if (oldstate1 and Button(PORTB, 1, 1, 0)) then
         PORTD.1 = NOT (PORTD.1)
         oldstate1 = 0
      end if

      if (Button(PORTB, 2, 1, 1)) then
         oldstate2=1
      end if
      if (oldstate2 and Button(PORTB, 2, 1, 0)) then
         PORTD.2 = NOT PORTD.2
         oldstate2 = 0
      end if

      if (Button(PORTB, 3, 1, 1)) then
         oldstate3=1
      end if
      if (oldstate3 and Button(PORTB, 3, 1, 0)) then
         PORTD.3 = NOT (PORTD.3)
         oldstate3 = 0
      end if

wend
end.
Muchas gracias a todos
 
Atrás
Arriba