Programming Microcontroller

Similar documents
Introduction to Embedded Systems

Microcontroller basics

ECE 353 Lab 4. General MIDI Explorer. Professor Daniel Holcomb Fall 2015

Programming Microcontroller Assembly and C

Robosoft Systems in association with JNCE presents. Swarm Robotics

INTERRUPTS in microprocessor systems

Lab Course Microcontroller Programming

Software debouncing of buttons

FPGAs in a Nutshell - Introduction to Embedded Systems-

AVR XMEGA TM. A New Reference for 8/16-bit Microcontrollers. Ingar Fredriksen AVR Product Marketing Director

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

PART 1 : MR-162. PART 2 : CPU Board. PART 3 : Software Tools. PART 4 : Compile and Download. 1. Introduction 2. Features

MICROPROCESSOR BASED SYSTEM DESIGN

Embedded programming, AVR intro

7KLVSDJHDQGDOOOLQNHGGR XPHQWVKDYHEHHQZULWWHQE\/HLWQHU+DUDOG

AVR Microcontrollers Architecture

ET-BASE AVR ATmega64/128

Systematic Hardware Platform Selection - Introduction to Embedded Systems-

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

CN310 Microprocessor Systems Design

ARDUINO MEGA INTRODUCTION

Layman definition: Gadgets and devices Technical definition: Self-controlled devices Usually, such systems consist of I/O (input/output) devices such

C Programming in Atmel Studio 7 Step by Step Tutorial

INTERFACING HARDWARE WITH MICROCONTROLLER

8-bit Microcontroller with 2K Bytes of Flash. ATtiny28L ATtiny28V

8-bit Microcontroller with 1K Byte of In-System Programmable Flash AT90S1200

Introduction to Micro-controllers. Anurag Dwivedi

VLSI Design Lab., Konkuk Univ. Yong Beom Cho LSI Design Lab

AVR Timers TIMER0. Based on:

