Chapter 12 Capturing Input. Di Jasio - Programming 32-bit Microcontrollers in C Button Inputs.

Slides:



Advertisements
Similar presentations
Chapter 12 Capturing Input
Advertisements

Review: Interrupts & Timers
More fun with Timer/Counters
Microcontroller Programming II MP6-1
C Examples 1.
Programmable Interval Timer
Chung-Ta King National Tsing Hua University
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
Chapter 11 It’s an Analog World Learning to use the Analog to Digital Converter.
EE 316 Computer Engineering Junior Lab Word Mastermind.
EE 316 Computer Engineering Junior Lab PS/2 Keyboard.
Programming I/O for Embedded System. Page 2 Overview Basis: A DE2 Computer Architecture Parallel I/O 7-Segment Display Basic Manipulating 7-Segment Display.
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
Chapter 8 Communication Introduction to serial communication interfaces. Examples of use of the SPI module.
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
1 Keyboard Controller Design By Tamas Kasza Digital System Design 2 (ECE 5572) Summer 2003 Presentation for.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
V 0.91 Polled IO versus Interrupt Driven IO Polled Input/Output (IO) – processor continually checks IO device to see if it is ready for data transfer –Inefficient,
ARM Timers.
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Chapter 4 TIMER OPERATION
Team 6. Code Modules Codec Bluetooth Module Rotary Encoder Menu State Machine.
Page 1 D&C EBV Seminar June 2003 Motor Demo C868 Chevillot/Jansen June 2003 N e v e r s t o p t h i n k i n g. Infineon C868 Hands On Training CAPCOM6.
FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller
Time Measurement Capture Mode of the Capture/Compare/PWM module is used for time measurement. TMR1 or TMR3 16-bit value transferred to 16-bit capture.
C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
Microprocessors 1 MCS-51 Interrupts.
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.
ECE 371 – Unit 9 Interrupts (continued). Example Set up Two Interrupt Request Inputs: –Port H[0] Set Interrupt Flag on “0” to “1” transition (rising edge)
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
1 ECE 372 – Microcontroller Design Parallel IO Ports - Inputs.
Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.
EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Teachers Name : Suman Sarker Telecommunication Technology Subject Name : Microcontroller & Embedded System Subject Code : 6871 Semester : 7th Department.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
Saxion University of Applied Sciences Advanced Microcontrollers A practical approach.
Chapter 10 Glass Bliss Using the Parallel Master Port to communicate with Alphanumeric LCD displays.
Interrupts Microprocessor and Interfacing
Microprocessor and Interfacing Example: Writing a Game Need to Check Keyboard Input.
Real Time Interrupts Section Real-Time Interrupt (RTI) Most operating systems (OS) require an interrupt every T seconds by the RTI RTI interrupts.
Handling multiple input signals Version #2 – co-operative scheduler Version #3 – pre-emptive scheduler.
Chapter 5 - Interrupts.
Programming Microcontrollers in C Lecture L7.1. C Data Types TypeSizeRange char1 byte-128 – 127 unsigned char1 byte0 – 255 Int2 bytes – unsigned.
C Examples 1. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015.
The Silicon Laboratories C8051F020
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
UNIT 7 - INTRODUCTION TO I/O INTERFACING. TWO MAJOR TYPES OF I/O INTERFACING ISOLATED I/O - DEDICATED I/O INSTRUCTIONS ARE USED TO ACCESS I/O DEVICES.
Alpha/Numeric Keypad Functions using AVR Preliminary Design Review Luke R. Morgan ECE /17/2008.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
The 8051 Microcontroller Chapter 6 INTERRUPTS. 2/29 Interrupt is the occurrence of a condition an event that causes a temporary suspension of a program.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
Microprocessors A practical approach..
Microprocessor Systems Design I
Build a microSD Bootloader using a PIC microcontroller
Microprocessor Systems Design I
Example 19 Measuring Pulse Widths Using Interrupts
BVM Engineering College Electrical Engineering Department : Microprocessor and Microcontroller Interfacing Interrupts of 8051 Prepared by:
* * * * * * * 8051 Interrupts Programming.
Interrupts, Tasks and Timers
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Presentation transcript:

Chapter 12 Capturing Input

Di Jasio - Programming 32-bit Microcontrollers in C Button Inputs

