Embedded programming, AVR intro

Similar documents
More on C programming

The Atmel ATmega328P Microcontroller

The Atmel ATmega168A Microcontroller

8051 Microcontroller

Programming Microcontroller Assembly and C

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

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

Microprocessors And Microcontrollers (Practical)

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

Programming refresher and intro to C programming

Lab Course Microcontroller Programming

Beginning C Programming for Engineers

Microcontrollers. Microcontroller

INTERFACING HARDWARE WITH MICROCONTROLLER

Module 2: Introduction to AVR ATmega 32 Architecture

CN310 Microprocessor Systems Design

Microcontroller basics

Atmel 8 Bit Avr Microcontroller With 2 4 8k Bytes In

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

Ali Karimpour Associate Professor Ferdowsi University of Mashhad

CS/ECE 5780/6780: Embedded System Design

Introduction to the MC9S12 Hardware Subsystems

AVR- M16 development board Users Manual

SBAT90USB162 Atmel. SBAT90USB162 Development Board User s Manual

AVR Microcontrollers Architecture

MICROPROCESSOR BASED SYSTEM DESIGN

Introduction to Arduino. Wilson Wingston Sharon

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

ATmega128. Introduction

SECURE DIGITAL ACCESS SYSTEM USING IBUTTON

1. Attempt any three of the following: 15

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

ARDUINO MEGA INTRODUCTION

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

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

Robosoft Systems in association with JNCE presents. Swarm Robotics

Doc: page 1 of 6

Ali Karimpour Associate Professor Ferdowsi University of Mashhad

Doc: page 1 of 8

ECE2049-E18 Lecture 6 Notes 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

Microcontrollers. Outline. Class 1: Serial and Digital I/O. March 7, Quick Tour of the Board. Pins, Ports, and Their Registers

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

Hardware Manual. Crumb128. Rapid Prototyping Module with the Atmega128 AVR Microcontroller

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

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

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

Arduino Uno R3 INTRODUCTION

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #9: Exam Review w/ Solutions

LBAT90USB162 Atmel. LBAT90USB162 Development Board User s Manual

Overview. The C programming model. The C programming model. The C programming model. The C programming model 1/23/2009. Real-time Systems D0003E

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

Lecture (02) PIC16F84 (I)

ET-BASE AVR ATmega64/128

Doc: page 1 of 6

ECE2049-E17 Lecture 6 1. ECE2049: Embedded Computing in Engineering Design E Term Lecture #6: Exam Review

EE 109 Unit 4. Microcontrollers (Arduino) Overview

3.3V regulator. JA H-bridge. Doc: page 1 of 7

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

Microcontroller Overview

Mega128-DEVelopment Board Progressive Resources LLC 4105 Vincennes Road Indianapolis, IN (317) (317) FAX

EE 308: Microcontrollers

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

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

EE 354 Fall 2015 Lecture 1 Architecture and Introduction

PART 1 : MR Introduction 2. Features. PART 2 : CPU Board 1. Placement Diagram (Silkscreen) 2. Circuit Diagram 3.

Lab 1 Introduction to Microcontroller

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

MEXLE. International Educational Platform. International Educational Platform. for Informatics based on Embedded Systems

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

Interconnects, Memory, GPIO

STK200 Starter Kit User Guide May 2004

Mega128-Net Mega128-Net Mega128 AVR Boot Loader Mega128-Net

Why embedded systems?

8-bit Atmel Microcontroller with 8/16K Bytes In-System Programmable Flash AT90PWM81 AT90PWM161

Introduction to Assembly language

Interrupts and timers

Microcontroller systems Lec 2 PIC18LF8722 Microcontroller s s core

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

M32 Development Board

STK User Guide

The naked computer. The naked computer. Separate bus architecture. Real-time Systems SMD138

LAB A Translating Data to Binary

Diploma in Embedded Systems

(Embedded) Systems Programming Overview

Various power connectors. 3.3V regulator. 64K Flash (Internal) 2K EEPROM (Internal) 4K SRAM (Internal) JA Mem Adr/ Data. Doc: page 1 of 9

CMPE C Programming & Embedded Systems. Discussion I (Version 2.0) August 31, 2014

Figure 1.1: Some embedded device. In this course we shall learn microcontroller and FPGA based embedded system.

Systems Programming. Lecture 4 Z16 Architecture and Programming

DBAT90USB162 Atmel. DBAT90USB162 Enhanced Development Board User s Manual

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

REQUIRED MATERIALS Epiphany-DAQ board Wire Jumpers Switch LED Resistors Breadboard Multimeter (if needed)

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

Special Topics for Embedded Programming

Introduction. Unit 4. Numbers in Other Bases in C/C++ BIT FIDDLING. Microcontrollers (Arduino) Overview Digital I/O

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR. ELECTRONICS & COMMUNICATION DEPARTMENT Question Bank- 1

