I2C data cable

Source: Internet
Author: User
This post was last edited by L. Fish


I respect the author of the original article. This article is not original. The feibit forum is a good forum. I hope you can read more about this website.

[Note: This article is sourced from www.feibit.com -- "feibi" ZigBee Forum. Please keep this line for reprinting.]

Recently, the wireless module has been used for an acceleration sensor project.

The boss of the hero decided to use ZigBee for transmission.

As a result, we ran out of some column problems.

One of them is the data transmitted by the mxc6202 Accelerometer Using the IIC protocol.

Use cc2430 for IIC Simulation

Because I have touched cc2430 for a while, I know it's still 51 kernel.

I thought it was very simple. It should be similar to ATMEL's 8051.

However, we encountered a series of problems.

I finally found it after a circle. Oh, it turns out like this.

One of the major differences between 2430 chips and 51 is:
The input and output directions of the IO port must be set manually.

That is, you need to set whether the pxdir of the pin of each port is 1 or 0.


If space is still widespread, let's go to the program ......


Compared with the 51 program, most of them are the same. Here I will mainly explain the differences between the two.

It can only be counted as a platform transplant ....

The source program will be appended


Reading and Writing SDA and SCL

In fact, the implementation of IIC Bus Protocol is to control the two lines so that data can be transmitted at the specified time.

Among them, the most important thing is the write operation on the SCL and the reading and writing of the SDA.


The following is the i2c_1.c source program .....


// I2c_1.c

# Include "iocc2430.h"

# Include "I2C. H"


# Define true 1

# Define false 0

/* My pin definition is

SDA is defined as p1.5

Specify p1.4 as the SCL */

# Define SCL p1_4

# Define SDA p1_5


/*

A nop is a machine's command cycle = 1/32 MHz

The 32 NOP is 1us.

---- Here is the explanation provided by outman. Thank you again.

*/

Void delay_1u (unsigned int microsecs ){

While (microsecs --)

{

/* 32 NOPs = 1 usecs */

ASM ("NOP ");

ASM ("NOP ");

ASM ("NOP ");

ASM ("NOP ");

ASM ("NOP ");

ASM ("NOP ");

ASM ("NOP"); ASM ("NOP ");

}

}


Unsigned char error;/* error prompt, global variable */


/*

When writing data to the outside through the cc2430 I/O port

You must set the corresponding IO port data direction to output

When dirpx_y is 1 in cc2430, the IO port is output.

When dirpx_y is 0, the IO port is the input function.

When my SDA is p1.5 and SDA is an output function

The Dir of the P1 port should be 0010 0000

That is, 0x20, and the others can be in turn ......

Continue the program ....

*/

Void writesda1 (void) // SDA outputs 1, which is equivalent to SDA = 1 {

P1dir | = 0x20;

SDA = 1;

}



Void writesda0 (void) // SDA output 0 {

P1dir | = 0x20;

SDA = 0;

}



Void writescl1 (void {

P1dir | = 0x10;

SCL = 1;

}



Void writescl0 (void {

P1dir | = 0x10;

SCL = 0;

}



Void readsda (void) // set the sda io port dir to receive data {

P1dir & = 0xdf;

}


/* Start the I2C bus function. When the SCL is high, the SDA generates a negative hop change */

Void i2c_start_1 (void)

{

Writesda1 ();

Writescl1 ();

Delay_1u (50 );

Writesda0 ();

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

}


/* Terminate the I2C bus. When the SCL is high, the SDA generates a positive hop */

Void i2c_stop_1 (void)

{

Writesda0 ();

Delay_1u (50 );

Writescl1 ();

Delay_1u (50 );

Writesda1 ();

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

}


/* Send 0, and set the SDA signal to low in high-power mode of the SCL */

Void send_0_1 (void)/* send ACK */

{

Writesda0 ();

Writescl1 ();

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

}


/* Send 1. Enable the SDA signal to be high in high-power mode of the SCL */

Void send_1_1 (void)

{

Writesda1 ();

Writescl1 ();

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

}


/* Check the device response signal after sending one byte */

Char check_acknowledge_1 (void)

{

Writesda1 ();

Writescl1 ();

Delay_1u (50 );

F0 = SDA;

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

If (f0 = 1)

Return false;

Return true;

}



Void write_acknowledge_1 (void)

{

Writesda0 ();

Delay_1u (50 );

Writescl1 ();

Delay_1u (50 );

Writescl0 ();

Delay_1u (50 );

}


/* Write a byte to the I2C bus */

Void writei2cbyte_1 (char B)

{

Char I;

For (I = 0; I <8; I ++)

{

If (B <I) & 0x80)

{

Send_1_1 ();

}

Else

{

Send_0_1 ();

}

}

}


/* Read a byte from the I2C bus */

Char readi2cbyte_1 (void)

{

Char B = 0, I;

Writesda1 ();


For (I = 0; I <8; I ++)

{

Writescl0 ();

Delay_1u (50 );

Writescl1 ();

Delay_1u (50 );


Readsda ();

F0 = SDA; // one digit in the register, used to store one bit of data in SDA


If (f0 = 1)

{

B = B <1;

B = B | 0x01;

}

Else

B = B <1;

}

Writescl0 ();

Return B;

}


PS:

The implementation principle of IIC is not explained here.

For example, when to send 1 and when to send 0

Here we will mainly explain the problems that need to be paid attention to during the transplantation process.

Ask again if you have any questions.

Note the following:
1. You need to manually set the IO direction

2. The delay setting method is related to the crystal oscillator. For how long it takes, refer to the previous procedure.

The program has read the at24cxx driver article from the robin's evolution with cc2430.

And thanks to the on the way in the group for giving me a patient explanation of IIC 51.

Finally, I 'd like to thank outman. Thank you for reminding me.

Over

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.