Embedded systems. Exercise session 3. Microcontroller Programming Lab Preparation

Similar documents
Embedded Systems Lab 2 - Introduction to interrupts

EEE394 Microprocessor and Microcontroller Laboratory Lab #6

Laboratory Exercise 5 - Analog to Digital Conversion

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

Embedded Systems Module. 6EJ505. C Tutorial 3: using the ICD3 rev tjw

Laboratory 10. Programming a PIC Microcontroller - Part II

PIC Microcontroller Introduction

Laboratory: Introduction to Mechatronics

Laboratory 9. Programming a PIC Microcontroller - Part I

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet Example Application...

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

Infineon C167CR microcontroller, 256 kb external. RAM and 256 kb external (Flash) EEPROM. - Small single-board computer (SBC) with an

acret Ameya Centre for Robotics & Embedded Technology Syllabus for Diploma in Embedded Systems (Total Eight Modules-4 Months -320 Hrs.

C and Embedded Systems. So Why Learn Assembly Language? C Compilation. PICC Lite C Compiler. PICC Lite C Optimization Results (Lab #13)

Microcontroller systems Lec 2 PIC18LF8722 Microcontroller s s core

ME 6405 Introduction to Mechatronics

Chapter 11: Interrupt On Change

A. This Errata sheet represents the known bugs, anomalies and work-arounds for the ADuC812 MicroConverter.

Introduction to Microcontroller Apps for Amateur Radio Projects Using the HamStack Platform.

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

8051 Microcontroller

Embedded System Design

1 Introduction to Computers and Computer Terminology Programs Memory Processor Data Sheet... 4

8051 Microcontroller Interrupts

Capture Mode of Pic18F252

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

Using Timers of Microchip PIC18F Microcontrollers

Getting acquainted with the development tools June 27, 2006 ELE492 Embedded System Design Exercise 1

Introduction to Embedded Systems

CENG-336 Introduction to Embedded Systems Development. Timers

Understanding the basic building blocks of a microcontroller device in general. Knows the terminologies like embedded and external memory devices,

Contents. The USB Logic Tool... 2 Programming... 2 Using the USB Logic Tool... 6 USB Logic Tool Features... 7 Device Hardware...

Speed Control of a DC Motor using Digital Control

Embedded Systems Programming and Architectures

Microprocessors B (17.384) Spring Lecture Outline

Emulating an asynchronous serial interface (ASC0) via software routines

Microchip 18F4550 Interface, Signal conditioning, USB, USB- RS-232, 16x2 LCD Interface

Manual of Board ET-PIC STAMP 18F8722-K22 ET-PIC STAMP 18F8722-K22

MICROPROCESSORS B (17.384) Spring Lecture Outline

Lecture (03) PIC16F84 (2)

SquareWear Programming Reference 1.0 Oct 10, 2012

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

Microcontroller Overview

TKT-3500 Microcontroller systems

AP16050 SAB C161V/K/O. Emulating an asynchronous serial interface (ASC) via software routines. Microcontrollers. Application Note, V 1.0, Feb.

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

MICROPROCESSORS B (17.384) Spring Lecture Outline

Introduction to ARM LPC2148 Microcontroller

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 1.

The MCU s Pulse. Internal clock or oscillator to synchronize operation. One clock cycle = 1 TOSC = 1/fOSC. t TOSC

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

Microprocessors B Lab 1 Spring The PIC24HJ32GP202

DERTS Design Requirements (1): Microcontroller Architecture & Programming

DEV-1 HamStack Development Board

Question Bank Microprocessor and Microcontroller

The Freescale MC908JL16 Microcontroller

PIC32&Overview& E155&

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

Department of Electronics and Instrumentation Engineering Question Bank

Getting Started with InnoSwitch TM 3-Pro Code Library using PIC16F18325

Embedded Systems Lab Lab 1 Introduction to Microcontrollers Eng. Dalia A. Awad

MPLAB SIM. MPLAB IDE Software Simulation Engine Microchip Technology Incorporated MPLAB SIM Software Simulation Engine

M16C/62P QSK QSK62P Plus Tutorial 1. Software Development Process using HEW4

Renesas 78K/78K0R/RL78 Family In-Circuit Emulation

Diploma in Embedded Systems

Physics 335 Intro to MicroControllers and the PIC Microcontroller

University of Hawaii EE 361L MPLab Quick Tutorial and Project 2.1 Last updated September 1, 2011

MCS-51 Serial Port A T 8 9 C 5 2 1

CPEG300 Embedded System Design. Lecture 8 Timer

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING MSC SYSTEMS ENGINEERING AND ENGINEERING MANAGEMENT SEMESTER 2 EXAMINATION 2016/2017

LCD Display. Other I/O. LCD display Flash ROM SPI EPROM Keyboard (PS/2) UART connectors DAC ADC. 2-line, 16 character LCD display

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

Lecture (04) PIC 16F84A programming I

Week1. EEE305 Microcontroller Key Points

I Introduction to Real-time Applications By Prawat Nagvajara

EECS 373 Midterm 2 Fall 2018

CORTEX Microcontroller and Joystick User Guide

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

Learning Objectives:

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

AN10210 Using the Philips 87LPC76x microcontroller as a remote control transmitter

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING. BEng(Hons) Electrical and Electronics Engineering SEMESTER 1 EXAMINATION 2016/2017

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

PIC KIT 2 BASIC-USERS GUIDE FEMTO ELECTRONICS

Interrupts on PIC18F252 Part 2

Ali Karimpour Associate Professor Ferdowsi University of Mashhad

PIC 16F84A programming (II)

Unlocking the Potential of Your Microcontroller

The University of Texas at Arlington Lecture 21_Review

PIC Discussion By Eng. Tamar Jomaa

MICROPROCESSORS A (17.383) Fall Lecture Outline

Section 14. Timer1 HIGHLIGHTS. Timer1. This section of the manual contains the following major topics:

The Atmel ATmega168A Microcontroller

Mechatronics and Measurement. Lecturer:Dung-An Wang Lecture 6

More PIC Programming. Serial and parallel data transfer External busses Analog to digital conversion

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: 8051 Architecture Module No: CS/ES/5 Quadrant 1 e-text

Transcription:

Embedded systems Exercise session 3 Microcontroller Programming Lab Preparation

Communications Contact Mail : michael.fonder@ulg.ac.be Office : 1.82a, Montefiore Website for the exercise sessions and the project : http://www.montefiore.ulg.ac.be/~mfonder/info0064/ Lab Session First lab session Fri 27th Oct. Duration : one hour starting from 10h30 or 11h30 depending on your position in a schedule to be published. Same groups than for the project. Preparation required. Statement to be uploaded soon. Note: the preparation should take more or less one our of your time if tasks are split cleverly between the group members. Toolboxes will be made available the day of the lab 2

Microcontroller Programming The most interesting property of microcontrollers is obviously their capacity to reprogrammed in order to execute some particular tasks. In order to do this, each microcontroller has its own instruction set which can be used by the programmer to perform the desired tasks. Assembly Code As all other registers, the special function registers can be modified during the program execution. Microcontroller programming is a low level programming task. If some manufacturers provide some C compilers to ease this, it is often advised to stick to assembly for simple programs. At the opposite of C, assembly code does indeed offer a full control on what is performed on the microcontroller at each time instant. 3

Data Memory Organization Data memory uses a 12-bit address. The upper five bits of the address define the Bank address and the lower seven bits select the registers/ram in that bank. The data memory is partitioned in 32 memory banks with 128 bytes in a bank. Each bank consists of : 12 core registers 20 Special Function Registers (SFR) Up to 80 bytes of General Purpose RAM (GPR) 16 bytes of common RAM The active bank is selected by writing the bank number into the Bank Select Register (BSR). Unimplemented memory will read as 0. All data memory can be accessed either directly (via instructions that use the file registers) or indirectly via the two File Select Registers (FSR). 4

Special Function Registers Aim Special function registers (SFR) are specific slots of the data memory (~RAM) which hold the state of the device and its configuration. Note As all other registers, the special function registers can be modified during the program execution by overwriting them. This is done by first selecting the correct memory bank, then writing the at the corresponding address. Use Program execution IO ports configuration Internal properties configuration (interrupts, timers, A/D converters, PWM, ) Flags 5

Watchdog Timer and Master Clear Aim Both functionalities are used to reset the microcontroller in its original state. Watchdog Timer Timer which is incremented at a given rate (based on clock transitions) and which resets the microcontroller when it reaches the overflow Used to cure potential dead- or livelocks during tasks execution Master Clear Pin which resets the microcontroller when its input goes to 1 Used to manually reset the microcontroller Note If those functionalities are misused, they can lead to some unpredictable program behaviour 6

I/O ports Problem Each microcontroller has a finite number of pin. Each of them can handle a subset of specific tasks which depend on their location. => for a given application there is a necessity to chose a microcontroller which can handle all requirements. Solution Choose a microcontroller based on its specifications (those can be found in the datasheet) Example PIC16F1789 : pin RB2 Digital IO Analogic input for channel 8 of ADC, Analogic ouput for channel 3 of DAC Inverting input for op-amp 2 7

I/O ports configuration Idea Each pin of the microcontroller can be used for one or several purposes, but it can only be assigned to one task at a time => each pin needs to be configured Way to proceed The configuration is made by setting bits in some registers to a value given by the datasheet of the microcontroller. Example with the PIC16F1789 How can I set RD1 as a 1 digital output and RA0 as an input for an A/D converter? 8

I/O ports configuration Idea Each pin of the microcontroller can be used for one or several purposes, but it can only be assigned to one task at a time => each pin needs to be configured Way to proceed The configuration is made by setting bits in some registers to a value given by the datasheet of the microcontroller. Example with the PIC16F1789 How can I set RD1 as a 1 digital output and RA0 as an input for an A/D converter? TRISDbits.RD1 = 0; LATDbits.RD1 = 1; TRISAbits.RA0 = 1; Note By default all I/O pins of a Microchip 16Fxxxx microcontroller are set as a 0 digital output. 9

Clock configuration Idea Once the hardware is equipped with an oscillator, the microcontroller should by parametrized in order to use this oscillator as clock signal. Way to proceed As for the I/O configuration : read the datasheet and set values in registers. Example with the PIC16F1789 How can I set the microcontroller to use its internal oscillator at 1MHz in pair with the integrated frequency multiplier (4x)? FOSC2:FOSC0 = 0b100 is configured outside of the program OSCTUNE = 0b*0000000 OSCCON = 0b11011*1* Note Clock frequency Number of instructions per second 10

Timer configuration Idea To manage timing, it is sometimes necessary to keep track of the time by counting the clock cycles or the transitions on a given pin. This is the role of the timers. Important parameters : Number of bits of the timer : defines the maximum timer value possible Prescaler value : reduces the timer incrementation speed by a given factor Example with the PIC16F1789 How can I configure the microcontroller to count a period of 0.5s with a clock at 1MHz? 0.5s == 500,000 clock cycles or 125,000 instructions too much for 16bits need of a prescaler 125,000/65,535 = 1.9 prescaler = 1:2 Timer1 is the only solution for the PIC 16F1789 : T1CON = 0b000100*1 Counter value stored in TMR0H and TMR0L 0.5s == 125,000/2 = 62,500 timer value 11

Interrupts Jump to 0x0008 in the program memory Instruction i-1 Save context Event occurs triggering an interrupt request Instruction i Instruction i+1 Determine interrupt source Execute corresponding interrupt routine time Restore context 12

Interrupts All interrupt sources are enabled by a certain bit in a SFR (the bit GIE in the INTCON register) Many interrupt sources = Timers, input pin, UART, ADC, etc. Interrupt sources have three bits (within SFR) to control their operation: Flag bit: indicating that interrupt event occurred Enable bit: allowing the program to jump to the interrupt vector when the flag bit is set Priority bit : indicating if the corresponding interrupt has high or low priority If a flag bit is set (as well as the corresponding enable bit), the MCU jumps automatically to the interrupt vector. 13

Interrupts When using interrupts, remember: Clear the interrupt flag before enabling the interrupt Check if the interrupt is enabled before checking its interrupt flag Once in the interrupt code, first thing to do: clear the interrupt flag (it is not done automatically!) 14

A/D Converter Concept The A/D converter converts an input votage into a binary number according to a reference voltage : How does it work? The conversion is made with successive comparisons with the reference voltage. V in [V] Sample & Hold Comparator V ref [V] D/A converter Control logic Note : V ref MUST be smaller or equal to V dd 15

A/D Converter Sample & Hold circuit Concept Samples the input signal by charging a capacitor and holds the value of the voltage constant during the time of the conversion How does it work? Sampling Switch V S&H V IN Source E S R S R IC R SS C HOLD V IN V S&H t Note The switch should remain closed long enough for the capacitor to fully charge. This charging period is called the minimum acquisition time and is proportional to τ = (R S +R IC +R SS )C HOLD R S should be as small as possible! 16

A/D Converter Converter circuit How does it work? VDA = (register value/ 2 10 )*V REF Initially, the register is cleared (VDA = 0 V) Starting from the MSB to the LSB, current bit is set : If VDA < V S&H, current bit stays set If VDA > V S&H, current bit is clear At the end, the value stored in the register corresponds to the converted voltage LSB bit0 bit1 bit2 V ref [V] V S&H bit3 Comparator Control D/A logic V DA bit4 bit5 bit6 bit7 bit8 bit9 MSB 17

A/D Converter 4-bit converter example V RE V S&H V DA = (register/2 4 ) * V ref F V DA MSB LSB 1 0 0 0 1 1 0 0 V DA = V ref /2, V S&H > V DA bit3 = 1 V DA = (3/4)*V ref, V S&H > V DA bit2 = 1 1 1 0 1 MSB LSB t 1 1 1 0 V DA = (7/8)*V ref, V S&H < V DA bit1 = 0 1 1 0 1 V DA = (13/16)*V ref, V S&H > V DA bit0 = 1 Note When the possible input voltage variation is limited to a known range, you can change the voltage references to increase the resolution of the conversion. You cannot use references which are out of the Vdd-Vss range! 18

A/D Converter Example : PIC18F4620 19

A/D Converter Configuration example : PIC18F4620 1. Configure Port: Disable pin output driver (Refer to the TRIS register) Configure pin as analog (Refer to the ANSEL register) Disable weak pull-ups 2.Configure the A/D module Configure analog pins, voltage reference and digital I/O (ADCON1) Select A/D input channel (ADCON0) Select A/D acquisition time (ADCON2) Select A/D conversion clock (ADCON2) Turn on A/D module (ADCON0) 3. Configure A/D interrupt (if desired): Clear ADIF bit Set ADIE bit Set GIE bit 4. Wait the required acquisition time (if needed). 5. Start conversion: Set GO/DONE bit (ADCON0 register) 6. Wait for A/D conversion to complete, by either: Polling for the GO/DONE bit to be cleared OR Waiting for the A/D interrupt 7. Read A/D Result registers (ADRESH:ADRESL); clear bit ADIF, if required. 8. For next conversion, go to step 1 or step 2, as required. The A/D conversion time per bit is defined as T AD. A minimum wait of 2 T AD is required before the next acquisition starts. All of this is given in the datasheet! 20

PWM Concept PWM == Pulse Width Modulation : 2 parameters : TPWM = period of the signal d = duty cycle : Proportion of the period which remains high Average voltage : V out d 25% t V out T PWM d 90% t 21 T PWM

PWM Integrated generator Concept Some microcontrollers contain PWM modules which automatically generate a PWM signal on one of its output pins. How does it work? The period and the duty cycle are set by registers. The period of the signal is set to be the period of a timer. At each time instant, the duty cycle value is compared to the timer value to set the output of the pin. Note The resolution of the duty cycle is dependent of the MCU clock frequency and of the frequency of the desired PWM signal : 22

PWM Setup example for PIC18F4620 Operations 1) Set the PWM period by writing to the PR2 register. 2) Set the PWM duty cycle by writing to the CCPRxL register and CCPxCON<5:4> bits. 3) Configure the CCPx module for PWM operation. 4) Configure and start Timer2. 5) Make the CCPx pin an output by clearing the appropriate TRIS bit. All of this is given in the datasheet! 23

