![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
I2C with AT24C02
Fork of I2C_on_mbed_AT24cxxx by
main.cpp@3:488563715282, 2017-10-09 (annotated)
- Committer:
- makeworks
- Date:
- Mon Oct 09 09:01:14 2017 +0000
- Revision:
- 3:488563715282
- Parent:
- 0:a9d57902bf67
??24C04????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:a9d57902bf67 | 1 | |
RedBearLab | 0:a9d57902bf67 | 2 | #include "mbed.h" |
RedBearLab | 0:a9d57902bf67 | 3 | |
makeworks | 3:488563715282 | 4 | #define DEV_ADDR 0xA0 |
makeworks | 3:488563715282 | 5 | #define I2C_PageSize 8 /* AT24C02每页有8个字节 */ |
RedBearLab | 0:a9d57902bf67 | 6 | |
makeworks | 3:488563715282 | 7 | I2C i2c(I2C_SDA, I2C_SCL); |
RedBearLab | 0:a9d57902bf67 | 8 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:a9d57902bf67 | 9 | |
RedBearLab | 0:a9d57902bf67 | 10 | /********************************************************************** |
makeworks | 3:488563715282 | 11 | * @brief : Write data to AT24C02 |
makeworks | 3:488563715282 | 12 | * |
RedBearLab | 0:a9d57902bf67 | 13 | * @param[in] addr : The address of stored |
RedBearLab | 0:a9d57902bf67 | 14 | * @param[in] *pbuf : The pointer of data |
RedBearLab | 0:a9d57902bf67 | 15 | * @param[in] length : The length of data |
RedBearLab | 0:a9d57902bf67 | 16 | * |
RedBearLab | 0:a9d57902bf67 | 17 | * @return none |
makeworks | 3:488563715282 | 18 | 24C02一页的大小是8,所以写入的时候每到8的倍数的时候要等5ms换页 |
RedBearLab | 0:a9d57902bf67 | 19 | **********************************************************************/ |
makeworks | 3:488563715282 | 20 | void AT24C02_WriteBytes(uint8_t addr, const uint8_t *const pbuf, uint16_t length) |
RedBearLab | 0:a9d57902bf67 | 21 | { |
makeworks | 3:488563715282 | 22 | char *_pbuf = (char *)pbuf; |
makeworks | 3:488563715282 | 23 | uint8_t NumOfFirstPageAvailable = I2C_PageSize - addr % I2C_PageSize; |
makeworks | 3:488563715282 | 24 | //页是绝对的,按整页大小排列,不是从开始写入的地址开始算。 |
makeworks | 3:488563715282 | 25 | //页写与字节写的区别就是,页写可以一次写多个数据,而字节写只能一次写一个数据。 |
makeworks | 3:488563715282 | 26 | //但由于AT24C02A的一页才8个字节,所以页写也最多写8个数据,而且只能在该页内写,不会发生一次页写同时写两页的情况。 |
makeworks | 3:488563715282 | 27 | if (length <= NumOfFirstPageAvailable) |
makeworks | 3:488563715282 | 28 | { |
makeworks | 3:488563715282 | 29 | char buf[length+1]; |
makeworks | 3:488563715282 | 30 | buf[0] = (char)(addr); |
makeworks | 3:488563715282 | 31 | memcpy(&buf[1], _pbuf, length); |
makeworks | 3:488563715282 | 32 | i2c.write(DEV_ADDR, buf, length+1, false); |
makeworks | 3:488563715282 | 33 | wait_ms(5); |
makeworks | 3:488563715282 | 34 | } |
makeworks | 3:488563715282 | 35 | else |
makeworks | 3:488563715282 | 36 | { |
makeworks | 3:488563715282 | 37 | char buf[NumOfFirstPageAvailable+1]; |
makeworks | 3:488563715282 | 38 | buf[0] = (char)(addr); |
makeworks | 3:488563715282 | 39 | memcpy(&buf[1], _pbuf, NumOfFirstPageAvailable); |
makeworks | 3:488563715282 | 40 | i2c.write(DEV_ADDR, buf, NumOfFirstPageAvailable+1, false); |
makeworks | 3:488563715282 | 41 | wait_ms(5); |
makeworks | 3:488563715282 | 42 | |
makeworks | 3:488563715282 | 43 | uint16_t NumOfRemainPages = (length - NumOfFirstPageAvailable) / I2C_PageSize; |
makeworks | 3:488563715282 | 44 | uint8_t NumOfRemainBytes = (length - NumOfFirstPageAvailable) % I2C_PageSize; |
makeworks | 3:488563715282 | 45 | |
makeworks | 3:488563715282 | 46 | addr += NumOfFirstPageAvailable;//修改寄存器写入地址 |
makeworks | 3:488563715282 | 47 | _pbuf += NumOfFirstPageAvailable;//修改buf地址 |
makeworks | 3:488563715282 | 48 | while (NumOfRemainPages--)//按page写入 |
makeworks | 3:488563715282 | 49 | { |
makeworks | 3:488563715282 | 50 | //一次可以写入8个字节的数据 |
makeworks | 3:488563715282 | 51 | char buf[I2C_PageSize+1]; |
makeworks | 3:488563715282 | 52 | buf[0] = (char)(addr); |
makeworks | 3:488563715282 | 53 | memcpy(&buf[1], _pbuf, I2C_PageSize); |
makeworks | 3:488563715282 | 54 | i2c.write(DEV_ADDR, buf, I2C_PageSize+1, false); |
makeworks | 3:488563715282 | 55 | wait_ms(5); |
makeworks | 3:488563715282 | 56 | addr += I2C_PageSize;//修改寄存器写入地址 |
makeworks | 3:488563715282 | 57 | _pbuf += I2C_PageSize;//修改buf地址 |
makeworks | 3:488563715282 | 58 | } |
makeworks | 3:488563715282 | 59 | if (NumOfRemainBytes != 0)//将剩下的不足一个page的写入 |
makeworks | 3:488563715282 | 60 | { |
makeworks | 3:488563715282 | 61 | char buf[NumOfRemainBytes+1]; |
makeworks | 3:488563715282 | 62 | buf[0] = (char)(addr); |
makeworks | 3:488563715282 | 63 | memcpy(&buf[1], _pbuf, NumOfRemainBytes); |
makeworks | 3:488563715282 | 64 | i2c.write(DEV_ADDR, buf, NumOfRemainBytes+1, false); |
makeworks | 3:488563715282 | 65 | wait_ms(5); |
makeworks | 3:488563715282 | 66 | } |
makeworks | 3:488563715282 | 67 | } |
RedBearLab | 0:a9d57902bf67 | 68 | } |
makeworks | 3:488563715282 | 69 | |
RedBearLab | 0:a9d57902bf67 | 70 | /********************************************************************** |
makeworks | 3:488563715282 | 71 | * @brief : Read data from AT24C02 |
makeworks | 3:488563715282 | 72 | * |
makeworks | 3:488563715282 | 73 | * @param[in] addr : The address of read in AT24C02 |
makeworks | 3:488563715282 | 74 | * @param[out] *pbuf : The pointer of buffer |
RedBearLab | 0:a9d57902bf67 | 75 | * @param[in] length : The length of data |
RedBearLab | 0:a9d57902bf67 | 76 | * |
RedBearLab | 0:a9d57902bf67 | 77 | * @return none |
makeworks | 3:488563715282 | 78 | 读取数据可以一直读到最后一个地址.序列读没有一页8个字节的限制 |
makeworks | 3:488563715282 | 79 | 读没有页的问题,可以从任意地址开始读取任意大小数据,只是超过整个存储器容量时地址才回卷。 |
RedBearLab | 0:a9d57902bf67 | 80 | **********************************************************************/ |
makeworks | 3:488563715282 | 81 | void AT24C02_ReadBytes(uint8_t addr, uint8_t *const pbuf, uint16_t length) |
RedBearLab | 0:a9d57902bf67 | 82 | { |
makeworks | 3:488563715282 | 83 | uint8_t *_pbuf = (uint8_t *)pbuf; |
makeworks | 3:488563715282 | 84 | char buf[1]; |
makeworks | 3:488563715282 | 85 | buf[0] = (char)(addr); |
makeworks | 3:488563715282 | 86 | i2c.write(DEV_ADDR, buf, 1, false); |
makeworks | 3:488563715282 | 87 | i2c.read(DEV_ADDR, (char *)_pbuf, length, false); |
RedBearLab | 0:a9d57902bf67 | 88 | } |
RedBearLab | 0:a9d57902bf67 | 89 | |
RedBearLab | 0:a9d57902bf67 | 90 | |
makeworks | 3:488563715282 | 91 | uint8_t w_buf[]= "Abcdefghijklmnopqrstuvwxyz1234567890"; |
makeworks | 3:488563715282 | 92 | uint8_t r_buf[100]; |
RedBearLab | 0:a9d57902bf67 | 93 | |
RedBearLab | 0:a9d57902bf67 | 94 | |
RedBearLab | 0:a9d57902bf67 | 95 | int main(void) |
RedBearLab | 0:a9d57902bf67 | 96 | { |
RedBearLab | 0:a9d57902bf67 | 97 | uint8_t index; |
makeworks | 3:488563715282 | 98 | int len = sizeof(w_buf) - 1; |
makeworks | 3:488563715282 | 99 | pc.printf("len = %d\r\n", len); |
RedBearLab | 0:a9d57902bf67 | 100 | |
RedBearLab | 0:a9d57902bf67 | 101 | pc.baud(9600); |
RedBearLab | 0:a9d57902bf67 | 102 | pc.printf("IIC Demo Start \r\n"); |
makeworks | 3:488563715282 | 103 | |
makeworks | 3:488563715282 | 104 | while(1) { |
RedBearLab | 0:a9d57902bf67 | 105 | wait(2); |
makeworks | 3:488563715282 | 106 | AT24C02_WriteBytes(0, w_buf, len); |
RedBearLab | 0:a9d57902bf67 | 107 | wait(0.5); |
makeworks | 3:488563715282 | 108 | AT24C02_ReadBytes(0, r_buf, len); |
makeworks | 3:488563715282 | 109 | for(index=0; index<len; index++) |
makeworks | 3:488563715282 | 110 | pc.printf("%c", r_buf[index]); |
makeworks | 3:488563715282 | 111 | |
RedBearLab | 0:a9d57902bf67 | 112 | pc.printf("\r\n"); |
RedBearLab | 0:a9d57902bf67 | 113 | } |
RedBearLab | 0:a9d57902bf67 | 114 | } |