void RCC_Config(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
while(HSEStartUpStatus == ERROR)
{
}
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
}
void I2C_config(void)
{
I2C_InitTypeDef I2C_InitStruct;
I2C_Cmd(I2C1, ENABLE);
I2C_InitStruct.I2C_Mode =I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle =I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 =0x30;
I2C_InitStruct.I2C_Ack =I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress =I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed =40000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStruct);
}
void i2c_write(int reg, int data)
{
I2C_GenerateSTART(I2C1, ENABLE);//产生 I2Cx传输 START条件
/* Test on EV5 and clear it */
//EEPROM_ADDRESS=50;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //检查最近一次 I2C事件是否是输入的事件
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1,0x34, I2C_Direction_Transmitter);//向指定的从 I2C设备传送地址字,选择发送方向
/* Test on EV6 and clear it */
//EEPROM_ADDRESS=51;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//检查最近一次 I2C事件是否是输入的事件
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, reg);//通过外设 I2Cx发送地址
/* Test on EV8 and clear it */
//EEPROM_ADDRESS=52;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检查最近一次 I2C事件是否是输入的事件
/* Send the byte to be written */
I2C_SendData(I2C1, data); //通过外设 I2Cx发送数据 写入一个B
/* Test on EV8 and clear it */
//EEPROM_ADDRESS=53;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检查最近一次 I2C事件是否是输入的事件
/* Send STOP condition */
//I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);//产生 I2Cx传输 STOP条件
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1 | RCC_APB1Periph_SPI2 , ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
RCC_APB2Periph_AFIO, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
此帖出自
小平头技术问答
一周热门 更多>