Capture module Concept Sometimes, it can be interesting to now exactly when an external signal was triggered on a pin. This can be done thanks to a capture module. How does it work? When a falling or a rising edge (one or the other, not both) is detected on the dedicated pin, the value of a given timer at this moment is copied inside of a register. An interrupt can be triggered once a capture occurs Note Be careful that if two successive triggers of the pin are too spaced in time, several timer overflows may have been reached during this time. Be careful with input bounces! 24

Compare module Concept Sometimes, it can be interesting to modify the value of an output pin at a precise moment. This can be done thanks to a compare module. How does it work? The user set a value in a dedicated register. This value is constantly compared with a given timer. When the timer value matches the user value, an output pin can be triggered high or low depending on the user need. An interrupt can be triggered when a compare event occurred Note Keep in mind that the time required for a timer to reach a given value may depend of the rest of your code (timer resets for example). 25

UART Concept The Universal Asynchronous Receiver Transmitter (UART) is used for transmitting digital informations between two devices. How does it work? The communication is made thanks to two wires, where each wire is used to carry the communication in one direction. Note The transmission speed (baudrate == bits/s) is set by the user and should be the same for both devices. 26

UART Communication protocol for a line 1 St Start bit, always low 2 (n) Data bits (5 to 8) 3 P Parity bit (optional), can be even or odd 4 Sp Stop bit, always high 5 IDLE No transfer on the line, always high 27

