Design with Microprocessors

Similar documents
Design with Microprocessors

ATmega128 USART. Overview

Design with Microprocessors

COMP2121: Microprocessors and Interfacing

spi 1 Fri Oct 13 13:04:

Serial versus Parallel Data Transfers

EE 456 Fall, Table 1 SPI bus signals. Figure 1 SPI Bus exchange of information between a master and a slave.

ATmega640/1280/1281/2560/2561

Serial Communications

Serial Peripheral Interface (SPI)

ATmega128. Serial Communication (RS-232C)

Embedded Systems and Software. Serial Interconnect Buses I 2 C (SMB) and SPI

EE 308: Microcontrollers

The 9S12 Serial Peripheral Inteface (SPI) Huang Section 10.2 through 10.6 SPI Block User Guide

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

Amarjeet Singh. January 30, 2012

a Serial Peripheral Interace (SPI). Embedded RISC Microcontroller Core Peripheral

19.1. Unit 19. Serial Communications

OUTLINE. SPI Theory SPI Implementation STM32F0 SPI Resources System Overview Registers SPI Application Initialization Interface Examples

Interfacing Techniques in Embedded Systems

Addressing scheme to address a specific devices on a multi device bus Enable unaddressed devices to automatically ignore all frames

< W3150A+ / W5100 Application Note for SPI >

SPI: Serial Peripheral Interface

Universität Dortmund. IO and Peripheral Interfaces

SPI Block User Guide V02.07

Menu. What is SPI? EEL 3744 EEL 3744 SPI

Serial Communication. Simplex Half-Duplex Duplex

USART Register Description

More on the 9S12 SPI Using the Dallas Semiconductor DS1302 Real Time Clock with the 9S12 SPI

Module 3.C. Serial Peripheral Interface (SPI) Tim Rogers 2017

AT89S4D12. 8-Bit Microcontroller with 132K Bytes Flash Data Memory AT89S4D12. Features. Description. Pin Configurations

[TUT] Newbie's Guide to AVR Interrupts

Unit 19 - Serial Communications 19.1

Serial Peripheral Interface (SPI) Host Controller Data Sheet

EE 308 Spring Using the 9S12 SPI

Hello, and welcome to this presentation of the STM32 Universal Synchronous/Asynchronous Receiver/Transmitter Interface. It covers the main features

Understanding SPI with Precision Data Converters

Asynchronous & Synchronous Serial Communications Interface. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Basics of UART Communication

Marten van Dijk Department of Electrical & Computer Engineering University of Connecticut

8-bit RISC Microcontroller. Application Note. AVR151: Setup And Use of The SPI

Microcontrollers and Interfacing

C8051F700 Serial Peripheral Interface (SPI) Overview

Asynchronous & Synchronous Serial Communications Interface. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Hello, and welcome to this presentation of the STM32 Low Power Universal Asynchronous Receiver/Transmitter interface. It covers the main features of

Embedded Systems and Software. Serial Communication


Digital Storage Oscilloscope

Serial Communication. Spring, 2018 Prof. Jungkeun Park

Synchronous = SPI (3 options)

Laboratory 5 Communication Interfaces

Serial Peripheral Interface (SPI) Last updated 8/7/18

Review for Exam 3. Write 0x05 to ATD0CTL4 to set at fastest conversion speed and 10-bit conversions

IV B.Tech. I Sem (R13) ECE : Embedded Systems : UNIT -4 1 UNIT 4

USER GUIDE EDBG. Description

PARALLEL COMMUNICATIONS

Serial Communication. Simplex Half-Duplex Duplex

Marten van Dijk, Syed Kamran Haider

Embedded Systems and Software

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

Raspberry Pi - I/O Interfaces

Serial Communication Prof. James L. Frankel Harvard University. Version of 2:30 PM 6-Oct-2015 Copyright 2015 James L. Frankel. All rights reserved.

Laboratory Finite State Machines and Serial Communication

UART. Embedded RISC Microcontroller Core Peripheral

Using the Z8051 MCU s USI Peripheral as an SPI Interface

or between microcontrollers)

Serial Peripheral Interface (SPI)

UART TO SPI SPECIFICATION


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

EDBG. Description. Programmers and Debuggers USER GUIDE

