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

Slides:



Advertisements
Similar presentations
Interrupts How to do 2 things at the same time. Need for interrupts Computers often have to deal with asynchronous events. That is to say events that.
Advertisements

Control structures Hot to translate high level control structures to assembler.
Working with time: interrupts, counters and timers Chapter Six Dr. Gheith Abandah1.
Electronics Design Lab TUTORIAL PIC Microcontrollers Francesco Tenore 2/10/2006.
Button Input: On/off state change Living with the Lab Gerald Recktenwald Portland State University
م/محمد علي عطية حسين. Pin Diagram DescriptionPin Name Oscillator crystal input/external clock source input. OSC1/CLKIN Oscillator crystal output.OSC2/CLKOUT.
Programmable Interval Timer
Input/Output (I/O) Ports and Interfacing
Input/Output Ports and Interfacing ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Microprocessor and Microcontroller
Processor System Architecture
Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical.
68HC11 Polling and Interrupts
Debouncing Switches Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically.
Registers  Flip-flops are available in a variety of configurations. A simple one with two independent D flip-flops with clear and preset signals is illustrated.
Railway Foundation Electronic, Electrical and Processor Engineering.
Getting the O in I/O to work on a typical microcontroller Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
16.317: Microprocessor System Design I Instructor: Dr. Michael Geiger Spring 2012 Lecture 30: PIC data memory.
Railway Foundation Electronic, Electrical and Processor Engineering.
INTERRUPTS PROGRAMMING
Microcontroller Programming How to make something almost do something else Raffi Krikorian MAS November 2003.
Department of Electronic & Electrical Engineering Embedded system Aims: Introduction to: Hardware. Software Ideas for projects ? Robotics/Control/Sensors.
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
System Clocks.
Parallel Ports, Power Supply and the Clock Oscillator Material to be covered  Parallel I/O ports  Interfacing external switches and LEDs  Clock Oscillator.
Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow.
MICROPROCESSOR INPUT/OUTPUT
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
One of the most important feature of the microcontroller is a number of input/output pins used for connection with peripherals. In the case of PIC 167CX,
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
8279 KEYBOARD AND DISPLAY INTERFACING
Timer Timer is a device, which counts the input at regular interval (δT) using clock pulses at its input. The counts increment on each pulse and store.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
8051 Micro controller. Architecture of 8051 Features of 8051.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
PHY 201 (Blum)1 Microcode Source: Digital Computer Electronics (Malvino and Brown)
1 © Unitec New Zealand Interrupt Lecture 6 Date: - 20 Sept, 2011 Embedded Hardware ETEC 6416.
EEE237 Introduction to Microprocessors Week x. SFRs.
Architecture and instruction set. Microcontroller Core Features:  Operating speed: DC - 20 MHz clock input DC ns instruction cycle Up to 8K x.
Intel 8051 Another family of microcontroller is the Intel 8051 family. The basic 8051 microcontroller has four parallel input/output ports, port 0, 1,
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
8279 KEYBOARD AND DISPLAY INTERFACING
Department of Electronic & Electrical Engineering Template program. Input / Output (IO) ● Template program. ● Introduction to IO ● Setting up for IO ●
Department of Electronic & Electrical Engineering Lecture 2 ● Introduction to IO ● Using a subroutine ● Driving a 7seg display.
بسم الله الرحمن الرحيم MEMORY AND I/O.
Working with Time: Interrupts, Counters and Timers
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Department of Electronic & Electrical Engineering Lecture 3. ● Template program. ● Introduction to IO ● PORTA PORTB TRISA TRISB ● Using a subroutine ●
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
5-2-1 PIC microcontroller. Learning objectives To aid revision, areas that are examinable are identified by a vertical line in the left margin. Where.
I/O PORTS. General purpose I/O pins can be considered the simplest of peripherals. They allow the PICmicro™ to monitor and control other devices. To add.
Special Features. Device Configuration bits Revision Device Configuration bits Revision On-chip Power-on Reset (POR) Revision On-chip Power-on Reset (POR)
Microprocessors A practical approach..
Chapter 10 Interrupts. Basic Concepts in Interrupts  An interrupt is a communication process set up in a microprocessor or microcontroller in which:
Microprocessor Systems Design I
LCD AND KEYBOARD INTERFACING
68HC11 Interrupts & Resets.
Microprocessor Systems Design I
UNIT – Microcontroller.
Microprocessor Systems Design I
Microprocessor Systems Design I
Programmable Interval Timer
Interrupts, Counter and Timers
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
EECE.3170 Microprocessor Systems Design I
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
8279 – Programmable Keyboard/Display Interface
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Presentation transcript:

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. mpu column select lines row select lines switches

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 select lines switches

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 select lines switches in port out port

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

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.

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.

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

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?

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.

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