UART Inside of the microcontroller l Configuration Set UART configuration registers for your need Set baudrate of the line Enable interrupts if desired Transmission Load data to transmit in the dedicated register When the stop bit of the previous transmission is reached: Transfer of the data to the transfer register (interrupt can be triggered for this event) Flag is raised to tell that the transfer register is full Transmission begins 28

UART Inside of the microcontroller ll Reception Flag is raised when some data reception completed (an interrupt corresponding to this flag can be triggered is desired) Collect the received data by reading the register which temporarily hold them Determine if any errors occurred during the reception and clear corresponding flags if necessary 29

PIC Programming Basic idea 30

PIC Programming IDE Idea The IDE is a software which helps you to write, compile and debug a program MPLABX IDE for microcontrollers from Microchip. Available for Windows, Linux, OSx Enables you to directly write a program in MPASM (assembly) Enables you to program in C if you install a dedicated compiler : For 8bits microcontrollers (PIC16Fxxxx) : XC8 compiler The XC8 compiler comes with a complete documentation in its installation folder (xc8/1.3x/docs for example) MPLABX You can find a lot of documentation on the MPLAB environment online Links to the IDE on the website 31

PIC Programming Programmer Idea The compiled program needs to be uploaded in the microcontroller. This is done by using a programmer Microchip programmers Pickit 3 Inexpensive (~40$) Low programming speed Available in the toolbox ICD3 Expensive (~400$) High programming speed Enables to set software breakpoints (useful for debugging) Available in the lab (R100) Pickit 3 ICD3 32

