标题:
测试代码2
[打印本页]
作者:
海洋狂吻
时间:
2013-12-21 11:06
标题:
测试代码2
#include "L3G4200D.h"
#include <math.h> //Keil library
/*******************************************************************************
* Function Name : I2C_Configuration
* Description :
* Input : None
* Output : None
* Return : None
* Attention
: None
*******************************************************************************/
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
/* Configure I2C1 pins: PB10->SCL and PB11->SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = L3G4200_Addr;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Cmd(I2C2, ENABLE);
I2C_Init(I2C2, &I2C_InitStructure);
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
/*******************************************************************************
* Function Name : I2C_delay
* Description :
* Input : None
* Output : None
* Return : None
* Attention
: None
*******************************************************************************/
static void I2C_delay(uint16_t cnt)
{
while(cnt--);
}
/*******************************************************************************
* Function Name : I2C_AcknowledgePolling
* Description :
* Input : I2C_TypeDef * , uint8_t
* Output : None
* Return : None
* Attention
: None
*******************************************************************************/
static void I2C_AcknowledgePolling(I2C_TypeDef *I2Cx,uint8_t I2C_Addr)
{
vu16 SR1_Tmp;
do
{
I2C_GenerateSTART(I2Cx, ENABLE);
SR1_Tmp = I2C_ReadRegister(I2Cx, I2C_Register_SR1);
//#ifdef AT24C01A
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
/*
#else
I2C_Send7bitAddress(I2Cx, 0, I2C_Direction_Transmitter);
#endif
*/
}while(!(I2C_ReadRegister(I2Cx, I2C_Register_SR1) & 0x0002));
I2C_ClearFlag(I2Cx, I2C_FLAG_AF);
I2C_GenerateSTOP(I2Cx, ENABLE);
}
/*******************************************************************************
* Function Name : I2C_Read
* Description :
* Input :
* Output :
* Return :
* Attention
: None
*******************************************************************************/
uint8_t I2C_Read(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num)
{
if(num==0)
return 1;
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_AcknowledgeConfig(I2Cx, ENABLE);
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
//#ifdef AT24C01A
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, addr);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/*
#else
I2C_Send7bitAddress(I2Cx, addr<<1, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#endif
*/
while (num)
{
if(num==1)
{
I2C_AcknowledgeConfig(I2Cx, DISABLE);
I2C_GenerateSTOP(I2Cx, ENABLE);
}
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
*buf = I2C_ReceiveData(I2Cx);
buf++;
/* Decrement the read bytes counter */
num--;
}
I2C_AcknowledgeConfig(I2Cx, ENABLE);
return 0;
}
/*******************************************************************************
* Function Name : I2C_WriteOneByte
* Description :
* Input :
* Output : None
* Return :
* Attention
: None
*******************************************************************************/
uint8_t I2C_WriteOneByte(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t value)
{
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
//#ifdef AT24C01A
I2C_Send7bitAddress(I2Cx, I2C_Addr, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, addr);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/*
#else
I2C_Send7bitAddress(I2Cx, addr<<1, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
#endif
*/
I2C_SendData(I2Cx, value);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_AcknowledgePolling(I2Cx,I2C_Addr);
I2C_delay(1000);
return 0;
}
/*******************************************************************************
* Function Name : I2C_Write
* Description :
* Input :
* Output : None
* Return :
* Attention
: None
*******************************************************************************/
uint8_t I2C_Write(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t *buf,uint16_t num)
{
uint8_t err=0;
while(num--)
{
if(I2C_WriteOneByte(I2Cx, I2C_Addr,addr++,*buf++))
{
err++;
}
}
if(err)
return 1;
else
return 0;
}
//************初始化L3G4200D*********************************
void Init_L3G4200D(void)
{
I2C_WriteOneByte(I2C2,L3G4200_Addr,CTRL_REG1, 0x0f);
I2C_WriteOneByte(I2C2,L3G4200_Addr,CTRL_REG2, 0x00);
I2C_WriteOneByte(I2C2,L3G4200_Addr,CTRL_REG3, 0x08);
I2C_WriteOneByte(I2C2,L3G4200_Addr,CTRL_REG4, 0x00);
I2C_WriteOneByte(I2C2,L3G4200_Addr,CTRL_REG5, 0x00);
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0