#include "stm32f10x_lib.h"
void I2C_AcknowledgePolling(u8 sla); void I2C_SequentialRead_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len); void I2C_PageWrite_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len);
void I2C_AcknowledgePolling(u8 sla){ I2C_ClearFlag(I2C1,I2C_FLAG_ADDR); do{ I2C1->CR1 |= CR1_START_Set; I2C1->DR = sla; }while(!(I2C_ReadRegister(I2C1,I2C_Register_SR1)&0x0002));
I2C_ClearFlag(I2C1,I2C_FLAG_AF); } void I2C_SequentialRead_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){ u8 i;
I2C_AcknowledgePolling(sla); I2C1->CR1 |= CR1_START_Set; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C1->DR = sla; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C1->DR = suba; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C1->CR1 |= CR1_START_Set; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C1->DR = sla+1; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); I2C1->CR1 |= CR1_ACK_Set;
if(len>1){ for(i=0;i<len-1;i++){ while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); *pHead ++= I2C1->DR; I2C1->CR1 |= CR1_ACK_Set; } } while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); *pHead = I2C1->DR; I2C1->CR1 &= CR1_ACK_Reset; I2C1->CR1 |= CR1_STOP_Set; } void I2C_PageWrite_AT24C01_02(u8 sla,u8 suba,u8 *pHead,u8 len){ u8 wLen,addr; wLen=len; addr=suba; ScrollPage:I2C_AcknowledgePolling(sla); I2C1->CR1 |= CR1_START_Set; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); I2C1->DR = sla; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C1->DR = addr; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C1->DR = *pHead++; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); addr++; wLen--; while(wLen){ if((addr%8)!=0){ I2C1->DR = *pHead++; while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); addr++; wLen--; } else{ I2C1->CR1 |= CR1_STOP_Set; goto ScrollPage; } } I2C1->CR1 |= CR1_STOP_Set; } |