Sei sulla pagina 1di 6

Microprocessor Interfacing

Overview
Register and Bit Definitions
initI2C( )

I2C Inter-Integrated Circuit Example


Microprocessor Interfacing

Building Blocks Example


Write Byte Read Byte

I2C in PIC
4 Registers control the function I2C in the PIC microcontroller The values to be placed in the registers will often depend on your application See device datasheet for details The next few slides will discuss the registers that control the MSSP module, and how it is used for I2C in Master Mode.

SSPCON1
Here are the bits in the SSPCON Register:
WCOL SSPOV SSPEN SSPM3:SSPM0 (4 bits) Write Collision Overflow Enable Function Control

CKP is also in SSPCON but is not used for master mode

SSPCON1
WCOL stands for Write COLlision and is set when the user tries to write to SSPBUF, but the I2C bus is not ready Generally used for debugging or for MultiMaster Mode. SSPOV stands for SSP OVerflow and is set when there is an overflow error Read data in SSPBUF before new data comes in to prevent this

SSPCON1
SSPEN stands for SSP Enable Set SSPEN to 1 to turn on the MSSP module Leave on for the entire time the MSSP module is in use SSPEN can be cleared to 0 to disable the MSSP module and to help conserve power

Copyright James Grover, 2004 1

Microprocessor Interfacing
SSPCON1
SSPM3:SSPM0 control the MSSP mode. To enable Master Mode use the binary value of 1000

SSPCON21
Here are the bits in the SSPCON2 Register:
GCEN ACKSTAT ACKDT ACKEN RCEN PEN RSEN SEN General Call Enable ACK Bit Status ACK Transmit Data ACK Transmit Enable Receive Enable Stop Condition Enable Restart Condition Enable Start Condition
1

Not used in SPI mode

SSPCON2
GCEN allows an interrupt to be generated then an I2C general Call Address is generated. This feature is almost never used in I2C systems. This feature is not used in the example in this presentation

SSPCON2
ACKSTAT indicates whether an ACK (acknowledge) or NACK (not acknowledge) was sent by the slave When talking to an I2C device (a slave), the device will return either an ACK or NACK for each byte transferred The user must read and interpret these conditions in their program

SSPCON2
ACKDT is the data the master will transmit when ACKing an I2C device. When responding to an I2C device, there are only 2 possibilities: ACK and NACK, this bit controls which of the two is sent.

SSPCON2
ACKEN controls WHEN the PIC Microcontroller will send the ACK or NACK signal. Once the user program has set or cleared ACKDT to set up an ACK or NACK condition, set this bit to start sending

Copyright James Grover, 2004 2

Microprocessor Interfacing
SSPCON2
RCEN enables I2C receive mode. When the PIC Microcontroller must listen to data from another device, set RCEN RCEN automatically clears when one receive byte completes. This will cause the MSSP to revert back to transmit mode Set each time more data is to be received.

SSPCON2
PEN sends a STOP condition Remember, P refers to a stoP condition Set this to start sending a stop condition on the I2C bus. PEN automatically clears when the STOP condition completes.

SSPCON2
RSEN sends a RESTART condition Remember, R refers to a Restart condition Set this to start sending a restart condition on the I2C bus. RSEN automatically clears when the STOP condition completes. A restart condition is used when a start bit is needed, but there was no stop before it.

SSPCON2
SEN sends a START condition Remember, S refers to a Start condition Set this to start sending a start condition on the I2C bus. SEN automatically clears when the START condition completes.

SSPSTAT
Looking at the SSPSTAT Register, three bits help to control master mode I2C transfers:
SMP CKE BF Slew Rate Control Signal Level Control Buffer full

SSPSTAT
SMP enables a Slew rate control to reduce EMI in 400 kps mode. The slew rate is enabled by the user when using 400 kbps on I2C. If using 400 kbps on I2C, clear this bit to enable the slew rate control. If other rates, set SMP to 1 to disable the slew rate control.

Note: The other bits in this register are used for I2C, but not in the mode discussed for this presentation.

Copyright James Grover, 2004 3

Microprocessor Interfacing
SSPSTAT
CKE controls the I2C input levels When using standard I2C, this bit is cleared to meet I2C levels. When using SMBus (similar to I2C), the voltage levels are different. This bit is set to 1 to conform to SMBus levels.

SSPSTAT
BF stands for Buffer Full BF is set when the SSPBUF needs to be read BF is set and cleared by the PIC Microcontroller

