c & c++ - Input

Contact
PC-compilers
last-news
PIC-microcontroller
AVR-microcontroller
Supports
weakly newsletter
c++
Counter
DS1821Thermostat Programmer
pic-asm-foundations
=> first programme
=> Time delays
=> Subroutines
=> Input
=> Further Input
free links downloads
free downloads
mikroc library
PWM software
Home



 

Input:

As we've studied the 4 bit counter, we've learned a lot about PIC processors. However, our programme only counts, and it would be nice if we could make it respond to our commands. But before we do that, we'll start with a much simpler program.

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
       return

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.


ein Bild

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 is common in practice.

 Programme 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:

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

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

       btfss    PORTA, 0       ;Is the switch pressed?
       call     Switch         ; Yes
                               ; No

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 and in this case the subroutine will be called. When planning the flow chart, we ensure that this line is executed frequently so that we don't miss the key-presses.

       btfss    PORTA, 0       ;Is "ON" switch pressed?
       bsf      PORTB, 0       ; Yes
                               ; No

This code will check for the switch connected to RA0, and turn on the LED connected to RB0 if the switch is pressed. Next, we need to check for the "OFF" switch, connected to RA1:

       btfss    PORTA, 1        ;Is "OFF" switch pressed?
       bcf      PORTB, 0       ; Yes
                               ; 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
                               ; No
       btfss    PORTA, 1       ;Is "OFF" switch pressed?
       bcf      PORTB, 0       ; Yes
                               ; 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:

Summary and conclusions:

This simple program represents a significant step forward - we can now interact with our programmes. On the next page we will develop this further.

On to the next section - Further input.

Today, there have been 19 visitors (87 hits) on this page!
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free