Introduction the Serial Communications Huang Sections 9.2, 10.2 SCI Block User Guide SPI Block User Guide

DIGITAL COMMUNICATION SWAPNIL UPADHYAY

Growing Together Globally Serial Communication Design In Embedded System

Serial Buses in Industrial and Automotive Applications

Parallel Data Transfer. Suppose you need to transfer data from one HCS12 to another. How can you do this?

CSCE 236 Embedded Systems, Fall 2017 Homework 5

Serial Communications

Orangutan X2 Command Documentation v1.01

Doc: page 1 of 8

AN Multifunction Serial Interface of FM MCU. Contents. 1 Introduction

Section 5 SERCOM. Tasks SPI. In this section you will learn:

Communication. Chirag Sangani

Using the USART Serial Communications

Introduction to I2C & SPI. Chapter 22

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

RL78 Serial interfaces

Serial Peripheral Interface. What is it? Basic SPI. Capabilities. Protocol. Pros and Cons. Uses

Real-Time Embedded Systems. CpE-450 Spring 06

How to use RFpro in Packet Mode

SPI 3-Wire Master (VHDL)

Microcontroller basics

An SPI interface for the 65(C)02 family of microprocessors

Arduino Uno R3 INTRODUCTION

McMaster University Embedded Systems. Computer Engineering 4DS4 Lecture 6 Serial Peripherals Amin Vali Feb. 2016

C-CODE EXAMPLE FOR SCP1000-D01 PRESSURE SENSOR

i_csn i_wr i_rd i_cpol i_cpha i_lsb_first i_data [15:0] o_data [15:0] o_tx_ready o_rx_ready o_rx_error o_tx_error o_tx_ack o_tx_no_ack

Doc: page 1 of 6

ARDUINO MICRO WITHOUT HEADERS Code: A000093

keyestudio Keyestudio MEGA 2560 R3 Board

Transcription:

Design with Microprocessors Lecture 6 Interfaces for serial communication Year 3 CS Academic year 2017/2018 1 st Semester Lecturer: Radu Dănescu

Serial communication modules on AVR MCUs Serial Peripheral Interface (SPI) Synchronous serial communication Full duplex operation Master or Slave configuration Variable frequency (bit rate) Can be used for connecting two MCUs, or for connecting the MCU with different peripherals Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) Synchronous or asynchronous serial communication Variable frequency (baud rate) Data frames of 5-9 bits, with or without parity Can use interrupts for transmission control Error detection Can be used for connecting the MCUs with the PC (Serial port), or with other MCUs or peripherals

Serial communication modules on AVR MCUs Two Wire Serial Interface (TWI) Complex protocol based on only two lines (clock and data) Atmel implementation of the I2C (Inter Integrated Circuit) protocol The TWI controller supports master and slave operation modes 7 bit addressing Multiple masters arbitration support Programmable slave address

Serial Peripheral Interface (SPI) Signals SCLK Serial clock, generated by Master MOSI Master Output, Slave Input, data sent by Master MISO Master Input, Slave Output, data received by Master SS Slave select Slave device activation signal, driven by Master, active LOW Operation Master initializes communication by activating SS Master generates the clock signal SCLK Every clock period, a bit is transferred from Master to Slave, and one bit from Slave to Master After each data frame (8, 16 bits, ) SS is de-activated, for synchronization

Serial Peripheral Interface (SPI) Operating principle Both partners have an internal shift register, their ends connected to MISO and MOSI Both registers operate on the same clock, SCLK Together the shift registers form a rotation register After a number of clock periods equal to the size of a shift register, Master and Slave exchange data.

Serial Peripheral Interface (SPI) Data synchronization with the clock signal Shifting and reading the data are done on opposing clock edges CPOL clock polarity whether the first edge is rising or falling CPHA clock phase For CPHA = 0 Latch data on the first clock edge Shift (set up) the data on the second clock edge

Serial Peripheral Interface (SPI) Data synchronization with the clock signal For CPHA = 1 Shift the data on the first clock edge Latch the data on the second clock edge

Serial Peripheral Interface (SPI) Using the SS signal For a Slave device, SS is an input signal SS = 0 means the slave device is activated. A transition from 0 to 1 means re-setting the transfer cycle (marks the end of a data frame) SS = 1 the slave device is inactive For a Master device, SS can be either: Output used for activating the Slave for communication Input if more Masters are allowed, a 0 on the SS line means the device enters Slave mode. Multiple devices configuration: independent SS signals, or daisy chain