SSPADD
The SSPADD register controls the speed of the I2C bus transmissions. It controls the baud rate generator. Calculating I2C Baud Rate: SSPADD =Fosc/[4 * (SSPADD + 1)]

SSPADD
Baud Rate Calculation:
Fosc = 4 MHz SSPADD = 9 (decimal)

Fosc/[4 * (SSPADD + 1)]


= 4 MHz / (9 + 1) * 4 = 4 MHz / 10 * 4 = 4 MHz / 40 = 100 kHz or 100 kbps

initI2C( )
void initI2C(void){ SSPBUF=0; //Initialize SSBUF to 0 //Setting the I2C Clock Rate (44 KHz @ 10 MHz) SSPADD = 0b00111000; //SDA and SCL Configuration TRISC4 = 1; TRISC3 = 1; //SSPCON1 setting SSPEN = 0 ; //Turn off while modifying mode SSPM3 = 1; //I2C Master mode, SSPM2 = 0; //SCK=Fosc/(4*(SSPADD+1)) SSPM1 = 0; SSPM0 = 0;

initI2C( )
CKP = 1; SSPEN = 1; //I2C Error TRISD0=0; //unused in Master mode //Enable I2C //ACK error bit as output

Copyright James Grover, 2004 4

Microprocessor Interfacing
Building Blocks of I2C
S void StartEnCmd(void){ SEN=1; while(SEN); } void stoPEnCmd (void) { PEN=1; while(PEN); } //Start I2C transfer //Send Start Bit //Wait for Send Bit to Clear //Stop I2C Transfer //Send Stop Bit //Wait for Stop Bit to Clear R

Building Blocks of I2C


void ReStartEnCmd(void){ //Repeated start command RSEN=1; //Send Resend Bit while(RSEN); //Wait for Resend Bit to Clear } void ACKEnCmd(unsigned char datum){ if(datum) ACKDT = 1; else ACKDT = 0; ACKEN = 1; //start acknowledge sequence }

Building Blocks of I2C


A void checkACK (void){ //Check Slave ACK Bit errorI2C.ACKError = ACKSTAT; //ACK error sticky bit } struct errorType { unsigned char unused : 7; unsigned char ACKError : 1; }; Data

Building Blocks of I2C

void putByte(unsigned char datum){ SSPIF = 0; //clear pending SSP interrupt SSPBUF = datum; //transmit data while(SSPIF == 0);//wait for end-of-transmission (EOT) }

Building Blocks of I2C


Data unsigned char getByte(void){ unsigned char datum; SSPIF=0; //Clear Interrupt Flag to begin transfer datum=SSPBUF; //Copy SSPBUF to the character buffer ACKEnCmd(ACK); //Send master acknowledge bit while(SSPIF==0); //Wait for Interrupt Flag to return high return(datum); }

Write Example
Data Data P Master S Data Slave A A A
void writeByteI2C (char commandW, char addr, char datum){ StartEnCmd(); //send start condition putByte(commandW); //send slave address checkACK(); //Check if Slave ACK Bit was set putByte(addr); //Send Hi-address of start address checkACK(); //Check if Slave ACK Bit was set putByte(datum); //Copy SSPBUF to the character buffer checkACK(); //Check if Slave ACK Bit was set stoPEnCmd(); //Stop I2C Transfer }

Copyright James Grover, 2004 5

Microprocessor Interfacing
Read Example
Master S Data Data R Data N P Slave A A A Data
unsigned char readByteI2C (char commandW, char commandR, char addr){ char datum; StartEnCmd(); //send start condition putByte(commandW); //send slave address checkACK(); //Check if Slave ACK Bit was set putByte(addr); //Send address of start address checkACK(); //Check if Slave ACK Bit was set ReStartEnCmd(); //send restart command putByte(commandR); //Copy SSPBUF to the character buffer checkACK(); //Check if Slave ACK Bit was set datum=getByte(); //Get byte from slave ACKEnCmd(nACK); //Master doesn't send ACK bit ending transfer stoPEnCmd(); //Stop I2C Transfer }

ACLU #19B
When should one use a restart condition instead of a start condition? Write a fragment of code that does the ACK polling.

Summary
Register and Bit Definitions
initI2C( )

Building Blocks Example


Write Byte Read Byte

Copyright James Grover, 2004 6

Potrebbero piacerti anche