Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 1 Introduction to Embedded Systems A Practical Approach
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 2 Objective(s) ● Introduce Embedded Systems ● Understand small µ-Controllers ● Design and Implement SW for µ-Controllers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 3 Prerequisite(s) ● Programming experience with any high-level programming language
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 4 Note(s) ● Ask any time. ● Cell phones silent, please! ● Certificates are not for attendance.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 5 References ● The slides are compilation of 3 books below. When you see the book picture on a slide, it means it has been taken from that book.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 6 License ● Creative Common Attribution 4.0 International License ● Universities and colleges are allowed to use it on condition that we are informed ahead. ● Please, do not use in any commercial activity without written permission.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 7 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 8 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 9 Embedded System ● A computer system w/ a dedicated function. – Part of a bigger system may be
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 10 Deep Embedded Systems ● Our focus in this course ● Safety or Mission critical systems ● Reliability is a big concern.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 11 Introduction to µ-Controllers World
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 12 Central Processing Unit and Buses
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 13 General Purpose (GPR’s) and Special Function (SFR’s)Registers ● GPR – Inside or outside CPU – Hold intermediate processing results – Fastest memory access ● SFR – Inside or outside CPU – Predetermined function – Controls or states CPU and IO behavior
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 14 Input/Output (IO) Ports ● Connect a µ-controller w/ external world ● Have maximum current load capacity ● Pull-up, pull-down or float
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 15 Interrupt ● A mean of getting CPU attention ● Polling alternative in real-time architectures – Might not be a good one
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 16 Memory ● Used for data storage ● Each location has unique address. ● Can be volatile or non-volatile
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 17 Non-Volatile Memories ● Read Only Memory (ROM) – Masked ROM (MROM) – One-Time Programmable ROM (OTP ROM) – UV Erasable Programmable ROM (UV EPROM) – Electrical Erasable Programmable ROM (EEPROM) ● Flash – NAND – NOR ● Non-Volatile RAM (NV RAM)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 18 Volatile Memories ● Random Access Memory (RAM) – Static RAM (SRAM) ● Expensive ● Small in size ● Internal ● e.g. cache memory – Dynamic RAM (DRAM) ● Cheap ● Large in size ● External ● Needs refreshing ● e.g. external memories
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 19 Serial Communication ● Parallel connection is ideal on shorter distances. – several meters ● Many serial protocols – HW controlled – Buad rate – Examples: ● Inter Integrated Circuit (I2C) ● Serial Peripheral Interface (SPI) ● Universal Asynchronous Receiver/Transmitter (UART)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 20 Oscillator ● Synchronizes electronics circuits ● Machine Cycle >> Oscillator Cycle – Instructions are made of several steps.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 21 Power Supply Circuit ● Brown out – Dangerous condition when µ-controller being turned off or power supply voltage drops to a minimum – Can cause its out-of-control performance – µ-controller has a built-in circuit for brown out reset as soon as it incurs a state of emergency. ● Reset pin – Marked as Master Clear Reset (MCLR) pin – For external reset by applying a signal for a duration of time
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 22 Timer/Counter ● Timer == Internal oscillator pulses counter – Measures time ● Counter == External pin pulses counter – Counts events
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 23 Watch-Dog Timer ● A timer with separate oscillator circuit ● Resets µ-controller if not refreshed before its overflow
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 24 A/D Converter ● Translates analog sensor readings into digital values ● Accepts certain input range ● Signal conditioning may be needed to interface sensor with A/D
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 25 Lab 0: Development Environment ● Follow your instructor to: – Install the development environment – Know your development environment – Run your first code
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 26 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 27 Machine Code
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 28 Assembly Language
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 29 C Language ● Mid-level language ● Very efficient ● Easy to learn ● Popular and well understood!! ● Good, well-proven compilers for 8- to 32-bits CPU’s ● A lot of resources available online C may not be perfect for developing embedded systems, but it is a good choice.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 30 Programming Embedded Systems in C Work-Flow
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 31 Preprocessor Directives ● Commands to the compiler processed before compilation ● starts with ‘#’ ● Examples: – #include – #define
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 32 Simple C Program
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 33 C Data Types
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 34 Variables ● Any # changing its value during program operation. ● Stored in RAM ● Variable names – Case sensitive – A-Z (a-z), 0 – 9, _ – Must not start with a digit – Must not be reserved
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 35 Pointers ● Hold addresses of variables
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 36 Constants ● Have fixed values ● Stored in program flash ● Can be: – Integer – Floating – Character – ASCII – Enumerated
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 37 Constants Examples
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 38 Operators ● Arithmetic ● Assignment ● Increment/Decrement ● Relational
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 39 Operatorscont’d ● Logic – &&, || and ! ● Bitwise – &, I, ~ and ^ ● Shifting – >> and << ● Precedence
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 40 Data Types Conversion ● Auto-conversion is done @: – Operations: different types promoted to higher priority – Assignments: different types converts to assignee
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 41 Conditional Operators ● if expression ● switch expression
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 42 Loops ● While loop ● for loop ● do-while loop
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 43 Arrays ● 1 D ● 2 D
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 44 Functions ● Has 1 return value ● Multiple or no arguments ● May return nothing
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 45 Function Libraries ● Names of functions being are stored in header files. ● Functions are sorted in libraries. ● It is necessary to specify proper header file using #include command at the program start. ● If compiler encounters unknown function, it will look for its declaration in the specified libraries.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 46 Standard C Library ● 24 C libraries ● Comes with every compiler
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 47 Typical Software Architecture for PC Computers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 48 Desktop Code
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 49 The Super Loop ● Minimal embedded C program ● Needed as we have no OS to return to
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 50 Pros and Cons of Super Loop ● Pros – Simplicity – Minimal HW resources – Highly portable ● Cons – Inaccurate timing – High power consumption
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 51 Example: Central-Heating Controller
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 52 Task-Oriented Software Engineering ● This is a task, implemented as a C function ● OS’s and schedulers call these functions (or “release” these tasks) ● Most of our systems will be built from “void void” functions ● Data transfer will be carried out using global variables
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 53 Embedded Code
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 54 Lab 1: Software Delays ● Follow your instructor to: – Analyze SW delays – Refactor the code you have
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 55 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 56 Our Target - PIC16F887 PIC16 family is suitable for everything from automotive and aerospace systems to TV “remotes”.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 57 PIC16F887 Pins
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 58 RAM in PIC16F887 ● RAM = GPR’s + SFR’s ● Divided into 4 banks – 2 bits in STATUS SFR control bank selection – Bank selection is handled by C compiler – Common SFR’s have same address in all banks
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 59 Stack in PIC16F887 ● A part of RAM ● LIFO ● 8 x 13-bit registers ● Used in subroutine implementation – Return address saving ● Treated as a circular buffer and no overflow detection mechanism ● Not accessed by programmer
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 60 Interrupt System in PIC16F887
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 61 STATUS Register ● IRP = Indirect Addressing Bank Selection (0 == Bank 2, 3) ● RP1, RP0 = Direct Addressing Bank Selection ● TO = Time-Out (0 == WDT timeout) ● PD = Power Down (0 == After SLEEP instruction) ● Z = Zero flag ● DC = Digital Carry/Borrow (for nibbles) ● C = Carry/Borrow (for MSB)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 62 OPTION_REG Register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 63 Interrupt System ● IF = Interrupt Flag bit (1 == Interrupt occurred) ● IE = Interrupt Enable bit (1 == Interrupt enabled)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 64 Interrupt System cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 65 INTCON Register Bit Description GIE Global Interrupt Enable PEIE Peripheral Interrupt Enable T0IE TMR0 Overflow Interrupt Enable INTE RB0/INT External Interrupt Enable Bit Description RBIE RB Port Change Interrupt Enable T0IF TMR0 Overflow Interrupt Flag INTF RB0/INT External Interrupt Flag bit RBIF RB Port Change Interrupt Flag
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 66 PIE1 Register Bit Description ADIE A/D Converter Interrupt Enable RCIE EUSART Receive Interrupt Enable TXIE EUSART Transmit Interrupt Enable SSPIE Master Synchronous Serial Port (MSSP) Interrupt Enable Bit Description CCP1IE CCP1 Interrupt Enable TMR2IE TMR2 to PR2 Match Interrupt Enable TMR1IE TMR1 Overflow Interrupt Enable
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 67 PIE2 Register Bit Description OSFIE Oscillator Fail Interrupt Enable C2IE Comparator C2 Interrupt Enable C1IE Comparator C1 Interrupt Enable EEIE EEPROM Write Operation Interrupt Enable Bit Description BCLIE Bus Collision Interrupt Enable ULPWUIE Ultra Low-Power Wake-up Interrupt Enable CCP2IE CCP2 Interrupt Enable
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 68 PIR1 Register Bit Description ADIF A/D Converter Interrupt Flag RCIF EUSART Receive Interrupt Flag TXIF EUSART Transmit Interrupt Flag SSPIF Master Synchronous Serial Port (MSSP) Interrupt Flag Bit Description CCP1IF CCP1 Interrupt Flag TMR2IF Timer2 to PR2 Interrupt Flag TMR1IF Timer1 Overflow Interrupt Flag
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 69 PIR2 Register Bit Description OSFIF Oscillator Fail Interrupt Flag C2IF Comparator C2 Interrupt Flag C1IF Comparator C1 Interrupt Flag EEIF EE Write Operation Interrupt Flag Bit Description BCLIF Bus Collision Interrupt Flag ULPWUIF Ultra Low-power Wake-up Interrupt Flag CCP2IF CCP2 Interrupt Flag
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 70 PCON Register Bit Description ULPWUE Ultra Low-Power Wake-up Enable SBOREN Software BOR Enable POR Power-on Reset Status BOR Brown-out Reset Status
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 71 PCL and PCLPATH Registers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 72 Addressing Modes
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 73 Connecting Our PIC16F887 Power Supply Reset Signal Clock Signal
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 74 Clock Signal (Heartbeat) Options Quartz Crystal for better stabilization Ceramic Resonator; cheaper but less stable
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 75 Clock Signal (Heartbeat) Options cont’d RC Oscillator; cheapest and least accurate option External Oscillator; used to synchronize with other µ-controllers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 76 Clock Frequency and Performance ● Higher frequency == Higher performance ● Higher frequency == Higher power consumption ● Keep clock @ lowest frequency. – Not all application require maximum frequency. – Power consumption reduced – Programming of peripherals is simplified @ low frequencies. – EMI is reduced.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 77 Lab 2: Know Your µ-Controller ● Follow your instructor to: – Skim your µ-controller data sheet – Read a board schematic – Read your µ-controller header file
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 78 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 79 IO Ports in PIC16F887 ● 5 ports (A – E) w/ 35 pins ● Common features: – Many IO pins have alternate functions – Each port has its satellite (TRISX)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 80 Port A ● Controlled by TRISA and ANSEL registers – ANSEL determine if pins are analog inputs or digital IO ● Only RA0 to RA5
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 81 Wake-Up Unit on RA0 ● A µ-controller can wake-up itself by periodic checking of an external signal. – After charging capacitor, device is put into SLEEP mode. – IO pin is configured as input and generates interrupt on logic state change. – Capacitor discharges causing interrupt to wake-up the device. ● Capacitor leaks fast. – ULPWU slows leakage.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 82 Port B ● Controlled by TRISA and ANSELH registers – ANSEL determine if pins are analog inputs or digital IO ● Only RB0 to RB5
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 83 Port B Built-In Pull-Up Resistors ● Ideal for push buttons (keyboard), switches and optocouplers ● Controlled by WPUB register (1 == PU enabled)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 84 Port B Interrupts on Changing Bits ● Used for checking push buttons as they register any button press – No need to ‘scan’ inputs ● Controlled by IOCB register (1 == IOCB enabled)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 85 Port B Special Functions ● RB0/INT – Only ‘true’ external interrupt source – Can react to signal raising or falling edge based on INTEDG bit of OPTION_REG register ● RB6/RB7 – Used in ICSP – RB6 as clock – RB7 as data
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 86 Port C and Port D
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 87 Port E ● Controlled by TRISE and ANSEL registers – ANSEL determine if pins are analog inputs or digital IO ● Only RE0 to RE2 – RE3 pin is always configured as an input
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 88 ANSEL and ANSELH Registers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 89 Switches and Push Buttons ● Mechanical devices ● Not as simple as they look ● Beware of bouncing! – Unnoticed by humans but noticed by µ-controllers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 90 De-Bouncing ● HW de-bouncing – Cheap solution – Expensive solution ● aka SPDT switch ● SW de-bouncing 1. Test input pin and detects a change 2. Check one more time after a certain delay 3. If change confirmed, switch changed position.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 91 Relays (Electrical Switches) ● Used w/ high-power devices(motors, transformers, heaters, bulbs …)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 92 LED Diodes ● Beware of quick and slow burning!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 93 LED Displays ● Several LEDs molded in same case – Each diode is treated separately. – Ones using low current LEDs (2mA/LED)are preferable ● 7-segments displays are most common. ● Can be common anode/cathode display
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 94 Multiplexing LED Displays ● To reduce pins used for connectivity ● Only a digit at a time is active, but they change conditions quickly making impression that all digits are simultaneously active.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 95 Mapping Digits to LED Displays
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 96 Optocouplers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 97 LCD Displays ● Can display all letters – Every character consists of 5×8 or 5×11 dot matrix. ● Support automatic message shift, cursor, LED backlight … ● Hav 3 memories
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 98 LCD Display Pins
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 99 Display Data RAM (DDRAM) ● Stores characters to be displayed ● Up to 80 characters
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 100 Character Generator ROM (CGROM) ● Contains standard character map that can be displayed – Each character is assigned to a memory location
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 101 Character Generator RAM (CGRAM) ● Stores user defined characters to be displayed ● 64 bytes – Memory registers are 8 bits wide – Only 5 lower bits are used – 1 == dimmed dot – 8 locations grouped represent one character
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 102 LCD Basic Commands ● All data transferred to D0-D7 will be interpreted as a command or a data. – RS == 1: D0 – D7 are addresses of the characters to be displayed. ● Character map address ● DDRAM address specifies location on which the character is to be displayed – Defined prior to transferring character or automatically incremented – RS == 0: D0 – D7 are commands
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 103 LCD Basic Commands cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 104 LCD Connecting
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 105 LCD Initialization – Default Mode 1.Display is cleared for 15 ms. 2.Mode – DL = 1 – Communication through 8-bit interface – N = 0 – Messages are displayed in one line – F = 0 – Character font 5 x 8 dots 3.Display/Cursor on/off – D = 0 – Display off – U = 0 – Cursor off – B = 0 – Cursor blink off 4.Character entry – ID = 1 Displayed addresses are automatically incremented by 1 – S = 0 Display shift off
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 106 LCD Initialization – 8-Bit Mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 107 LCD Initialization – 4-Bit Mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 108 Lab 3: Counting Goats ● Complete the missing code. ● Make sure it runs!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 109 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 110 View on Object Oriented Code OO languages are no readily available for small embedded systems because of their features overhead
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 111 Object Oriented C? ● Modular file based classes
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 112 Example: PC_IO Library – PC_IO.H
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 113 Example: PC_IO Library – PC_IO.C
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 114 Example: PC_IO Library – PC_IO.C cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 115 Example: PC_IO Library – PC_IO.C cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 116 Project Header – MAIN.H ● Groups device key information you need along with other key parameters – Easier to read code – Easier to port to other devices
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 117 Example: MAIN.H
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 118 Example: MAIN.H cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 119 Port Header File – PORT.H ● Groups all the pin allocations to a single file for the project ● Avoids potential conflicts
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 120 Lab 4: Counting Goats – Revisited ● Refactor the Counting Goats project.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 121 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 122 Real-Time Systems ● RT system = Correct function @ Correct time ● RT systems are: – Hard: missing deadlines will cause a disaster – Firm: missing deadlines is not tolerated and will cause malfunctioning – Soft: missing deadline can be tolerated
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 123 Importance of Real-Time Systems
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 124 Food for Thought ● Which is more important when designing a RT system? – Best Case Execution Time (BCET) – Worst Case Execution Time (WCET) – Relation between BCET and WCET ● How can we generate accurate timing?
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 125 What are the Problems Below?
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 126 The Loop Timeout Mechanism ● Assuming 16-bit variables, the loop will run a maximum of 65535 times before timeout ● The exact maximum time of the loop has to be tested, but ensures that loop eventually exits. ● The timeout duration can be changed by using different starting values for the timeout variable.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 127 Hardware Timeout Mechanism 1.Initialize HW timer with an initial value 2.Start SW task 3.Check HW timer did not overflow
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 128 Sandwich Delays
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 129 A Simple Cyclic Executive
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 130 Sandwich Delays in Action
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 131 Timer TMR0 ● 8-bit timer/counter with the following features: – 8-bit prescaler (shared with Watchdog timer) – Programmable internal or external clock source – Interrupt on overflow – Programmable external clock edge selection ● Controlled by OPTION_REG register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 132 Timer TMR0 cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 133 Notes about Timer TMR0 ● If prescaler assigned to TMR0, any write to the TMR0 register clear prescaler. ● If prescaler assigned to WDT, CLRWDT instruction clear both prescaler and WDT. ● Write to TMR0 timer, will cause pulse counting to start after two instruction cycles delay. ● TMR0 overflow interrupt cannot wake-up the processor from SLEEP mode. ● When used as an external clock counter, without prescaler, a minimal pulse length or a delay between two pulses must be 2 TOSC + 20 nS. ● When used as an external clock counter with prescaler, a minimal pulse length or interval between two pulses is only 10nS. ● 8-bit prescaler register cannot be directly read or written to.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 134 Timer TMR1 ● 16-bit timer/counter with the following features: – Programmable internal or external clock source – 3-bit prescaler (1, 2, 4 or 8) – Optional LP oscillator – Synchronous or asynchronous operation – Timer TMR1 gate control (count enable) via comparator or T1G pin – Interrupt on overflow – Wake-up on overflow (external clock) – Time base for Capture/Compare function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 135 Timer TMR1 cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 136 Notes about Timer TMR1 ● Synchronization can be done in case of external clock source only. ● Prescaler register cannot be directly read or written to. ● External clock source can wake-up device in SLEEP mode. ● Counter registers a logic one (1) on input pins. @ least one falling edge must be registered prior to starting pulse counting.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 137 Timer TMR2 ● 8-bit special timer ● The TMR2 and PR2 registers are both fully readable and writable.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 138 T2CON Register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 139 Lab 5: Counting Goats – Revisited ● Refactor the Counting Goats project to use HW delays and timeouts.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 140 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 141 Need for Accurate Timing ● Most embedded applications require tasks to be performed periodically: – Generating sound from stored samples – Measuring the speed of a vehicle every 0.5 seconds – Refreshing a display 40 times per second – Logging vibration data from sensor at a rate of 1000 Hz – Scanning a keypad every 200 ms – ...
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 142 The Super Loop – Revisited ● This will be fine, if: – We know the precise duration of function X() – Period never varies
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 143 Basis of Small Embedded OS
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 144 Basis of Small Embedded OS cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 145 Features of Small Embedded Operating System ● Can run tasks @ specified rates ● ISR’s are easy to manage in high-level language. ● Framework to develop embedded systems ● Puts the processor “to sleep” between tasks: – To conserve power – To reduce task jitter ● Easily portable to other controllers
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 146 Small Embedded OS Listing – Timer ISR
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 147 Small Embedded OS Listing – Timer Initialization
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 148 Small Embedded OS Listing – Timer Initialization cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 149 Small Embedded OS Listing – Power Down
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 150 Constraints for Small Embedded OS ● Tasks and scheduling – sEOS can schedule a single task (function in C) to be run periodically ● Tick interval – Automation of tick interval is really needed – Using “odd” frequency oscillators limits tick period precision
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 151 The Super Loop – Re-factored
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 152 Creating Useful Tasks for Small Embedded OS ● “Embedded C” gives a number of examples of tasks for use with TTC architectures ● “Patterns for Time- Triggered Embedded Systems” also has many relevant examples
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 153 Example: Reading a Switch
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 154 C/C’s of Small Embedded OS ● sEOS == TTC scheduler – Co-operative environment avoids overheads and potential problems of multitasking environments. – Workload of a TT system can be ensured to be evenly distributed at design time: in most cases, unlike a system in which multiple interrupts are enabled.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 155 1 µ-Controller == 1 Interrupt ● In theory, for an embedded system w/ TT architecture we mean that we can determine in advance what it will do at every moment of time during its execution. ● In practice, TT usually means: – One interrupt source per CPU – The interrupt is often (not always) a “timer tick” – Peripherals are ‘polled’.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 156 Limitations of TTC Schedulers ● As active task controls system and cannot be interrupted by scheduler, a task overrunning its allotted time can cause problems. ● Its by design to ensure (WCET) of any task is less that the tick interval.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 157 Limitations of TTC Schedulers ● All code ends up in one ISR. ● Hard to maintain ● Minimal separation between OS and task set
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 158 A Better TTC Scheduler ● Builds on sEOS ● Supports execution of multiple periodic tasks with different periods and different initial delays ● Moves user tasks out of the Timer ISR – Greater task independence (easier to re-use / recycle) – Can detect if a task overruns ● Still easy to understand with limited resources
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 159 TTC Functions and Data Structures ● Task array ● Initialization function ● Start function ● Update function ● Dispatcher function ● Sleep function ● Add task function ● Delete task function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 160 Task Array
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 161 Initialization Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 162 Add Task Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 163 Start Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 164 Update Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 165 Dispatcher Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 166 Dispatcher Function cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 167 Dispatcher Function cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 168 Sleep Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 169 Delete Task Function
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 170 Using TT Cooperative Scheduler ● Phase 1 – Initialization 1. Scheduler is initialized by the SCH_Init function. 2. Tasks are added to the task set using SCH_Add_Task function. 3. Scheduler is started using the SCH_Start function. ● Phase 2 – Normal operation 1. Dispatcher SCH_Dispatch_Tasks is called in a while(1) loop. 2. After running all tasks scheduled to run in a tick, the scheduler puts the system into sleep.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 171 TTC Considerations – Task Execution Sequence ● If multiple tasks to run @ same tick, sequence is determined by storage order in task array. – Varying task sequence == Different algorithms ● If scheduler not overloaded, jitter for a given task depends on: – Scheduler overheads (small and predictable) – Execution times of other tasks running before it in the same tick
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 172 TTC Considerations – Task Execution Time ● Ideally, sum of WCET of all tasks in a tick < tick ● However, if sum of WCET of all tasks in a tick > tick: – Scheduler will finish running tasks in current tick in their order before tasks in next tick. – Scheduler will catch up if there is sufficient slack in subsequent ticks. – System will meet specifications if no deadlines are missed.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 173 Lab 6: Milk Pasteurization ● Complete the missing code. ● Make sure it runs!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 174 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 175 Multiple States in Systems ● Most embedded systems can be modeled in terms of their states. – Transition between states may result in the execution of a particular sequence of functions. ● Some examples – Auto pilot system – Industrial robots – Traffic lights – Automatic washing machine
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 176 Classifications of Multi-State Systems ● Time-dependent, multi-state systems – Basic traffic light control system ● Time- and input-dependent, multi-state systems – Washing machine ● Input-dependent, multi-state systems
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 177 Example: Basic Traffic Light ● Timed four-state system 1.Red (for 20 seconds) 2.Red and amber (for 5 seconds) 3.Green (for 30 seconds) 4.Amber (for 5 seconds) ● No inputs to the system
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 178 Example: Basic Traffic Light Listing
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 179 Example: Basic Traffic Light Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 180 Example: Basic Traffic Light Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 181 Example: Basic Traffic Light Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 182 Example: Basic Traffic Light Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 183 Example: Basic Traffic Light Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 184 Lab 7: Washing Machine Controller ● Complete the missing code. ● Make sure it runs!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 185 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 186 What is RS-232? ● Recommended Standard 232 first appeared in 1960s and defines: – Protocol to be used for data transmission – Voltages to be used on the signal lines – Connectors to be used to link equipment together ● Widely used at data rates up to around 115 or 330 kbits /second (k baud) ● Used to communicate with devices at 15 meters or more
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 187 Is It Significant? ● Used by most of the newer µ-controllers for in system programming (ISP) or in circuit programming (ICP) ● Provides a link between a PC and the µ-controller that can be used for testing, debugging and data acquisition ● Simple, unlike USB or Bluetooth, and is still used in some of the recent devices (e.g. GPS sensors, TV boxes for updating the device configuration, etc.)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 188 RS-232 Basics ● Asynchronous data transmission ● +3V to +15V for bit value of 0 ● -3V to -15V for bit value of 1 ● Quiescent (idle) state: (1, indefinite duration) ● Start bit: (0, 1 x bit duration) ● Data: (typically 7-8 data bits, with optional parity bit) ● Stop bit(s): (0, duration of 1, 1.5 or 2 bits) ● Defined baud rates ● HW handshaking possible
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 189 EUSART ● Full-duplex asynchronous transmit and receive ● Programmable 8- or 9-bit wide characters ● Address detection in 9-bit mode ● Input buffer overrun error detection ● Half-duplex communication in synchronous mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 190 EUSART Connection
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 191 EUSART Asynchronous Transmitter ● TXEN in TXSTA register == 1 ● SYNC in TXSTA register == 0 ● SPEN in RCSTA register == 1 ● TX9D is used in 9-bit mode when TX9 in TXSTA == 1
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 192 EUSART Asynchronous Receiver ● CREN in RCSTA register == 1 ● SYNC in TXSTA register == 0 ● SPEN in RCSTA register == 1 ● RX9D is used in 9-bit mode when RX9 in RCSTA == 1
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 193 Receiver Error Detection ● Framing error – Indicated by FERR bit of RCSTA register – Does not generate interrupt – FERR bit cleared by reading received data – FERR bit canbe cleared by clearing SPEN bit of RCSTA register ● Overrun error – Indicated by OERR bit of RCSTA register – No additional data will be received until the OERR bit is cleared – Cleared by: ● Clearing CREN bit of RCSTA register ● Clearing the SPEN bit of RCSTA register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 194 9-Bit Mode ● TX9D must be written before writing to TXREG. ● RX9D must be read before reading RCREG.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 195 Address Detection
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 196 TXSTA Register Bit Description CSRC Clock Source Select TX9 9-bit Transmit Enable TXEN Transmit Enable SYNC EUSART Mode Select Bit Description SENDB Send Break Character BRGH High Baud Rate Select TRMT Transmit Shift Register Status TX9D Ninth bit of Transmit Data
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 197 RCSTA Register Bit Description SPEN Serial Port Enable RX9 9-bit Receive Enable SREN Single Receive Enable CREN Continuous Receive Enable Bit Description ADDEN Address Detect Enable FERR Framing Error OERR Overrun Error RX9D Ninth bit of Receive Data
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 198 ESUART Baud Rate Generator ● Beside above registers, BRGH bit of TXSTA register and BRGH16 bit of BAUDCTL register affect clock frequency
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 199 EUSART Baud Rate Formula ● n == SPBRG16:SPBRG
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 200 BAUDCTL Register Bit Description ABDOVF Auto-Baud Detect Overflow RCIDL Receive Idle Flag SCKP Synchronous Clock Polarity Select Bit Description BRG16 16-bit Baud Rate Generator WUE Wake-up Enable ABDEN Auto-Baud Detect Enable
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 201 Master Synchronous Serial Port Module
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 202 SPI ● MSSP module uses 4 registers: – SSPSTAT – status register – SSPCON – control register – SSPBUF – buffer register – SSPSR – shift register (not directly available)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 203 I2 C
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 204 I2 C cont’d ● MSSP module uses 6 registers: – SSPCON – control register – SSPCON2 – control register2 – SSPSTAT – status register – SSPBUF – buffer register – SSPSR – shift register (not directly available) – SSPADD – address register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 205 SSPSTAT Register Bit Description SMP Sample State in SPI or Slew Rate in I2 C CKE Clock Edge Select D/A Data/Address flag in I2 C P Stop bit in I2 C Bit Description S Start bit in I2 C R/W Read/Write in I2 C UA Update Address in 10-bit I2 C BF Buffer Full Status
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 206 SSPCON Register Bit Description WCOL Write Collision Detect in I2 C SSPOV Receive Overflow Indicator SSPEN Synchronous Serial Port Enable CKP Clock Polarity Select in SPI Bit Description SSPMx Synchronous Serial Port Mode Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 207 Synchronous Serial Port Mode Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 208 SSPCON2 Register Bit Description GCEN General Call Enable in I2 C Slave Mode AKSTAT Acknowledge Status in I2 C Master Transmit Mode AKDT Acknowledge Data in I2 C Master Receive Mode ACKEN Acknowledge Sequence Enable in I2 C Master Receive Mode Bit Description RCEN Receive Enable in I2 C Master Mode PEN STOP condition Enable in I2 C Master Mode RSEN Repeated START Condition Enabled in I2 C Master Mode SEN START Condition Enabled/Stretch Enabled in I2 C Master Mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 209 I2 C in Master Mode Initialization Data Transmission Data Reception
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 210 MSSP Baud Rate Formula
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 211 Final Notes about MSSP ● In case of WCOL, SSPBUF is not written. WCOL must be cleared by SW. ● BF is automatically cleared when SSPBUF is consumed. ● SDA and SCL pins turn into Open Drain outputs. – They must be connected to externally to pull-up resistors.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 212 Long TTC Task Problem
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 213 Example: Long TTC Task Problem
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 214 Example: Display Elapsed Time ● Send time elapsed since system power on over a serial link
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 215 Example: Display Elapsed Time Listing
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 216 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 217 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 218 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 219 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 220 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 221 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 222 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 223 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 224 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 225 Example: Display Elapsed Time Listing cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 226 Lab 8: Data Acquisition ● Complete the missing code. ● Make sure it runs!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 227 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 228 Capture Compare Modules ● 2 CCP module allows user to time and control events in 3 modes: – Capture: access TMR1 changing register – Compare: compare TMR1 register with another register – PWM: generate a varying duty cycle signal ● Controlled by CCP1CON register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 229 CCP1 in Capture Mode ● TMR1 must operate as timer or synchronous counter. ● It should be switched OFF before (re-)configuration to avoid unwanted interrupts.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 230 CCP1 in Compare and PWM Modes ● Compare mode – TMR1 must be synchronized w/ internal clock. ● PWM mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 231 PWM C/C’s ● Period = (PR2 +1) * 4Tosc * TMR2 Prescale Value ● Pulse Width = Pulse Width = (CCPR1L,DC1B1,DC1B0) * Tosc * TMR2 Prescale Value – If pulse width > period, output will be always 1. ● Resolution – @ fixed frequency, it is the number of duty cycle combinations we can get
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 232 CCP1CON Register Bit Description P1Mx PWM Output Configuration DC1Bx PWM Duty Cycle Least Significant bits CCP1Mx CCP1 Mode Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 233 CCP1CON Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 234 CCP1CON Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 235 CCP2 Module ● True difference from CCP1 in capture mode
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 236 CCP2CON Register Bit Description DC2Bx PWM Duty Cycle Least Significant bits CCP2Mx CCP2 Mode Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 237 CCP2CON Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 238 CCP2CON Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 239 Single Output PWM Mode ● 1 PWM signal can be simultaneously available on maximum of 4 different output pins basic or inverted. – In inverted mode, similar pulses are generated in pairs: (P1A, P1C) and(P1B, P1D).
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 240 Half-Bridge Mode ● PWM output on P1A, and its complementary on P1B – It is necessary to provide a short delay between switching drivers on and off by using PDC0-PDC6 of PWM1CON register.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 241 Full-Bridge Mode ● All four pins are used as outputs in 2 configurations: forward and reverse
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 242 Full-Bridge Forward and Reverse Configurations ● Forward ● Reverse
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 243 PWM1CON Register Bit Description PRSEN PWM Restart Enable PDCx PWM Delay Count
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 244 PSTRCON Register Bit Description STRSYNC Steering Sync STRx Steering Enable x
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 245 ECCPAS Register Bit Description ECCPASE ECCP Auto-Shutdown Event Status ECCPASx ECCP Auto-Shutdown Source Select PSSAC1, PSSAC0 Pins P1A, P1C Shutdown State Control PSSBD1, PSSBD0 Pins P1B, P1D Shutdown State Control
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 246 ECCPAS Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 247 ECCPAS Register cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 248 A/D Converter ● 10-bit successive approximation result is stored in ADRESL and ADRESH registers ● 14 separate analog inputs – Pins should be configured as analog inputs by TRIS and ANSEL (ANSELH) registers ● -ve and + ve reference voltages
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 249 A/D Converter cont’d ● Controlled by 4 registers: – ADRESH - high byte of conversion – ADRESL - low byte of conversion – ADCON0 - control register 0 – ADCON1 - control register 1
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 250 A/D Converter cont’d ● ADFM bit of ADCON1 register controls adjustment. ● For ADC accuracy, it is necessary to provide time delay between selecting analog input and measurement itself. – This time is called ‘acquisition time’ ~ 20 µs ●
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 251 A/D Clock Period ● TAD == time needed to complete a bit conversion – @ least 1.6 µs – 10-bit A/D takes around 11 TAD. ● Clock frequency and source are controlled by ADCS1 and ADCS0 bits in ADCON0 register.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 252 A/D Clock Period cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 253 ADCON0 Register Bit Description ADCSx A/D Conversion Clock Select CHSx Analog Channel Select Bit Description GO/DONE A/D Conversion Status ADON A/D Enable
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 254 ADCON1 Register Bit Description ADFM A/D Result Format Select VCFGx Voltage Reference Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 255 Analog Comparators ● 2 comparators – Inputs: RA03 – RA04 – Outputs: RA4 and RA5 ● Controlled by the following registers: – CM1CON0 is in control of comparator C1 – CM2CON0 is in control of comparator C2 – CM2CON1 is in control of comparator C2 ● Work during SLEEP mode and can wake-up µ- controller
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 256 Analog Comparators cont’d ● Internal Vref exists for comparators and A/D. – 2 sources – 2 ranges – Can output final CVref ● RA2 – Simple D/A!!! ● Any comparator’s output can causes interrupt. – Checked by CMIF of PIR register – If CMIE of PIE register and PEIE and GIE bits of INTCON register are set
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 257 Analog Comparators cont’d
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 258 CM1CON0 Register Bit Description CxON Comparator Cx Enable CxOUT Comparator Cx Output CxOE Comparator Cx Output Enable CxPOL Comparator Cx Output Polarity Select Bit Description CxR Comparator Cx Reference Select CxCHy Comparator Cx Channel Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 259 CM2CON1 Register Bit Description MCxOUT Mirror Copy of CxOUT CxRSEL Comparator Cx Reference Select T1GSS Timer1 Gate Source Select C2SYNC Comparator C2 Output Synchronization
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 260 Clock Oscillator ● The oscillator mode is selected by the bits of bytes, called Config Word, sent during programming.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 261 OSCCON Register Bit Description IRCFx Internal Oscillator Frequency Select OSTS Oscillator Start-up Time-out Status Bit Description HTS HFINTOSC Status LTS LFINTOSC Stable SCS System Clock Select
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 262 External Oscillator in EC Mode ● Up to 20 MHz ● Advantages: – OSC2 is available as IO pin. – No frequency stabilization – Synchronize µ-controller w/ board electronics – Temp external clock disabling stops device while leaving all data intact. ● After restarting external clock, the device proceeds with operation as if nothing has happened.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 263 External Oscillator in LP, XT Or HS Modes ● Quartz crystal or ceramic resonators – Ceramics are cheaper but less stable ● LP (low power) mode – Drive only 32.768 kHz crystals ● XT mode – Intermediate frequency up to 8 MHz ● HS (high speed) mode – High frequency > 8 MHz
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 264 External Oscillator in RC and RCIO Modes ● Much cheaper than resonators ● RC mode – OSC2 outputs RC frequency/4. ● RCIO mode – OSC2 is available as IO pin. ● f = 1/T= 1/(R*C)
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 265 Internal Oscillator Modes ● INTOSC mode – OSC1 is available as IO. – OSC2 outputs RC frequency/4. ● INTOSCIO mode – Both are available as IO pins.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 266 2-Speed Clock Start-Up Mode ● For power saving if µ-controller goes to SLEEP mode and configured in LP, XT or HS mode. ● During SLEEP, external oscillator is switched off. ● After wake-up, 1024 pulses are wasted before execution. ● Internal oscillator is used for 1024 instead of external one. ● Controlled by Int-Ext Switchover option
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 267 Fail-Safe Clock Monitor ● Monitors external oscillator and allows µ- controller to proceed with execution if external oscillator fails – Internal oscillator takes over. ● Can generate interrupt in case of failure ● External source takes over if reset successfully. ● Controlled by Fail-Safe Clock option
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 268 OSCTUNE Register
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 269 EEPROM Memory ● 256 data locations controlled by: – EECON1 - control register – EECON2 - control register – EEDAT - data register – EEADR - address register ● Take care, shared with program flash.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 270 EECON1 Register Bit Description EEFGD Program/Data EEPROM Select WRERR EEPROM Error Flag WREN EEPROM Write Enable Bit Description WR Write Control RD Read Control
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 271 Reading and Writing to EEPROM ● Reading 1. Write address in EEADDR. 2. Clear EEPGD. 3. Set RD. 4. Data is ready in EEDAT. ● Writing 1. Write address in EEADDR. 2. Put data in EEDAT. 3. Clear EEPGD. 4. SET WREN. 5. Disable interrupts. 6. Write 0x55 then 0xAA to EECON2. 7. Set WR. 8. Enable interrupts. 9. Clear WREN.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 272 Reset ● Can be external or internal ● µ-controller takes time after power before it can operate. – PWRT and OST internal timers monitor Treset. – PWRT is configurable during flashing. – PWRT resets µ-controller @ VCC = 1.2V – 1.7V. – After 72 ms, OST resets µ-controller for 1024 quartz cycles. – @ the end if MCLR high, µ-controller runs.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 273 Reset cont’d ● Black-out reset when power goes off. ● Brown-out when power goes off slowly. – Really dangerous – Stored in on-chip memory ● Noise is special case of brown-out.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 274 Lab 9: Miscellaneous Interfacing ● Complete the missing code. ● Make sure it runs!
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 275 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 276 Lab 10: Car Hazard/Turning Indicators ● Using the techniques in this course to design and implement Car Hazard Turning Indicator. ● System specs are described in the handout requirements document.
Jul 6, 2016 Introduction to Embedded Systems - A Practical Approach 277 ● To contact us: – www.swift-act.com – training@swift-act.com – (+2)0122-3600-207

