This sketch write an array into a memory, then, read back the data and print them to a serial port.
Dependencies: Hotboards_eeprom mbed
main.cpp@0:1caeae863b7f, 2016-02-05 (annotated)
- Committer:
- Hotboards
- Date:
- Fri Feb 05 21:35:42 2016 +0000
- Revision:
- 0:1caeae863b7f
first Release: Hotboards_eeprom_write_array
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:1caeae863b7f | 1 | /* |
Hotboards | 0:1caeae863b7f | 2 | Hotboards eeprom Library - write_array |
Hotboards | 0:1caeae863b7f | 3 | Demonstrates the use a spi eeprom. The Hotboards_eeprom |
Hotboards | 0:1caeae863b7f | 4 | library works with all Microchip 25xxx serial memory series |
Hotboards | 0:1caeae863b7f | 5 | like the one presented eeprom board (http://www.hotboards.org). |
Hotboards | 0:1caeae863b7f | 6 | This sketch write an array into a memory, then, read back the data |
Hotboards | 0:1caeae863b7f | 7 | and print them to a serial port. |
Hotboards | 0:1caeae863b7f | 8 | |
Hotboards | 0:1caeae863b7f | 9 | The circuit: |
Hotboards | 0:1caeae863b7f | 10 | * VDD --> 3.3v |
Hotboards | 0:1caeae863b7f | 11 | * SO --> PB_14 |
Hotboards | 0:1caeae863b7f | 12 | * SI --> PB_15 |
Hotboards | 0:1caeae863b7f | 13 | * SCK --> PB_13 |
Hotboards | 0:1caeae863b7f | 14 | * CS --> PC_4 |
Hotboards | 0:1caeae863b7f | 15 | * GND --> GND |
Hotboards | 0:1caeae863b7f | 16 | |
Hotboards | 0:1caeae863b7f | 17 | Library created by Diego from Hotboards |
Hotboards | 0:1caeae863b7f | 18 | example added by Pedro from Hotboards |
Hotboards | 0:1caeae863b7f | 19 | */ |
Hotboards | 0:1caeae863b7f | 20 | #include "mbed.h" |
Hotboards | 0:1caeae863b7f | 21 | #include "Hotboards_eeprom.h" |
Hotboards | 0:1caeae863b7f | 22 | /*Open an instance of serial port*/ |
Hotboards | 0:1caeae863b7f | 23 | Serial pc(USBTX,USBRX); |
Hotboards | 0:1caeae863b7f | 24 | /* initialize an instance of SPI bus,setting the SPI pins*/ |
Hotboards | 0:1caeae863b7f | 25 | SPI device(PB_15,PB_14,PB_13); /* SO, SI, SCK*/ |
Hotboards | 0:1caeae863b7f | 26 | /*initialize the library with the numbers of the interface pins*/ |
Hotboards | 0:1caeae863b7f | 27 | Hotboards_eeprom eeprom(device,PC_4,HT_EEPROM25xx_32Kb); /* (spi,Cs,Density) */ |
Hotboards | 0:1caeae863b7f | 28 | |
Hotboards | 0:1caeae863b7f | 29 | int main() |
Hotboards | 0:1caeae863b7f | 30 | { |
Hotboards | 0:1caeae863b7f | 31 | /*SPI transfer at 8 bits, MODE = 3*/ |
Hotboards | 0:1caeae863b7f | 32 | device.format(8,3); |
Hotboards | 0:1caeae863b7f | 33 | /* set the spi frequency to 5MHz */ |
Hotboards | 0:1caeae863b7f | 34 | device.frequency(5000000); |
Hotboards | 0:1caeae863b7f | 35 | /*initialize CS pin*/ |
Hotboards | 0:1caeae863b7f | 36 | eeprom.init(); |
Hotboards | 0:1caeae863b7f | 37 | /*array to be write*/ |
Hotboards | 0:1caeae863b7f | 38 | uint8_t write_array[]= "[25AA320-EXAMPLE]\n"; |
Hotboards | 0:1caeae863b7f | 39 | /*array for read */ |
Hotboards | 0:1caeae863b7f | 40 | uint8_t read_array[20]; |
Hotboards | 0:1caeae863b7f | 41 | |
Hotboards | 0:1caeae863b7f | 42 | /*write the entire array starting at 0 adress*/ |
Hotboards | 0:1caeae863b7f | 43 | eeprom.write(0,write_array,sizeof(write_array)); |
Hotboards | 0:1caeae863b7f | 44 | |
Hotboards | 0:1caeae863b7f | 45 | /*read back to "read_array"*/ |
Hotboards | 0:1caeae863b7f | 46 | eeprom.read(0,(uint8_t*)read_array,20); |
Hotboards | 0:1caeae863b7f | 47 | |
Hotboards | 0:1caeae863b7f | 48 | /*print adress and data to serial port*/ |
Hotboards | 0:1caeae863b7f | 49 | printf("ADRESS|DATA \n"); |
Hotboards | 0:1caeae863b7f | 50 | for(int j=0;j<17;j++) |
Hotboards | 0:1caeae863b7f | 51 | { |
Hotboards | 0:1caeae863b7f | 52 | printf("%2d %c \n",j,read_array[j]); |
Hotboards | 0:1caeae863b7f | 53 | } |
Hotboards | 0:1caeae863b7f | 54 | |
Hotboards | 0:1caeae863b7f | 55 | while(1) |
Hotboards | 0:1caeae863b7f | 56 | { |
Hotboards | 0:1caeae863b7f | 57 | /*loop*/ |
Hotboards | 0:1caeae863b7f | 58 | } |
Hotboards | 0:1caeae863b7f | 59 | } |