Lesson 56--Microduino EEPROM Data Flow Serial Input
PurposeThis course will use Microduino to achieve data flow serial input. EquipmentSchematicProgram #include <EEPROM.h>
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}
unsigned long incomingByte = 0; // Define the initial value of the unsigned long integer variable “incomingByte” to be 0.
void setup() {
Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600.
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.parseInt(); //Read the next valid serial incoming integer and assign the integer to variable incomingByte.
EEPROM_write(0,incomingByte) //Write EEPROM into incomingByte from 0 address bit.
}
EEPROM_read(0,incomingByte) //Read EEPROM from 0 address bit and write the value into variable incomingByte.
Serial.println(incomingByte); //Output by the serial port.
delay(1000);
} DebuggingStep 1: Download the code and run it. Here we use Serial.parseInt() , whose function is to search the next valid integer in the incoming serial data flow. Serial.read() can only read one byte each time, which is inconvenient while Serial.parseInt() can read a data flow each time, making it easier in the aspect. Step 2: Open the serial communication and meantime, in each one second should display the “unsigned long” value once in the EEPROM address 0. (Here the value of address 0 is 123456.) Step 3: Input any value (0~4294967295) and the data flow will be output at the bottom of EEPROM. (Here is output value is 77777.) ResultThe data flow input in the serial port will be saved into EEPROM. Video |