Using s5pc100 Gpio to simulate IIC

Source: Internet
Author: User

Reprint: http://blog.chinaunix.net/uid-26833883-id-3823530.html

first, the hardware connection

Let's take a look at the wiring on the LM75 hardware:

Where i2c_sda0 is the data line, I2c_scl0 is the clock line, they are received s5pc100 GPD3 and GPD4, as shown in

When simulating IIC, use the GPD3 pin to send data or read data, with the GPD4 pin to provide a clock signal.

second, data structuredue to the large number of pins in this group of S5pc100 's GPD, it is not good to have a single pin operation on one of them, in order to be able to operate alone on one of the pins, the C language bit field is used here.
typedefstruct{uint8 gpddat_0:1; Uint8 gpddat_1:1; Uint8 gpddat_2:1; Uint8 Gpddat_3:1; Uint8 Gpddat_4:1; Uint8 gpddat_5:1; Uint8 gpddat_6:1; Uint8 gpddat_7:1; } gpddat_t; #defineGpd_dat (* (volatile gpddat_t *) 0xe0300084)#defineSDA Gpd_dat. Gpddat_3#defineSCL Gpd_dat. Gpddat_4

third, the Operation function(1) Production of IIC start signalWhen the SCL is high, SDA jumps from high to low to start transmitting data
/*IIC START:SCL = 1,SDA = 1->0*/ voidIic_start () {SDA=High ; SCL=High ; Delay ( -); //high to low jumps produce a start signalSDA =Low ; Delay ( -); //SDA must remain stable at SCL high, and SDA can change arbitrarily when the SCL is low//The goal of lowering the SCL here is to send the data nextSCL =Low ; Delay ( -); return; } 

(2) Production of IIC stop signalWhen the SCL is high, SDA jumps from low to high, ending the transfer of data
/*IIC STOP:SCL = 1,SDA = 0->1*/ voidIic_stop () {SDA=Low ; SCL=Low ; Delay ( -); SCL=High ; Delay ( -); //When the SCL is high, SDA jumps from low to high//to produce a stop signalSDA =High ; Delay ( -); return; } 

(3) Send data
/*Write 1 Byte to IIC*/ voidiic_write_byte (uint8 data) {uint8 loop;  for(Loop =8; Loop >0; Loop--) {//first send the highest bit, at the SCL high, SDA must remain stableSDA = Data >>7; SCL=High ; Delay ( -); //When the SCL is low, SDA can change arbitrarilySCL =Low ;//move low to highData <<=1; Delay ( -); }   return; } 

(4) reading data
/*Read 1 byte from IIC*/uint8 Iic_read_byte () {uint8 loop; Uint8 value=0;  for(Loop =8; Loop >0; Loop--) {SCL=High ; Delay ( -); Value<<=1; //read 1-bit dataValue |=SDA; SCL=Low ; Delay ( -); }             returnvalue;} 

(5) The host sends an ACK signal to the slave machine
void Iic_send_ack () {     = high ;      = Low ;     Delay (a);      = Low ;     Delay (a);      return

(6) Get the ACK signal from the machine to the host
uint8 Iic_get_ack () {     uint8 ret;      = High ;     Delay (a);       = SDA;      = Low ;     Delay (a);      return ret;   

Four, read the temperature value of LM75 measurementthe timing of the temperature values measured by the IIC reading LM75 is as follows:

The instance code is as follows:

Unsignedint__read_lm75 () {uint8 ack;     Uint8 High,low; //set the PIN for the IIC connection to output modeSet_gpio_mode (GPD. Gpdcon,3,1); Set_gpio_mode (GPD. Gpdcon,4,1); Delay ( -); //generate a start signalIic_start (); //Send Slave addressIic_write_byte (0x91); //set the IIC connection pin (SDA) to input modeSet_gpio_mode (GPD. Gpdcon,3,0); //waiting for an ACK from the machine     Do{ack=Iic_get_ack (); } while(ACK); //read the data sent from the slave machine .High =Iic_read_byte (); //set the IIC connection pin (SDA) to output modeSet_gpio_mode (GPD. Gpdcon,3,1); //Send ACK SignalIic_send_ack (); //set the IIC connection pin (SDA) to input modeSet_gpio_mode (GPD. Gpdcon,3,0); //read the data sent from the slave machine .Low =Iic_read_byte (); //set the IIC connection pin (SDA) to output modeSet_gpio_mode (GPD. Gpdcon,3,1); //Send Stop signalIic_stop (); return(High <<8) |Low ;}

experience in experiments:1. The ACK signal sent from the slave is not read, because the SDA line is not set to the input mode2. In the process of doing, at the beginning of each reading time the value of the temperature has not changed, and later found that because each time there is no send stop signal generated.

Finish.

Using s5pc100 Gpio to simulate IIC

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.