Lab 10. Very informative

12
1 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram EE-324Microprocessors and Interfacing LAB 10 I2C and SPI Communication Technique Date: __________________ Instructor: Dr. Jahangir Ikram Lab Engineer: M. Anas Teaching Assistants: Muhammad Nouman Qaiser, Muhammad Ali Tariq, Bilal Ahmed, Muhammad Usama, Muhammad Siddique Khan Student Roll Number: ______________________________ Marks: What Is SPI and I2C? SPI Stand for Serial peripheral interface. We have already studied serial port communication which we have done in lab by MAX232. The difference between Communication with MAX232 and SPI is synchronization. The serial communication of PIC Microcontroller and MAX232 was asynchronous. But the communication between PIC microcontroller and other device through SPI will be synchronous. The difference between Synchronous and asynchronous communication is clock cycling. We Max and Controller do not control each other by clock cycle. They just transmit and receive data without cycling. But let suppose if we want to synchronize two PIC microcontrollers for data sending we will use SPI Communication which is synchronous. They control each other with the mean of clock cycles. We will do in this lab not in between controllers but also in other SPI control devices. I2C stand for Inter-Integrated Circuit Communications. It is almost same as SPI. But it has different setting as compare to SPI. It is used to communicate data to and from a PICmicro Microcontroller and a serial EEPROM. It is also consistent at synchronous communication between two devices. I2C is a popular protocol and is supported by many devices. This presentation answers some questions about I2C and explains with a full example how to connect a PICmicro MSSP module to an EEPROM. I2C is also bi-directional. This is implemented by an “Acknowledge” system. The “Acknowledge” system or “ACK” system allows data to be sent in one direction to one item on the I2C bus, and then, that item will “ACK” to indicate the data was received. We will look at this in detail later, as you can see, this is a powerful feature of I2C. Since a peripheral can acknowledge data, there is little confusion on whether the data reached the peripheral and whether it was understood. Initialization of SPI:- There are two ways to initialize SPI in Microcontroller in MicroC Compiler.I am telling you this because in future, there might be other compilers which will need all control register to be initialize. By Default configuration: In this configuration we will not need to do anything because our compiler will initialize all SSPCON registers according to build code which will give the following setting MASTER MODE FOSC/4 Clock idle cycle will be low Data will flow from low to high edge

description

This is some lab 10.

Transcript of Lab 10. Very informative

Page 1: Lab 10. Very informative

1 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

EE-324–Microprocessors and Interfacing – LAB 10

I2C and SPI Communication Technique

Date: __________________ Instructor: Dr. Jahangir Ikram Lab Engineer: M. Anas

Teaching Assistants: Muhammad Nouman Qaiser, Muhammad Ali Tariq, Bilal Ahmed, Muhammad Usama, Muhammad

Siddique Khan

Student Roll Number: ______________________________ Marks:

What Is SPI and I2C?

SPI Stand for Serial peripheral interface. We have already studied serial port communication which we have done

in lab by MAX232. The difference between Communication with MAX232 and SPI is synchronization. The serial

communication of PIC Microcontroller and MAX232 was asynchronous. But the communication between PIC

microcontroller and other device through SPI will be synchronous.

The difference between Synchronous and asynchronous communication is clock cycling. We Max and Controller do

not control each other by clock cycle. They just transmit and receive data without cycling. But let suppose if we want to

synchronize two PIC microcontrollers for data sending we will use SPI Communication which is synchronous. They control

each other with the mean of clock cycles. We will do in this lab not in between controllers but also in other SPI control

devices.

I2C stand for Inter-Integrated Circuit Communications. It is almost same as SPI. But it has different setting as

compare to SPI. It is used to communicate data to and from a PICmicro Microcontroller and a serial EEPROM. It is also

consistent at synchronous communication between two devices. I2C is a popular protocol and is supported by many devices. This presentation answers some questions about I2C and explains with a full example how to connect a PICmicro MSSP module to an EEPROM.

I2C is also bi-directional. This is implemented by an “Acknowledge” system. The “Acknowledge” system or “ACK” system allows data to be sent in one direction to one item on the I2C bus, and then, that item will “ACK” to indicate the data was received. We will look at this in detail later, as you can see, this is a powerful feature of I2C. Since a peripheral can acknowledge data, there is little confusion on whether the data reached the peripheral and whether it was understood.

