How to use 51 microcontroller to implement IIC communication

Publisher:sumigLatest update time:2023-07-11 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There are no internal IIC communication resources in the previous MCS-51 series of microcontrollers. Therefore, if you want to use the 51 microcontroller to implement IIC communication, you can only simulate its timing through software, so that the function of IIC communication can also be realized.


How to use 51 microcontroller to implement IIC communication

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

#ifndef _IIC_H_

#define _IIC_H_

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

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

#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 instructions, addresses and data to an IIC device

extern uchar Read_Add(uchar add, uchar comd);//Write instructions 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 Responses(void)//Accept 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);

Responses();//wait for response

Write_Byte(add);

Responses();//wait for response

Write_Byte(wdata);

Responses();//wait for response

Signal_Stop();//Generate a termination signal

}

uchar Read_Add(uchar add, uchar comd)

{

uchartdata;

Signal_Start();//Generate a start signal

Write_Byte(comd);

Responses(); //wait for response

Write_Byte(add);

Responses();//wait for response

Signal_Start();//Generate another start signal

Write_Byte(comd|0x01);

Responses(); //wait for response

tdata = Read_Byte();

Signal_Stop();//Generate a termination 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--);

}


Reference address:How to use 51 microcontroller to implement IIC communication

Previous article:Design analysis of electronic therapeutic instrument based on embedded device AT89C51SNDlC
Next article:Program design of real-time monitoring of temperature and humidity based on microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号