PIC Programming Programmer PIC For the connection between the ICD3 and the MCU, you have to look at the datasheet (of both the ICD3 and the MCU). RJ 11 USB to PC Note In order to be flashed, the microcontroller should be powered (either by an external source or by the ICD3) 33

PIC Programming Walkthrough What to do step-by-step: 1)Download and install MPLAB X (MPASM comes with it) 2)Launch MPLAB and create a new project (specify your PIC, your compiler and your programmer) 3)Add the linker file which corresponds to your PIC (located in MPLABX installation path /vx.xx/mpasmx/lkr/ directory) 4)Right click on Source Files folder >> New >>.asm file 5)Write your program 6)Connect the programming tool and your PIC >> power up your PIC >> plug the programming tool to your PC (you can also use it to power up your PIC) 7)Click on Make and program device 34

PIC Programming Instruction set The microcontroller understands only binary instructions. Those instructions can be obtained by two different means. The first one is to get them out of the binary file produced by a compiler from a higher level programming language and the second one is to code as close as possible to the hardware by using assembly. The idea behind assembly code is to have a simple set of instructions which can be directly transformed into opcode, i.e. instructions for the microcontroller. Each microcontroller has its own instruction set. The latest being documented in the datasheet. In the case of the PIC16f1789 : only 49 instructions documented in section 30. The advantage of assembly is the close control on what is performed at each time instant. The drawback being that with only 49 instructions simple operations such as multiplications and divisions have to be reimplemented. 35

