Adatgy2014 / Mbed 2 deprecated SPI_test

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalIn mybutton(USER_BUTTON); // Activate button
00004 DigitalOut chipselect(D10); // ChipSelect (CS) pin
00005 SPI eeprom(SPI_MOSI, SPI_MISO, SPI_SCK); // Activate SPI to ST eeprom
00006 Serial pc(SERIAL_TX, SERIAL_RX);
00007 
00008 #define WREN 0x06
00009 #define RDSR 0x05
00010 #define READ_LOW 0x03
00011 #define WRITE_LOW 0x02
00012  
00013 int main() {
00014     char char_in = 't';
00015     char char_out;
00016     int i = 0;
00017 
00018     chipselect =1;
00019     eeprom.frequency(1000000); // set 1 MHz clock rate
00020     while(1) {
00021         if (mybutton == 0) { // Button is pressed
00022             chipselect = 0;
00023             eeprom.write(RDSR);
00024             eeprom.write(0x00);
00025             chipselect = 1;
00026             chipselect = 0;
00027             eeprom.write(WREN);
00028             chipselect = 1;
00029             wait_us(1);
00030             while((char_in == 'y') == (char_in == 'n')){
00031                 pc.printf("\n\rDo you want type in some characters? Answer (y/n): ");
00032                 char_in=pc.getc();
00033                 pc.putc(char_in);
00034                 if((char_in != 'n') == (char_in != 'y')) {
00035                     pc.printf("\n\rPlease substitute %c with (y/n)-t", char_in);
00036                 }
00037             }
00038             if(char_in == 'y'){
00039                 pc.printf("\n\rWrite something (max. 16 chars): ");
00040                 i = 0;
00041                 while(i < 16){
00042                     char_in=pc.getc();
00043                     if(char_in == '\n'){
00044                         i = 16;
00045                         break;
00046                     }
00047                     chipselect = 0;
00048                     eeprom.write(WREN);
00049                     chipselect = 1;
00050                     wait_us(1);
00051                     chipselect = 0;
00052                     eeprom.write(WRITE_LOW);
00053                     eeprom.write(i);
00054                     eeprom.write(char_in);
00055                     chipselect = 1;
00056                     i++;
00057                     pc.putc(char_in);
00058                 }
00059                 char_in = 't';
00060             }
00061             if(char_in == 'n'){
00062                 pc.printf("\n\rFirst page: ");
00063                 for(i=0; i<16; i++){
00064                     chipselect = 0;
00065                     eeprom.write(READ_LOW);
00066                     eeprom.write(i);
00067                     char_out = eeprom.write(0x00);
00068                     chipselect = 1;
00069                     pc.putc(char_out);
00070                 }
00071                 char_in = 't';
00072             }
00073             pc.printf("\n\r");
00074             wait(2);
00075         }
00076     }
00077 }
00078