Input programming activity

Microcontroller related information

Now, let's re-examine the function of the TRIS registers. The TRISA and TRISB registers control the direction of data transfer on each pin of Port A and Port B, respectively. The easy way to remember the TRIS register settings is to match the numbers 0 and 1 to the letters O and I:

0 is like O, as in Output
1 is like I, as in Input

How input circuits work

The CHRP circuit board includes a number of input circuits, such as pushbutton switches, phototransistors (light sensors), and a potentiometer (position sensor). All of these inputs operate in a similar, though slightly different way. Examine the switch input schematic diagram, below:

Switch input circuit image

The switch (S1) and pull-up resistor (R1) form a series circuit, also known as a voltage divider. The microcontroller input is connected to the output at the mid-point of the voltage divider, and senses the electrical potential across the switch. Since any microcontroller pins configured as inputs have a high impedance, they will just sense the externally applied voltage but won't conduct any appreciable current themselves. This essentially means that the effect of the series resistor (R2) between the voltage divider and the microcontroller is negligible and can be safely ignored—R2 is installed to limit the current due to ESD (electro-static discharge), protecting the microcontroller from static charges conducted into the switch circuit by a user's fingers.

Ignoring R2, the operation of the switch input circuit can be explained by the interaction of the switch and pull-up resistor R1. You can think about how this works either by Ohm's law analysis, or by thinking about it as a voltage divider. Using Ohm's Law analysis, we know that when the switch is open, no current flows through the pull-up resistor. Since no current flows, the voltage loss across the resistor is zero, and the microcontroller input pin senses the full power supply voltage connected to the resistor—5V, or a logic 1. You could say the resistor pulled-up the switch circuit voltage to 5V.

When the switch is pressed, current flows through both the switch and resistor. Since the switch has effectively zero resistance, there is a zero volt loss across the switch. Ohm's Law shows that the current flow through the resistor causes all of the applied voltage to be dissipated across the resistor, and so the microcontroller ends up sensing the ground potential, 0V, or a logic 0.

Thinking about the circuit as a voltage divider, we know that the two elements in a series circuit divide the applied voltage in the ratio of their resistances. The voltage at the mid-point of the switch circuit will be proportional to the resistance of the switch, which has either infinite resistance (switch open), or zero resistance (switch closed). When the switch is open, its infinite resistance is infinitely higher than that of the pull-up resistor, causing the entire applied voltage to be measured across the switch, and producing a logic 1 (5V). When the switch is closed, its (ideally) zero resistance is infinitely smaller than that of the pull-up resistor, causing no voltage drop, and producing a logic 0 (0V).


;INPUT-POT.ASM 	v2.0	Last modified on August 3, 2010
;===============================================================================
;Description:	Senses potentiometer input digitally.

;Start of MPLAB and processor configuration.

	include	"p16f676.inc"		;Include processor definitions 

	__config _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT 

;End of MPLAB and processor configuration.

;Start of hardware equates.

S1		equ	3		;Switch S1 Port A bit position
P1		equ	4		;Potentiometer P1 Port A bit position

	org	3FFh			;Oscillator calibration location

oscillator				

	org	00h			;Start of program memory

		bsf	STATUS,RP0	;Select memory register page 1
		call	oscillator	;Store pre-programmed oscillator calibration
		movwf	OSCCAL		;constant in OSCCAL register
		goto	init		;Start program after Interrupt vector

	org     05h             	;Continue after interrupt vector

init		;Initialize ports A and C for digital I/O

		bcf	STATUS,RP0
		movlw	7		;Disable comparator and make
		movwf	CMCON		;RA0-2 digital I/O
		banksel	ANSEL		;Switch to register bank 1
		movlw	11010111b	;Disable Port A pull-ups, set TMR0 to
		movwf	OPTION_REG	;internal clock with prescaler 256
		clrf	ANSEL		;Set all PORTA pins to digital
		movlw	11011111b	;Set RA5 as output and all
		movwf	TRISA		;other PORTA pins as inputs
		clrf	TRISC		;Set all PORTC pins as outputs
		banksel	PORTC		;Return to PORTC register bank
		clrf	PORTA		;Turn off phototranstor LED
		clrf	PORTC		;Turn all LEDs off

checkS1		btfsc	PORTA,S1	;Check if S1 is pressed
		goto	S1Off		;If high, not pressed. Turn LEDs off
		movlw	00101010b	;If low, pressed. Turn some LEDs on
		movwf	PORTC
		goto	checkS1		;Check again

S1Off		clrf	PORTC		;Turn all LEDs off
		goto	checkS1		;Check again

	end
		

Test your knowledge

  1. Explain the purpose of the equate statements in these programs, as well as two advantages of using equates instead of statically-coded references.
  2. Explain the difference between the btfsc and btfss instructions.
  3. How many clock cycles does it take to execute a goto instruction? Why is this different from other instructions, like movlw, or bsf?
  4. When a PORT B switch is pressed, will the microcontroller input be low, or high? Why?
  5. When a light sensor connected to PORT A sees light, will its I/O pin be low, or high? Why?

Apply your skills

  1. Assemble the Input program. What do you think will happen when both buttons are pressed? Verify this with the simulator or on your CHRP circuit.
  2. Modify the Input program to be a push-on, push-off light switch. Use S2 to turn one or more lights on, and S3 to turn the same lights off.
  3. Make a copy of the program in which the three switches S2, S3, and S4 each illuminate a different light pattern.
  4. Modify the InputA program to use the potentiometer input, and using a CHRP board measure the input threshold voltages—the potential at which the input pin switches from 0 to 1, and back from 1 to 0.