這個i2c搞了好幾天,網(wǎng)上很多人都講這是ST封裝庫的問題,而且基本上講的都是STM32F1系列的片子,甚至給出了一些他們自己研究的成果,至于F4,這方面的說法不多。
沒辦法,從頭來吧。
研究了下BMP085的datasheet,就是要用I2C讀寫寄存器,地址為0xee(寫),從而計算溫度和氣壓。
創(chuàng)建了一個項目文件,把I2C的庫文件放進去,寫main函數(shù)
首先配置uart1和I2c:
USART_Configuration();//這個函數(shù)就不說了,問題不大
I2C_Configuration();
主要說下這個函數(shù)的內(nèi)容,源碼:
#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);
}
開始在這個函數(shù)里面犯了個低級的錯誤,誤將RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE)寫為RCC_AHB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE),這是在復制過程中容易出現(xiàn)的問題。
結果會導致的問題就是程序一直停在while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));,這句話會出現(xiàn)在后面的I2C讀函數(shù)I2C_Read中。
這個比較好理解,因為時鐘沒開,所以F4的I2C的寄存器都沒有寫進去,I2C還沒有開始工作。
I2C_Read()函數(shù)的源碼:
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,%drn",buf[0],buf[1],(buf[0]<<8|buf[2]),cal_data);
/* Decrement the read bytes counter */
I2C_AcknowledgeConfig(I2C1, ENABLE);
return 0;
}
在這個函數(shù)中也遇到了問題,又是因為復制代碼,沒有看清楚,誤將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)),為什么在這里死循環(huán)我還沒搞明白。
如果沒有出現(xiàn)上述的錯誤,這時候就可以在main函數(shù)里面添加函數(shù)read_calibration_data()去讀BMP085內(nèi)部E2PROM的校準數(shù)據(jù)了
下面整理出最終的代碼
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=%drn",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("rn****************************************************************rn");
while (1)
{
printf("rn Read start rn");
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 rn",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 rn",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 rn",press_reg);
//下面計算公式要注意括號的使用,模棱兩可的都給它用上,一開始我覺得有些地方應該可以不用,計算出的氣壓結果卻是錯的,后來加上括號就對了
b6 = b5 - 4000;
printf("b6 %ld rn",b6);
x1 = (b2 * (b6 * b6 )>> 12) >> 11;
printf("x1 %ld rn",x1);
x2 = (ac2 * b6) >> 11;
printf("x2 %ld rn",x2);
x3 = x1 + x2;
printf("x3 %ld rn",x3);
b3 = ((((long)ac1 * 4 + x3) << oss) + 2) / 4;
printf("b3 %ld rn",b3);
x1 = (ac3 * b6) >> 13;
printf("x1 %ld rn",x1);
x2 = (b1 * (b6 * b6) >> 12) >> 16;
printf("x2 %ld rn",x2);
x3 = ((x1 + x2 )+ 2) >> 2;
printf("x3 %ld rn",x3);
b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;
printf("b4 %ld rn",b4);
b7 = ((unsigned long)press_reg - b3) * (50000 >> oss);
printf("b7 %ld rn",b7);
if(b7 < 0x80000000)
{
pressure = (b7 * 2) / b4;
}
else
{
pressure = (b7 / b4) * 2;
}
printf("pressure %ld rn",pressure);
x1 = (pressure >> 8) * (pressure >> 8);
printf("x1 %ld rn",x1);
x1 = (x1 * 3038) >> 16;
printf("x1 %ld rn",x1);
x2 = (-7357 * pressure) >> 16;
printf("x2 %ld rn",x2);
pressure = pressure + ((x1 + x2 + 3791) >> 4);
printf("pressure %ld rn",pressure);
for(i=0; i<200*3; i++)
I2C_delay(200000);//delay 4.5ms
}
}