void gpio_eeprom_configuration() //éèÖÃGPIOB6-7Îa¸′óÿa©êä3ö£¨êä3ö¸ßμçƽDè½óéÏà-μç×裩£¬2é¿′ÖDÎÄêÖ2áíaéèμÄGPIOÅäÖá£
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //¿aGPIOê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //I2C1_SCL
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //I2C1_SDA
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);
}
void I2C1_eeprom_configuration()
{
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_InitStructure.I2C_Mode =I2C_Mode_I2C; //éèÖÃI2CÎaI2CÄ£ê½
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //éèÖÃI2CÕ¼¿Õ±è
I2C_InitStructure.I2C_OwnAddress1 = 0x10; //éèÖÃstm32μØÖ·
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //ê1Äüó|′e
I2C_InitStructure.I2C_AcknowledgedAddress =I2C_AcknowledgedAddress_7bit; //éèÖÃμØַλÎa7
I2C_InitStructure.I2C_ClockSpeed = 200000; //éèÖÃê±ÖóËù¶èÎa200KHZ
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE); //ê1ÄüI2C
}
void eeprom_init()
{
gpio_eeprom_configuration();
I2C1_eeprom_configuration();
delay_ms(500);
}
void I2C_ByteWrite(u8 WriteAddr,u8 pBuffer) //Ïò′óI2CÖDμÄwriteAddrμØÖ·ÖDD′*pBufferÖ죬·¢Ëíò»¸ö×Ö½ú
{
// while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //μè′y×üÏß¿ÕÏD
/* Send STRAT condition */
I2C_GenerateSTART(I2C1, ENABLE); //Ö÷»ú·¢3öÆô¶ˉDÅoÅ
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //ó|′e£¬£¨Ñ¡ÔñÖ÷ģ꽣©
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter); //·¢Ëí′ó»úμØÖ·EEPROM_ADDRESS£¬à′Ñ°Ö·′ó»ú¡£2¢éèÖÃÎaÖ÷»ú·¢Ëíêy¾YÄ£ê½
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//μè′y′ó»úó|′e¡££¨ó|′e3é1|ÔòÖ÷»ú·¢ËíÄ£ê½Î»ÖÃ1£©
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, WriteAddr); //·¢Ëí′ó»úÖD¼Ä′æÆ÷μØÖ·£¨ÎaáËÏòÆäÖDD′μØÖ·£©
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //μè′y′ó»úó|′e£¨Ö÷»ú·¢Ëí×Ö½úλÖÃ1£©
/* Send the byte to be written */
I2C_SendData(I2C1, pBuffer); //·¢Ëíêy¾Y×Ö½ú£¬D′Ôú¶Ôó|′ó»úμĶÔó|μØÖ·¿Õ¼äÖD
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //μè′y′ó»úó|′e£¨Ö÷»ú·¢Ëí×Ö½úλÖÃ1£©
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE); //Ö÷»ú·¢3öí£Ö1DÅÏ¢
}
void I2C_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite) //D′¶à×Ö½ú
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //μè′y×üÏß¿ÕÏD
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE); //¿aê¼
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //′ó»úó|′eÎaÖ÷»úÄ£ê½
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter);//éèÖÃ′ó»úμØÖ·¼°Ö÷»ú·¢ËíÄ£ê½
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //ó|′eÎaÖ÷»ú·¢ËíÄ£ê½
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, WriteAddr); //éèÖÃ′ó»úÄú′æμØÖ·
/* Test on EV8 and clear it */
while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ó|′eÎaÖ÷»ú×Ö½ú·¢Ëí
/* While there is data to be written */
while(NumByteToWrite--)
{
/* Send the current byte */
I2C_SendData(I2C1, *pBuffer); //·¢Ëíêy¾Y
//while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXE)==RESET);
/* Point to the next byte to be written */
pBuffer++;
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE); //í£Ö1
}
void I2C_BufferRead(u8* pBuffer, u8 ReadAddr, u8 NumByteToRead) //¶áêy¾Y
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //μè′y×üÏß¿ÕÏD
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE); //¿aê¼
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //ó|′eÖ÷»úÄ£ê½
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter); //·¢Ëí′ó»úμØÖ·¼°D′2ù×÷
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //ó|′eÖ÷»ú·¢ËíÄ£ê½Ñ¡Ôñ
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, ReadAddr); //·¢Ëí′ó»úÄú′æμØÖ·
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //¼ì2éê¼t·¢Ëí×Ö½ú
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE); //¿aê¼
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //ó|′eê¼t5
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Receiver); //Ö÷»ú·¢Ëí′ó»úμØÖ·¼°½óêÕÖ¸áî
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //′ó»úó|′e£¬Ö÷»ú½óêÕÄ£ê½
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1) //èôÖ»′«ò»¸ö×Ö½ú¡£ÔòÖ÷»úÏìó|·Çó|′eDÅoÅ£¬è»oóí£Ö1
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) //μ±¼ì2aμ½Ö÷»ú½óêÕíê3飬Ôò½«½óêÕμ½μÄêy¾Y±£′æÔúpBufferÖD
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2C1);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C1, ENABLE); //Ôù′Î′ò¿aó|′eê1Äü£¬ÎaÏÂ′ζáD′êy¾Y×ö×¼±¸
}
void eeprom_write()
{
u8 eeprom_writedata[2];
eeprom_writedata[0]=L; //ê£óà3¤¶èμÄμí8λ
eeprom_writedata[1]=L>>8; //ê£óà3¤¶èμĸß8λ
// eeprom_writedata[0]=0x12;
// eeprom_writedata[1]=0x34;
I2C_PageWrite(eeprom_writedata, 0x08, 2);
}
void eeprom_read()
{
u8 eeprom_readdata[2];
I2C_BufferRead(eeprom_readdata, 0x08, 2);
L=(eeprom_readdata[1]<<8)+eeprom_readdata[0];
if(L==0x0000 || L==0xffff)
L=0xafc8;
}
每次复位后,读取的值都是L的初始值,然而L在程序中,是不断变化的。
一周热门 更多>