I2C bus learning--example code

Publisher:和谐共存Latest update time:2017-01-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Take the three-axis acceleration sensor MMA7660 as an example:

#define MMA7660_SDA GPIO_Pin_10 // PC10 //IIC data line interface

#define MMA7660_SCL GPIO_Pin_15 // PA15 //IIC clock line interface

#define MMA7660_INT GPIO_Pin_14 // PA14 

  1 /*

  2 *================================================ =========

  3 * Function: IIC bus initialization signal

  4 * Parameters: None

  5 * Function return value: None 

  6 * SDA ---

  7 * |________

  8 * SCL ----------

  9 * |____  

 10 *================================================ =========   

 11 */

 12 void I2C_Start(void)

 13 {

 14 MMA7660_SCL_H;

 15 MMA7660_SDA_H;

 16 Delayus(1);

 17 MMA7660_SDA_L;

 18 Delayus(1);  

 19 } 

 20 

 twenty one /*

 22 *================================================ =========

 23 * Function: IIC bus stop signal

 24 * Parameter: None

 25 * Function return value: None 

 26 * SDA ---------

 27 * ______|

 28 * SCL ---------

 29 * |___

 30 *================================================ =========   

 31 */

 32 void I2C_Stop(void)

 33 {

 34 MMA7660_SCL_H;

 35 MMA7660_SDA_L;

 36 Delayus(1);

 37 MMA7660_SDA_H;

 38 Delayus(1);

 39 }

 40 /*

 41 *================================================ =========

 42 * Function: Receive the ACK signal initiated by the slave to the host.

 43 * Parameter: ACKBIT

 44 *          

 45 * Function return value: MMA_ERROR/MMA_OK   

 46 *================================================ =========   

 47 */

 48 unsigned char I2C_SlaveAck(void)

 49 {

 50 unsigned char ts=0;

 51 MMA7660_SCL_L;

 52 MMA7660_SDA_H;

 53   

 54 MMA_SDA_IOIN(); //SDA is set as input

 55 Delayus(1);

 56 MMA7660_SCL_H;

 57   

 58 while(MMA7660_SDA_D!=0)

 59 {

 60ts++;

 61 if(ts>200)

 62 {

 63 MMA_SDA_IOOUT(); //SDA is set as output

 64 MMA7660_SCL_L;

 65 return MMA_ERROR; //Return error    

 66 }

 67 }

 68 MMA7660_SCL_L;

 69 MMA_SDA_IOOUT(); //SDA is set to output

 70 //MMA7660_SDA_H;

 71 Delayus(1);

 72 return MMA_OK; //Return error      

 73 }

 74 

 75 /*

 76 *================================================ =========

 77 * Function: IIC writes one byte of data

 78 * Parameter: a: One byte of data to be written

 79 * Function return value: None

 80 *================================================ =========   

 81 */

 82 void I2C_WriteByte(unsigned char a) 

 83 {

 84 unsigned char i; 

 85 for(i=0; i<8; i++)

 86 {          

 87 MMA7660_SCL_L;

 88 if((a&0x80)!=0) MMA7660_SDA_H;

 89 else MMA7660_SDA_L;

 90 a <<= 1;

 91 Delayus(1);

 92 MMA7660_SCL_H;

 93 Delayus(1);

 94 }   

 95 MMA7660_SCL_L;  

 96 if(I2C_SlaveAck()==MMA_ERROR) //Wait for the ACK signal from the slave.

 97 {

 98 return ;

 99 }

100 }

101 

102 /*

103 *================================================ =========

104 * Function: IIC reads one byte of data

105 * Parameter: None

106 * Function return value: returns a byte of data read out

107 *================================================ =========   

108 */

109 unsigned char I2C_ReadByte(void)

110 {

111 unsigned char a =0;

112 unsigned char i;

113 MMA_SDA_IOIN(); //SDA is set as input    

114 for(i=0; i<8; i++)

115 {

116 a <<= 1;  

117 MMA7660_SCL_H;

118 Delayus(1);

119 if(MMA7660_SDA_D==1) a |= 0x01;

120 Delayus(1);

121 MMA7660_SCL_L;

122 Delayus(2);       

123 }

124 return a;

125 }

126 

127 /*

128 ************************************************* *********

129 *

130 * MMA7660 related functions

131 *

132 ************************************************* *********

133 */

134 /*

135 *================================================ =========

136 * Function: Write MAA7660 register

137 * Parameters:     

138 * Regs_Addr - Register address

139 * Regs_Data - Register value

140 * Function return value:

141 *================================================ =========   

142 */

143 void MMA7660_WriteReg(unsigned char Regs_Addr, unsigned char Regs_Data) 

144 {  

145 I2C_Start();

146 I2C_WriteByte(MMA7660_DEV_WRITE); //Write the Slave address first and configure it to write mode

147 I2C_WriteByte(Regs_Addr); //Write register address

148 I2C_WriteByte(Regs_Data); //Write register contents

149 I2C_Stop(); //End this IIC process

150 }

151 

152 /*

153 *================================================ =========

154 * Function: Read MAA7660 single byte

155 * Parameters

156 * Regs_Addr - Register address

157 * Function return value: register value

158 *================================================ =========   

159 */

160 unsigned char MMA7660_ReadReg(unsigned char Regs_Addr) 

161 {

162 unsigned char ret;

163    

164 I2C_Start();

165    

166 I2C_WriteByte(MMA7660_DEV_WRITE); //Write the Slave address first and configure it to write mode

167 I2C_WriteByte(Regs_Addr); //Write register address

168    

169 I2C_Start();

170 I2C_WriteByte(MMA7660_DEV_READ); //Write the Slave address and configure it to read mode

171 ret=I2C_ReadByte(); //Read data from the sensor

172 I2C_SlaveAck();

173 I2C_Stop(); //End this IIC process 

174    

175 return ret;

176 

177 } 



Reference address:I2C bus learning--example code

Previous article:Microcontroller ultrasonic ranging module learning notes_transmitter learning
Next article:I2C bus learning (IV)--reading and writing process

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号