Introduction to Embedded Systems a Practical Approach

  • 1.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 1 Introduction to Embedded Systems A Practical Approach
  • 2.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 2 Objective(s) ● Introduce Embedded Systems ● Understand small µ-Controllers ● Design and Implement SW for µ-Controllers
  • 3.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 3 Prerequisite(s) ● Programming experience with any high-level programming language
  • 4.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 4 Note(s) ● Ask any time. ● Cell phones silent, please! ● Certificates are not for attendance.
  • 5.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 5 References ● The slides are compilation of 3 books below. When you see the book picture on a slide, it means it has been taken from that book.
  • 6.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 6 License ● Creative Common Attribution 4.0 International License ● Universities and colleges are allowed to use it on condition that we are informed ahead. ● Please, do not use in any commercial activity without written permission.
  • 7.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 7 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 8.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 8 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 9.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 9 Embedded System ● A computer system w/ a dedicated function. – Part of a bigger system may be
  • 10.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 10 Deep Embedded Systems ● Our focus in this course ● Safety or Mission critical systems ● Reliability is a big concern.
  • 11.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 11 Introduction to µ-Controllers World
  • 12.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 12 Central Processing Unit and Buses
  • 13.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 13 General Purpose (GPR’s) and Special Function (SFR’s)Registers ● GPR – Inside or outside CPU – Hold intermediate processing results – Fastest memory access ● SFR – Inside or outside CPU – Predetermined function – Controls or states CPU and IO behavior
  • 14.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 14 Input/Output (IO) Ports ● Connect a µ-controller w/ external world ● Have maximum current load capacity ● Pull-up, pull-down or float
  • 15.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 15 Interrupt ● A mean of getting CPU attention ● Polling alternative in real-time architectures – Might not be a good one
  • 16.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 16 Memory ● Used for data storage ● Each location has unique address. ● Can be volatile or non-volatile
  • 17.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 17 Non-Volatile Memories ● Read Only Memory (ROM) – Masked ROM (MROM) – One-Time Programmable ROM (OTP ROM) – UV Erasable Programmable ROM (UV EPROM) – Electrical Erasable Programmable ROM (EEPROM) ● Flash – NAND – NOR ● Non-Volatile RAM (NV RAM)
  • 18.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 18 Volatile Memories ● Random Access Memory (RAM) – Static RAM (SRAM) ● Expensive ● Small in size ● Internal ● e.g. cache memory – Dynamic RAM (DRAM) ● Cheap ● Large in size ● External ● Needs refreshing ● e.g. external memories
  • 19.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 19 Serial Communication ● Parallel connection is ideal on shorter distances. – several meters ● Many serial protocols – HW controlled – Buad rate – Examples: ● Inter Integrated Circuit (I2C) ● Serial Peripheral Interface (SPI) ● Universal Asynchronous Receiver/Transmitter (UART)
  • 20.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 20 Oscillator ● Synchronizes electronics circuits ● Machine Cycle >> Oscillator Cycle – Instructions are made of several steps.
  • 21.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 21 Power Supply Circuit ● Brown out – Dangerous condition when µ-controller being turned off or power supply voltage drops to a minimum – Can cause its out-of-control performance – µ-controller has a built-in circuit for brown out reset as soon as it incurs a state of emergency. ● Reset pin – Marked as Master Clear Reset (MCLR) pin – For external reset by applying a signal for a duration of time
  • 22.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 22 Timer/Counter ● Timer == Internal oscillator pulses counter – Measures time ● Counter == External pin pulses counter – Counts events
  • 23.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 23 Watch-Dog Timer ● A timer with separate oscillator circuit ● Resets µ-controller if not refreshed before its overflow
  • 24.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 24 A/D Converter ● Translates analog sensor readings into digital values ● Accepts certain input range ● Signal conditioning may be needed to interface sensor with A/D
  • 25.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 25 Lab 0: Development Environment ● Follow your instructor to: – Install the development environment – Know your development environment – Run your first code
  • 26.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 26 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 27.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 27 Machine Code
  • 28.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 28 Assembly Language
  • 29.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 29 C Language ● Mid-level language ● Very efficient ● Easy to learn ● Popular and well understood!! ● Good, well-proven compilers for 8- to 32-bits CPU’s ● A lot of resources available online C may not be perfect for developing embedded systems, but it is a good choice.
  • 30.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 30 Programming Embedded Systems in C Work-Flow
  • 31.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 31 Preprocessor Directives ● Commands to the compiler processed before compilation ● starts with ‘#’ ● Examples: – #include – #define
  • 32.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 32 Simple C Program
  • 33.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 33 C Data Types
  • 34.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 34 Variables ● Any # changing its value during program operation. ● Stored in RAM ● Variable names – Case sensitive – A-Z (a-z), 0 – 9, _ – Must not start with a digit – Must not be reserved
  • 35.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 35 Pointers ● Hold addresses of variables
  • 36.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 36 Constants ● Have fixed values ● Stored in program flash ● Can be: – Integer – Floating – Character – ASCII – Enumerated
  • 37.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 37 Constants Examples
  • 38.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 38 Operators ● Arithmetic ● Assignment ● Increment/Decrement ● Relational
  • 39.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 39 Operatorscont’d ● Logic – &&, || and ! ● Bitwise – &, I, ~ and ^ ● Shifting – >> and << ● Precedence
  • 40.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 40 Data Types Conversion ● Auto-conversion is done @: – Operations: different types promoted to higher priority – Assignments: different types converts to assignee
  • 41.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 41 Conditional Operators ● if expression ● switch expression
  • 42.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 42 Loops ● While loop ● for loop ● do-while loop
  • 43.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 43 Arrays ● 1 D ● 2 D
  • 44.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 44 Functions ● Has 1 return value ● Multiple or no arguments ● May return nothing
  • 45.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 45 Function Libraries ● Names of functions being are stored in header files. ● Functions are sorted in libraries. ● It is necessary to specify proper header file using #include command at the program start. ● If compiler encounters unknown function, it will look for its declaration in the specified libraries.
  • 46.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 46 Standard C Library ● 24 C libraries ● Comes with every compiler
  • 47.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 47 Typical Software Architecture for PC Computers
  • 48.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 48 Desktop Code
  • 49.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 49 The Super Loop ● Minimal embedded C program ● Needed as we have no OS to return to
  • 50.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 50 Pros and Cons of Super Loop ● Pros – Simplicity – Minimal HW resources – Highly portable ● Cons – Inaccurate timing – High power consumption
  • 51.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 51 Example: Central-Heating Controller
  • 52.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 52 Task-Oriented Software Engineering ● This is a task, implemented as a C function ● OS’s and schedulers call these functions (or “release” these tasks) ● Most of our systems will be built from “void void” functions ● Data transfer will be carried out using global variables
  • 53.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 53 Embedded Code
  • 54.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 54 Lab 1: Software Delays ● Follow your instructor to: – Analyze SW delays – Refactor the code you have
  • 55.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 55 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 56.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 56 Our Target - PIC16F887 PIC16 family is suitable for everything from automotive and aerospace systems to TV “remotes”.
  • 57.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 57 PIC16F887 Pins
  • 58.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 58 RAM in PIC16F887 ● RAM = GPR’s + SFR’s ● Divided into 4 banks – 2 bits in STATUS SFR control bank selection – Bank selection is handled by C compiler – Common SFR’s have same address in all banks
  • 59.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 59 Stack in PIC16F887 ● A part of RAM ● LIFO ● 8 x 13-bit registers ● Used in subroutine implementation – Return address saving ● Treated as a circular buffer and no overflow detection mechanism ● Not accessed by programmer
  • 60.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 60 Interrupt System in PIC16F887
  • 61.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 61 STATUS Register ● IRP = Indirect Addressing Bank Selection (0 == Bank 2, 3) ● RP1, RP0 = Direct Addressing Bank Selection ● TO = Time-Out (0 == WDT timeout) ● PD = Power Down (0 == After SLEEP instruction) ● Z = Zero flag ● DC = Digital Carry/Borrow (for nibbles) ● C = Carry/Borrow (for MSB)
  • 62.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 62 OPTION_REG Register
  • 63.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 63 Interrupt System ● IF = Interrupt Flag bit (1 == Interrupt occurred) ● IE = Interrupt Enable bit (1 == Interrupt enabled)
  • 64.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 64 Interrupt System cont’d
  • 65.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 65 INTCON Register Bit Description GIE Global Interrupt Enable PEIE Peripheral Interrupt Enable T0IE TMR0 Overflow Interrupt Enable INTE RB0/INT External Interrupt Enable Bit Description RBIE RB Port Change Interrupt Enable T0IF TMR0 Overflow Interrupt Flag INTF RB0/INT External Interrupt Flag bit RBIF RB Port Change Interrupt Flag
  • 66.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 66 PIE1 Register Bit Description ADIE A/D Converter Interrupt Enable RCIE EUSART Receive Interrupt Enable TXIE EUSART Transmit Interrupt Enable SSPIE Master Synchronous Serial Port (MSSP) Interrupt Enable Bit Description CCP1IE CCP1 Interrupt Enable TMR2IE TMR2 to PR2 Match Interrupt Enable TMR1IE TMR1 Overflow Interrupt Enable
  • 67.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 67 PIE2 Register Bit Description OSFIE Oscillator Fail Interrupt Enable C2IE Comparator C2 Interrupt Enable C1IE Comparator C1 Interrupt Enable EEIE EEPROM Write Operation Interrupt Enable Bit Description BCLIE Bus Collision Interrupt Enable ULPWUIE Ultra Low-Power Wake-up Interrupt Enable CCP2IE CCP2 Interrupt Enable
  • 68.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 68 PIR1 Register Bit Description ADIF A/D Converter Interrupt Flag RCIF EUSART Receive Interrupt Flag TXIF EUSART Transmit Interrupt Flag SSPIF Master Synchronous Serial Port (MSSP) Interrupt Flag Bit Description CCP1IF CCP1 Interrupt Flag TMR2IF Timer2 to PR2 Interrupt Flag TMR1IF Timer1 Overflow Interrupt Flag
  • 69.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 69 PIR2 Register Bit Description OSFIF Oscillator Fail Interrupt Flag C2IF Comparator C2 Interrupt Flag C1IF Comparator C1 Interrupt Flag EEIF EE Write Operation Interrupt Flag Bit Description BCLIF Bus Collision Interrupt Flag ULPWUIF Ultra Low-power Wake-up Interrupt Flag CCP2IF CCP2 Interrupt Flag
  • 70.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 70 PCON Register Bit Description ULPWUE Ultra Low-Power Wake-up Enable SBOREN Software BOR Enable POR Power-on Reset Status BOR Brown-out Reset Status
  • 71.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 71 PCL and PCLPATH Registers
  • 72.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 72 Addressing Modes
  • 73.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 73 Connecting Our PIC16F887 Power Supply Reset Signal Clock Signal
  • 74.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 74 Clock Signal (Heartbeat) Options Quartz Crystal for better stabilization Ceramic Resonator; cheaper but less stable
  • 75.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 75 Clock Signal (Heartbeat) Options cont’d RC Oscillator; cheapest and least accurate option External Oscillator; used to synchronize with other µ-controllers
  • 76.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 76 Clock Frequency and Performance ● Higher frequency == Higher performance ● Higher frequency == Higher power consumption ● Keep clock @ lowest frequency. – Not all application require maximum frequency. – Power consumption reduced – Programming of peripherals is simplified @ low frequencies. – EMI is reduced.
  • 77.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 77 Lab 2: Know Your µ-Controller ● Follow your instructor to: – Skim your µ-controller data sheet – Read a board schematic – Read your µ-controller header file
  • 78.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 78 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 79.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 79 IO Ports in PIC16F887 ● 5 ports (A – E) w/ 35 pins ● Common features: – Many IO pins have alternate functions – Each port has its satellite (TRISX)
  • 80.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 80 Port A ● Controlled by TRISA and ANSEL registers – ANSEL determine if pins are analog inputs or digital IO ● Only RA0 to RA5
  • 81.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 81 Wake-Up Unit on RA0 ● A µ-controller can wake-up itself by periodic checking of an external signal. – After charging capacitor, device is put into SLEEP mode. – IO pin is configured as input and generates interrupt on logic state change. – Capacitor discharges causing interrupt to wake-up the device. ● Capacitor leaks fast. – ULPWU slows leakage.
  • 82.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 82 Port B ● Controlled by TRISA and ANSELH registers – ANSEL determine if pins are analog inputs or digital IO ● Only RB0 to RB5
  • 83.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 83 Port B Built-In Pull-Up Resistors ● Ideal for push buttons (keyboard), switches and optocouplers ● Controlled by WPUB register (1 == PU enabled)
  • 84.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 84 Port B Interrupts on Changing Bits ● Used for checking push buttons as they register any button press – No need to ‘scan’ inputs ● Controlled by IOCB register (1 == IOCB enabled)
  • 85.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 85 Port B Special Functions ● RB0/INT – Only ‘true’ external interrupt source – Can react to signal raising or falling edge based on INTEDG bit of OPTION_REG register ● RB6/RB7 – Used in ICSP – RB6 as clock – RB7 as data
  • 86.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 86 Port C and Port D
  • 87.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 87 Port E ● Controlled by TRISE and ANSEL registers – ANSEL determine if pins are analog inputs or digital IO ● Only RE0 to RE2 – RE3 pin is always configured as an input
  • 88.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 88 ANSEL and ANSELH Registers
  • 89.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 89 Switches and Push Buttons ● Mechanical devices ● Not as simple as they look ● Beware of bouncing! – Unnoticed by humans but noticed by µ-controllers
  • 90.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 90 De-Bouncing ● HW de-bouncing – Cheap solution – Expensive solution ● aka SPDT switch ● SW de-bouncing 1. Test input pin and detects a change 2. Check one more time after a certain delay 3. If change confirmed, switch changed position.
  • 91.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 91 Relays (Electrical Switches) ● Used w/ high-power devices(motors, transformers, heaters, bulbs …)
  • 92.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 92 LED Diodes ● Beware of quick and slow burning!
  • 93.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 93 LED Displays ● Several LEDs molded in same case – Each diode is treated separately. – Ones using low current LEDs (2mA/LED)are preferable ● 7-segments displays are most common. ● Can be common anode/cathode display
  • 94.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 94 Multiplexing LED Displays ● To reduce pins used for connectivity ● Only a digit at a time is active, but they change conditions quickly making impression that all digits are simultaneously active.
  • 95.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 95 Mapping Digits to LED Displays
  • 96.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 96 Optocouplers
  • 97.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 97 LCD Displays ● Can display all letters – Every character consists of 5×8 or 5×11 dot matrix. ● Support automatic message shift, cursor, LED backlight … ● Hav 3 memories
  • 98.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 98 LCD Display Pins
  • 99.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 99 Display Data RAM (DDRAM) ● Stores characters to be displayed ● Up to 80 characters
  • 100.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 100 Character Generator ROM (CGROM) ● Contains standard character map that can be displayed – Each character is assigned to a memory location
  • 101.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 101 Character Generator RAM (CGRAM) ● Stores user defined characters to be displayed ● 64 bytes – Memory registers are 8 bits wide – Only 5 lower bits are used – 1 == dimmed dot – 8 locations grouped represent one character
  • 102.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 102 LCD Basic Commands ● All data transferred to D0-D7 will be interpreted as a command or a data. – RS == 1: D0 – D7 are addresses of the characters to be displayed. ● Character map address ● DDRAM address specifies location on which the character is to be displayed – Defined prior to transferring character or automatically incremented – RS == 0: D0 – D7 are commands
  • 103.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 103 LCD Basic Commands cont’d
  • 104.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 104 LCD Connecting
  • 105.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 105 LCD Initialization – Default Mode 1.Display is cleared for 15 ms. 2.Mode – DL = 1 – Communication through 8-bit interface – N = 0 – Messages are displayed in one line – F = 0 – Character font 5 x 8 dots 3.Display/Cursor on/off – D = 0 – Display off – U = 0 – Cursor off – B = 0 – Cursor blink off 4.Character entry – ID = 1 Displayed addresses are automatically incremented by 1 – S = 0 Display shift off
  • 106.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 106 LCD Initialization – 8-Bit Mode
  • 107.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 107 LCD Initialization – 4-Bit Mode
  • 108.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 108 Lab 3: Counting Goats ● Complete the missing code. ● Make sure it runs!
  • 109.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 109 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 110.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 110 View on Object Oriented Code OO languages are no readily available for small embedded systems because of their features overhead
  • 111.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 111 Object Oriented C? ● Modular file based classes
  • 112.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 112 Example: PC_IO Library – PC_IO.H
  • 113.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 113 Example: PC_IO Library – PC_IO.C
  • 114.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 114 Example: PC_IO Library – PC_IO.C cont’d
  • 115.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 115 Example: PC_IO Library – PC_IO.C cont’d
  • 116.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 116 Project Header – MAIN.H ● Groups device key information you need along with other key parameters – Easier to read code – Easier to port to other devices
  • 117.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 117 Example: MAIN.H
  • 118.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 118 Example: MAIN.H cont’d
  • 119.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 119 Port Header File – PORT.H ● Groups all the pin allocations to a single file for the project ● Avoids potential conflicts
  • 120.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 120 Lab 4: Counting Goats – Revisited ● Refactor the Counting Goats project.
  • 121.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 121 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 122.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 122 Real-Time Systems ● RT system = Correct function @ Correct time ● RT systems are: – Hard: missing deadlines will cause a disaster – Firm: missing deadlines is not tolerated and will cause malfunctioning – Soft: missing deadline can be tolerated
  • 123.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 123 Importance of Real-Time Systems
  • 124.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 124 Food for Thought ● Which is more important when designing a RT system? – Best Case Execution Time (BCET) – Worst Case Execution Time (WCET) – Relation between BCET and WCET ● How can we generate accurate timing?
  • 125.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 125 What are the Problems Below?
  • 126.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 126 The Loop Timeout Mechanism ● Assuming 16-bit variables, the loop will run a maximum of 65535 times before timeout ● The exact maximum time of the loop has to be tested, but ensures that loop eventually exits. ● The timeout duration can be changed by using different starting values for the timeout variable.
  • 127.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 127 Hardware Timeout Mechanism 1.Initialize HW timer with an initial value 2.Start SW task 3.Check HW timer did not overflow
  • 128.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 128 Sandwich Delays
  • 129.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 129 A Simple Cyclic Executive
  • 130.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 130 Sandwich Delays in Action
  • 131.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 131 Timer TMR0 ● 8-bit timer/counter with the following features: – 8-bit prescaler (shared with Watchdog timer) – Programmable internal or external clock source – Interrupt on overflow – Programmable external clock edge selection ● Controlled by OPTION_REG register
  • 132.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 132 Timer TMR0 cont’d
  • 133.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 133 Notes about Timer TMR0 ● If prescaler assigned to TMR0, any write to the TMR0 register clear prescaler. ● If prescaler assigned to WDT, CLRWDT instruction clear both prescaler and WDT. ● Write to TMR0 timer, will cause pulse counting to start after two instruction cycles delay. ● TMR0 overflow interrupt cannot wake-up the processor from SLEEP mode. ● When used as an external clock counter, without prescaler, a minimal pulse length or a delay between two pulses must be 2 TOSC + 20 nS. ● When used as an external clock counter with prescaler, a minimal pulse length or interval between two pulses is only 10nS. ● 8-bit prescaler register cannot be directly read or written to.
  • 134.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 134 Timer TMR1 ● 16-bit timer/counter with the following features: – Programmable internal or external clock source – 3-bit prescaler (1, 2, 4 or 8) – Optional LP oscillator – Synchronous or asynchronous operation – Timer TMR1 gate control (count enable) via comparator or T1G pin – Interrupt on overflow – Wake-up on overflow (external clock) – Time base for Capture/Compare function
  • 135.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 135 Timer TMR1 cont’d
  • 136.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 136 Notes about Timer TMR1 ● Synchronization can be done in case of external clock source only. ● Prescaler register cannot be directly read or written to. ● External clock source can wake-up device in SLEEP mode. ● Counter registers a logic one (1) on input pins. @ least one falling edge must be registered prior to starting pulse counting.
  • 137.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 137 Timer TMR2 ● 8-bit special timer ● The TMR2 and PR2 registers are both fully readable and writable.
  • 138.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 138 T2CON Register
  • 139.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 139 Lab 5: Counting Goats – Revisited ● Refactor the Counting Goats project to use HW delays and timeouts.
  • 140.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 140 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 141.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 141 Need for Accurate Timing ● Most embedded applications require tasks to be performed periodically: – Generating sound from stored samples – Measuring the speed of a vehicle every 0.5 seconds – Refreshing a display 40 times per second – Logging vibration data from sensor at a rate of 1000 Hz – Scanning a keypad every 200 ms – ...
  • 142.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 142 The Super Loop – Revisited ● This will be fine, if: – We know the precise duration of function X() – Period never varies
  • 143.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 143 Basis of Small Embedded OS
  • 144.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 144 Basis of Small Embedded OS cont’d
  • 145.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 145 Features of Small Embedded Operating System ● Can run tasks @ specified rates ● ISR’s are easy to manage in high-level language. ● Framework to develop embedded systems ● Puts the processor “to sleep” between tasks: – To conserve power – To reduce task jitter ● Easily portable to other controllers
  • 146.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 146 Small Embedded OS Listing – Timer ISR
  • 147.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 147 Small Embedded OS Listing – Timer Initialization
  • 148.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 148 Small Embedded OS Listing – Timer Initialization cont’d
  • 149.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 149 Small Embedded OS Listing – Power Down
  • 150.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 150 Constraints for Small Embedded OS ● Tasks and scheduling – sEOS can schedule a single task (function in C) to be run periodically ● Tick interval – Automation of tick interval is really needed – Using “odd” frequency oscillators limits tick period precision
  • 151.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 151 The Super Loop – Re-factored
  • 152.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 152 Creating Useful Tasks for Small Embedded OS ● “Embedded C” gives a number of examples of tasks for use with TTC architectures ● “Patterns for Time- Triggered Embedded Systems” also has many relevant examples
  • 153.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 153 Example: Reading a Switch
  • 154.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 154 C/C’s of Small Embedded OS ● sEOS == TTC scheduler – Co-operative environment avoids overheads and potential problems of multitasking environments. – Workload of a TT system can be ensured to be evenly distributed at design time: in most cases, unlike a system in which multiple interrupts are enabled.
  • 155.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 155 1 µ-Controller == 1 Interrupt ● In theory, for an embedded system w/ TT architecture we mean that we can determine in advance what it will do at every moment of time during its execution. ● In practice, TT usually means: – One interrupt source per CPU – The interrupt is often (not always) a “timer tick” – Peripherals are ‘polled’.
  • 156.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 156 Limitations of TTC Schedulers ● As active task controls system and cannot be interrupted by scheduler, a task overrunning its allotted time can cause problems. ● Its by design to ensure (WCET) of any task is less that the tick interval.
  • 157.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 157 Limitations of TTC Schedulers ● All code ends up in one ISR. ● Hard to maintain ● Minimal separation between OS and task set
  • 158.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 158 A Better TTC Scheduler ● Builds on sEOS ● Supports execution of multiple periodic tasks with different periods and different initial delays ● Moves user tasks out of the Timer ISR – Greater task independence (easier to re-use / recycle) – Can detect if a task overruns ● Still easy to understand with limited resources
  • 159.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 159 TTC Functions and Data Structures ● Task array ● Initialization function ● Start function ● Update function ● Dispatcher function ● Sleep function ● Add task function ● Delete task function
  • 160.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 160 Task Array
  • 161.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 161 Initialization Function
  • 162.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 162 Add Task Function
  • 163.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 163 Start Function
  • 164.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 164 Update Function
  • 165.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 165 Dispatcher Function
  • 166.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 166 Dispatcher Function cont’d
  • 167.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 167 Dispatcher Function cont’d
  • 168.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 168 Sleep Function
  • 169.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 169 Delete Task Function
  • 170.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 170 Using TT Cooperative Scheduler ● Phase 1 – Initialization 1. Scheduler is initialized by the SCH_Init function. 2. Tasks are added to the task set using SCH_Add_Task function. 3. Scheduler is started using the SCH_Start function. ● Phase 2 – Normal operation 1. Dispatcher SCH_Dispatch_Tasks is called in a while(1) loop. 2. After running all tasks scheduled to run in a tick, the scheduler puts the system into sleep.
  • 171.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 171 TTC Considerations – Task Execution Sequence ● If multiple tasks to run @ same tick, sequence is determined by storage order in task array. – Varying task sequence == Different algorithms ● If scheduler not overloaded, jitter for a given task depends on: – Scheduler overheads (small and predictable) – Execution times of other tasks running before it in the same tick
  • 172.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 172 TTC Considerations – Task Execution Time ● Ideally, sum of WCET of all tasks in a tick < tick ● However, if sum of WCET of all tasks in a tick > tick: – Scheduler will finish running tasks in current tick in their order before tasks in next tick. – Scheduler will catch up if there is sufficient slack in subsequent ticks. – System will meet specifications if no deadlines are missed.
  • 173.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 173 Lab 6: Milk Pasteurization ● Complete the missing code. ● Make sure it runs!
  • 174.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 174 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 175.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 175 Multiple States in Systems ● Most embedded systems can be modeled in terms of their states. – Transition between states may result in the execution of a particular sequence of functions. ● Some examples – Auto pilot system – Industrial robots – Traffic lights – Automatic washing machine
  • 176.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 176 Classifications of Multi-State Systems ● Time-dependent, multi-state systems – Basic traffic light control system ● Time- and input-dependent, multi-state systems – Washing machine ● Input-dependent, multi-state systems
  • 177.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 177 Example: Basic Traffic Light ● Timed four-state system 1.Red (for 20 seconds) 2.Red and amber (for 5 seconds) 3.Green (for 30 seconds) 4.Amber (for 5 seconds) ● No inputs to the system
  • 178.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 178 Example: Basic Traffic Light Listing
  • 179.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 179 Example: Basic Traffic Light Listing cont’d
  • 180.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 180 Example: Basic Traffic Light Listing cont’d
  • 181.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 181 Example: Basic Traffic Light Listing cont’d
  • 182.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 182 Example: Basic Traffic Light Listing cont’d
  • 183.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 183 Example: Basic Traffic Light Listing cont’d
  • 184.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 184 Lab 7: Washing Machine Controller ● Complete the missing code. ● Make sure it runs!
  • 185.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 185 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 186.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 186 What is RS-232? ● Recommended Standard 232 first appeared in 1960s and defines: – Protocol to be used for data transmission – Voltages to be used on the signal lines – Connectors to be used to link equipment together ● Widely used at data rates up to around 115 or 330 kbits /second (k baud) ● Used to communicate with devices at 15 meters or more
  • 187.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 187 Is It Significant? ● Used by most of the newer µ-controllers for in system programming (ISP) or in circuit programming (ICP) ● Provides a link between a PC and the µ-controller that can be used for testing, debugging and data acquisition ● Simple, unlike USB or Bluetooth, and is still used in some of the recent devices (e.g. GPS sensors, TV boxes for updating the device configuration, etc.)
  • 188.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 188 RS-232 Basics ● Asynchronous data transmission ● +3V to +15V for bit value of 0 ● -3V to -15V for bit value of 1 ● Quiescent (idle) state: (1, indefinite duration) ● Start bit: (0, 1 x bit duration) ● Data: (typically 7-8 data bits, with optional parity bit) ● Stop bit(s): (0, duration of 1, 1.5 or 2 bits) ● Defined baud rates ● HW handshaking possible
  • 189.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 189 EUSART ● Full-duplex asynchronous transmit and receive ● Programmable 8- or 9-bit wide characters ● Address detection in 9-bit mode ● Input buffer overrun error detection ● Half-duplex communication in synchronous mode
  • 190.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 190 EUSART Connection
  • 191.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 191 EUSART Asynchronous Transmitter ● TXEN in TXSTA register == 1 ● SYNC in TXSTA register == 0 ● SPEN in RCSTA register == 1 ● TX9D is used in 9-bit mode when TX9 in TXSTA == 1
  • 192.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 192 EUSART Asynchronous Receiver ● CREN in RCSTA register == 1 ● SYNC in TXSTA register == 0 ● SPEN in RCSTA register == 1 ● RX9D is used in 9-bit mode when RX9 in RCSTA == 1
  • 193.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 193 Receiver Error Detection ● Framing error – Indicated by FERR bit of RCSTA register – Does not generate interrupt – FERR bit cleared by reading received data – FERR bit canbe cleared by clearing SPEN bit of RCSTA register ● Overrun error – Indicated by OERR bit of RCSTA register – No additional data will be received until the OERR bit is cleared – Cleared by: ● Clearing CREN bit of RCSTA register ● Clearing the SPEN bit of RCSTA register
  • 194.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 194 9-Bit Mode ● TX9D must be written before writing to TXREG. ● RX9D must be read before reading RCREG.
  • 195.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 195 Address Detection
  • 196.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 196 TXSTA Register Bit Description CSRC Clock Source Select TX9 9-bit Transmit Enable TXEN Transmit Enable SYNC EUSART Mode Select Bit Description SENDB Send Break Character BRGH High Baud Rate Select TRMT Transmit Shift Register Status TX9D Ninth bit of Transmit Data
  • 197.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 197 RCSTA Register Bit Description SPEN Serial Port Enable RX9 9-bit Receive Enable SREN Single Receive Enable CREN Continuous Receive Enable Bit Description ADDEN Address Detect Enable FERR Framing Error OERR Overrun Error RX9D Ninth bit of Receive Data
  • 198.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 198 ESUART Baud Rate Generator ● Beside above registers, BRGH bit of TXSTA register and BRGH16 bit of BAUDCTL register affect clock frequency
  • 199.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 199 EUSART Baud Rate Formula ● n == SPBRG16:SPBRG
  • 200.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 200 BAUDCTL Register Bit Description ABDOVF Auto-Baud Detect Overflow RCIDL Receive Idle Flag SCKP Synchronous Clock Polarity Select Bit Description BRG16 16-bit Baud Rate Generator WUE Wake-up Enable ABDEN Auto-Baud Detect Enable
  • 201.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 201 Master Synchronous Serial Port Module
  • 202.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 202 SPI ● MSSP module uses 4 registers: – SSPSTAT – status register – SSPCON – control register – SSPBUF – buffer register – SSPSR – shift register (not directly available)
  • 203.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 203 I2 C
  • 204.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 204 I2 C cont’d ● MSSP module uses 6 registers: – SSPCON – control register – SSPCON2 – control register2 – SSPSTAT – status register – SSPBUF – buffer register – SSPSR – shift register (not directly available) – SSPADD – address register
  • 205.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 205 SSPSTAT Register Bit Description SMP Sample State in SPI or Slew Rate in I2 C CKE Clock Edge Select D/A Data/Address flag in I2 C P Stop bit in I2 C Bit Description S Start bit in I2 C R/W Read/Write in I2 C UA Update Address in 10-bit I2 C BF Buffer Full Status
  • 206.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 206 SSPCON Register Bit Description WCOL Write Collision Detect in I2 C SSPOV Receive Overflow Indicator SSPEN Synchronous Serial Port Enable CKP Clock Polarity Select in SPI Bit Description SSPMx Synchronous Serial Port Mode Select
  • 207.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 207 Synchronous Serial Port Mode Select
  • 208.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 208 SSPCON2 Register Bit Description GCEN General Call Enable in I2 C Slave Mode AKSTAT Acknowledge Status in I2 C Master Transmit Mode AKDT Acknowledge Data in I2 C Master Receive Mode ACKEN Acknowledge Sequence Enable in I2 C Master Receive Mode Bit Description RCEN Receive Enable in I2 C Master Mode PEN STOP condition Enable in I2 C Master Mode RSEN Repeated START Condition Enabled in I2 C Master Mode SEN START Condition Enabled/Stretch Enabled in I2 C Master Mode
  • 209.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 209 I2 C in Master Mode Initialization Data Transmission Data Reception
  • 210.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 210 MSSP Baud Rate Formula
  • 211.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 211 Final Notes about MSSP ● In case of WCOL, SSPBUF is not written. WCOL must be cleared by SW. ● BF is automatically cleared when SSPBUF is consumed. ● SDA and SCL pins turn into Open Drain outputs. – They must be connected to externally to pull-up resistors.
  • 212.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 212 Long TTC Task Problem
  • 213.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 213 Example: Long TTC Task Problem
  • 214.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 214 Example: Display Elapsed Time ● Send time elapsed since system power on over a serial link
  • 215.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 215 Example: Display Elapsed Time Listing
  • 216.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 216 Example: Display Elapsed Time Listing cont’d
  • 217.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 217 Example: Display Elapsed Time Listing cont’d
  • 218.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 218 Example: Display Elapsed Time Listing cont’d
  • 219.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 219 Example: Display Elapsed Time Listing cont’d
  • 220.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 220 Example: Display Elapsed Time Listing cont’d
  • 221.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 221 Example: Display Elapsed Time Listing cont’d
  • 222.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 222 Example: Display Elapsed Time Listing cont’d
  • 223.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 223 Example: Display Elapsed Time Listing cont’d
  • 224.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 224 Example: Display Elapsed Time Listing cont’d
  • 225.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 225 Example: Display Elapsed Time Listing cont’d
  • 226.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 226 Lab 8: Data Acquisition ● Complete the missing code. ● Make sure it runs!
  • 227.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 227 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 228.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 228 Capture Compare Modules ● 2 CCP module allows user to time and control events in 3 modes: – Capture: access TMR1 changing register – Compare: compare TMR1 register with another register – PWM: generate a varying duty cycle signal ● Controlled by CCP1CON register
  • 229.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 229 CCP1 in Capture Mode ● TMR1 must operate as timer or synchronous counter. ● It should be switched OFF before (re-)configuration to avoid unwanted interrupts.
  • 230.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 230 CCP1 in Compare and PWM Modes ● Compare mode – TMR1 must be synchronized w/ internal clock. ● PWM mode
  • 231.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 231 PWM C/C’s ● Period = (PR2 +1) * 4Tosc * TMR2 Prescale Value ● Pulse Width = Pulse Width = (CCPR1L,DC1B1,DC1B0) * Tosc * TMR2 Prescale Value – If pulse width > period, output will be always 1. ● Resolution – @ fixed frequency, it is the number of duty cycle combinations we can get
  • 232.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 232 CCP1CON Register Bit Description P1Mx PWM Output Configuration DC1Bx PWM Duty Cycle Least Significant bits CCP1Mx CCP1 Mode Select
  • 233.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 233 CCP1CON Register cont’d
  • 234.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 234 CCP1CON Register cont’d
  • 235.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 235 CCP2 Module ● True difference from CCP1 in capture mode
  • 236.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 236 CCP2CON Register Bit Description DC2Bx PWM Duty Cycle Least Significant bits CCP2Mx CCP2 Mode Select
  • 237.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 237 CCP2CON Register cont’d
  • 238.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 238 CCP2CON Register cont’d
  • 239.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 239 Single Output PWM Mode ● 1 PWM signal can be simultaneously available on maximum of 4 different output pins basic or inverted. – In inverted mode, similar pulses are generated in pairs: (P1A, P1C) and(P1B, P1D).
  • 240.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 240 Half-Bridge Mode ● PWM output on P1A, and its complementary on P1B – It is necessary to provide a short delay between switching drivers on and off by using PDC0-PDC6 of PWM1CON register.
  • 241.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 241 Full-Bridge Mode ● All four pins are used as outputs in 2 configurations: forward and reverse
  • 242.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 242 Full-Bridge Forward and Reverse Configurations ● Forward ● Reverse
  • 243.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 243 PWM1CON Register Bit Description PRSEN PWM Restart Enable PDCx PWM Delay Count
  • 244.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 244 PSTRCON Register Bit Description STRSYNC Steering Sync STRx Steering Enable x
  • 245.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 245 ECCPAS Register Bit Description ECCPASE ECCP Auto-Shutdown Event Status ECCPASx ECCP Auto-Shutdown Source Select PSSAC1, PSSAC0 Pins P1A, P1C Shutdown State Control PSSBD1, PSSBD0 Pins P1B, P1D Shutdown State Control
  • 246.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 246 ECCPAS Register cont’d
  • 247.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 247 ECCPAS Register cont’d
  • 248.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 248 A/D Converter ● 10-bit successive approximation result is stored in ADRESL and ADRESH registers ● 14 separate analog inputs – Pins should be configured as analog inputs by TRIS and ANSEL (ANSELH) registers ● -ve and + ve reference voltages
  • 249.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 249 A/D Converter cont’d ● Controlled by 4 registers: – ADRESH - high byte of conversion – ADRESL - low byte of conversion – ADCON0 - control register 0 – ADCON1 - control register 1
  • 250.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 250 A/D Converter cont’d ● ADFM bit of ADCON1 register controls adjustment. ● For ADC accuracy, it is necessary to provide time delay between selecting analog input and measurement itself. – This time is called ‘acquisition time’ ~ 20 µs ●
  • 251.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 251 A/D Clock Period ● TAD == time needed to complete a bit conversion – @ least 1.6 µs – 10-bit A/D takes around 11 TAD. ● Clock frequency and source are controlled by ADCS1 and ADCS0 bits in ADCON0 register.
  • 252.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 252 A/D Clock Period cont’d
  • 253.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 253 ADCON0 Register Bit Description ADCSx A/D Conversion Clock Select CHSx Analog Channel Select Bit Description GO/DONE A/D Conversion Status ADON A/D Enable
  • 254.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 254 ADCON1 Register Bit Description ADFM A/D Result Format Select VCFGx Voltage Reference Select
  • 255.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 255 Analog Comparators ● 2 comparators – Inputs: RA03 – RA04 – Outputs: RA4 and RA5 ● Controlled by the following registers: – CM1CON0 is in control of comparator C1 – CM2CON0 is in control of comparator C2 – CM2CON1 is in control of comparator C2 ● Work during SLEEP mode and can wake-up µ- controller
  • 256.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 256 Analog Comparators cont’d ● Internal Vref exists for comparators and A/D. – 2 sources – 2 ranges – Can output final CVref ● RA2 – Simple D/A!!! ● Any comparator’s output can causes interrupt. – Checked by CMIF of PIR register – If CMIE of PIE register and PEIE and GIE bits of INTCON register are set
  • 257.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 257 Analog Comparators cont’d
  • 258.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 258 CM1CON0 Register Bit Description CxON Comparator Cx Enable CxOUT Comparator Cx Output CxOE Comparator Cx Output Enable CxPOL Comparator Cx Output Polarity Select Bit Description CxR Comparator Cx Reference Select CxCHy Comparator Cx Channel Select
  • 259.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 259 CM2CON1 Register Bit Description MCxOUT Mirror Copy of CxOUT CxRSEL Comparator Cx Reference Select T1GSS Timer1 Gate Source Select C2SYNC Comparator C2 Output Synchronization
  • 260.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 260 Clock Oscillator ● The oscillator mode is selected by the bits of bytes, called Config Word, sent during programming.
  • 261.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 261 OSCCON Register Bit Description IRCFx Internal Oscillator Frequency Select OSTS Oscillator Start-up Time-out Status Bit Description HTS HFINTOSC Status LTS LFINTOSC Stable SCS System Clock Select
  • 262.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 262 External Oscillator in EC Mode ● Up to 20 MHz ● Advantages: – OSC2 is available as IO pin. – No frequency stabilization – Synchronize µ-controller w/ board electronics – Temp external clock disabling stops device while leaving all data intact. ● After restarting external clock, the device proceeds with operation as if nothing has happened.
  • 263.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 263 External Oscillator in LP, XT Or HS Modes ● Quartz crystal or ceramic resonators – Ceramics are cheaper but less stable ● LP (low power) mode – Drive only 32.768 kHz crystals ● XT mode – Intermediate frequency up to 8 MHz ● HS (high speed) mode – High frequency > 8 MHz
  • 264.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 264 External Oscillator in RC and RCIO Modes ● Much cheaper than resonators ● RC mode – OSC2 outputs RC frequency/4. ● RCIO mode – OSC2 is available as IO pin. ● f = 1/T= 1/(R*C)
  • 265.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 265 Internal Oscillator Modes ● INTOSC mode – OSC1 is available as IO. – OSC2 outputs RC frequency/4. ● INTOSCIO mode – Both are available as IO pins.
  • 266.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 266 2-Speed Clock Start-Up Mode ● For power saving if µ-controller goes to SLEEP mode and configured in LP, XT or HS mode. ● During SLEEP, external oscillator is switched off. ● After wake-up, 1024 pulses are wasted before execution. ● Internal oscillator is used for 1024 instead of external one. ● Controlled by Int-Ext Switchover option
  • 267.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 267 Fail-Safe Clock Monitor ● Monitors external oscillator and allows µ- controller to proceed with execution if external oscillator fails – Internal oscillator takes over. ● Can generate interrupt in case of failure ● External source takes over if reset successfully. ● Controlled by Fail-Safe Clock option
  • 268.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 268 OSCTUNE Register
  • 269.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 269 EEPROM Memory ● 256 data locations controlled by: – EECON1 - control register – EECON2 - control register – EEDAT - data register – EEADR - address register ● Take care, shared with program flash.
  • 270.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 270 EECON1 Register Bit Description EEFGD Program/Data EEPROM Select WRERR EEPROM Error Flag WREN EEPROM Write Enable Bit Description WR Write Control RD Read Control
  • 271.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 271 Reading and Writing to EEPROM ● Reading 1. Write address in EEADDR. 2. Clear EEPGD. 3. Set RD. 4. Data is ready in EEDAT. ● Writing 1. Write address in EEADDR. 2. Put data in EEDAT. 3. Clear EEPGD. 4. SET WREN. 5. Disable interrupts. 6. Write 0x55 then 0xAA to EECON2. 7. Set WR. 8. Enable interrupts. 9. Clear WREN.
  • 272.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 272 Reset ● Can be external or internal ● µ-controller takes time after power before it can operate. – PWRT and OST internal timers monitor Treset. – PWRT is configurable during flashing. – PWRT resets µ-controller @ VCC = 1.2V – 1.7V. – After 72 ms, OST resets µ-controller for 1024 quartz cycles. – @ the end if MCLR high, µ-controller runs.
  • 273.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 273 Reset cont’d ● Black-out reset when power goes off. ● Brown-out when power goes off slowly. – Really dangerous – Stored in on-chip memory ● Noise is special case of brown-out.
  • 274.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 274 Lab 9: Miscellaneous Interfacing ● Complete the missing code. ● Make sure it runs!
  • 275.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 275 Outline ● Introduction ● Programming for Embedded Systems ● PIC16F887 ● IO Ports and Simple Interfacing ● Object Orientation in C ● Meeting Real-Time Constraints ● Creating a Small Embedded Operating System ● State Machine Design ● Serial Communication ● Miscellaneous Interfacing ● Case Study: Hazard and Turning Indicators
  • 276.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 276 Lab 10: Car Hazard/Turning Indicators ● Using the techniques in this course to design and implement Car Hazard Turning Indicator. ● System specs are described in the handout requirements document.
  • 277.
    Jul 6, 2016Introduction to Embedded Systems - A Practical Approach 277 ● To contact us: – www.swift-act.com – training@swift-act.com – (+2)0122-3600-207