,$5$SSOLFDWLRQ1RWH$95 (IILFLHQWSURJUDPPLQJRI$WPHO V $95 PLFURFRQWUROOHUV

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

An FTDI connection: The ATtiny microcontrollers don t have a hardware UART External Crystal header pins for an optional crystal

AVR Training Board-I. VLSI Design Lab., Konkuk Univ. LSI Design Lab

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

Installation and Maintenance

2. Tutorial ESC Programming myavr MK2 USB UFO Doctor, June 5 rd, 2010

PART 1 : MR-16. PART 2 : CPU Board. PART 3 : Software Tools. PART 4 : Compile and Download. 1. Introduction 2. Features

ATmega128. Introduction

8051 Microcontroller

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

Microcontroller basics

Microprocessors And Microcontrollers (Practical)

EE 308: Microcontrollers

TIMSK=0b ; /* enables the T/C0 overflow interrupt in the T/C interrupt mask register for */

Microprocessors & Interfacing

Clock and Fuses. Prof. Prabhat Ranjan Dhirubhai Ambani Institute of Information and Communication Technology, Gandhinagar

Arduino Uno R3 INTRODUCTION

Introduction to the MC9S12 Hardware Subsystems

An Arduino Controlled 1 Hz to 60 MHz Signal Generator

AGH University of Science and Technology Cracow Department of Electronics

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

SquareWear Programming Reference 1.0 Oct 10, 2012

Microcontroller systems Lec 2 PIC18LF8722 Microcontroller s s core

SECURE DIGITAL ACCESS SYSTEM USING IBUTTON

IAS0430 MICROPROCESSOR SYSTEMS

ATMega128 Rapid Robot Controller Board [RKI-1148]

AVR- M16 development board Users Manual

8-bit Microcontroller with 1K Bytes Flash. ATtiny10 ATtiny11 ATtiny12. Preliminary. Features. Pin Configuration

LAMPIRAN. Universitas Sumatera Utara

Microcontroller Overview

AVR42772: Data Logger Demo Application on XMEGA A1U Xplained Pro. Introduction. Features. AVR XMEGA Microcontrollers APPLICATION NOTE

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2323 AT90LS2323 AT90S2343 AT90S/LS2323. Features.

Topic 11: Interrupts ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

8-bit Microcontroller with 16K Bytes In-System Programmable Flash. ATmega169V ATmega169. Features. Notice: Not recommended in new designs.

MCU: Interrupts and Timers. Ganesh Pitchiah

PIC Microcontroller Introduction

Introduction to Arduino. Wilson Wingston Sharon

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22 ATtiny22L. Preliminary. Features. Description

Now you have the basic hardware tools, its time to setup the software environment. The main softwares you will need are:

8-bit Microcontroller with 8K Bytes In-System Programmable Flash. ATmega8515 ATmega8515L. Features

MR-Servo8 User Manual

Building Interactive Devices and Objects. Prof. Dr. Michael Rohs, Dipl.-Inform. Sven Kratz MHCI Lab, LMU München

By the end of Class. Outline. Homework 5. C8051F020 Block Diagram (pg 18) Pseudo-code for Lab 1-2 due as part of prelab

Cerebot Nano Reference Manual. Overview. Revised April 15, 2016 This manual applies to the Cerebot Nano rev. A

Objectives. I/O Ports in AVR. Topics. ATmega16/mega32 pinout. AVR pin out The structure of I/O pins I/O programming Bit manipulating 22/09/2017

Development Tools. 8-Bit Development Tools. Development Tools. AVR Development Tools

Introduction to Microcontrollers

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

Distributed Real- Time Control Systems

School of Electrical, Computer and Telecommunications Engineering University of Wollongong Australia

Doc: page 1 of 8

AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Features. Introduction. AVR 8-bit Microcontrollers APPLICATION NOTE

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

Chapter 2. Overview of Architecture and Microcontroller-Resources

Programmer AVR USBasp

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2313

8-bit Microcontroller with 1K Byte Flash. ATtiny15L

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

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

AGH University of Science and Technology Cracow Department of Electronics

AVR 40 Pin Rapid Robot controller board

ERIKA Enterprise Manual for the Atmel AVR5 targets

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

Robotics Training Module ABLab Solutions

Doc: page 1 of 6

AVR Helper Library 1.3. Dean Ferreyra

Lecture (03) PIC16F84 (2)

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Rapidly Developing Embedded Systems Using Configurable Processors

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

Transcription:

Programming Microcontroller Dipl.- Ing. Falk Salewski Lehrstuhl Informatik XI RWTH Aachen salewski@informatik.rwth-aachen.de Summer term 2006

Microcontroller basics Microcontroller = CPU + Memory + internal Peripherals Folie 2

AVR Microcontroller Family Devices range from 1 to 256KB Pin count range from 8 to 100 Full code compatibility Pin/feature compatible families One set of development tools In-System Programming In-System Debugging More on http://www.atmel.com/products/avr/overview.asp Folie 3

ATmega16 We will use the ATMEL ATmega16-8-bit Microcontroller with 16K Bytes In-System Programmable Flash - 512 Bytes EEPROM, 1K Byte Internal SRAM - Two 8-bit Timer/Counters, One 16-bit Timer/Counter - 8-channel, 10-bit ADC - Programmable Serial USART - Programmable Watchdog Timer with Separate On-chip Oscillator - JTAG Port for in-system programming and debugging - More details: http://www.atmel.com/products/avr/ Folie 4

ATmega16 ATmega16 8bit CPU Memory I/O Ports Folie 5

Reset RS232 Power supply for external boards 7,5V Folie 6

Question How can we access the - I/O Ports, - the Timers and - other internal peripherals? Folie 7

ATmega16 The different blocks can be accessed via dedicated registers. e.g. PORTA can be accessed throught three 8bit registers and one bit in the CPU register Folie 8

ATmega16 I/O Ports Port Pin Configurations PORTA: DDRA PORTA PUD I/O Pull-up Comment 0 0 X Input No Tri-state 0 1 0 Input Yes 0 1 1 Input No Tri-state 1 0 X Output No Output Low 1 1 X Output No Output High The port pin can always be read through the PINA Register bit. Folie 9

Example: turn on an LED 5V 5V D D R A PO RT A Signal on PINA1 PinA1 PortA1= 1 Z 0 Pin = 0 (0V) LED 0 0 1 0 1 1 1 0 PortA1 1 1 PortA1 X: don t care U: unknown LED is on! Folie 10

Example: turn the LED off 5V 5V D D R A PO RT A Signal on PINA1 PinA1 PortA1= 1 Z 0 Pin = 1 (5V) LED 0 0 1 0 1 1 1 0 PortA1 1 1 PortA1 X: don t care U: unknown LED is off! Folie 11

Example: Reading Input 5V DD RA POR TA Signal on PINA1 PinA1 0 0 S1 closed: 0 S1 open: U 0 1 S1 closed: 0 S1 open: 1 1 1 0 PortA1 PortA1= Z 0 S1 1 1 PortA1 GND X: don t care U: unknown Input is not defined if S1 is open! Folie 12

Example: Reading Input 5V DD RA PO RTA Signal on PINA1 PinA1 0 0 S1 closed: 0 S1 open: U 0 1 S1 closed: 0 S1 open: 1 1 1 0 PortA1 PortA1= Z 0 S1 1 1 PortA1 GND X: don t care U: unknown Input is now also defined if S1 is open! Folie 13

Register in C Lesen Schreiben Bitweise bit_is_set (<port>, <pin>); bit_is_clear (<port>, <pin>); sbi (<register>, <bitnummer>); cbi (<register>, <bitnummer>); Rückgabewert bool (false=0, true=1) Byteweise Rückgabewert inp(<register>); Or Var = PINA; unsigned char outp (<wert>, <register>); Or PORTA = 0xFF; Include the following header: #include <avr/io.h> Folie 14

Example in C //LED to 5V at PINA1; SWITCH to GND at PINA0 #include <avr/io.h> int main (void) { outp(0xfe,ddra); //PortA: Pin0: Input, Pin1..7: Output outp(0xff,porta); //PortA: Pin0: pull up, Pin1..7: high = LED off while(1) { if(bit_is_set (PINA,0)) //check if PinA0 is high { cbi(porta,1); //clear PinA1 = LED on } else { sbi(porta,1); //set PinA1 = LED off } } } Folie 15

Design steps Create C source code in Programmers Notepad Create Makefile with Mfile and safe it to the folder of your source code in Programmers Notepad execute Tools -> [WinAVR] Make All Connect MCU board to PC Start Atmel AVR Studio Click "open" and choose the created.cof-file Download should start automatically Main File Name... (Name of the main program without the extension(.c)) MCU Type (ATmega16) Debug format (AVR-ext-coff) Use the following programs: Programmers Notepad Mfile Atmel AVR Studio Execution & Debugging Folie 16

Exercise Download and test LED Program Use a breakpoint to stop program if switch is pressed Folie 17

Using Interrups (IR) ATmega16: 20 different IR sources! (details in iom16.h) Each IR leads to a jump to an individual Interupt Service Routine (ISR) 4 external IR sources are available (IR by external event) - External IR0 (SIG_INTERRUPT0) - External IR1 (SIG_INTERRUPT1) - External IR2 (SIG_INTERRUPT2) - Input Capture 1 (SIG_INPUT_CAPTURE1) The following headers are needed: - #include <avr/interrupt.h> - #include <avr/signal.h> Folie 18

Example with IR //LEDs an PORTD gegen 5V & Schalter an PINB2(external IR2) gegen GND #include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> //für Zugriff auf I/O-Register //für IR-Unterstützung //für "SIGNAL" (entspricht ISR) void delay(unsigned int j) { some delay function } int main (void) { outp(0xff,ddrd); //PORTD: output outp(0x00,ddrb); //PORTB: input outp(0xff,portb); //PORTB: Pull-ups aktivated cbi(mcucsr,6); //IR2 with falling edge (default) sbi(gicr,5); //ext. IR2 aktivieren individual IR activation sbi(sreg,7); //Global IR enable global IR activation while(1) { //some main program } return(1); } //ISR für externen IR2, wird aufgerufen sobald fallende Flanke an PINB2 SIGNAL (SIG_INTERRUPT2) //alternative: SIGNAL(_VECTOR(18)) { outp(0x00,portd); //Pin=low => LEDs an delay(5); outp(0xff,portd); //Pin=high => LEDs aus } Folie 19

All interrupt sources: iom16.h /* Interrupt vectors, path: C:\Programme\WinAVR\avr\include\avr */ #define SIG_INTERRUPT0 _VECTOR(1) #define SIG_INTERRUPT1 _VECTOR(2) #define SIG_OUTPUT_COMPARE2 _VECTOR(3) #define SIG_OVERFLOW2 _VECTOR(4) #define SIG_INPUT_CAPTURE1 _VECTOR(5) #define SIG_OUTPUT_COMPARE1A _VECTOR(6) #define SIG_OUTPUT_COMPARE1B _VECTOR(7) #define SIG_OVERFLOW1 _VECTOR(8) #define SIG_OVERFLOW0 _VECTOR(9) #define SIG_SPI _VECTOR(10) #define SIG_UART_RECV _VECTOR(11) #define SIG_UART_DATA _VECTOR(12) #define SIG_UART_TRANS _VECTOR(13) #define SIG_ADC _VECTOR(14) #define SIG_EEPROM_READY _VECTOR(15) #define SIG_COMPARATOR _VECTOR(16) #define SIG_2WIRE_SERIAL _VECTOR(17) #define SIG_INTERRUPT2 _VECTOR(18) #define SIG_OUTPUT_COMPARE0 _VECTOR(19) #define SIG_SPM_READY _VECTOR(20) priority Folie 20

Atmega16 Timer/Counter Timer: - counting the system clock (6MHz in our case) - a prescaler can be used Counter: - counting an external signal The ATmega16 has 3 devices that can be used as Timer or Counter - Timer/Counter0: 8bit - Timer/Counter1: 16bit - Timer/Counter2: 8bit Folie 21

ATmega16 Timer/Counter0 Control Register Timer Overflow IR Counter Register Compare Register Folie 22

New example: Traffic light Timer External Signal clock State machine traffic light reset red(40%) => yellow(10%) => green(40%) => yellow(10%) => red If reset: blink yellow (~1Hz) for 4 seconds, then start cycle with red The whole cycle should take 3-5 seconds Use the standard MCU RESET on board Folie 23

Example: Traffic Light (2) You need: - Describtion development board (CD \Data sheets\atmega16 Development Board) - Describtion MCU-Peripheral (CD \Data sheets\atmega16 Development Board) - WinAVR (already installed, free download from the web possible) - ATMEL Studio 4.0 (already installed, free download from the web possible) Folie 24

Example: Traffic Light (3) Add the following feature (pedestrian light): S1 External Signal WAIT Wait1 S2 External Signal WAIT Wait2 As soon as S1 has been pressed Wait1 has to blink (~2Hz) until the traffic light is red. It has to be off until S1 is pressed again. As soon as S2 has been pressed Wait2 has to blink (~2Hz) until the traffic light is red. It has to be off until S2 is pressed again. Folie 25

Example: Traffic Light (4) How did you realize sequential and concurrent structures? Pro and cons of concurrent structures? Do not overload ISRs! Pro and cons of sequential structures? problems to deal quickly with external events Find optimal combination! Folie 26

WD Timer To enable an automatic reset in the case that the MCU hangs the intgrated watchdog timer should be used. If enabled the WD has to be triggerd within a certain time If not triggered in time the WD resets the MCU Folie 27

Useful Links http://www.mikrocontroller.net/wiki/avr-gcc-tutorial http://atmel.com/products/avr/ Die Programmiersprache C. Ein Nachschlagewerk Regionales Rechenzentrum für Niedersachsen/Universität Hannover http://www.rz.rwth-aachen.de/computing/sw/rrzn/index.php C Programmieren von Anfang an Helmut Erlenkötter, ISBN 3-499-60074-9 Programming Embedded Systems in C and C++ Michael Barr, ISBN 1-56592-354-5 use Windows calculater for 0x04 => 0b00000100 => 4 Folie 28