46
An example Program switch reading, the most basic computer input operation

An example Program

  • Upload
    naomi

  • View
    20

  • Download
    0

Embed Size (px)

DESCRIPTION

An example Program. switch reading , the most basic computer input operation. Keyboards. A computer keyboard typically uses a microprocessor to scan the switches. Similarly in remote control units for TVs etc. row select lines. switches. mpu. column select lines. scanning. - PowerPoint PPT Presentation

Citation preview

Page 1: An example Program

An example Program

switch reading, the most basic computer input

operation

Page 2: An example Program

Keyboards

A computer keyboard typically uses a microprocessor to scan the switches. Similarly in remote control units for TVs etc.

mpu

column select lines

row selectlines switches

Page 3: An example Program

scanning

one column at a time has a voltage applied. If a switch on the column is pressed the corresponding row has the voltage

mpu

column select lines

row selectlines switches

Page 4: An example Program

ports

This requires some of the lines on the MPU to be configured as input or output lines. These collections of wires are termed ‘ports’.

mpu

column select lines

row selectlines switches

inport

outport

Page 5: An example Program

Port scanning

Typically each port is several bits wide, we use a shifting bit to scan

outport:=1 --- set col 1 on

repeat

if inport>0 then processs switch

outport:=outport+outport; --- shift column bit up

until outport>32

Page 6: An example Program

The problem

Mechanical switches play an important and extensive role in practically every computer, microprocessor and microcontroller application. Mechanical switches are inexpensive, simple and reliable.

In addition, switches can be very noisy. The apparent noise is caused by the closing and opening action that seldom results in a clean electrical transition. The connection makes and breaks several, perhaps even hundreds, of times before the final switch state settles.

Page 7: An example Program

Need to de-bounce switches

The consequences of uncorrected switch bounce can range from being just annoying to catastrophic. For example, imagine advancing the TV channel, but instead of getting the next channel, the selection skips one or two. This is a situation a designer should strive to avoid.

Page 8: An example Program

use of a shift register

Whenever the switch is open, the high input on D propagates to Qn after n clock cycles. If it closes even momentarily, the output goes low and stays low until there has been a period of n cycles without a switch closure

Page 9: An example Program

software debounce

Consider a simple push button application. In this case, some event should occur when a button is pushed. Ideally the invoked event will occur immediately, and only once for each button push. The system should be also ready to respond to a repeat button push as soon as possible after the button is released. This presents an apparent dilemma.

How is the difference between switch bounce and repeated button pushes determined?

Page 10: An example Program

brute force approach Assume that the bounce period is less than 10

milliseconds. If the switch input level is stable for longer than 10

milliseconds, then bouncing has stopped and the input level represents the pushed or released switch state.

The Brute Force method only cares about a button-push event because this is what invokes the action. It recognizes the switch release state as the stable state and everything else is considered unstable.

When the switch becomes unstable, the action is invoked permitting nothing to happen until the switch returns to the released stable state.

Page 11: An example Program

Program Design

To go any further we need to know how to:

1. read input bits

2. set the LEDs (Light Emiting Diodes).

3. Control the timer

Page 12: An example Program

The Micro controller development kit we are using

this is the microcontroller

Page 13: An example Program

This is the chip we are using PIC12F675

power ground

pins 2 ..7 provide 6 general purpose programmable input output lines

Page 14: An example Program

The switch is connected to pin 4When you push it the input goes towards ground, in digital termsit reads as 0

Page 15: An example Program

LED driving

The board has 8 LEDs It has only 4 input output pins available

to drive them This would imply that it could drive only 4

LEDs, How does it manage it ?

Page 16: An example Program

Use of a diode matrixEach LED is switched on by activating one pin high and another pin lowIf pin 2 = 5v and pin 3 = 0 v, then LED D1 will lightif pin 2 = 0v and pin 3 = 5v then LED D0 will light etc

technique used for controlling indicator lights on stereos, dashboards etc

Page 17: An example Program

multiple LEDs on at once?

How could we get LED D0 and D1 on at the same time?

Electrically this is impossible. We can fool the human eye though, by

rapidly flashing the two LEDs thousands of times a second.

It then looks as though both are on all the time.

