The MC9S12 IIC Interface

Similar documents
Dallas Semiconductor DS1307 Real Time Clock. The DS 1307 is a real-time clock with 56 bytes of NV (nonvolatile)

Lecture 25 March 23, 2012 Introduction to Serial Communications

ECE 4510/5530 Microcontroller Applications Week 10

Functional block diagram AD53x1 (Analog Devices)

Introduction the Serial Communications Parallel Communications Parallel Communications with Handshaking Serial Communications

HCS12 Inter-Integrated Circuit(IIC) Block Guide V02.06

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

Raystar Microelectronics Technology Inc.

EE 308 Spring Using the 9S12 SPI

HCS12 Inter-Integrated Circuit(IIC) Block Guide V02.08

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

Application Note 304. Flex Power Modules. Microcontroller Programming for 3E Digital Products

How to Use IIC Module on M68HC08, HCS08, and HCS12 MCUs Stanislav Arendarik Application Engineer Roznov, Czech Republic

Review for Exam III. Analog/Digital Converters. The MC9S12 has two 10-bit successive approximation A/D converters - can be used in 8-bit mode

Section 19. Inter-Integrated Circuit (I 2 C )

Section 21. Inter-Integrated Circuit (I 2 C )

PT7C4563 Real-time Clock Module (I2C Bus)

DS1676 Total Elapsed Time Recorder, Erasable

Table 1. Basic functions of PT7C4363 Item Function PT7C4363. Source: Crystal: kHz Oscillator enable/disable - Oscillator fail detect

Lecture 36 April 25, 2012 Review for Exam 3. Linking Assembly Subroutine with a C Program. A/D Converter

Features. Description PT7C4363B. Real-time Clock Module (I 2 C Bus)

EE 308 Spring Lab on IIC Bus

17. I 2 C communication channel

PARALLEL COMMUNICATIONS

DS 1682 Total Elapsed Time Recorder with Alarm

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

Asynchronous Data Transfer

DS1845 Dual NV Potentiometer and Memory

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

The MC9S12 Input Capture Function

DS1625. Digital Thermometer and Thermostat FEATURES PIN ASSIGNMENT

DS1302. Trickle Charge Timekeeping Chip FEATURES PIN ASSIGNMENT PIN DESCRIPTION

AN-1025 APPLICATION NOTE

DS1306. Serial Alarm Real Time Clock (RTC)

Add a Non-Volatile Clock to the MC68HC705J1A. Some applications of using the DS1307 are:

DS1305EN. Serial Alarm Real-Time Clock

DS1305EN. Serial Alarm Real-Time Clock

DS1305 Serial Alarm Real Time Clock (RTC)

DS1306 Serial Alarm Real-Time Clock

A B C D E F 0480 FE B F5 3B FC F3 E 1A 1D 2A 2D 3A 3D 4A 4D 5A 5D 6A 6D 7A 7D

1.3inch OLED User Manual

DS1846 NV Tri-Potentiometer, Memory, and MicroMonitor

DS1855 Dual Nonvolatile Digital Potentiometer and Secure Memory

GT34C02. 2Kb SPD EEPROM

The I2C BUS Interface

DS1305. Serial Alarm Real Time Clock (RTC) FEATURES PIN ASSIGNMENT ORDERING INFORMATION

DS75 Digital Thermometer and Thermostat

256-Kbit (32 K 8) Serial (I 2 C) nvsram with Real Time Clock

Section 16. Basic Sychronous Serial Port (BSSP)

FM3104/16/64/256 Integrated Processor Companion with Memory Features

24LC08. 8K-Bit Serial EEPROM OVERVIEW FEATURES ORDERING INFORMATION

CryptoMemory Specification For Standard Mode of Operation

White Paper Using the MAX II altufm Megafunction I 2 C Interface

1-Mbit (128K 8) Serial (SPI) nvsram with Real-Time Clock

These three counters can be programmed for either binary or BCD count.

