Simulating IIC Bus Timing Based on 51 Single-Chip Microcomputer

Publisher:andyliow1980Latest update time:2017-01-03 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Recently, I need to use IIC bus operation when using the light measurement module BH1750FVI, so I spent some time to learn it and basically understood it. So, I used the IO port of 51 to simulate the bus timing and correctly operate a series of modules that need to be accessed by IIC bus. I originally wanted to write an article to briefly introduce my understanding of IIC bus, but I found that I didn’t have time, so I will do it later. Today I will only give a sample program, which can work well on my 51 single-chip computer system. iic.h lists all the operation functions of IIC bus. The source code is given below:


//iic.h - implementation related header file

//Girls Don't Cry 2013-01-18

#ifndef __IIC_H__

#define __IIC_H__


void iic_start(void);

void iic_stop(void);

bit iic_get_ack(void);

void iic_write_byte(uchar dat);

uchar iic_read_byte(void);

void iic_send_ack(bit ack);


#endif//!__IIC_H__



//IIC bus simulation implementation file

//Girls Don't Cry 22:11 2013-1-18

#include "common.h"

#include "iic.h"


sbit SDA = P1^2;

sbit SCL = P1^0;


//Generate START signal

void iic_start(void)

{

    SDA = 1; //need to be set before SCL

    SCL = 1; //Hardware enters SDA detection state

    delay_us(5); //Delay at least 4.7us

    SDA = 0; //SDA changes from 1->0, generating a start signal

    delay_us(5); //delay at least 4us

    SCL = 0; //SCL becomes invalid

}


//Generate STOP signal

void iic_stop(void)

{

    SDA = 0; // pull low before SCL

    SCL = 1; //Hardware enters SDA detection state

    delay_us(5); //Delay at least 4us

    SDA = 1; //SDA changes from 0->1, generating an end signal

    delay_us(5); //Delay at least 4.7us

}


//Write 1 byte to the IIC bus

void iic_write_byte(uchar dat)

{

    uchar loop = 8; //Must be one byte

    while(loop--){

        SDA = dat&0x80; //Write bit by bit starting from MSB

        SCL = 1;

        delay_us(5); //delay at least 4us

        SCL = 0;

        dat <<= 1; //Move from low to high

        delay_us(5);

    }

}


//Read 1 byte from the IIC bus

uchar iic_read_byte(void)

{

    uchar loop = 8; //Must be one byte

    uchar ret = 0;

    while(loop--){

        SDA = 1; //8051 internal resistor pull-up

        SCL = 1;

        delay_us(5); //delay at least 4us

        ret <<= 1;

        ret |= SDA; //read 1 bit

        SCL = 0 ;

        delay_us(5);

    }

    return ret;

}


//Send response code from master to slave

//0-ACK,1-NAK

void iic_send_ack(bit ack)

{

    SDA = ack; //Generate response level

    SCL = 1; //Send response signal

    delay_us(5); //delay at least 4us

    SCL = 0; //Maintain the response signal during the whole period

}


//Get the response code from slave to master

bit iic_get_ack(void)

{

    bit ret; // used to receive the return value

    SDA = 1; //Resistor pull-up, enter read (8051)

    SCL = 1; //Enter response detection

    delay_us(5); //Delay at least 4us

    ret = SDA; //Save the response signal

    SCL = 0;

    return ret;

}



  The above code gives all the IIC bus programs.


  Functions implemented in common.h and related definitions


Reference address:Simulating IIC Bus Timing Based on 51 Single-Chip Microcomputer

Previous article:AT93C46 read and write program (C language) through debugging
Next article:8051 MCU Study Notes/Summary/Summary/Memo

Recommended ReadingLatest update time:2024-11-16 16:33

Application of AT89C2051 MCU in wireless doorbell alarm
  Doorbells were rarely heard in ancient China. Rich families would install decorative door knockers on their front doors. People who knocked at the door could use the door knockers to hit the door nails under the knockers to make a loud noise, which served the same purpose as the modern "doorbell".   Nowadays, wire
[Microcontroller]
Application of AT89C2051 MCU in wireless doorbell alarm
AT89C51 is the first choice for microcontroller beginners
      1 Introduction   In 1980, Intel developed a simple 8031 ​​CPU. At that time, the performance of the CPU was not much worse than that of the 8086, but the price was relatively cheap, so it was selected by many low-end applications. As the market was optimistic, ATMEL purchased the 8031 ​​core, integrated the Flas
[Microcontroller]
51 single chip microcomputer program - use buttons to control the start and stop of the buzzer
#include   #define uint unsigned int //macro definition sbit SPK=P3^5;             //define speaker port sbit key=P3^1;             //The corresponding key on the development board is s18   void delay(uint z) { uint x,y; for(x=z;x 0;x--) for(y=110;y 0;y--); }   void main() {     while(1)
[Microcontroller]
51 MCU 4×4 matrix key program
ORG 0000H LJMP MAIN ; Jump to the main program ORG 0100H MAIN: LCALL KEY_IN MOV P0,A LCALL DEL AJMP MAIN ;====================== ; Check if there is a key, if there is no key, return directly ;KEY_IN: MOV P1,#0F0H ; Set the row line to low level, read the column line status (in the upper 4 bits, all are 1 if no key is
[Microcontroller]
8051 MCU serial port baud rate and timer 1 initial value calculation
In mode 0 and mode 2, the serial port baud rate is fixed, mode 0 is Fosc/12, mode 2 is Fosc/32 or Fosc/64, which is determined by the SMOD bit of the PCON register. In mode 1 and mode 3, the baud rate is a variable value. The baud rate can be generated by timer 1 (8052 can be generated by timer 2). So what is the baud
[Microcontroller]
Design of RF card based on MCU AT89C51CC01 and FM1712 chip
Radio frequency card (contactless IC card) is a new technology developed in recent years. Compared with traditional contact IC card and magnetic card, the contactless IC card developed by radio frequency identification technology has successfully solved the problems of passive and contactless, which is a major break
[Microcontroller]
Design of RF card based on MCU AT89C51CC01 and FM1712 chip
#51 MCU #DS18B20 hardware principle and communication working sequence
DS18B20 Hardware Principle - Temperature Memory The temperature measurement range of DS18B20 is -55~+125°C. As shown in the figure above, the DS18B20 temperature memory has two bytes in total. LSB is the low byte, MSB is the high byte. Msb is the high bit, Lsb is the low bit. The S in the figure represents the sign
[Microcontroller]
#51 MCU #DS18B20 hardware principle and communication working sequence
GPS positioning and track recorder made of stc51 single chip microcomputer
GPS positioning and track recorder based on C3-370C! I bought a C3-370C GPS from 51hei and prepared to do GPS track recording, because I always wanted to add coordinate information to travel photos... I started to use M48 as the control MCU, but when I was writing, I found that the Flash space was insufficient. I wa
[Microcontroller]
GPS positioning and track recorder made of stc51 single chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号