Initialization of SPI:- There are two ways to initialize SPI in Microcontroller in MicroC Compiler.I am telling you this because in future,

there might be other compilers which will need all control register to be initialize.

By Default configuration:

In this configuration we will not need to do anything because our compiler will initialize all SSPCON registers

according to build code which will give the following setting

MASTER MODE

FOSC/4

Clock idle cycle will be low

Data will flow from low to high edge

Page 2: Lab 10. Very informative

2 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

Input data sample in the middle of Interval

CUSTOM configuration:

This depends on your use. That has been show in the following picture.

SPI Device Identification:

The identification of SPI communication system can be done by MSSP and SSP Module which manufacturer will

be mentioned in Datasheet.

SPI whole communication Registers and PINs:-

SS: This signal is known as Slave Select. When it goes low, the slave device will listen for SPI clock and data signals.

SCK: This is the serial clock signal. It is generated by the master device and controls when data is sent and when it is read.

SDO: This is the Serial Data Output signal. SDO carries data out of a device.

SDI: SDI is the Serial Data Input line. It carries data into a device. It must be noted that SDI of one device should be connect

with the SDO of Other Device Vise versa.

Page 3: Lab 10. Very informative

3 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

SPPRS: SSPSR is the shift register for the SPI module. It shifts data in and out of the device. The data travels in a loop to the

next shift register. The data is shifted out the SDO pin of one device and into the SDI pin of the other.

SSPBUF: Once a byte of data has been exchanged between the two devices, it is copied to the SSPBUF register. The SSPBUF

is then read by the user software.

SSPOV: SSPOV - indicates “Synchronous Serial Port Overflow”

SSPEN: SSPEN - stands for “Synchronous Serial Port Enable”

CKP: CKP - controls “ClocK Polarity”

SSPM: SSPM bits 3 though 0. SSPM bits control if the SSP module is in an SPI mode and if in Master mode, they also help

to configure the clock rate.

I2C Initialization:- It has all auto configuration but we need to take care of starting and stopping I2C Module.

SCL:

This is the Serial Clock signal. It is generated by the master device and controls when data is sent and when it is read. As

mentioned earlier, the signal can be forced low so that no clock can occur. This is done by a device that has become too

busy to accept more data.

Normally, the master device controls the clock line, SCL. This line dictates the timing of all transfers on the I2C bus. Other

devices can manipulate this line, but they can only force the line low. This action means that item on the bus can not

Deal with more incoming data. By forcing the line low, it is impossible to clock more data in to any device. This is known as

“Clock Stretching”.

As stated earlier, no data will be transferred unless the clock is manipulated. All slaves are controlled by the same clock,

SCL.I2C is a Bi-directional protocol. Data can flow in any direction on the I2C bus, but when it flows is controlled by the

master device

SDA:

From this pin data will flow in both direction. But will be controlled by Master device.

I2C lines can have only two possible electrical states. These states are known as “float high” and “drive low”. I2C works by

having a pull-up resistor on the line and only devices pull the line low. If no device is pulling on the line, it will “float high”.

This is why pull-up resistors are important in I2C.

If no pull-up resistor were used, the line would float to an unknown state. If one tried to drive the line high, it might cause contention with a device trying to drive the line low. This contention could damage the either or both devices driving the line.

To prevent this, the pull-up-drive low system controls when one device has control of the bus. If another device

tried to use the bus when it was busy, it would find the bus to be driven low already and know it was busy. Even if it tried to

use the bus accidentally, it would only drive it low and not damage other devices. The diagrams shown are symbolic. In each

case, the solid diagram represents the ACTIVE part of the bus. In the case of driving low, the buffer is actively pulling the

Page 4: Lab 10. Very informative

4 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

line low. In the case of floating high, the resistor pulls the line high, while the buffer is turned off. A buffer turned off has

very high impedance and behaves as if it were disconnected. Only the output buffers are shown for simplicity.

The first element we need to look at is the Start condition. A start condition indicates that a device would like to transfer data on the I2C bus. Pictured here is the block with an “S” in it and what it the signals look like on the I2C bus. As you can see, SDA is first pulled low, followed by SCL. The PICmicro microcontroller will take care of the timing details for you. However it will need to be told you want a start condition and you will check for when it completes. We will look at how these blocks relate to using a PICmicro device later.