INTERFACING INTERFACING. Richa Upadhyay Prabhu. NMIMS s MPSTME February 25, 2016

Microcontroller Systems. ELET 3232 Topic 23: The I 2 C Bus

FM3104/16/64/256 Integrated Processor Companion with Memory Features

Introduction to I2C & SPI. Chapter 22

I 2 C Slave Controller. I 2 C Master o_timeout_intr

If It s Electronic, It Needs a Clock

Microcontrollers and Interfacing week 10 exercises


GT24C02. 2-Wire. 2Kb Serial EEPROM (Smart Card application)

Microcontrollers and Interfacing

Specification E2 Interface

DS1682 Total-Elapsed-Time Recorder with Alarm

SILICON MICROSTRUCTURES

DS1216 SmartWatch RAM DS1216B/C/D/H SmartWatch ROM DS1216E/F

FM24C64 64Kb FRAM Serial Memory Features

spi 1 Fri Oct 13 13:04:

64-Kbit/256-Kbit Integrated Processor Companion with F-RAM

ORDERING INFORMATION. OPERATION Measuring Temperature A block diagram of the DS1621 is shown in Figure 1. DESCRIPTION ORDERING PACKAGE

CoolRunner XPLA3 SMBus Controller Implementation

DS1485/DS1488. RAMified Real Time Clock 8K x 8 FEATURES PIN ASSIGNMENT

The 8255A: Programmable Peripheral Interface

DS1673 Portable System Controller

BL24C02/BL24C04/BL24C08/BL24C16

64-Kbit/256-Kbit Integrated Processor Companion with F-RAM

17. Serial communication - I 2 C

How to Implement I 2 C Serial Communication Using Intel MCS-51 Microcontrollers

Discontinued IP. OPB IIC Bus Interface (v1.01d) Introduction. Features. LogiCORE Facts

Programmable Interval Timer CEN433 King Saud University Dr. Mohammed Amer Arafah

64-Kbit (8 K 8) SPI nvsram with Real Time Clock

Display Real Time Clock (RTC) On LCD. Version 1.2. Aug Cytron Technologies Sdn. Bhd.

Handson Technology. I2C Specification and Devices. 1

JMY504M User's Manual

DS1216B. SmartWatch/RAM 16K/64K FEATURES PIN ASSIGNMENT PIN DESCRIPTION

Mask Set Errata. Introduction. MCU Device Mask Set Identification. MCU Device Date Codes. MCU Device Part Number Prefixes

DS1305 Serial Alarm Real-Time Clock

FM3135 Integrated RTC/Alarm/F-RAM & Embedded Crystal Features

JMY505G User's Manual

User-configurable Resolution. 9 to 12 bits (0.5 C to C)

Interface DAC to a PC. Control Word of MC1480 DAC (or DAC 808) 8255 Design Example. Engineering 4862 Microprocessors

EE 308 Spring Exam 1 Feb. 27

Digital Thermometer and Thermostat

FM24C02B/04B/08B/16B 2-Wire Serial EEPROM

Remote 16-bit I/O expander for Fm+ I 2 C-bus with interrupt and reset

DS Real Time Clock FEATURES PIN ASSIGNMENT PIN DESCRIPTION

ACE24C512C Two-wire serial EEPROM

Transcription:

The MC9S12 IIC Interface The IIC bus can control multiple devices using only two wires The two wires are Clock and Data The devices connect to the wires using a wired ND method The lines are normally high. ny device on the bus can bring them low. Each device on the bus has a unique address n IIC master starts the process by sending out a serial stream with the seven-bit address of the slave it wants to talk to, and an eighth bit indicating if it wants to write to the slave or read from the slave If it writes to the slave, it will continue send data on the serial data line until all the data is sent. If it reads from the slave, it will release the data line, and activate the clock line. The slave takes over the data line, and sends out its data in response to the clock provided by the master. fter all the data is transfered, the master releases both the clock and the data lines V DD SD Line SCL Line CLK1 OUT Data1 OUT CLK2 OUT Data2 OUT CLK1 IN Data1 IN CLK2 IN Data2 IN Device 1 Device 2 1

