在线时间237 小时
UID146802
注册时间2015-3-6
NXP金币0
TA的每日心情 | 开心 2019-7-11 19:28 |
---|
签到天数: 141 天 连续签到: 1 天 [LV.7]常住居民III
金牌会员
 
- 积分
- 2492
- 最后登录
- 2020-9-16
|
这个功能是在原有的I2C功能上更改的,原板用的是AT24C02, 页写和地址方式 的AT24C64有些区别,这里做子一些改动1, 先在硬件上的 AT24C02更换成 AT24C64,如果不好更换可以外接, 地址线要做一下处理 ,第是A0要接VCC与原有的板子地址不能一样
2, 在源码地改成 写入地址方式
附源码:
- /*******************************************************************/
- /*!
- * Write a byte of Data to specified register on MMA7660
- * @param u8RegisterAddress is Register Address
- * @param u8Data is Data to write
- * C64芯片 unsigned char u8RegisterAddress 更换成 unsigned int u8RegisterAddress
- */
- void I2C0WriteMultiRegister(unsigned char SlaveAddr, unsigned int u8RegisterAddress, char *u8Data, unsigned int length)
- {
- uint16 i;
- /* send data to slave */
- //MWSR =write MRSW =read
- IIC0_StartTransmission(SlaveAddr,MWSR);
- i2c0_Wait();//等待
- //要写入的地址 高位
- I2C0_D = u8RegisterAddress>>8;
- i2c0_Wait();
- //要写入地址 低位
- I2C0_D = u8RegisterAddress;
- i2c0_Wait();
- //进入连续写入内容,这里和芯片的页写有关系,如果是C02 每次最多只能写入8个字节
- //如果是C64 ,每次可以最多写入 32个字节内容
- for(i=0; i<length; i++)
- {
- I2C0_D = u8Data[i];
- i2c0_Wait();
- }
-
- i2c0_Stop();
-
- Pause(7);
- //Pause(50);
- }
复制代码 2,在读取的地方更改
- /*******************************************************************/
- /*!
- * Read first three registers from the MMA7660
- * @param u8RegisterAddress is Register Address
- * @return Data stored in Register
- * C64芯片 unsigned char u8RegisterAddress 更换成 unsigned int u8RegisterAddress
- */
- unsigned char I2C0ReadMultiRegisters(unsigned char SlaveAddr, unsigned int u8RegisterAddress, unsigned char bytes, char buf[])
- {
- unsigned char n=bytes;
- int i;
- // Send Slave Address 从机地址
- IIC0_StartTransmission(SlaveAddr,MWSR);
- i2c0_Wait();
- // Write Register Address 要写入的地址,这里如果是C02到C08用一位地址方式 ,如果是16或C256是双位地址
- //这里的 u8RegisterAddress 地址更改会16位双8位地址
- /* Write Register Address */
- I2C0_D = u8RegisterAddress>>8;
- i2c0_Wait();
-
- I2C0_D = u8RegisterAddress;
- i2c0_Wait();
- /* Do a repeated start */
- I2C0_C1 |= I2C_C1_RSTA_MASK;
- /* Send Slave Address */
- I2C0_D = (EEPROM_I2C_ADDRESS << 1) | 0x01; //read address
- i2c0_Wait();
- /* Put in Rx Mode */
- I2C0_C1 &= (~I2C_C1_TX_MASK);
- /* Ensure TXAK bit is 0 */
- I2C0_C1 &= ~I2C_C1_TXAK_MASK;
- /* Dummy read */
- result[0] = I2C0_D ;
- i2c0_Wait();
- for(i=0;i<n-2;i++)
- {
- /* Read first byte */
- buf[i] = I2C0_D;
- i2c0_Wait();
- }
- /* Turn off ACK since this is second to last read*/
- I2C0_C1 |= I2C_C1_TXAK_MASK;
- /* Read second byte */
- buf[i++] = I2C0_D;
- i2c0_Wait();
- /* Send stop */
- i2c0_Stop();
- /* Read third byte */
- buf[i++] = I2C0_D;
- // printf("%3d %3d %3d\n",result[0],result[2],result[4]);
- return result[0];
- }
复制代码 3,在main()做测试
- /*********************************************************************************************************
- ** Function name: main
- ** Descriptions: I2C驱动EEPROM AT24C02 Demo
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- ** Created by:
- ** Created Date:
- **--------------------------------------------------------------------------------------------------------
- ** Modified by:
- ** Modified date:
- *********************************************************************************************************/
- int main (void)
- {
- char ucData[64] = {0x00};
- INT8U i = 0;
- SystemCoreClockUpdate();
- SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK /* 管脚时钟初始化 */
- | SIM_SCGC5_PORTB_MASK
- | SIM_SCGC5_PORTD_MASK
- | SIM_SCGC5_PORTE_MASK );
- uart0Init(9600,0,0,8,1);
- printf("IIC Test start\r\n"); /* I2C外设初始化 */
- init_I2C0();
- IO_FUN_SEL(MKL_PORTB,18,1); /* LED5,6,7初始化 */
- IO_FUN_SEL(MKL_PORTB,19,1);
- IO_FUN_SEL(MKL_PORTD,1,1);
-
- GPIO_DDR_OUTPUT(MKL_PORTB,18);
- GPIO_DDR_OUTPUT(MKL_PORTB,19);
- GPIO_DDR_OUTPUT(MKL_PORTD,1);
- GPIO_CLR(MKL_PORTB,18); //D5亮,程序开始
- GPIO_SET(MKL_PORTB,19);
- GPIO_SET(MKL_PORTD,1);
-
-
- for (i = 0; i < 64; i++) { /* 数组初始化 */
- ucData[i] = i;
-
- }
- printf("Now Start Write ...\n");
- I2C0WriteMultiRegister(EEPROM_I2C_ADDRESS, 0x00, ucData,64);
-
- for (i = 0; i <64; i++) { /* 数组初始化 */
- ucData[i] = 0;
- }
- printf("Now Start Read...\n");
- /* 读数据操作 */
- I2C0ReadMultiRegisters(EEPROM_I2C_ADDRESS, 0x0000, 64,ucData);
- printf("Now Start verify...");
- for (i = 0; i < 64; i++) {
- if (ucData[i] != i) {
- GPIO_TOGGLE(MKL_PORTB,19); /* 验证失败,D6亮 */
- printf("ERR!");
- while (1);
- }
- } /* 验证正确,D7亮 */
- GPIO_TOGGLE(MKL_PORTD,1);
- printf("OK!");
- while(1);
- }
复制代码 4,仿真结果
这里可以看出 第32位置 以后内容就不连续了,
|
|