Stop Condition: The next element we will discuss is the Stop condition. A start condition indicates that a device has finished its transfer on the I2C bus and would like to release the bus. Once released other devices may use the bus to transmit data. As you can see, a block with a “T” in it represent the stop condition? A “T” is used because “S” was already used for start earlier. This convention will continue to be used thought this presentation. The signaling used for a stop is a release of the SCL line followed by a release of the SDA line. Remember that releasing a line turns off the driver, and since there is a pull-up resistor on it, the line floats high. Once the stop condition completes, both SCL and SDA will be high. This is considered to be an idle bus. Once the bus is idle a Start condition can be used to send more data. Again, the PICmicro microcontroller will take care of the timing details of this for you. You will only need to tell it you want a stop condition and wait for it to complete.

Page 5: Lab 10. Very informative

5 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

But we will use build in functions.

I2C Example:

I did this task with the synchronization of 24C02 IC which is 2k EEPROM (Electrical Erasable Programmable Read only memory). I am using this IC because it has build in I2C module which is working as slave in this for my I2C task. There are many EEPROM devices those are used to control by I2C serial communication. In this task I sent the data to 24C02 IC and then I read it back to PORTB where I used the LEDs to detect it successfully. You have to make sure that you have contact the SDA and SCL pins of PICmicro with the SDA and SCL PINs of 24C02 IC. void main(){ PORTB = 0; TRISB = 0; // Configure PORTB as output I2C1_Init(100000); // initialize I2C communication I2C1_Start(); // issue I2C start signal I2C1_Wr(0xA2); // send byte via I2C (device address + W) I2C1_Wr(2); // send byte (address of EEPROM location) I2C1_Wr(0xAA); // send data (data to be written) I2C1_Stop(); // issue I2C stop signal

Page 6: Lab 10. Very informative

6 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

Delay_100ms(); I2C1_Start(); // issue I2C start signal I2C1_Wr(0xA2); // send byte via I2C (device address + W) I2C1_Wr(2); // send byte (data address) I2C1_Repeated_Start(); // issue I2C signal repeated start I2C1_Wr(0xA3); // send byte (device address + R) PORTB = I2C1_Rd(0); // Read the data (NO acknowledge) I2C1_Stop(); // issue I2C stop signal } In this program I have sent to each and every this has been shown in the Comments.

I2C1_Wr(0xA2); Device code will be selected with MSB bits and CHIP Enable will be selected by LSB bits.

Page 7: Lab 10. Very informative

7 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

I2C Task 1: This code sets only seconds, minutes and hours and shows it on

screen. Understand the coder properly and try to set Day, Date and

Month and show it to TA. Proteus diagram is given in end. // Lcd pinout settings sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D7 at RB5_bit; sbit LCD_D6 at RB4_bit; sbit LCD_D5 at RB3_bit; sbit LCD_D4 at RB2_bit; // Pin direction sbit LCD_RS_Direction at TRISB0_bit; sbit LCD_EN_Direction at TRISB1_bit; sbit LCD_D7_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB2_bit; void Controller_I2C_Data_Transmit(); void Controller_I2C_Data_Receive(); unsigned short temp,i=0; unsigned short received_read[4]; unsigned char Conv[2]; //void Controller_I2C_Data_Receive(); void main() { trisb=0; trisa=0; I2C1_Init(100000); LCD_init(); Controller_I2C_Data_Transmit(); while(1){ Controller_I2C_Data_Receive(); } } //................COntroller to DS1307................. void Controller_I2C_Data_Transmit(){ I2C1_START(); I2C1_WR(0XD0); //Tell Ds1307 that i am going to write on yur Register DELAY_US(10);

Page 8: Lab 10. Very informative

8 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

I2C1_WR(0X00); // This is the Register Location where it is going to write I2C1_WR(0X30); // On 0x00 address it will write seconds of Time //this location is given in Data sheet delay_us(10); I2C1_WR(0X59); // This will be the next location (0x01) it writes the minutes of time DELAY_US(10); I2C1_WR(0X08); // this is location the location of Hours of time I2C1_STOP(); } //.................DS1307 to controller............. void Controller_I2C_Data_Receive(){ I2C1_Start(); I2C1_WR(0XD0); // This is again to tell to Ds1307 i am going to write I2C1_WR(0X00); // where to write it will make reset Adress Register of DS1307 I2C1_Repeated_Start(); // Restart the DS1307 I2C1_WR(0XD1); //tells the RTC that microcontroller will read from its memory DELAY_US(10); temp=I2C1_Rd(1); // It will read the data from 0x00 location of DS1307 temp=Bcd2Dec16(temp); // our BCD to decimal conversion WordtoStr(temp,Conv); // decimal to string to display on LCD lcd_out(1,11,Conv); temp=I2C1_Rd(1); //it will read the data from 0x01 or next address location of DS1307 temp=Bcd2Dec16(temp); // with acknowledge signal . 1 in read function will show the Acknowledge WordtoStr(temp,Conv); // signal lcd_out(1,6,Conv); temp=I2C1_Rd(0); //it will read the data from 0x01 or next address location of DS1307 temp=Bcd2Dec16(temp); // with no acknowledge signal. "0" in read represent not acknoledge WordtoStr(temp,Conv); lcd_out(1,1,Conv); I2C1_STOP(); // Stop I2C to work }

Page 9: Lab 10. Very informative

9 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

I have shown the Address picture of DS1307. In this picture our seconds are at position 0x00 address. Minutes are at 0x01h address and so on. In coding I need just the starting address of DS1307 which is 00h and others address will automatically update in next instructions. But in reading case, when we would start to read the data from Ds1307 then we have to bring our pointer back to ooh address. In read function first three lines are used for this purpose. Then restart the I2C and start reading. It will read automatically from RTC IC.

Simulation Analysis: In write function I have send 30 first then 59 and finally 08 which represent seconds, Minutes and hours respectively. Change date yourself. Now in this picture these are the results after analysis. I2Cdebugger has diplayed this result. I will explain it step by step.

1) First “S” tells that I2C has been start 2) “D0” is the writing address of Ds1307 3) “A” is acknowledge signal back to controller shows that DS1307 is ready for next instructions 4) “00” is the register Address of DS1307 where to write the time setting 5) “A”is again acknowledement signal 6) “30” respresent the seconds to set at DS1307 7) “A”is again acknowledement signal 8) “59” are minutes to set at DS1307 9) “A”is again acknowledement signal 10) “08” are Hours to set at DS1307 11) “1-7” is the day of week if you will set it in I2C1_wr(1);then It wil be Sunday if 2 then Monday and so on. These same setting has been shown into the blow table.

