How to realize IIC communication with 51 single chip microcomputer

There is no IIC communication resource in the previous MCS-51 series microcontroller, so if you want to use the 51 microcontroller to realize IIC communication, you can only simulate its timing through software, which can also realize the function of IIC communication.


How to realize IIC communication with 51 single chip microcomputer

This is the header file of IIC, which is easy to use and call:

#ifndef _IIC_H_

#define _IIC_H_

/***The macro definitions of ucahr and uint are very important, otherwise the following functions will not work properly ******/

#define uchar unsigned char //Define uchar data as unsigned

#define uint unsigned int //Define uint type data as unsigned type

sbit SCL = P2^3;

sbit SDA = P2^4;

/***Declare external functions****/

extern void delay_1ms(void);

extern void IIC_Init(void);//IIC initialization

extern void Signal_Start(void);//IIC stop signal

extern void Signal_Stop(void);//IIC stop signal

extern void Write_Byte(uchar wdata);//Write a byte data function

extern uchar Read_Byte();//Read a byte data function

extern void Write_Add(uchar add, uchar wdata, uchar comd);//Write instruction, address and data to an IIC device

extern uchar Read_Add(uchar add, uchar comd);//Write instruction to an IIC device to read the data in an address

#endif

The following is the C language source code for IIC communication:

#include

#include

#include "IIC.h"

#define uchar unsigned char

#define uint unsigned int

void IIC_Init(void) //IIC initialization

{

SDA = 1;

delay_1ms();

SCL = 1;

delay_1ms();

}

void Signal_Start(void)//IIC start signal

{

SDA = 1;

delay_1ms();

SCL = 1;

delay_1ms();

SDA = 0;

delay_1ms();

}

void Signal_Stop(void)//IIC stop signal

{

SDA = 0;

delay_1ms();

SCL = 1;

delay_1ms();

SDA = 1;

delay_1ms();

}

void Respons(void)//Acknowledge signal

{

uint i = 0;

SCL = 1;

delay_1ms();

while((sda==1)&&(i<=300))

i++;

SCL = 0;

delay_1ms();

}

void Write_Byte(uchar wdata)

{

uchar i, mdata;

mdata = wdata;

for(i=0;i<8;i++)

{

mdata <<= 1;

SCL = 0;

delay_1ms();

SDA = CY;

delay_1ms();

SCL = 1;

delay_1ms();

}

SCL = 0;

delay_1ms();

SCL = 1;

delay_1ms();

}

uchar Read_Byte()

{

uchar i, rdata = 0;

SCL = 0;

delay_1ms();

SCL = 1;

for(i=0;i<8;i++)

{

SCL = 1;

delay_1ms();

rdata = (rdata<<1)|SDA;

SCL = 0;

delay_1ms();

}

return rdata;

}

void Write_Add(uchar add, uchar wdata, uchar comd)

{

Signal_Start();//Generate a start signal

Write_Byte(comd);

Respons();//Wait for the answer

Write_Byte(add);

Respons();//Wait for the answer

Write_Byte(wdata);

Respons();//Wait for the answer

Signal_Stop();//Generate a stop signal

}

uchar Read_Add(uchar add, uchar comd)

{

uchar tdata;

Signal_Start();//Generate a start signal

Write_Byte(comd);

Respons(); //waiting for response

Write_Byte(add);

Respons();//Wait for the answer

Signal_Start();//Generate a start signal again

Write_Byte(comd|0x01);

Respons(); //waiting for response

tdata = Read_Byte();

Signal_Stop();//Generate a stop signal

return tdata;

}

void delay_1ms (void) //error 0us delay 1ms

{

uchar a, b, c;

for(c=1;c>0;c--)

for(b=142;b>0;b--)

for(a=2;a》0;a--);

}

Ego E cigarette

Suizhou simi intelligent technology development co., LTD , https://www.msmsmart.com