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.
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--);
}
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
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How to learn FPGA technology?
- Please help me
- "Playing with the Board" + Replaying MicroPython on the STM32F7DISC (3)
- Make Magazine: The Rise of Python and the Microcontroller of the Year
- 【GD32450I-EVAL】+ 05FreeRTOS porting and task creation
- [Zero-knowledge ESP8266 tutorial] Quick start 27 Use of ADXL345 sensor module
- How to perform OTA upgrade on the OKT507-C development board on the Android system
- RT-Thread Application Practice-TI Temperature and Humidity HDC1000 Software Package Design and Production
- About Flash memory sectors, blocks, and pages
- Components Science Popularization: Component Selection Specifications