SPI sub-system architecture SPI on AVR MCUs Shift register Pin connection logic Clock generation Control

SPI on AVR MCUs SPI Configuration The SPCR register: SPIE SPI Interrupt Enable, activates interrupt generation at the end of transmission SPE SPI Enable. Must be set to 1 to use the SPI system. DORD Data Order. 1=LSB first, 0 = MSB first MSTR Master, if 1, Slave, if 0 CPOL, CPHA select the clock polarity and phase SPR1, SPR0 along with SPI2x from SPSR configure the speed of the SPI clock

SPI configuration - continued SPI on AVR MCUs The SPSR register: SPI2X Along with SPR1 and SPR0 from SPCR configure the speed of the SPI clock WCOL Write collision set if SPDR is written before transmission is complete SPIF SPI Interrupt flag set when the transmission is complete. If SPIE is set, an interrupt request is generated.

Using the SPI Master mode SPI on AVR MCUs 1. Configuring the direction of the I/O pins: The SPI pins are found on Port B (on ATMega 2560) 2. Configuring the SPCR and SPSR registers with the chosen work mode and speed 3. Activating SS ( PB(0) <- 0, explicit! ) 4. Write data to SPDR starts the transmission 5. Wait until SPIF is set in SPSR transmission complete 6. Read data from SPDR data sent by Slave 7. De-activate SS (PB(0) <- 1 )

SPI on AVR MCUs Using the SPI Master mode Source code.org 0x0000 jmp reset reset: ldi r16,0b00000111 out DDRB,r16 ldi r16, 0b00000001 out PORTB, r16 cbi SPSR, 0 ldi r16,0b01010011 out SPCR,r16 loop: cbi PORTB, 0 ; SS <- 0 ldi r16, 0b10010101 ; data to send out SPDR, r16 wait: sbis SPSR, 7 rjmp wait in r16,spdr sbi PORTB, 0 ; SS <- 1 ldi r18, 0 wait2: dec r18 brne wait2 rjmp loop ;MISO input, MOSI, SCK and SS output ;Initially, SS<--1, SPI Slave inactive ; set bit 0 of SPSR to zero for clock speed selection ;No interrupts, SPI Enabled, LSB first, Master, CPOL=0 first edge is rising, CPHA = 0 latch on the first edge, Slowest clock speed ; check bit 7 of SPSR for transmission complete ; short pause between transmissions, then go again

Using the SPI Master mode Result SPI on AVR MCUs SCK MOSI 1 0 0 1 0 1 0 1

SPI on AVR MCUs Connecting peripherals using SPI Digilent PMOD DA1 Digital to Analog Converter SS MOSI MISO SCK

SPI on AVR MCUs Connecting peripherals using SPI Digilent PMOD DA1 Digital to Analog Converter Transmission of 16 bits (2x8 bits) first 8 data, next 8 command

Connecting peripherals using SPI Digilent PMOD SF Serial Flash SPI on AVR MCUs 00000011 = READ

USART USART UART with optional synchronization using a clock signal UART Asynchronous serial communication interface Asynchronous the interval between data frames is undefined. The receiver detects the start and end of a frame. The time interval between bits (bit frequency, baud rate) is fixed and must be known by the transmitter and by the receiver. Transmission and reception can be performed simultaneously (full duplex). Each side can initiate a transmission. The basic UART signals Rx input, reception Tx output, transmission USART has the additional xck (external clock) signal, input or output, which will synch the transmission and reception. We will only discuss the UART operation. Rx Tx Rx Tx

USART Data transmission: A frame (packet) is made of St: 1 start bit, of value 0 D: Data bits (5 9, size known by both participants) P: 1 parity bit. Parity can be: - Absent: no P bit - Even - Odd Sp: 1 or 2 stop bits, of value 1 the number of stop bits must be known by both participants