The Micro controller development kit we are using this is the microcontroller

This is the chip we are using PIC12F675 power ground pins 2..7 provide 6 general purpose programmable input output lines

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

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 ?

Use of a diode matrix Each LED is switched on by activating one pin high and another pin low If pin 2 = 5v and pin 3 = 0 v, then LED D1 will light if pin 2 = 0v and pin 3 = 5v then LED D0 will light etc technique used for controlling indicator lights on stereos, dashboards etc

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.

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.

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

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.

PIC timers 3 timers 1. Watchdog timer used to wake from sleep 2. 8 bit TIMER bit Timer 1

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 rate n 1/2 n+1 6 1/128 the maximum option value is 7

prescaler logic

10 ms We want to wait 10 ms. Since the instruction cycle of the chip is 200ns, this amounts to 50,000 instructions = c350 hex If we set the prescaler to divide by 256, then when the timer reaches c4 hex we have waited just over 10ms

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

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

status reg ( reg 3) C =carry flag z = zero flag PD,TO give cause of timeout interrupts RP0 selects the register bank to 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

intcon reg (reg 11) this bit is set when the timer0 wraps round past 255 if this bit is set an interrupt will be generated on wrap round of timer 0 keep it 0 for now

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 =60 then it will wrap round at the right time

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

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

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

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.

GPIO ( General Purpose I/O)

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.

TRISIO( Tristate I/O)

How to Initialise GPIO #define STATUS 3 #define RP0 5 bcf STATUS,RPO ;Bank 0 clrf GPIO ;Init GPIO movlw 07h ;Set GP to movwf CMCON ;digital IO bsf STATUS,RP0 ;Bank 1 clrf ANSEL ;Disable analog input movlw 0Ch ;Set GP as inputs movwf TRISIO ;and set GP ;as outputs

To test if the switch is on or off do the following btfss5,3 ; test SWITCH input gotoSwitchDebounce ; SWITCH was low - reset timer If switch was low then bit 3 will be 0 and btfss will not skip the next instruction thus you will do the goto If switch was high, then the bit 3 will be 1 and then the goto will be skipped. GPIO register = 5 select bit 3

list p=12F675 ; list directive to ;define processor #include ; 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.

#define Bank00x00 #define Bank10x80 #define SWITCHGPIO,3 #define D0_1TrisB' ' #define D0OnB' ' #define D0OffB' #define Flags0x20 #define LEDOnFlags,0 We 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

Define entry point ORG 0x000 ; processor reset vector goto Init ; go to beginning of prog ;Interrupt Vector ORG0x004 return ; 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

Initialise i/o Init call 0x3FF ; retrieve factory calibration value ; comment instruction if using simulator BANKSEL Bank1 movwf OSCCAL ; update register with factory cal value movlw D0_1Tris ; set direction so LEDs D0, D1 are outputs movwf TRISIO ; all others are inputs (high-z) clrfANSEL ; configure A/D I/O as digital banksel Bank0 ; change back to PORT memory bank movlw CM2 | CM1 | CM0 ; comparator digital I/O movwf CMCON clrf Flags ; set initial LED state as off callToggleLED ; light initial LED goto Main

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 gotoMain repeat while not switch down do nothing; toggle LED wait for debounce forever; Assembler

Toggle LED subroutine ToggleLED btfssLEDOn; test flag of present LED condition gotoTurnLedOn; the LED is presently off - go turn it on TurnLedOff bcfLEDOn; clear flag to indicate LED is off movlwD0Off; data for all LED outputs low movwfGPIO; send data to GPIO port return; return to calling routine TurnLedOn bsfLEDOn; set flag to indicate LED is on movlwD0On; data to forward bias LED0 ; and reverse bias LED1 movwfGPIO; send data to GPIO port return; return to calling routine

Test state btfssLEDOn gotoTurnLedOn goto TurnLedOff The 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

Debounce routine SwitchDebounce call inittim SD2 btfss5,3 ; test SWITCH input gotoSwitchDebounce ; SWITCH was low - reset timer btfss PIR1,0 ; wrap round? gotoSD2 ; not counted to zero yet, continue return ; full countdown ; and no bounces – exit END ; directive 'end of program ; this must terminate code