This sketch read a single byte into a variable, then, the variable is added by two, the result is written into memory. Finally, byte is readed again. the values are printed to serial port too.
Dependencies: Hotboards_eeprom mbed
main.cpp@0:696194ed3c66, 2016-02-05 (annotated)
- Committer:
- Hotboards
- Date:
- Fri Feb 05 19:06:02 2016 +0000
- Revision:
- 0:696194ed3c66
first release: eeprom write_byte
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:696194ed3c66 | 1 | /* |
Hotboards | 0:696194ed3c66 | 2 | Hotboards eeprom Library - write_byte |
Hotboards | 0:696194ed3c66 | 3 | Demonstrates the use a spi eeprom. The Hotboards_eeprom |
Hotboards | 0:696194ed3c66 | 4 | library works with all Microchip 25xxx serial memory series |
Hotboards | 0:696194ed3c66 | 5 | like the one presented eeprom board (http://www.hotboards.org). |
Hotboards | 0:696194ed3c66 | 6 | This sketch read a single byte into a variable, then, the variable is added by two, |
Hotboards | 0:696194ed3c66 | 7 | the result is written into memory. Finally, byte is readed again. |
Hotboards | 0:696194ed3c66 | 8 | the values are printed to serial port too. |
Hotboards | 0:696194ed3c66 | 9 | |
Hotboards | 0:696194ed3c66 | 10 | The circuit: |
Hotboards | 0:696194ed3c66 | 11 | * VDD --> 3.3v |
Hotboards | 0:696194ed3c66 | 12 | * SO --> PB_14 |
Hotboards | 0:696194ed3c66 | 13 | * SI --> PB_15 |
Hotboards | 0:696194ed3c66 | 14 | * SCK --> PB_13 |
Hotboards | 0:696194ed3c66 | 15 | * CS --> PC_4 |
Hotboards | 0:696194ed3c66 | 16 | * GND --> GND |
Hotboards | 0:696194ed3c66 | 17 | |
Hotboards | 0:696194ed3c66 | 18 | Library created by Diego from Hotboards |
Hotboards | 0:696194ed3c66 | 19 | example added by Pedro from Hotboards |
Hotboards | 0:696194ed3c66 | 20 | */ |
Hotboards | 0:696194ed3c66 | 21 | |
Hotboards | 0:696194ed3c66 | 22 | #include "mbed.h" |
Hotboards | 0:696194ed3c66 | 23 | #include "Hotboards_eeprom.h" |
Hotboards | 0:696194ed3c66 | 24 | |
Hotboards | 0:696194ed3c66 | 25 | /*Open an instance of serial port*/ |
Hotboards | 0:696194ed3c66 | 26 | Serial pc(USBTX,USBRX); |
Hotboards | 0:696194ed3c66 | 27 | /* initialize an instance of SPI bus,setting the SPI pins*/ |
Hotboards | 0:696194ed3c66 | 28 | SPI device(PB_15,PB_14,PB_13); /* SO, SI, SCK*/ |
Hotboards | 0:696194ed3c66 | 29 | /*initialize the library with the numbers of the interface pins*/ |
Hotboards | 0:696194ed3c66 | 30 | Hotboards_eeprom eeprom(device,PC_4,HT_EEPROM25xx_32Kb); /* (spi,Cs,Density) */ |
Hotboards | 0:696194ed3c66 | 31 | |
Hotboards | 0:696194ed3c66 | 32 | int main() |
Hotboards | 0:696194ed3c66 | 33 | { |
Hotboards | 0:696194ed3c66 | 34 | /*SPI transfer at 8 bits, MODE = 3*/ |
Hotboards | 0:696194ed3c66 | 35 | device.format(8,3); |
Hotboards | 0:696194ed3c66 | 36 | /* set the spi frequency to 5MHz */ |
Hotboards | 0:696194ed3c66 | 37 | device.frequency(5000000); |
Hotboards | 0:696194ed3c66 | 38 | /*initialize CS pin*/ |
Hotboards | 0:696194ed3c66 | 39 | eeprom.init(); |
Hotboards | 0:696194ed3c66 | 40 | |
Hotboards | 0:696194ed3c66 | 41 | /* variable to read data*/ |
Hotboards | 0:696194ed3c66 | 42 | char Data; |
Hotboards | 0:696194ed3c66 | 43 | |
Hotboards | 0:696194ed3c66 | 44 | /* read firts byte*/ |
Hotboards | 0:696194ed3c66 | 45 | Data=eeprom.read(0); |
Hotboards | 0:696194ed3c66 | 46 | /* send to serial port*/ |
Hotboards | 0:696194ed3c66 | 47 | printf("%X \n",Data); |
Hotboards | 0:696194ed3c66 | 48 | |
Hotboards | 0:696194ed3c66 | 49 | /* readed data is added by two */ |
Hotboards | 0:696194ed3c66 | 50 | Data+= 2; |
Hotboards | 0:696194ed3c66 | 51 | /* write the result*/ |
Hotboards | 0:696194ed3c66 | 52 | eeprom.write(0,Data); |
Hotboards | 0:696194ed3c66 | 53 | |
Hotboards | 0:696194ed3c66 | 54 | /* read firts byte again*/ |
Hotboards | 0:696194ed3c66 | 55 | Data=eeprom.read(0); |
Hotboards | 0:696194ed3c66 | 56 | /* send to serial port */ |
Hotboards | 0:696194ed3c66 | 57 | printf("%X \n",Data); |
Hotboards | 0:696194ed3c66 | 58 | |
Hotboards | 0:696194ed3c66 | 59 | while(1) |
Hotboards | 0:696194ed3c66 | 60 | { |
Hotboards | 0:696194ed3c66 | 61 | /*loop*/ |
Hotboards | 0:696194ed3c66 | 62 | } |
Hotboards | 0:696194ed3c66 | 63 | |
Hotboards | 0:696194ed3c66 | 64 | } |