PIC-I/O Multifunction I/O Controller

LED Matrix Scrolling using ATmega32 microcontroller

Explanation of PIC 16F84A processor data sheet Part 1: overview of the basics

SOME ASSEMBLY REQUIRED

Transcription:

Applied mechatronics, Lab project Embedded programming, AVR intro Sven Gestegård Robertz Department of Computer Science, Lund University 2017

Outline 1 Low-level programming Bitwise operators Masking and shifting Data types and representation 2 Microcontrollers Introduction Atmel AVR A minimal AVR board Microcontroller programming 3 AVR programming Accessing GPIO pins 4 More on low-level programming Logical operators Hexadecimal and binary representation Bitwise operators

Outline 1 Low-level programming Bitwise operators Masking and shifting Data types and representation 2 Microcontrollers Introduction Atmel AVR A minimal AVR board Microcontroller programming 3 AVR programming Accessing GPIO pins 4 More on low-level programming Logical operators Hexadecimal and binary representation Bitwise operators Embedded programming, AVR intro 2/26

Boolean and bitwise operators Boolean operators (evaluate to true or false)! not && and or Bitwise operators (evaluate to a number) not & and or ˆ exclusive or (xor) Low-level programming Embedded programming, AVR intro 3/26

Bitwise operators, examples 1111 0101 = 0000 1010 1110 0111 & 0111 1100 = 0110 0100 0010 1010 1100 0001 = 1110 1011 & is used for bit masking and for clearing bits is used for setting bits ˆ is used for toggling bits Low-level programming : Bitwise operators Embedded programming, AVR intro 4/26

Masking and setting bits Example: A register : CTRL_REG A bitmask : FLAGS Named bits : ENABLE_X, ENABLE_Y,... Clear the bits specified by FLAGS and set individual bits. // clear FLAGS using bitmask CTRL_REG = CTRL_REG & ~ FLAGS ; // set individual bits CTRL_REG = CTRL_REG ENABLE_X ENABLE_Y ; Can also be written: CTRL_REG &= ~ FLAGS ; CTRL_REG = ENABLE_X ENABLE_Y ; Low-level programming : Masking and shifting Embedded programming, AVR intro 5/26

Shift operators In addition to the bitwise logical operators, C and Java has operators for shifting the bits in a number x << n shifts x n steps to the left x >> n shifts x n steps to the right Example: 2 << 2 == 8 (0b0010 << 2 == 0b1000) Shifting can be viewed as multiplication or division by powers of two. Low-level programming : Masking and shifting Embedded programming, AVR intro 6/26

Masking and shifting Low level drivers often access hardware registers where a single, or a few, bits control something Example: motor servo control word (16 bits) - - - posref[8] velref[4] enable Reading and writing posref: # define POS_OFF 5 # define POS_MSK 0 xff unsigned short cr;// temporary for register value unsigned char pr; // temporary for posref value cr= read_ctrl_reg (); pr = ( cr >> POS_OFF ) & POS_MSK ; // shift and mask cr &= ~( POS_MSK << POS_OFF ); // clear posref bits pr = pr + 10; cr = ( pr << POS_OFF ); // new value for register write_ctrl_reg ( cr ); Low-level programming : Masking and shifting Embedded programming, AVR intro 7/26

Unsigned integer types unsigned char unsigned short unsigned int avoids sign extension char x = 0 xff ; unsigned char y = 0 xff ; int ix = x; // ix = -1 : 0 xffffffff int iy = y; // iy = 255 : 0 x000000ff On unsigned types, the shift operators always shift in zeroes. NB! On signed types, >> is implementation-defined. Typically preserves the sign bit Always use unsigned types for bit operations etc. Or: always use unsigned types as default Low-level programming : Data types and representation Embedded programming, AVR intro 8/26

Byte order Data types larger than one byte are often represented (in memory) and sent (on communication channels) as a sequence of bytes The byte order is different in different architectures/systems The two most common variants Little endian: least significant byte first Used by, e.g., Intel processors, 8-bit AVR Big endian: most significant byte first Used by, e.g., Motorola processors, Java, network byte order Don t confuse with bit order on serial lines Low-level programming : Data types and representation Embedded programming, AVR intro 9/26

Example: Little endian memory In a byte-addressed memory, byte order must be defined for larger values First byte means lowest address Example: *a = 0x0A0B0C0D; Low-level programming : Data types and representation Embedded programming, AVR intro 10/26

Microcontrollers A complete computer system in a single package Processor Memory RAM Flash ROM EEPROM Peripherals Communication modules Timers / PWM A/D converters General purpose digital I/O Microcontrollers : Introduction Embedded programming, AVR intro 11/26

