Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
1
CB-029 32k x 8-bit EEPROM Module
September 05, 2014
Introduction
This application note will show two minimal code set ups to operate the EEPROM module CB-029 one with an Arduino board and the other with Cardinal’s CB-026 Microchip microcontroller module.
The CB-029 EEPROM has 256k bits arranged as 32k 8-bit words. This note will show how to write to and reading from the EEPROM.
Arduino
The Arduino example will write a text phrase to the EEPROM, read it back and count the occurrence of the letter ‘a’ in the read phrase. The Arduino serial monitor port will be used to display the result.
Before proceeding any further, please have the Arduino software install to your computer. Visit Arduino’s user setup guide at http://arduino.cc/en/Guide/windows for step-by-step instructions.
Hardware
Parts List:
Arduino Uno USB type B connector CB-I-022 Cardinal Interface Board CB-029 EEPROM module
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
2
Connect the components as shown in the picture below
Plug the CB-I-022 interface into the Arduino Uno board and the CB-029 EEPROM module into the interface’s Electro-Set 6 pin connector. Connect the USB cable between the computer and the Arduino Uno.
The computer will supply 5 volts through the USB cable which will be regulated on the Arduino interface board to 3.3 volts to run the CB-029 EEPROM module.
Three jumpers on the CB-029 can be used to change the slave address of the EEPROM. For this code example we are using 0x57 as the 7-bit slave address so the three jumper should be populated towards the center of the board as shown in the picture above. This makes the lower three slave address bits a logic one (high), the 7 in 0x57 slave address.
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
3
The Software
#include <Wire.h> // Wire I2C library
#define EEPROM_ADDRESS 0x57 // 7-bit I2C EEPROM_ADDRESS of eeprom
static char someText[] = "Mary had a little lamb it's fleece was white as snow.";
unsigned int index = 0;
unsigned int len,i;
char myChar;
unsigned int count=0;
void setup()
{
Serial.begin(9600); // initialize serial monitor to 9600 baud
Serial.println("Electro-Set"); // welcome message
Serial.println("CB-029 Test"); // welcome message
Wire.begin(); // initialize I2C port
len=strlen(someText); // the length of the text to write, the number of bytes
Serial.print("The number of characters in someText to write is ");
Serial.println(len); // print number of bytes to serial monitor
}
void loop()
{
Serial.println("Writing:"); // Display info to the serial monitor
while(index<len){ // A while loop to write the text to eeprom
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
4
Wire.beginTransmission(EEPROM_ADDRESS); // start of I2C tramsmission to eeprom
Wire.write(index>>8); // MSB of 16-bit eeprom memory address
Wire.write(index&0xff); // LSB of 16-bit eeprom memory address
Wire.write(someText[index]); // data byte to write to eeprom memory
Wire.endTransmission(); // Complete the I2C transmission
delay(5); // The delay is very important, the eeprom write is slow
Serial.print(someText[index]); // Display the text being written to the serial monitor
index++; // Increment the index
} // end of while()
Serial.println(); // New line
Serial.println("Reading:"); // Display info to the serial monitor
for(i=0;i<len;i++){ // A for loop to read back the text and count the 'a's
Wire.beginTransmission(EEPROM_ADDRESS); // start of I2C tramsmission to eeprom
Wire.write(i>>8); // MSB of 16-bit eeprom memory address
Wire.write(i&0xff); // LSB of 16-bit eeprom memory address
Wire.endTransmission(); // Complete the I2C transmission
Wire.requestFrom(EEPROM_ADDRESS, 1); // Read 1 bytes from eeprom into Wire input buffer
if(Wire.available() == 1){ // If we received one bytes, that is good
myChar=Wire.read(); // retrieve the byte from the buffer
Serial.print(myChar); // display the char read on the serial monitor
if (myChar=='a') // compare the character with the charater 'a'
count++; // if it matches add 1 to the count
}
} // end of for()
Serial.println(); // New line
Serial.println("Result:"); // Display info to the serial monitor
Serial.print("Number of 'a' is "); // Output the count to the serial monitor
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
5
Serial.println(count);
while(1); // Go no further, we are done, loop forever
} // end of loop()
Code Operation
The EEPROM module has a 7-bit I2C slave address as 0x57 which we make a #define.
The setup() function is called when sketch starts. The setup code, initializes the serial monitor baud rate at 9600, outputs a welcome message and then initializes the I2C port as a master. We compute the length of the text phrase stored in the “someText” array and output this length (‘len’) to the serial monitor which for our example turns out to be 53 bytes.
After setup has completed, sketch causes the function loop() to be repeatedly called.
The variable ‘index’ is used to access through the “someText” array and is also used as the memory address in the EEPROM. The EEPROM memory address is 16-bits wide, we use the upper byte of ‘index’ for the MSB of the EEPROM address and the lower byte of ‘index’ for the LSB of the EPROM address. In our example the MSB will always be 0x00 and LSB will range from 0x00 to 0x52. The ‘while’ loop takes one byte at a time from the ‘someText’ array and writes it to the EEPROM, looping ‘len’ (53) time to completely write the phrase.
Take special note of the ‘delay(5);’ code line. Without this the writes to EEPROM will be arriving too fast for the EEPROM and will be discarded. The write cycle time from the datasheet is specified at 5mS.
Next the code reads back the values from the EEPROM using a ‘for’ loop (just to be different). As each byte is retrieved it is compared against the character ‘a’, if matching the ‘count’ variable is incremented. Once the ‘for’ loop completes its 53 iterations, the value of the ‘count’ variable is output to the serial monitor. Lastly we execute the code ‘while(1);’, this puts the microcontroller into an endless loop doing nothing harmful and causing our code in the sketch ‘loop()’ function to execute only once. As an alternative, we could have placed all our code in the sketch ‘setup()’ function since it only executes once.
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
6
Microchip Module CB-026
The CB-026 example in PICBASIC PRO will write 10 data values into the EEPROM on the CB-029 module then read them back displaying the data values via the serial port to a terminal emulator such as hyperterminal
Before proceeding any further, please have the MicroCode Studio PICBASIC PRO suite installed on your computer.
Hardware
Parts List:
Electro-Set module CB-026 microcontroller USB type mini connector CB-029 EEPROM module
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
7
Connect the components as shown in the picture below
The Software
INCLUDE "modedefs.bas"
#CONFIG __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
#ENDCONFIG
DEFINE OSC 20 'Set for 20 MHz oscillator
SCL var PORTC.3 ‘ Set I2C pins
SDA var PORTC.4
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
8
Soutpin var PORTC.6 ‘ Set serial port pins
Sinpin var PORTC.7
baud1 var byte ‘ for serial port baud rate
EEpromadd var word ‘ 16-bit address of where to write the data in the EEPROM
eepromdata var byte ‘ the 8-bit data to write into the EEPROM
baud1 = 6 ‘ 38400 baud, non-inverted
test_EEProm: ‘ label to loop to repeat the sequence
serout2 soutpin,baud1,["TESTING EEProm",13,10] ‘ Welcome message to serial port
serout2 soutpin,baud1,["Writing to the first 10 locations of the EEProm",13,10]
pause 1000 ‘ wait 1 second
for eepromadd = 1 to 10 ‘ loop to cause eepromadd to increment from 1 to 10
eepromdata = eepromadd & $00FF ’ mask the address value to return the lower 8-bits
eepromdata = eepromdata * 2 ‘ double the value
serout2 soutpin, baud1,["Writing Address ",dec2 eepromadd," Data = ",dec2 eepromdata,13,10]
i2cwrite sda,scl,$AE,EEpromadd,[eepromdata],nodata ‘ write the data into the EEPROM
pause 250 ‘ Some delay is very important, EEPROM writes are slow
next eepromadd
pause 500
serout2 soutpin,baud1,["Reading the first 10 locations of the EEProm",13,10]
for eepromadd = 1 to 10 ‘ loop to cause eepromadd to increment from 1 to 10
i2cread sda,scl,$AE,eepromadd,[eepromdata],nodata ‘ read from EEPROM
serout2 soutpin,baud1,["Reading Address ",Dec eepromadd," Data = ",dec2 eepromdata,13,10]
pause 250 ‘ This delay not important, reads of EEPROM are fast
next eepromadd
serout2 soutpin,baud1,["Done",13,10]
pause 500
goto test_eeprom ‘ do it all over again
NoData: ‘ Excute this if the I2C communication fails
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
9
Serout2 soutpin,baud1,["EEProm Module Not Connected",13,10]
pause 500
goto Test_EEProm
end
Code Operation
The line
“# CONFIG __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF”
includes configuration for the programmer in the output hex file so it does not necessary to manually change the entries in the programmer configuration window every time you come to write a new version of your program to the microcontroller.
The port pins for the I2C interface are set on PORTC pins 3 and 4. The CB-026 board wires these to the Electro-Set 6 pin connector.
The microcontroller serial port pins are set on PORTC pins 6 and 7, these connect to the FTDI chip on the CB-026 which couples through to the USB and so when plugged into a PC producing a virtual COMM port where the microcontroller’s serial data can be seen on a terminal emulator such as hyperterminal. The terminal emulator should be set to connect to the virtual COMM port assigned to the FTDI driver and connect at 38400,8,N,1.
We declare the variable baud1 for use in the serout2 to set the baud rate. For a baud rate of 38400, baud1 is set to 6, see the MicroChip documentation for other rates and options.
The first parameters for the PicBasicPro functions i2cread and i2cwrite uses and 8-bit slave address which is a one bit left shift of the 7-bit slave address, so the A/D module’s 8-bit slave address as 0xAE.
The line
for eepromadd = 1 to 10
sets up a ‘for’ loop to sequentially increment the 16-bit word variable eepromadd from 1 to 10. These are the address locations we will write to in this example. In this loop we take the lower 8-bits of eepromadd, double that value and save the result in the byte variable eepromdata, this is the value we will write using the following line of code:
i2cwrite sda,scl,$AE,EEpromadd,[eepromdata],nodata ‘ write the data into the EEPROM
Between successive writes to the EEPROM we need some delay, around 5mS. Without this delay the write will be rejected by the EEPROM.
_____________________________________________________________________________________
Rev. 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
10
Finally we set up another ‘for’ loop to read and display the data on the serial port.
.