USART Data reception: - The receiver must known the transmission parameters (Baud, Number of data bits, Number of stop bits, Parity). 1. A transition from 1 to 0 on Rx is detected Reception initiated! 2. Check the middle bit period for the start bit. If Rx is still 0, continue with the reception sequence, otherwise go back to idle more (assume noise). 3. Check the middle of interval for the next bits (data, parity, stop), and reconstruct the data frame. 4. If a zero is found in the position of the stop bits, generate a framing error. 5. If the computed parity at the receiver does not match the parity bit P, generate a parity error. For robustness, the receiver samples the input signal with a frequency 8-16 times higher than the baud rate. Different baud rates at transmitter and at receiver may lead to bit sampling errors! These errors may be detected as framing errors, parity errors, but they may also be undetected, leading to false reception!

USART UART and RS232: Adapting voltage levels RS232 logic 1-5 -15 V RS232 logic 0 +5 +15 V Logic level conversion between AVR UART and RS232 is needed Pin correspondence

USART UART and USB: Use of a FTDI (Future Technology Devices International Ltd) adapter Arduino Mega uses the FT232RL chip - Seen as a virtual COM port on the PC - Bi-directional conversion between USB and UART http://www.ftdichip.com/support/documents/datasheets/ics/ds_ft232r.pdf USB D+ and D- UART Tx and Rx FTDI 232 RL

USART on AVR MCUs Four USART units on ATMega 2560: USART0 3 Baud generation General architecture: Transmission Control registers Reception

System configuration: - Status and control register UCSRnA USART on AVR MCUs - RXCn is 1 when reception is completed. Can trigger an interrupt request. - TXCn is 1 when transmission is completed. Can trigger an interrupt request. - UDREn Data Register Empty, signals that the data register can be written. - FEn signals Frame Error - DORn Data overrun when a reception start is detected before the already received data are read from the UDRn register - UPEn signals Parity Error - U2Xn When 1 = Doubling the USART data rate (baud rate) - MPCMn Activates multiprocessor communication mode

System configuration: - Status and control register UCSRnB USART on AVR MCUs - RXCIEn If set to 1, interrupt request is generated at the end of reception - TXCIEn If set to 1, interrupt request is generated at the end of transmission - UDRIEn - If set to 1, interrupt request is generated when the data register is empty - RXEn activate reception - TXEn activate transmission - UCSZn2 combined with UCSZn1 and UCSZn0 from USCRnC sets the packet size - RXB8n 9-th received bit, when using 9 bit data frames. - TXB8n 9-th bit to transmit, when using 9 bit data frames.

System configuration: - Status and control register UCSRnC USART on AVR MCUs - UMSELn Selects asynchronous 0 or synchronous 1 mode - UPMn1 si UPMn0 Selection of parity mode - USBSn stop bits configuration: 0 1 bit, 1 2 bits - UCSZn1:UCSZn0 combined with UCSZn2 of UCSRnB, set the packet size - UCPOLn clock polarity for the synchronous mode

USART on AVR MCUs System configuration: - Frequency control registers: UBRRnH si UBRRnL - Together they form UBRRn, 12 bits Reading received data / writing data to be transmitted - Both actions are performed using the register UDRn

USART on AVR MCUs Example: communication between AVR and the PC simple ECHO Requirements: UART-USB adapter, USB cable, we use UART 1 1. Configuration Baud: 9600 Data size: 8 bits Stop bits: 2 Parity: none f osc = 16000000 UBRRn = 103 2. Wait for a character to be received - Check RXCn of UCSRnA, wait until it becomes 1 3. Read received character, from UDRn 4. Write the character back, to UDRn 5. Wait for the character to be transmitted - Check TXCn of UCSRnA, wait until it becomes 1 6. Jump to 2

USART on AVR MCUs Example: communication between AVR and the PC simple ECHO Source code ldi r16, 0b00011000 sts UCSR1B,r16 ldi r16, 0b00001110 sts UCSR1C,r16 ldi r16, 103 ldi r17, 0 sts UBRR1H, r17 sts UBRR1L, r16 mainloop: recloop: lds r20, UCSR1A sbrs r20, 7 rjmp recloop lds r16, UDR1 sts UDR1,r16 txloop: lds r20, UCSR1A sbrs r20, 5 rjmp txloop rjmp mainloop ; activate Rx and Tx ; frame size 8 bits, no parity, 2 stop bits ; Computed Baud rate, fits in 8 bits ; The higher order bits of UBRR are zero ; bit 7 of UCSR1A reception complete ; read the received data ; write back the received data to UART ; wait for the end of transmission

ASCII codes