stm32 i2c 怎么读取
时间:10-02
整理:3721RD
点击:
利用stm32的I2C总线读取加速度计的值,求大神看看哪里出错了?急急急!
#include "i2c_ee.h"
#include "stm32f10x.h"
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void I2C_Configuration(void);
void mma7455_Init();
unsigned char mma7455_readbyte(unsigned char address);
void mma7455_writebyte(unsigned char address, unsigned char thedata);
uint8_t xx=0,yy=0,zz=0;
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void delay()
{
for(uint8_t x = 10;x>10;x --);
}
void main(void)
{
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
GPIO_Configuration();
I2C_Configuration();
mma7455_Init();
while(1)
{
xx = mma7455_readbyte(0x06);
yy = mma7455_readbyte(0x07);
zz = mma7455_readbyte(0x08);
}
}
void RCC_Configuration(void)
{
SystemInit();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
}
void NVIC_Configuration(void)
{
}
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void I2C_Configuration(void)
{
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; /* fixed */
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; /* fixed */
I2C_InitStructure.I2C_OwnAddress1 = 0x3A; /* user parameter */
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; /* fixed */
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 300000; /* user parameter */
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
}
void mma7455_Init()
{
GPIOB->BSRR = 0x000000E0;
delay();
mma7455_writebyte(0x16, 0x05);
}
//读取
unsigned char mma7455_readbyte(unsigned char address)
{
uint8_t i = 0;
uint8_t ret = 0;
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && ( i<150)) i++;
i = 0;
/*en5*/
I2C_Send7bitAddress(I2C1, 0x3a, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) && ( i<150)) i++;
/*en6*/
i = 0;
I2C_Cmd(I2C1, ENABLE);
I2C_SendData(I2C1, address);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) && ( i<150)) i++;
/*en8*/
i = 0;
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && ( i<150)) i++;
i = 0;
I2C_Send7bitAddress(I2C1, 0x3b, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) && ( i<150)) i++;
i = 0;
ret = I2C_ReadRegister(I2C1, 0x06);
I2C_AcknowledgeConfig(I2C1, ENABLE);
return ret;
}
//写入
void mma7455_writebyte(unsigned char address, unsigned char thedata)
{
uint8_t i = 0;
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && ( i<150)) i++;
i = 0;
I2C_Send7bitAddress(I2C1, 0x3a, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) && ( i<150)) i++;
i = 0;
I2C_SendData(I2C1, address);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) && ( i<150)) i++;
i = 0;
I2C_SendData(I2C1, thedata);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) && ( i<150)) i++;
i = 0;
I2C_GenerateSTOP(I2C1, ENABLE);
}
先说你有什么问题,程序这么长,费时间
发送设备地址后没应答……
直接说问题哦
固件库里面有这个例子的啊
gip e w e dmjf nyii rhf pgq ua rs bm tfn ugmw fhw j rhk w