I2C with AT24Cxxx
Dependencies: mbed
main.cpp
00001 00002 #include "mbed.h" 00003 00004 #define DEV_ADDR 0xA0 00005 00006 I2C i2c(P0_29, P0_28); 00007 Serial pc(USBTX, USBRX); 00008 00009 /********************************************************************** 00010 * @brief : Write data to AT24C512 00011 * 00012 * @param[in] addr : The address of stored 00013 * @param[in] *pbuf : The pointer of data 00014 * @param[in] length : The length of data 00015 * 00016 * @return none 00017 **********************************************************************/ 00018 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) 00019 { 00020 char buf[length+2]; 00021 buf[0] = (uint8_t)(addr>>8); 00022 buf[1] = (uint8_t)(addr); 00023 memcpy(&buf[2], pbuf, length); 00024 i2c.write(DEV_ADDR, buf, length+2, false); 00025 } 00026 /********************************************************************** 00027 * @brief : Read data from AT24C512 00028 * 00029 * @param[in] addr : The address of read in AT24C512 00030 * @param[out] *pbuf : The pointer of buffer 00031 * @param[in] length : The length of data 00032 * 00033 * @return none 00034 **********************************************************************/ 00035 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) 00036 { 00037 char buf[2]; 00038 buf[0] = (uint8_t)(addr>>8); 00039 buf[1] = (uint8_t)(addr); 00040 i2c.write(DEV_ADDR, buf, 2, false); 00041 00042 i2c.read(DEV_ADDR, (char *)pbuf, length, false); 00043 } 00044 00045 00046 uint8_t w_buf[11]={'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!'}; 00047 uint8_t r_buf[11]; 00048 00049 00050 int main(void) 00051 { 00052 uint8_t index; 00053 00054 pc.baud(9600); 00055 pc.printf("IIC Demo Start \r\n"); 00056 00057 while(1) 00058 { 00059 wait(2); 00060 AT24C512_WriteBytes(0, w_buf, 11); 00061 wait(0.5); 00062 AT24C512_ReadBytes(0, r_buf, 11); 00063 for(index=0; index<11; index++) 00064 pc.putc(r_buf[index]); 00065 00066 pc.printf("\r\n"); 00067 } 00068 } 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088
Generated on Thu Jul 14 2022 15:35:46 by 1.7.2