PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mcp24lc515_i2c_eeprom.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 
32 /** \file
33 I2C Example: Demonstrates I2C functions by reading/writing
34 from a Microchip 24LC515 serial EEPROM.
35 
36 */
37 
38 #define EEPROM 0xA0 //LC515 address assuming both address pins tied low.
39 #define BLKSIZE (64)
40 
41 //Assumes WDT is configured for longer than EEPROM write time
42 void waitForWriteCompletion(uint8_t u8_i2cAddr) {
43  uint8_t u8_ack, u8_savedSWDTEN;
44  u8_savedSWDTEN = _SWDTEN;
45  _SWDTEN = 1; //enable WDT so that do not get stuck in infinite loop!
46  u8_i2cAddr = I2C_WADDR(u8_i2cAddr); //write operation, R/W# = 0;
47  do {
48  startI2C1();
49  u8_ack = putNoAckCheckI2C1(u8_i2cAddr);
50  stopI2C1();
51  } while (u8_ack == I2C_NAK);
52  _SWDTEN = u8_savedSWDTEN; //restore WDT to original state
53 }
54 
55 void memWriteLC515(uint8_t u8_i2cAddr, uint16_t u16_MemAddr, uint8_t *pu8_buf) {
56  uint8_t u8_AddrLo, u8_AddrHi;
57 
58  u8_AddrLo = u16_MemAddr & 0x00FF;
59  u8_AddrHi = (u16_MemAddr >> 8);
60  pu8_buf[0] = u8_AddrHi; //place address into buffer
61  pu8_buf[1] = u8_AddrLo;
62 
63  if (u16_MemAddr & 0x8000) {
64  // if MSB set , set block select bit
65  u8_i2cAddr = u8_i2cAddr | 0x08;
66  }
67  waitForWriteCompletion(u8_i2cAddr);
68  writeNI2C1(u8_i2cAddr,pu8_buf,BLKSIZE+2);
69 }
70 
71 void memReadLC515(uint8_t u8_i2cAddr, uint16_t u16_MemAddr, uint8_t *pu8_buf) {
72 
73  uint8_t u8_AddrLo, u8_AddrHi;
74 
75  u8_AddrLo = u16_MemAddr & 0x00FF;
76  u8_AddrHi = (u16_MemAddr >> 8);
77 
78  if (u16_MemAddr & 0x8000) {
79  // if MSB set , set block select bit
80  u8_i2cAddr = u8_i2cAddr | 0x08;
81  }
82  waitForWriteCompletion(u8_i2cAddr);
83  //set address counter
84  write2I2C1(u8_i2cAddr,u8_AddrHi, u8_AddrLo);
85  //read data
86  readNI2C1(u8_i2cAddr,pu8_buf, BLKSIZE);
87 }
88 
89 
90 int main (void) {
91  uint8_t au8_buf[BLKSIZE+2]; //2 extra bytes for address
92  uint16_t u16_MemAddr;
93  uint8_t u8_Mode;
94 
95  configBasic(HELLO_MSG);
96  configI2C1(400); //configure I2C for 400 KHz
97  outString("\nEnter 'w' for write mode, anything else reads: ");
98  u8_Mode = inCharEcho();
99  outString("\n");
100  u16_MemAddr = 0; //start at location 0 in memory
101  while (1) {
102  uint8_t u8_i;
103  if (u8_Mode == 'w') {
104  outString("Enter chars.\n");
105  //first two buffer locations reserved for starting address
106  for (u8_i = 2; u8_i< BLKSIZE+2; u8_i++) {
107  au8_buf[u8_i] = inCharEcho();
108  }
109  outString("\nDoing Write\n");
110  // write same string twice to check Write Busy polling
111  memWriteLC515(EEPROM,u16_MemAddr, au8_buf); // do write
112  u16_MemAddr = u16_MemAddr + BLKSIZE;
113  memWriteLC515(EEPROM,u16_MemAddr,au8_buf); // do write
114  u16_MemAddr = u16_MemAddr + BLKSIZE;
115  } else {
116  memReadLC515(EEPROM,u16_MemAddr,au8_buf); // do read
117  for (u8_i = 0; u8_i< BLKSIZE; u8_i++) outChar(au8_buf[u8_i]);
118  outString("\nAny key continues read...\n");
119  inChar();
120  u16_MemAddr = u16_MemAddr + BLKSIZE;
121  }
122  }
123 }