Simulate bus timing and bus timing with I/O Ports

Source: Internet
Author: User

Simulate bus timing and bus timing with I/O Ports

In the process of bus communication, we seldom use this method. Generally, when we select MCU, we will carry the communication interface you need. However, for some scenarios where simple communication should be used, in the data communication process of some sensors, the sensor manufacturers will make some changes to the communication protocol, these communication protocols do not have any standard provisions. So that the sensor has poor compatibility, and sometimes cannot find the MCU that can communicate with it, at this time, one method is to use the I/O port to simulate the timing of the communication bus (because of the I/O speed limit, it is generally only applicable to low-speed communication bus. Previously, I used I2C communication to do a temperature and humidity measurement project. This article uses an example to see how to use the I/O port to simulate the bus timing.

The RS232/485 commonly used by computers usually work in asynchronous working state with strict data clock restrictions, that is, what we call the baud rate, only two devices with the same baud rate can correctly communicate with each other. There is generally no strict time limit for synchronous communication. The bus uses high and low levels to identify whether the data is "0" or "1". There are two key moments: rising edge and falling edge. It reads and writes data when the rising and falling edges are used. That is to say, the communication frequency is not fixed because the devices that communicate "count" are the numbers of rising and falling edges, then read and write data on the data line. I have done some experiments to reduce the I2C communication frequency to about 10Hz. In this way, I can use an oscilloscope to capture every clock, and the communication result is correct.

Okay. Let's look at the case.

Generally, the I2C communication sequence is shown in. When the clock line is increased, a startup signal is generated when the data line is lowered. However, the start signal of the Sensor SHT is a pulse generated by the clock line when the data line is low, and then the data line is increased, the advantage of doing so is to ensure that the bus is correctly started to a certain extent, but it almost matches the MCU. At this time, we need to use the I/O simulation method to complete communication with SHT11.

 

 

 

1 # define iic_cl RC0 // I2C clock line 2 # define IIC_SDA RC1 // I2C data line 3 # define IIC_SCL_DIR TRISC0 // I2C clock Line Transmission Direction 4 # define IIC_SDA_DIR TRISC1/I2C data line transmission Direction 5 # define PORT_INPUT 1 6 # define PORT_OUTPUT 0 7 8 # define IIC_SCL_HIGH () IIC_SCL_DIR = PORT_INPUT // clock line height 9 # define IIC_SCL_LOW () IIC_SCL_DIR = PORT_OUTPUT; iic_cl = 0 // 10 # define IIC_SDA_HIGH () IIC_SDA_DIR = PORT_INPUT // data line height 11 # define IIC_SDA_LOW () IIC_SDA_DIR = PORT_OUTPUT; IIC_SDA = 0 // data cable down 12 13 14/***************** 15 * SHT11 startup time series 16 ******* * *******/17 void SHT_START (void) 18 {19 IIC_SCL_HIGH (); 20 IIC_SDA_HIGH (); 21 delay_us (5); 22 hour (); 23 delay_us (5); 24 IIC_SCL_LOW (); 25 delay_us (5 ); 26 IIC_SCL_HIGH (); 27 delay_us (5); 28 IIC_SDA_HIGH (); 29 delay_us (5); 30 IIC_SCL_LOW (); 31} 32/*************** 33 * SHT11 data transmission time series 34 *********** * ***/35 void SHT_SEND (uchar data) 36 {uchar I, data1; 37 for (I = 0; I <8; I ++) 38 {39 data1 = data <I; 40 if (! (Data1 & 0x80) 41 IIC_SDA_LOW (); 42 if (data1 & 0x80) 43 IIC_SDA_HIGH (); 44 IIC_SCL_LOW (); // pull down the clock line after writing one bit of data. Wait for 45 delay_us (5); 46 IIC_SCL_HIGH (); // The rising edge of the clock line, send 1-Bit Data 47 delay_us (5); // wait for 1-bit data to be sent 48} 49 IIC_SCL_LOW (); 50 IIC_SDA_HIGH (); // The 8-bit data is sent completely, data Line Height: Wait for the SLAVE device to respond to 51 delay_us (5); 52 IIC_SCL_HIGH (); // The Clock line height, whether the SLAVE device has a response 53 // while (IIC_SDA = 1); 54 delay_us (5); 55 IIC_SCL_LOW (); 56 IIC_SDA_HIGH (); // data line height, clock line low, waiting for the conversion to complete 57} 58/*************** 59 * SHT11 receives the data sequence 60 *********** * ***/61 uint SHT_REC (void) 62 {63 uint I; 64 uint REC1 = 0, REC0 = 0, REC = 0; 65 for (I = 0; I <8; I ++) 66 {67 IIC_SCL_HIGH (); // after the conversion is completed, the SLAVE device lowers the data line and generates a rising edge for reading 8-bit data from the height 68 REC1 = (REC1 <1) + IIC_SDA; 69 delay_us (5); 70 IIC_SCL_LOW (); // lower the clock line and wait for the arrival of the next rising edge 71 delay_us (5); 72} 73 SHT_ASK (); // After receiving the high 8-bit data, send the response signal 74 for (I = 0; I <8; I ++) 75 {76 IIC_SCL_HIGH (); // The conversion is completed, the SLAVE device lowers the data line and generates a rising edge for reading 8-bit data at a lower speed 77 REC0 = (REC0 <1) + IIC_SDA; 78 delay_us (5); 79 IIC_SCL_LOW (); 80 delay_us (5); 81} 82 SHT_STOP (); // After receiving the low 8-bit data, 83 REC = (REC1 <8) + REC0; 84 return REC; 85} 86/*************** 87 * SHT11 response time series 88 *************** /89 void SHT_ASK (void) 90 {91 IIC_SCL_LOW (); 92 IIC_SDA_LOW (); // data cable pulling down 93 delay_us (5); 94 IIC_SCL_HIGH (); // when the clock line is pulled, the response signal is 95 delay_us (5); 96 IIC_SDA_HIGH (); 97 IIC_SCL_LOW (); 98 delay_us (5 ); 99} 100/*************** 101 * SHT11 stop sequence 102 *************** /103 void SHT_STOP (void) 104 {105 rows (); 106 IIC_SCL_LOW (); 107 delay_us (5); 108 IIC_SCL_HIGH (); 109 delay_us (5); 110 IIC_SDA_HIGH (); 111 IIC_SCL_LOW (); 112}

 


How to use two I/O ports to simulate serial timing for communication

To design a protocol receiver after receiving a normal message to the sender, so that the sender can send new data, we suggest you look at the Tsinghua published Visual_Basic and _ RS-232 _ serial communication control
 
Is the I2C bus capable of using the same I/O ports as the single-chip microcomputer support as SPI? How can this problem be solved?

For the single-chip microcomputer of the main control, SDL must support two-way or semi-two-way output and input functions. Spi can also be simulated using software and common I/O Ports.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.