As we've studied the 4 bit counter, we've learned a lot about PIC processors. However, our programme only counts on its own, and it would be nice if we could somehow interact with it. But before we add that facility to our counter, let's start with a much simpler program that will demonstate the basics.

Setting up ports for input

We've already looked at how to configure a port as an output, using the TRIS registers. To use a port as an input, we just need to modify our initialisation routine:

Init   clrf     PORTA         ;all of porta low
       clrf     PORTB         ;all of portb low
       bsf      STATUS, RP0   ;change to bank1
       movlw    b'00000011'   ;all outputs except RA0, 1
       movwf    TRISA         ;
       clrf     TRISB         ;all of portb outputs
       bcf      STATUS, RP0   ;back to bank0

We've set up bits 0 and 1 of PORTA to be inputs by changing the clrf TRISA to the movlw/movwf combination, and note the binary notation - this makes it much easier to see which bits are inputs and outputs; a "1" looks awfully like "I" for Input. Likewise, "0" looks like "O" for Output. It's almost like someone actually thought about it :-)

Diagram showing switch connections (3K)Just as an aside, you might have noticed the clrf statements at the start of the subroutine. This is good practice, as the memory of the PIC will contain random numbers at power-up. Also, Microchip recommend clearing PORTA and PORTB before setting up the TRIS registers to avoid glitches.

Having set up RA0 and RA1 as inputs, accepting input signals is just a case of reading PORTA, and making some sort of sense of the results. We'll write a simple program to test our two inputs. We shall connect two switches to the ports, labelled "ON" and "OFF". The switches are arranged to connect the ports to ground when pressed - when the switch is not pressed, a pull-up resistor makes the port high. This "active-low" arrangement very is common in practice.

Program specification

As with our 4-bit counter, let's start with a program specification:

Pressing the "ON" switch will light an LED. Pressing the "OFF" switch will turn it off.

It's very simple, as you can see from the flow diagram:

Flow diagram (10K)

Clearly, this is going to be a straightforward programme to write, which leaves us free to think about the simple concepts here.

Checking for input

The easiest way to check for a switch is with a btfss statement:

       btfss    PORTA, 0     ;Is the switch pressed?
       call     Switch       ;  Yes, so do something...
                             ;  No, move on to the next thing.

When not pressed, RA0 is high. This means the next line is normally skipped. But when the switch does get pressed, the next line will be executed, which here means that the subroutine will be called. When planning the overall flow chart, we must ensure that this line is executed frequently so that we don't miss the key-presses.

This code will check for the switch connected to RA0, and turn on the LED connected to RB0 if the switch is pressed.

       btfss    PORTA, 0     ;Is "ON" switch pressed?
       bsf      PORTB, 0     ;  Yes, so turn on LED
                             ;  No

Next, we need to check for the "OFF" switch, connected to RA1:

       btfss    PORTA, 1     ;Is "OFF" switch pressed?
       bcf      PORTB, 0     ;  Yes, so turn off LED
                             ;  No

The LED is turned off with a bcf statement.

To complete the programme, we just need to arrange for these two lines to be executed frequently, as mentioned above.

Loop   btfss    PORTA, 0     ;Is "ON" switch pressed?
       bsf      PORTB, 0     ;  Yes, so turn on LED
                             ;  No
       btfss    PORTA, 1     ;Is "OFF" switch pressed?
       bcf      PORTB, 0     ;  Yes, so turn off LED
                             ;  No
       goto     Loop         ;

And this is all we need. The PIC will sit in a continuous loop, checking the two switches every few microseconds. You can download input.ASM here, and here is the circuit diagram:

Circuit diagram of
      the input test program (7kB)

Summary and conclusions

This simple program represents a significant step forward - we can now interact with our programmes. We are checking the switches frequently - every few microseconds here - and changing the state of the LED as needed. This method of continually checking the state of inputs is called polling, and is commonly used for simple applications - a more complex program would use the interrupt capability of the PIC to check for keypresses, freeing up the PIC to be doing other things in the meantime, but that's a subject for the future. For now, we'll develop the idea of polling further on the next page.