Page 10: Lab 10. Very informative

10 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

Page 11: Lab 10. Very informative

11 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

SPI Task2: Understand the code, Run it on Proteus and show it to TA. sbit Chip_Select at RC0_bit;

sbit Chip_Select_Direction at TRISC0_bit;

// End DAC module connections

unsigned int value;

void InitMain() {

TRISD=0xFF;

Chip_Select = 1; // Deselect DAC

Chip_Select_Direction = 0; // Set CS# pin as Output

SPI1_Init(); // Initialize SPI module

}

// DAC increments (0..4095) --> output voltage (0..Vref)

void DAC_Output(unsigned int valueDAC) {

char temp;

Chip_Select = 0; // Select DAC chip

// Send High Byte

temp = (valueDAC >> 8) & 0x0F; // Store valueDAC[11..8] to temp[3..0]

temp |= 0x30; // Define DAC setting, see MCP4921

datasheet

SPI1_Write(temp); // Send high byte via SPI

// Send Low Byte

temp = valueDAC; // Store valueDAC[7..0] to temp[7..0]

SPI1_Write(temp); // Send low byte via SPI

Chip_Select = 1; // Deselect DAC chip

}

void main() {

InitMain(); // Perform main initialization

value = 2048; // When program starts, DAC gives

// the output in the mid-range

while (1) { // Endless loop

if ((RD0_bit) && (value < 4095)) { // If RA0 button is pressed

value++; // increment value

}

else {

if ((RD1_bit) && (value > 0)) { // If RA1 button is pressed

value--; // decrement value

}

}

DAC_Output(value); // Send value to DAC chip

Delay_ms(1); // Slow down key repeat pace

}

}

Page 12: Lab 10. Very informative

12 Microcontroller and its Interfacing- Lab8 Dr Jahangir Ikram

You will perform this Task on the Protues software. CONNECT 2 PUSH BUTTONS ON RD1 AND RD2. SPI_init(); SPI_Read(); Spi_write(); I have mentioned one example. Just analysis it. Everything is mention in the above code.