Page 18: An example Program

Tristate Logic

Normal logic uses the convention that high voltage is 1 and low voltage is 0.

You will all recall that in addition to high an low outputs, a pin can be TRISTATE

Tristate is needed in order to drive the LED matrix.

Page 19: An example Program

If we are trying to light LED D0, then clearly only pins 2 and 3 must be active.

we want 2 low and 3 high

if pin 5 was active we would get another light on

if pin 5 was low, then d2 would be on as well

if pin 5 was high then d5 would be on as well

Thus pins 5 and 6 must be set to be tristate

Page 20: An example Program

Timers

A timer is basically a counter operated by the processor clock. It can count down a fixed number of clock cycles.

Almost all micro controllers have built in timers.

Page 21: An example Program

PIC timers

3 timers

1. Watchdog timer used to wake from sleep

2. 8 bit TIMER0

3. 16 bit Timer 1

Page 22: An example Program

timer0

This is in register 1, it counts instruction cycles

Its rate of counting is controlled by the bottom 3 bits of the option register, which is register 81

Option value count raten 1/2n+1

6 1/128the maximum option value is 7

Page 23: An example Program

prescaler logic

Page 24: An example Program

10 ms

We want to wait 10 ms. Since the instruction cycle of the chip is

200ns, this amounts to 50,000 instructions.

50000= c350 hex If we set the prescaler to divide by 256,

then when the timer reaches c4 hex we have waited just over 10ms

Page 25: An example Program

InitTimer

Set the option register Set the timer register. Problem the option register is register

81hex, we can only address registers 0 to 7Fhex with normal instructions

Page 26: An example Program

Register banks

Processor has two banks of registers, bank0 and bank1.

Bank0 for normal use, bank1 contains special control registers.

Which one you access is controlled by the status register

Page 27: An example Program

status reg ( reg 3)

C =carry flag z = zero flag PD,TO give cause of timeout interrupts RP0 selects the register bankto select bank 1 use instruction

BSF 3,5 ; set bit 5 of reg 3

to select bank 0 use instruction

BCF 3,5 ; clear bit 5 of reg 3

Page 28: An example Program

intcon reg (reg 11)

this bit is set when the timer0wraps round past 255

if this bit is set an interrupt will be generated on wrap round of timer 0

keep it 0 for now

Page 29: An example Program

using the t01f flag

This flag is set when the timer wraps round.

we want to count C4=196 cycles if we initialise the timer to 256-196=60 then it will wrap round at the right time

Page 30: An example Program

timer 1

This is a 16 bit timer that counts one step each instruction. Its low byte is in reg 0Eh and the high byte in register 0Fh

It is controlled by register 10h ( T1CON) To switch it on set bit 0 of T1CON When it wraps round it sets bit 0 of

register 0Ch This is much simpler to use for large

counts

Page 31: An example Program

timer init routine

inittim ; initialise timer

clrf TMR1L ; TMR1L defined as 0e

movlw 03ch ;

movwf TMR1H ; set the timer to 3c00

bsf T1CON,0 ; switch it on

bcf PIR1,0 ; clear the finish flag

return

Page 32: An example Program

Sensing the switch

Register 5 , also called GPIO is mapped to the i/o pins on the chip

We can test the switch by monitoring GPIO bit 3, to which pin 4 ( switch input) is connected+5

Page 33: An example Program

Control of I/O

GPIO is an 6-bit wide, bi-directional port. The corresponding data direction register is

TRISIO. Setting a TRISIO bit (= 1) will make the corresponding GPIO pin an input (i.e., put the corresponding output driver in a tristate mode).

Clearing a TRISIO bit (= 0) will make the corresponding GPIO pin an output (i.e., put the contents of the output latch on the selected pin).

The exception is GP3, which is input only and its TRISIO bit will always read as ‘1’.

Page 34: An example Program

GPIO ( General Purpose I/O)

Page 35: An example Program

reading or writing GPIO

Reading the GPIO register reads the status of the pins, whereas writing to it will write to the port latch.

All write operations are read-modify-write operations.

Therefore, a write to a port implies that the port pins are read,this value is modified, and then written to the port data latch.

Page 36: An example Program