Di Jasio - Programming 32-bit Microcontrollers in C Inputs Packing int readK( void) { // returns 0..F if keys pressed, 0 = none int c = 0; if ( !_RD6) // leftmost button c |= 8; if ( !_RD7) c |= 4; if ( !_RA7) c |= 2; if ( !_RD13) // rightmost button c |= 1; return c; } // readK

Di Jasio - Programming 32-bit Microcontrollers in C De-Bouncing int getK( void) { // wait for a key pressed and debounce int i=0, r=0, j=0; int c; // 1. wait for a key pressed for at least.1sec do{ Delayms( 10); if ( (c = readKEY())) { if ( c>r) // if more than one button r = c; // take the new code i++; } else i=0; } while ( i<10); // 2. wait for key released for at least.1 sec i =0; do { Delayms( 10); if ( (c = readKEY())) { if (c>r) // if more then one button r = c; // take the new code i=0; j++; // keep counting } else i++; } while ( i<10); // 3. check if a button was pushed longer than 500ms if ( j>50) r+=0x80; // add a flag in bit 7 // 4. return code return r; } // getK

Di Jasio - Programming 32-bit Microcontrollers in C Rotary Encoders

Di Jasio - Programming 32-bit Microcontrollers in C Polling Example /* ** Rotary.c */ // configuration bit settings, Fcy=72MHz, Fpb=36MHz #pragma config POSCMOD=XT, FNOSC=PRIPLL #pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1 #pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF #include #define ENCHA _RA9 // channel A #define ENCHB _RA10 // channel B main( void) { int i = 0; char s[16]; initLCD(); // main loop while( 1) { while( ENCHA); // detect CHA falling edge Delayms( 5); // debounce i += ENCHB ? 1 : -1; while( !ENCHA); // wait for CHA rising edge Delayms( 5); // debounce // display relative counter value clrLCD(); sprintf( s, "%d", i); putsLCD( s); } // main loop } // main

Di Jasio - Programming 32-bit Microcontrollers in C Interrupt Driven Rotary Encoder State Machine Diagram Rotary Encoder State Machine Transitions Table

Di Jasio - Programming 32-bit Microcontrollers in C Interrupt Driven Example /* ** Rotary2.c */ // configuration bit settings, Fcy=72MHz, Fpb=36MHz #pragma config POSCMOD=XT, FNOSC=PRIPLL #pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1 #pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF #include #define ENCHA _RA9 // encoder channel A #define ENCHB _RA10 // encoder channel B #define TPMS (FPB/1000) // PB clock ticks per ms // state machine definitions #define R_IDLE 0 #define R_DETECT 1 volatile int RCount; char RState; void initR( void) { // init state machine RCount = 0; // init counter RState = 0; // init state machine // init Timer2 T2CON = 0x8020; // enable Timer2, Fpb/4 PR2 = 5*TPMS/4; // 5ms period mT2SetIntPriority( 1); mT2ClearIntFlag(); mT2IntEnable( 1); } // init R

Di Jasio - Programming 32-bit Microcontrollers in C Interrupt Service Routine void __ISR( _TIMER_2_VECTOR, ipl1) T2Interrupt( void) { static char d; switch ( RState) { default: case R_IDLE: // waiting for CHA rise if ( ! ENCHA) { RState = R_DETECT; if ( ! ENCHB) d = -1; } else d = 1; break; case R_DETECT: // waitin for CHA fall if ( ENCHA) { RState = R_IDLE; RCount += d; } break; } // switch mT2ClearIntFlag(); } // T2 Interrupt

Di Jasio - Programming 32-bit Microcontrollers in C Example (cont.) main( void) { int i = 0; char s[16]; initEX16(); // init and enable interrupts initLCD(); // init LCD module initR(); // init Rotary Encoder // main loop while( 1) { Delayms( 100); // place holder for a complex app. clrLCD(); sprintf( s, "RCount = %d", RCount); putsLCD( s); } // main loop } // main

Di Jasio - Programming 32-bit Microcontrollers in C Keyboards Physical Interface (5-pin DIN) Physical Interface (6-pin mini-DIN)

Di Jasio - Programming 32-bit Microcontrollers in C PS2 Communication Protocol Host Communication Waveform

Di Jasio - Programming 32-bit Microcontrollers in C Input Capture Modules Figure 15-1 (DS61143)

Di Jasio - Programming 32-bit Microcontrollers in C PS/2 Bit Timing

Di Jasio - Programming 32-bit Microcontrollers in C IC State Machine

Di Jasio - Programming 32-bit Microcontrollers in C IC ISR void __ISR( _INPUT_CAPTURE_1_VECTOR, ipl1) IC1Interrupt( void) { // input capture interrupt service routine int d; // 1. reset timer on every edge TMR2 = 0; switch( PS2State){ default: case PS2START: if ( ! PS2DAT) // verify start bit { KCount = 8; // init bit counter KParity = 0; // init parity check PR2 = TMAX; // init timer period T2CON = 0x8000; // enable TMR2, 1:1 PS2State = PS2BIT; } break; case PS2BIT: KBDBuf >>=1; // shift in data bit if ( PS2DAT) KBDBuf += 0x80; KParity ^= KBDBuf; // update parity if ( --KCount == 0) // if all bit read, move on PS2State = PS2PARITY; break; case PS2PARITY: if ( PS2DAT) // verify parity bit KParity ^= 0x80; if ( KParity & 0x80) // if parity odd, continue PS2State = PS2STOP; else PS2State = PS2START; break; case PS2STOP: if ( PS2DAT) // verify stop bit { KBDCode = KBDBuf; // save code in mail box KBDReady = 1; // set flag, code available T2CON = 0; // stop the timer } PS2State = PS2START; break; } // switch state machine // clear interrupt flag d = IC1BUF; // discard capture mIC1ClearIntFlag(); } // IC1 Interrupt

Di Jasio - Programming 32-bit Microcontrollers in C Using Stimulus Files

Di Jasio - Programming 32-bit Microcontrollers in C Test Program /* ** PS2ICTest.c ** */ // configuration bit settings, Fcy=72MHz, Fpb=36MHz #pragma config POSCMOD=XT, FNOSC=PRIPLL #pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1 #pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF #include #include "PS2IC.h" main() { int Key; initEX16(); // init and enable interrupts initKBD(); // initialization routine while ( 1) { if ( KBDReady) // wait for the flag { Key = KBDCode; // fetch the key code KBDReady = 0; // clear the flag } } // main loop } //main

Di Jasio - Programming 32-bit Microcontrollers in C Simulator Profiler