本帖最后由 zddxmu 于 2011-5-18 09:19 编辑
官网例程如下
----------------------------------------------
void I2C_Init(void) {
GPIOE->ODR |= 6; //define SDA, SCL outputs, HiZ, Open drain, Fast
GPIOE->DDR |= 6;
GPIOE->CR2 |= 6;
#ifdef FAST_I2C_MODE
I2C->FREQR = 16; // input clock to I2C - 16MHz
I2C->CCRL = 15; // 900/62.5= 15, (SCLhi must be at least 600+300=900ns!)
I2C->CCRH = 0x80; // fast mode, duty 2/1 (bus speed 62.5*3*15~356kHz)
I2C->TRISER = 5; // 300/62.5 + 1= 5 (maximum 300ns)
#else
I2C->FREQR = 8; // input clock to I2C - 8MHz
I2C->CCRL = 40; // CCR= 40 - (SCLhi must be at least 4000+1000=5000ns!)
I2C->CCRH = 0; // standard mode, duty 1/1 bus speed 100kHz
I2C->TRISER = 9; // 1000ns/(125ns) + 1 (maximum 1000ns)
#endif
I2C->OARL = 0xA0; // own address A0;
I2C->OARH |= 0x40;
I2C->ITR = 1; // enable error interrupts
I2C->CR2 |= 0x04; // ACK=1, Ack enable
I2C->CR1 |= 0x01; // PE=1
}
----------------------------------------------------------
官网例程有先设置GPIO,但为何是高电平输出呢?
我的程序因为SDA和SCL始终是高电平,总线一直busy。所以将GPIO设置成低电平输出,用万用表查出来是低电平,为什么总线还是处于BUSY呢?
----------------------------------------------------------
void I2C_init(void)
{
PB_ODR = 0x00; //define SDA, SCL outputs, HiZ, Open drain, Fast
PB_DDR |= 0x30;
PB_CR2 |= 0x30;
I2C_FREQR = 8; // input clock to I2C - 8MHz
I2C_CCRL = 40; // CCR= 40 - (SCLhi must be at least 4000+1000=5000ns!)
I2C_CCRH = 0; // standard mode, duty 1/1 bus speed 100kHz
I2C_TRISER = 9; // 1000ns/(125ns) + 1 (maximum 1000ns)
I2C_OARL = 0xA0; // own address A0;
I2C_OARH |= 0x40;
I2C_ITR = 1; // enable error interrupts
I2C_CR2 |= 0x04; // ACK=1, Ack enable
I2C_CR1 |= 0x01; // PE=1
I2C_CR2 &= 0x08;
} |