The IIC Interface Normally, both SD and SCL are high. When transmitting data, SD changes only when SCL is low. When the master wants to talk to a slave, it brings SD low while SCL is high (the Start condition). This gives the master control of the bus The master sends eight bits of data on the SD bus, and toggles the SCL bus for each bit. The eight bits are the seven-bit address of the slave, and one bit for read-write a low indicates a write, and a high indicates a read. fter the eighth data bit, the master releases the SD line. There is a ten-bit address mode discussed in the text, which we will not discuss The master releases the SD bus and sends a ninth clock pulse. The adressed slave responds with an acknowledge by bringing SD low. If no slave responds, master knows not to continue It the master is reading data from the slave, it controls the SCL line and the slave controls the SD line. The master sends eight clock pulses on the SCL line, and the slave transfers one bit of data on each clock pulse. t the end of the eight bits, the master takes over the SD bus and sends an acknowledge (CK) on all but the last byte it wants. fter the last byte the master wants, the master sends an NCK (leaves SD high). If the master is writing data to the slave, it continues controlling both SD and SCL. It sends eight bits of data, releases SD, and waits for an acknowledge from the slave fter the transfer is complete (for both read and write), the master sends a Stop condition to indicate that it is releasing the bus. The Stop condition is indicated by bringing SD high while SCL is high. S 7 bit slave address R/W 8 Data Bits 8 Data Bits P 1 (Read) Repeat for more data S 7 bit slave address R/W 8 Data Bits 8 Data Bits / P 0 (Write) Repeat for more data From master to slave From slave to master = cknowledge (SD low) = Not cknowledge (SD high) S = Start condition P = Stop condition 2

3

4 SD STRT CONDITION * 1 2 L 0 0 M 0 1 N 1 0 P 1 1 0 1 1 0 0 1 2 SHDN CK D7 D6 D5 D4 D3 D2 D1 D0 MSB LSB MSB LSB 1 8 9 10 17 18 *SEE ORDERING INFORMTION. SLVE DDRESS BYTE DC CODE CK STOP CONDITION

Using the IIC Interface The IIC uses five registers 1. IBD (IIC Bus ddress Register) This is the address of the IIC when it is addressed as a slave. We will use the IIC in master mode only. Write something like 0x01 to this register (any address will work, as long as it is not the address of a slave on the bus). 2. IBFD (IIC Bus Frequency Divide Register) This register determines the speed of the transfers. It also determines the hold times for start and stop conditions. Table 3-4 of the data sheet shows what the divide times and hold times are for all possible values of IBFD. The SCL Divider is the most important. If the slave device has a maximum frequency of 100 khz, then 24 MHz/100 KHz = 240, so the SCL Divider must be greater than or equal to 240. Looking at the table, a value of 0x23 will give a divider of 256, so that is the one to use. 3. IBCR (IIC Bus Control Register) This register controls the IIC. IBEN: Enable the IIC Bus IBIE: Enable interrupts MS/SL: Switch into master mode Tx/Rx: Switch between transmit and recieve TKK: Send an acknowledge RST: Send a restart IBSWI: Specify whether or not the IIC clock should operate in WIT mode 4. IBSR (IIC Bus Status Register) This register indicates the status of the IIC, and is used to clear interrupts bits. TCF: Transmit complete flag. Interrupt generated when TCF goes from low to high when IBEN is set. IS: ddressed as slave IBB: IIC Bus Busy IBL: rbitration lost SRW: Slave read/write IBIF: Interrupt flag. Clear by writing a 1 to this bit RXK: Received cknowledge 5. IBDR (IIC Bus Data Register) For both write to and read from the slave, first write to IBDR must be slave address plus R/W bit. Write data to this register to send to slave Read data from this register to receive from slave 5

