这个i2c搞了好几天,网上很多人都讲这是ST封装库的问题,而且基本上讲的都是STM32F1系列的片子,甚至给出了一些他们自己研究的成果,至于F4,这方面的说法不多。
没办法,从头来吧。
研究了下BMP085的datasheet,就是要用I2C读写寄存器,地址为0xee(写),从而计算温度和气压。
创建了一个项目文件,把I2C的库文件放进去,写main函数
首先配置uart1和I2c:
USART_Configuration();//这个函数就不说了,问题不大
I2C_Configuration();
主要说下这个函数的内容,源码:
#define I2C_BMP085 I2C1
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C_BMP085);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xEE; //BMP085地址
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //ack enable
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000; //100k
I2C_Cmd(I2C_BMP085, ENABLE);
I2C_Init(I2C_BMP085, &I2C_InitStructure);
//I2C_AcknowledgeConfig(I2C_BMP085, ENABLE);
}
开始在这个函数里面犯了个低级的错误,误将RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE)写为RCC_AHB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE),这是在复制过程中容易出现的问题。
结果会导致的问题就是程序一直停在while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));,这句话会出现在后面的I2C读函数I2C_Read中。
这个比较好理解,因为时钟没开,所以F4的I2C的寄存器都没有写进去,I2C还没有开始工作。
I2C_Read()函数的源码:
char value[2];
uint8_t I2C_Read(uint8_t addr)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, 0xee, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, addr);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, 0xee, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
value[0] = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
value[1] = I2C_ReceiveData(I2C1);
//cal_data = value[0]<<8 | value[1];
//printf("buf[0] = %x,buf[1] = %x,%d,%d\r\n",buf[0],buf[1],(buf[0]<<8|buf[2]),cal_data);
/* Decrement the read bytes counter */
I2C_AcknowledgeConfig(I2C1, ENABLE);
return 0;
}
在这个函数中也遇到了问题,又是因为复制代码,没有看清楚,误将while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))写为while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)),导致的问题就是程序一直停在while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)),为什么在这里死循环我还没搞明白。
如果没有出现上述的错误,这时候就可以在main函数里面添加函数read_calibration_data()去读BMP085内部E2PROM的校准数据了
下面整理出最终的代码
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));
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));
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;
}
uint8_t I2C_Write(I2C_TypeDef *I2Cx,uint8_t I2C_Addr,uint8_t addr,uint8_t value)
{
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
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_SendData(I2Cx, value);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_AcknowledgeConfig(I2Cx, DISABLE);
//I2C_delay(200000);//4.5ms
return 0;
}
void temp_calibration(void)
{
uint8_t buf[2];
I2C_Read(Open_I2C,ADDR_24LC02,0xaa,buf, 2);
ac1 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xac,buf, 2);
ac2 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xae,buf, 2);
ac3 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xb0,buf, 2);
ac4 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xb2,buf, 2);
ac5 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xb4,buf, 2);
ac6 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xb6,buf, 2);
b1 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xb8,buf, 2);
b2 = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xba,buf, 2);
mb = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xbc,buf, 2);
mc = buf[0] << 8 |buf[1];
I2C_Read(Open_I2C,ADDR_24LC02,0xbe,buf, 2);
md = buf[0] << 8 |buf[1];
printf("ac1=%d,ac2=%d,ac3=%d,ac4=%d,ac5=%d,ac6=%d,b1=%d,b2=%d,mb=%d,mc=%d,md=%d\r\n",ac1,ac2,ac3,ac4,ac5,ac6,b1,b2,mb,mc,md);
}
int main(void)
{
long x1,x2,x3,b3,b5,b6,b7,press_reg,pressure,temp_reg,temp;
unsigned long b4;
int ret,i;
uint8_t ReadBuffer[10];
//unsigned short ;
//unsigned int ;
char oss = 0; //这个值在读气压时可以置进寄存器
//GPIO_Configuration();
USART_Configuration();
I2C_Configuration();
printf("\r\n****************************************************************\r\n");
while (1)
{
printf("\r\n Read start \r\n");
temp_calibration();
//read uncompensated temperature
I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,0x2e);
I2C_delay(200000);//delay 4.5ms
ret = I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,2);
temp_reg = ReadBuffer[0] << 8 | ReadBuffer[1];
printf("temp_reg %d \r\n",temp_reg);
//calculate true temperature
x1 = ((temp_reg - ac6) * ac5) >> 15;
x2 = (mc << 11) / (x1 + md);
b5 = x1 + x2;
temp = (b5 + 8) >> 4;
printf("x1:%d, x2:%d, b5:%d, temp(*0.1):%d \r\n",x1,x2,b5,temp);
//read uncompensated pressure
I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,(0x3e + oss<<6));
I2C_delay(200000);//delay 4.5ms
ret = I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,3);
press_reg = ((ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2]) >> (8 - oss);
printf("press_reg %d \r\n",press_reg);
//下面计算公式要注意括号的使用,模棱两可的都给它用上,一开始我觉得有些地方应该可以不用,计算出的气压结果却是错的,后来加上括号就对了
b6 = b5 - 4000;
printf("b6 %ld \r\n",b6);
x1 = (b2 * (b6 * b6 )>> 12) >> 11;
printf("x1 %ld \r\n",x1);
x2 = (ac2 * b6) >> 11;
printf("x2 %ld \r\n",x2);
x3 = x1 + x2;
printf("x3 %ld \r\n",x3);
b3 = ((((long)ac1 * 4 + x3) << oss) + 2) / 4;
printf("b3 %ld \r\n",b3);
x1 = (ac3 * b6) >> 13;
printf("x1 %ld \r\n",x1);
x2 = (b1 * (b6 * b6) >> 12) >> 16;
printf("x2 %ld \r\n",x2);
x3 = ((x1 + x2 )+ 2) >> 2;
printf("x3 %ld \r\n",x3);
b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;
printf("b4 %ld \r\n",b4);
b7 = ((unsigned long)press_reg - b3) * (50000 >> oss);
printf("b7 %ld \r\n",b7);
if(b7 < 0x80000000)
{
pressure = (b7 * 2) / b4;
}
else
{
pressure = (b7 / b4) * 2;
}
printf("pressure %ld \r\n",pressure);
x1 = (pressure >> 8) * (pressure >> 8);
printf("x1 %ld \r\n",x1);
x1 = (x1 * 3038) >> 16;
printf("x1 %ld \r\n",x1);
x2 = (-7357 * pressure) >> 16;
printf("x2 %ld \r\n",x2);
pressure = pressure + ((x1 + x2 + 3791) >> 4);
printf("pressure %ld \r\n",pressure);
for(i=0; i<200*3; i++)
I2C_delay(200000);//delay 4.5ms
}
}
终端看到的打印:
Read start
ac1=8361,ac2=-816,ac3=-14464,ac4=33064,ac5=25477,ac6=25325,b1=5498,b2=38,mb=-32768,mc=-11075,md=2432
temp_reg 33455
x1:6321, x2:-2591, b5:3730, temp(*0.1):233
press_reg 33455
b6 -270
x1 0
x2 107
x3 107
b3 8388
x1 476
x2 1
x3 119
b4 33184
b7 1253350000
pressure 75539
x1 87025
x1 4034
x2 -8480
pressure 75498
温度233×0.1=23.3度
气压为75498Pa
上一篇:STM32学习笔记(串口、IAP)
下一篇:STM32F407的UDP发送数据
推荐阅读最新更新时间:2024-03-16 16:00