Microcontroller families Microcontrollers come in families Same (or similar) instruction set different memory configurations different peripherals different packages Facilitates reuse of code and board designs Newer versions often pin compatible Examples: Atmel AVR, Microchip PIC Microcontrollers : Introduction Embedded programming, AVR intro 12/26

AVR ATMega88 The Atmel ATmega48/88/168 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATmega48/88/168 achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed. 2.1 Block diagram Figure 2-1. Block diagram. GND VCC Watchdog timer Watchdog oscillator Power supervision POR / BOD & RESET debugwire PROGRAM LOGIC 8 bit RISC CPU 8k flash ROM Oscillator circuits / clock generation Flash SRAM CPU 1k RAM EEPROM AVCC 512 bytes EEPROM 20 MIPS @ 20MHz In-system programmable (ISP) DATABUS 8bit T/C 0 8bit T/C 2 USART 0 16bit T/C 1 Analog comp. SPI A/D conv. Internal bandgap TWI 6 2 AREF GND PORT D (8) PORT B (8) PORT C (7) RESET XTAL[1..2] PD[0..7] PB[0..7] PC[0..6] ADC[6..7] The AVR core combines a rich instruction set with 32 general purpose working registers. All the Microcontrollers : Atmel AVR Embedded programming, AVR intro 13/26

AVR ATMega88 pin-out Each pin can be used as either General Purpose digital I/O (GPIO) Pin change interrupt Alternative function (peripherals) Peripherals you need: ISP / SPI (PB3--5) USART (PD01) OCny (i.e., PWM) ADCn Microcontrollers : Atmel AVR Embedded programming, AVR intro 14/26

AVR documentation Data sheets and application notes at http://www.atmel/com/ AVR042: Hardware design considerations Articles, guides, forums http://www.avrfreaks.net/ Microcontrollers : Atmel AVR Embedded programming, AVR intro 15/26

A minimal AVR board Few external components needed Built-in oscillator (no crystal required) Built-in pull-up resistors on I/O pins Power supply Reset signal ISP connector it is always useful to add a power LED and a reset button. Microcontrollers : A minimal AVR board Embedded programming, AVR intro 16/26

AVR ISP connectors Microcontrollers : A minimal AVR board Embedded programming, AVR intro 17/26

Microcontroller programming Tool chain Cross compiler/linker Object file translation Programmer Interrupt handling Memory mapped registers Microcontrollers : Microcontroller programming Embedded programming, AVR intro 18/26

AVR studio Free tool available from Atmel Integrated assembler Integrated simulator gcc plug-in Supports programming for all AVR devices Microcontrollers : Microcontroller programming Embedded programming, AVR intro 19/26

Task: Build an AVR board Read data sheets / application notes Keep it simple Use the six-pole programming connector Include some status LEDs Check output current ratings Outputs can sink more current than they can source Include a reset button (or jumper) Microcontrollers : Microcontroller programming Embedded programming, AVR intro 20/26

Accessing the GPIO pins Two concepts, three registers: data direction and data registers Example # include <avr / io.h> DDRx The Data Direction Register for port x PORTx The Output Register for port x PINx The Input Register for port x x {B, C, D} main () { DDRB = 0 xff ; // set all pins as outputs PORTB = 0 x55 ; // write 0101 0101 to pins while (1); // don t exit from main () } AVR programming : Accessing GPIO pins Embedded programming, AVR intro 21/26

Accessing the GPIO pins Using pin name macros # include <avr / io.h> main () { DDRB = ( 1 << PB7 ) (1 << PB2 ) (1 << PB0 ); PORTB = ( 1 << PB7 ) (1 << PB2 ) (1 << PB0 ); while (1); } AVR programming : Accessing GPIO pins Embedded programming, AVR intro 22/26

Accessing the GPIO pins Reading and writing # include <avr / io.h> main () /* A 4- bit buffer example */ { DDRB =0 xf0 ; while (1){ unsigned char tmp = ( PINB & 0x0F ); PORTB = ( tmp << 4); } } NB! When a pin is configured as an input, writing to PORTx controls the pull-up resistors on that pin. See data sheet for details. AVR programming : Accessing GPIO pins Embedded programming, AVR intro 23/26

Use shadow registers Sometimes you need to do more complex operations on a port (or control register) value and still write it atomically. You cannot write individual bits in a register, the entire register is always overwritten. Some registers are write-only. To modify their value, a local copy is useful. A common idiom is to use a variable in memory that is used as a shadow register. all bit operations are done on the shadow variable changes can be made in multiple steps inconsistent temporary values are not written to the register then the value of the shadow variable is written to the register AVR programming : Accessing GPIO pins Embedded programming, AVR intro 24/26

Shadow variable example unsigned char val_b ; // shadow variable for PORTB void setup () { DDRB = 0 xf0 ; val_b = 0x05 ); // turn on two pull - ups PORTB = val_b ; } main () { setup (); while (1){ unsigned char tmp = ( PINB & 0x0f ); val_b = ( val_b & 0 x0f ) ( tmp << 4); PORTB = val_b ; } } AVR programming : Accessing GPIO pins Embedded programming, AVR intro 25/26