TRISIO( Tristate I/O)

Page 37: An example Program

How to Initialise GPIO

#define STATUS 3#define RP0 5bcf STATUS,RPO ;Bank 0clrf GPIO ;Init GPIOmovlw 07h ;Set GP<2:0> tomovwf CMCON ;digital IObsf STATUS,RP0 ;Bank 1clrf ANSEL ;Disable analog inputmovlw 0Ch ;Set GP<3:2> as inputsmovwf TRISIO ;and set GP<5:4,1:0> ;as outputs

Page 38: An example Program

To test if the switch is on or off do the followingbtfss 5,3 ; test SWITCH inputgoto SwitchDebounce ; SWITCH was low - reset timer

If switch was low then bit 3 will be 0 and btfss will not skip the next instructionthus you will do the gotoIf switch was high, then the bit 3 will be 1and then the goto will be skipped.

GPIO register = 5

select bit 3

Page 39: An example Program

list p=12F675 ; list directive to ;define processor

#include <p12f675.inc> ; processor specific ; variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF

'__CONFIG' directive is used to embed configuration word within .asm file. The labels following the directive are located in the respective .inc file. See data sheet for additional information on configuration word settings.

Header section of program.

Page 40: An example Program

#define Bank0 0x00#define Bank1 0x80#define SWITCH GPIO,3#define D0_1Tris B'11001111'#define D0On B'00010000'#define D0Off B'00000000‘#define Flags 0x20#define LEDOn Flags,0We define Flags as register 20 it will be used to hold working values for the program

Define the constants to be used in this program

Page 41: An example Program

Define entry point

ORG 0x000 ; processor reset vectorgoto Init ; go to beginning of prog

;Interrupt Vector ORG 0x004return ; interrupt trap

; - returns without re-enabling

The ORG directive says where the instruction start in ROM.Address 0 is where the hardware starts running

Address 4 is where the hardware goes on an interrupt

Page 42: An example Program

Initialise i/oInit

call 0x3FF ; retrieve factory calibration value ; comment instruction if using simulator

BANKSEL Bank1movwf OSCCAL ; update register with factory cal value movlw D0_1Tris ; set direction so LEDs D0, D1 are outputsmovwf TRISIO ; all others are inputs (high-z)clrf ANSEL ; configure A/D I/O as digitalbanksel Bank0 ; change back to PORT memory bankmovlw CM2 | CM1 | CM0 ; comparator digital I/Omovwf CMCONclrf Flags ; set initial LED state as offcall ToggleLED ; light initial LED

goto Main

Page 43: An example Program

Main program

Main btfsc SWITCH ; wait in loop until SWITCH closure sensed goto Main ; SWITCH closure grounds input pin call ToggleLED ; SWITCH closure sensed - toggle LED call SwitchDebounce ; wait for switch to release and settle goto Main

repeat while not switch down do nothing; toggle LED wait for debounceforever;

Assembler

Page 44: An example Program

Toggle LED subroutineToggleLED

btfss LEDOn ; test flag of present LED conditiongoto TurnLedOn ; the LED is presently off - go turn it on

TurnLedOffbcf LEDOn ; clear flag to indicate LED is offmovlw D0Off ; data for all LED outputs lowmovwf GPIO ; send data to GPIO portreturn ; return to calling routine

TurnLedOnbsf LEDOn ; set flag to indicate LED is onmovlw D0On ; data to forward bias LED0

; and reverse bias LED1movwf GPIO ; send data to GPIO portreturn ; return to calling routine

Page 45: An example Program

Test state

btfss LEDOn goto TurnLedOn

goto TurnLedOffThe first instruction skips the following instruction

if the LEDOn flag is set.Thus if it is not on, the goto TurnLedOn

instruction is executed, if it is on, the mpu goes to TurnLedOff

Page 46: An example Program

Debounce routine

SwitchDebounce call inittim SD2

btfss 5,3 ; test SWITCH inputgoto SwitchDebounce ; SWITCH was low - reset timerbtfss PIR1,0 ; wrap round?goto SD2 ; not counted to zero yet, continuereturn ; full countdown

; and no bounces – exit

END ; directive 'end of program‘ ; this must terminate code