//=========================================================================
// ADC_Configuration(void)
//=========================================================================
void STM32_I2C_Configuration(void)
{
//GPIO_InitTypeDef GPIO_InitStructure;
//I2C_InitTypeDef I2C_InitStructure;
/* GPIOB Periph clock enable */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
STM32_Rcc_Regs->apb2enr.all|=(RCC_IOPBEN|RCC_AFIOEN); // 时钟使能
/* I2C1 Periph clock enable */
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
//STM32_Rcc_Regs->apb1enr.bit.all|=RCC_I2C1EN; // I2C1时钟使能
STM32_Rcc_Regs->apb1enr.bit.I2C1EN=1; // I2C1时钟使能
/* Configure I2C1 pins: SCL and SDA */
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;// 复用功能开漏输出模式
//GPIO_Init(GPIOB, &GPIO_InitStructure);
STM32_Gpiob_Regs->crl.bit.CNF6=Output_Af_open_drain; // PB.06 复用功能开漏输出模式
STM32_Gpiob_Regs->crl.bit.MODE6=Output_Mode_50mhz; // PB.06 SCL
STM32_Gpiob_Regs->crl.bit.CNF7=Output_Af_open_drain; // PB.07 复用功能开漏输出模式
STM32_Gpiob_Regs->crl.bit.MODE7=Output_Mode_50mhz; // PB.07 SDA
//I2C_DeInit(I2C1);
STM32_Rcc_Regs->apb1rstr.all |= RCC_I2C1RST;
STM32_Rcc_Regs->apb1rstr.all &= ~RCC_I2C1RST;
/* I2C1 Init */
// I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //((u16)0x0000)
// I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //((u16)0xBFFF)
// I2C_InitStructure.I2C_OwnAddress1 = 0x00; // A1,A0
// I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //((u16)0x0400)
// I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//((u16)0x4000)
// I2C_InitStructure.I2C_ClockSpeed = 200000; //200KHZ
// I2C_Init(I2C1, &I2C_InitStructure);
STM32_I2c1_Regs->cr2.bit.FREQ=36;// PCLK1=36MHZ; 27.77ns
STM32_I2c1_Regs->cr1.bit.PE=0; // 0:禁用I2C外设(注:只有当I2C被禁用(PE=0)时,才能设置TRISE[5:0])
STM32_I2c1_Regs->ccr.bit.FS=1; // 1:快速模式的I2C SCL上升时间为300ns
STM32_I2c1_Regs->ccr.bit.DUTY=0;// 快速模式时的占空比 0:快速模式下:Tlow/Thigh = 2
STM32_I2c1_Regs->trise.all=12; // 300ns/(100/36)+1 最大上升时间
STM32_I2c1_Regs->ccr.bit.CCR=60;// 分频系数 36MHZ/(200KHZ*(2+1)); 因为:(Tlow/Thigh = 2)
STM32_I2c1_Regs->cr1.bit.PE=1; // 1:启用I2C外设
STM32_I2c1_Regs->cr1.bit.SMBUS=0;// 0:I2C模式
STM32_I2c1_Regs->cr1.bit.SMBTYPE=0;//
STM32_I2c1_Regs->cr1.bit.ACK=1; // ACK:应答使能 1:在接收到一个字节后返回一个应答(匹配的地址或数据)
STM32_I2c1_Regs->oar1.bit.BITSET=1; // 必须设置并保持为1
STM32_I2c1_Regs->oar1.bit.ADDMODE=0; // 7位从地址
/* I2C1 Init */
//I2C_Cmd(I2C1, ENABLE);
STM32_I2c1_Regs->cr1.bit.PE=1; // 1:启用I2C外设
}//end sub
用户163582 2008-8-27 12:20