Suggested excercise if you haven t already, include a few LEDs on your AVR board connect a button to one of the GPIO pins write a small program that changes the pattern on the LEDs when the button is pressed, e.g. counting up/down shifting left/right... AVR programming : Accessing GPIO pins Embedded programming, AVR intro 26/26

Boolean and bitwise operators Boolean operators (evaluate to true or false)! not && and or Bitwise operators (evaluate to a number) not & and or ˆ exclusive or (xor) More on low-level programming : Logical operators Embedded programming, AVR intro 27/26

Boolean expressions evaluate to true or false same syntax as in Java but there is no boolean type in C instead, any integer (and hence any type) can be used zero is interpreted as false non-zero is interpreted as true Example: infinite loop while (1) { // corresponds to while ( true ) in Java printf ("."); } More on low-level programming : Logical operators Embedded programming, AVR intro 28/26

Bitwise operators? bits??? A number can be represented in binary (base 2) i.e. ones and zeroes each binary digit is called a bit Why is that interesting to us? In (low-level) programming, an integer variable or regsister is often used to represent a set of flags Each bit in a variable has a distinct meaning... and is either on (1, true, high, set)... or off (0, false, low, cleared) Small sets of bits can represent small numbers I.e., a few small numbers can fit into one variable or register. Often used in combination to attach a set of flags to a number More on low-level programming : Logical operators Embedded programming, AVR intro 29/26

Representation of numbers Positional number systems The decimal system: multiples of 10. ( Base 10 ) Example : 1502 10 = 1 10 3 + 5 10 2 + 0 10 1 + 2 10 0 base 2: binary digits: {0, 1} base 16: hexadecimal digits: {0... 9, a... f} More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 30/26

Convert to hexadecimal from binary Convert 0101 1100 2 to hex: bits 0... 3 : 1100 2 = 0 2 0 + 0 2 1 + 1 2 2 + 1 2 3 = = 12 10 = c 16 bits 4... 7 : 0101 2 = 1 2 4 + 0 2 5 + 1 2 6 + 0 2 7 = = 16 + 64 = 5 16 = 50 16 which gives the result (in hex) : 50 + c = 5c (= 92 10 ) Observation: Each hexadecimal digit corresponds to 4 bits One byte is two 4 bit nibbles, i.e., hex digits More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 31/26

Hexadecimal to binary Convert 5a (0x5a) to binary: the value of each nibble can be calculated independently i.e., you only need to use the values {1, 2, 4, 8} for each hexadecimal digit regardless of how big the number is high nibble: 5 = 4 + 1 = 0101 2 low nibble: a = 8 + 2 = 1010 2 which gives the result: 5a = 0101 1010 More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 32/26

Converting to decimal The hexadecimal number 0x73 (= 3 16 0 + 7 16 1 = 3 + 112 = 115) corresponds to the binary representation 0111 0011 which has the decimal value 1 2 0 + 1 2 1 + 0 2 2 + 0 2 3 + 1 2 4 + 1 2 5 + 1 2 6 + 0 2 7 = 1 1 + 1 2 + 1 16 + 1 32 + 1 64 = 115 More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 33/26

Representation of numbers Binary (base 2) and hexadecimal (base 16) representations are more convenient than decimal for doing bit operations In binary, each bit is directly represented In hex, each digit corresponds to 4 bits (a nibble ) Converting to/from decimal is tedious; there is no simple way to find the value(s) of a (set of) bit(s) More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 34/26

Constants in C and Java Binary digits: 0 1 constants in gcc and Java 7 are prefixed by 0b e.g., 0b0001101011110100 Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 a b c d e f constants in C and Java are prefixed by 0x e.g., 0x1af4 Hexadecimal notation is much more readable More on low-level programming : Hexadecimal and binary representation Embedded programming, AVR intro 35/26

Bitwise operators x - not x & y - and the result is x with each bit inverted the result has a 1 (one) in each bit position where the bit value of both x and y ==1 x y - (inclusive) or the result has a 1 (one) in each bit position where the bit value of either x or y ==1 x ˆ y - exclusive or the result has a 1 (one) in each bit position where the bit value of exactly one of x or y ==1 More on low-level programming : Bitwise operators Embedded programming, AVR intro 36/26

Bitwise operators Bitwise operators evaluate to a number Works on the binary representation of the operand(s) Example: bitwise not x - in the binary representation of x, invert each bit unsigned char x,y; // 8 bits long x = 10; y = ~x; x = 10 = 0x0a = 0000 1010 y = 1111 0101 = 0xf5 = 245 More on low-level programming : Bitwise operators Embedded programming, AVR intro 37/26