I2C with AT24Cxxx
Dependencies: mbed
main.cpp@0:a9d57902bf67, 2015-02-05 (annotated)
- Committer:
- RedBearLab
- Date:
- Thu Feb 05 03:16:51 2015 +0000
- Revision:
- 0:a9d57902bf67
Mbed i2c library demo with at24cxxx on Readbear nRF51822
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 | |
RedBearLab | 0:a9d57902bf67 | 4 | #define DEV_ADDR 0xA0 |
RedBearLab | 0:a9d57902bf67 | 5 | |
RedBearLab | 0:a9d57902bf67 | 6 | I2C i2c(P0_29, P0_28); |
RedBearLab | 0:a9d57902bf67 | 7 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:a9d57902bf67 | 8 | |
RedBearLab | 0:a9d57902bf67 | 9 | /********************************************************************** |
RedBearLab | 0:a9d57902bf67 | 10 | * @brief : Write data to AT24C512 |
RedBearLab | 0:a9d57902bf67 | 11 | * |
RedBearLab | 0:a9d57902bf67 | 12 | * @param[in] addr : The address of stored |
RedBearLab | 0:a9d57902bf67 | 13 | * @param[in] *pbuf : The pointer of data |
RedBearLab | 0:a9d57902bf67 | 14 | * @param[in] length : The length of data |
RedBearLab | 0:a9d57902bf67 | 15 | * |
RedBearLab | 0:a9d57902bf67 | 16 | * @return none |
RedBearLab | 0:a9d57902bf67 | 17 | **********************************************************************/ |
RedBearLab | 0:a9d57902bf67 | 18 | void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) |
RedBearLab | 0:a9d57902bf67 | 19 | { |
RedBearLab | 0:a9d57902bf67 | 20 | char buf[length+2]; |
RedBearLab | 0:a9d57902bf67 | 21 | buf[0] = (uint8_t)(addr>>8); |
RedBearLab | 0:a9d57902bf67 | 22 | buf[1] = (uint8_t)(addr); |
RedBearLab | 0:a9d57902bf67 | 23 | memcpy(&buf[2], pbuf, length); |
RedBearLab | 0:a9d57902bf67 | 24 | i2c.write(DEV_ADDR, buf, length+2, false); |
RedBearLab | 0:a9d57902bf67 | 25 | } |
RedBearLab | 0:a9d57902bf67 | 26 | /********************************************************************** |
RedBearLab | 0:a9d57902bf67 | 27 | * @brief : Read data from AT24C512 |
RedBearLab | 0:a9d57902bf67 | 28 | * |
RedBearLab | 0:a9d57902bf67 | 29 | * @param[in] addr : The address of read in AT24C512 |
RedBearLab | 0:a9d57902bf67 | 30 | * @param[out] *pbuf : The pointer of buffer |
RedBearLab | 0:a9d57902bf67 | 31 | * @param[in] length : The length of data |
RedBearLab | 0:a9d57902bf67 | 32 | * |
RedBearLab | 0:a9d57902bf67 | 33 | * @return none |
RedBearLab | 0:a9d57902bf67 | 34 | **********************************************************************/ |
RedBearLab | 0:a9d57902bf67 | 35 | void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) |
RedBearLab | 0:a9d57902bf67 | 36 | { |
RedBearLab | 0:a9d57902bf67 | 37 | char buf[2]; |
RedBearLab | 0:a9d57902bf67 | 38 | buf[0] = (uint8_t)(addr>>8); |
RedBearLab | 0:a9d57902bf67 | 39 | buf[1] = (uint8_t)(addr); |
RedBearLab | 0:a9d57902bf67 | 40 | i2c.write(DEV_ADDR, buf, 2, false); |
RedBearLab | 0:a9d57902bf67 | 41 | |
RedBearLab | 0:a9d57902bf67 | 42 | i2c.read(DEV_ADDR, (char *)pbuf, length, false); |
RedBearLab | 0:a9d57902bf67 | 43 | } |
RedBearLab | 0:a9d57902bf67 | 44 | |
RedBearLab | 0:a9d57902bf67 | 45 | |
RedBearLab | 0:a9d57902bf67 | 46 | uint8_t w_buf[11]={'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!'}; |
RedBearLab | 0:a9d57902bf67 | 47 | uint8_t r_buf[11]; |
RedBearLab | 0:a9d57902bf67 | 48 | |
RedBearLab | 0:a9d57902bf67 | 49 | |
RedBearLab | 0:a9d57902bf67 | 50 | int main(void) |
RedBearLab | 0:a9d57902bf67 | 51 | { |
RedBearLab | 0:a9d57902bf67 | 52 | uint8_t index; |
RedBearLab | 0:a9d57902bf67 | 53 | |
RedBearLab | 0:a9d57902bf67 | 54 | pc.baud(9600); |
RedBearLab | 0:a9d57902bf67 | 55 | pc.printf("IIC Demo Start \r\n"); |
RedBearLab | 0:a9d57902bf67 | 56 | |
RedBearLab | 0:a9d57902bf67 | 57 | while(1) |
RedBearLab | 0:a9d57902bf67 | 58 | { |
RedBearLab | 0:a9d57902bf67 | 59 | wait(2); |
RedBearLab | 0:a9d57902bf67 | 60 | AT24C512_WriteBytes(0, w_buf, 11); |
RedBearLab | 0:a9d57902bf67 | 61 | wait(0.5); |
RedBearLab | 0:a9d57902bf67 | 62 | AT24C512_ReadBytes(0, r_buf, 11); |
RedBearLab | 0:a9d57902bf67 | 63 | for(index=0; index<11; index++) |
RedBearLab | 0:a9d57902bf67 | 64 | pc.putc(r_buf[index]); |
RedBearLab | 0:a9d57902bf67 | 65 | |
RedBearLab | 0:a9d57902bf67 | 66 | pc.printf("\r\n"); |
RedBearLab | 0:a9d57902bf67 | 67 | } |
RedBearLab | 0:a9d57902bf67 | 68 | } |
RedBearLab | 0:a9d57902bf67 | 69 | |
RedBearLab | 0:a9d57902bf67 | 70 | |
RedBearLab | 0:a9d57902bf67 | 71 | |
RedBearLab | 0:a9d57902bf67 | 72 | |
RedBearLab | 0:a9d57902bf67 | 73 | |
RedBearLab | 0:a9d57902bf67 | 74 | |
RedBearLab | 0:a9d57902bf67 | 75 | |
RedBearLab | 0:a9d57902bf67 | 76 | |
RedBearLab | 0:a9d57902bf67 | 77 | |
RedBearLab | 0:a9d57902bf67 | 78 | |
RedBearLab | 0:a9d57902bf67 | 79 | |
RedBearLab | 0:a9d57902bf67 | 80 | |
RedBearLab | 0:a9d57902bf67 | 81 | |
RedBearLab | 0:a9d57902bf67 | 82 | |
RedBearLab | 0:a9d57902bf67 | 83 | |
RedBearLab | 0:a9d57902bf67 | 84 | |
RedBearLab | 0:a9d57902bf67 | 85 | |
RedBearLab | 0:a9d57902bf67 | 86 | |
RedBearLab | 0:a9d57902bf67 | 87 | |
RedBearLab | 0:a9d57902bf67 | 88 |