Using the IIC Bus in Master Mode To use the IIC bus in master mode, do the following: Initialize the IIC Bus. Write to the IBFD register to set the clock speed, write to the IBD register to set a slave address, and write to the IBCR register to enable the IIC bus: IBFD = 0x23; // Set to 100 KHz operation IBD = 0x01; // Slave address 1 IBCR = IBCR BIT7; // Set IBEN To write data to a slave: Make sure the bus is not busy Write to IBCR to enable XMIT and MSTER. The MC9S12 automatically emits a start condition when you do this. Send data word which contains the device address and indicates a write Wait for the transfer to complete Make sure CK received from slave. (If not, do something reasonable) Send data, wait for CK, repeat until all data sent fter all data send, send a stop condition by taking MC9S12 out of master mode. /* Write data to device with address 0x68 */ while ((IBSR & BIT5) == BIT5) ; // Wait for IBB flag to clear IBCR = IBCR BIT4 BIT5; // Set XMIT and MSTER mode, emit start IBDR = 0xD0; // Send device address, R/W bit = 0 => write while ((IBSR & BIT5) == 0) ; // Wait for IBB flag to set while ((IBSR & BIT1) == 0) ; // Wait for IBIF to set, indicating transfer complet IBSR = BIT1; // Clear IBIF if ((IBSR & BIT0) == BIT0) iic_stop(); // Do something if no slave answers... // Repeat for more data IBCR = IBCR & ~BIT5; // Done; generate stop signal 6

Program to write data to an IIC slave #include "hcs12.h" void iic_init(void); void iic_start(char address); void iic_response(void); void iic_stop(void); void iic_transmit(char data); main() /* Write to RTC */ iic_init(); iic_start(0xd0); while ((IBCR & BIT5) == 0) // Make sure we still own bus iic_transmit(0); while ((IBCR & BIT5) == 0) ; // Make sure we still own bus... // Transmit more data iic_stop(); asm(" swi"); void iic_init(void) IBFD = 0x23; // Set to 100 KHz operation IBD = 0x02; // Slave address 1 IBCR = IBCR BIT7; // Set IBEN to enable IIC bus void iic_start(char address) while ((IBSR & BIT5) == BIT5) ; // Wait for IBB flag to clear IBCR = IBCR BIT4 BIT5; // Set XMIT and MSTER mode, emit start IBDR = address; // Send device address and R/W bit while ((IBSR & BIT5) == 0) ; // Wait for IBB flag to set iic_response(); // Wait for response void iic_response(void) while ((IBSR & BIT1) == 0) ; IBSR = BIT1; // Wait for IBIF to set // Clear IBIF // Stop if NK if ((IBSR & BIT0) == BIT0) iic_stop(); // DB12FNP->printf("In iic_response: CK received\n\r"); 7

/* Release bus and stop (only if transmitting */ void iic_stop(void) IBCR = IBCR & ~BIT5; void iic_transmit(char data) IBDR = data; // Write data to device iic_response(); // Wait for response 8

Dallas Semiconductor DS1307 Real Time Clock The DS 1307 is a real-time clock with 56 bytes of NV (non-volatile) RM It uses the IIC bus, with address 1101000 2 It stores date and time Data are stored in BCD format It uses a 32.768 khz crystal to keep time It can generate a square wave output Frequency of square wave can be 1 Hz, 4.096 khz, 8.192 khz or 32.768 khz It uses a battery to hold the date and time when your board is not powered Using the Dallas Semiconductor DS1307 Real Time Clock Find the SCL frequency Determine the value to write to IBFD to get that frequency or slower Make sure start condition and stop condition times met To set the clock, write address to clock (with R/W low), then write a 0 (to select seconds register), then send second, minute, hour, day of week, day of month, month, year, control Control determines whether or not to enable square wave, and selects frequency To read the clock, write the address to the clock (with R/W low), then write a 0 (to select seconds register). fter that, write the address to the clock (with R/W high), then read the time information. If you want to store some data which will remain between power cycles, you can write it to the 56 bytes of NV RM 9