PIC Programming Example Program structure ; header files processor 16f1789 ; configuration bits settings ; Set main code entry point org 0x00 nop goto code_start ; declare interrupt vector org 0x08 goto interrupt_vector Declares all device specific types and variables, so accessing some SFRs consists in read/write some variables Examples: TRISB = 0; LATBbits.LATB3 = 1; You can check the names of those variables in the file in MPLAB X: Window >> PIC Memory Views >> SFRs 36

PIC Programming Example Configuration bits For the different configuration bits which can be parametrized for each PIC18F, look at the datasheet or at the file xc8/1.3x/docs/pic16_chipinfo.html MPLABX : Window >> PIC memory views >> configuration bits, set everything, then generate a config file. Example CONFIG OSC = INTIO67 CONFIG MCLRE = ON CONFIG BOREN = OFF CONFIG LVP = OFF CONFIG WDT = OFF ; Internal oscillator block ; Active master clear ; Disable Brown-out Reset ; Low voltage icps off ; Watchdog off 37

PIC Programming Example Interrupt vector interrupt_vector: ; check flags to find the source of the interrupt Interrupt vectors for interrupt with priority void interrupt low_priority MyLowIntVec(void) { // check flags to find the source of the interrupt } void interrupt high_priority MyHighIntVec(void) { // check flags to find the source of the interrupt } 38

Conclusion for today Next week Lab session Next session The datasheet is your friend! How to choose the appropriate software architecture Note Feedbacks on the exercise sessions are still welcome! 39