TUJUAN :
1. Mampu membuat program untuk melakukan penyimpanan data pada memori eeprom.
2. Mampu membuat program untuk melakukan pembacaan data dari memori eeprom.
PERALATAN :
1. Komputer : 1
set
2. Arduino Uno : 1
pcs
3. Kabel USB tipe B : 1 pcs
PERCOBAAN :
8.1 Baca dan Tulis Memori EEPROM
Pada percobaan kali ini akan dibuat program yang berfungsi penanganan memori eeprom. Antarmuka program menggunakan sebagai antarmuka
komunikasi serial.
Melalui Serial Terminal atau aplikasi lainnya, user dapat melakukan perintah baca dan tulis dari atau ke memori eeprom.
Prosedur :
#include <EEPROM.h>
boolean exitProgram = 0;
kemudian lakukan
void setup() { Serial.begin(9600);
Serial.println("========= EEPROM Access Via Serial ========"); Serial.println("Type this command to execute EEPROM operation"); Serial.println("write : For write data to EEPROM"); Serial.println("read : For read data from EEPROM"); Serial.println("clear : For clear all data in EEPROM"); Serial.println("exit : For exit from program");
}
void loop() { int command; while(!exitProgram) {
do {
Serial.print("Type Command >> "); while(Serial.available()==0); command = readCommandFromSerial();
switch(command) {
case 1 : case 2 : case 3 :
case 4 :
default :
}
}
Serial.println("Write EEPROM Selected");
writeEEPROM();
break;
Serial.println("Read EEPROM Selected");
readEEPROM();
break;
Serial.println("Clear EEPROM Selected");
clearEEPROM();
Serial.println("Clear EEPROM Finished");
break;
Serial.println("Exit From Program");
exitProgram = 1;
break;
Serial.println("Wrong Command, Please Type Again !");
break;
while(command
}
}
== 0);
int readCommandFromSerial() { char stringFromSerial[10]; char data;
nt command;
int countData = 0;
for(int i=0;i<1 span="">0;i++) {1>
stringFromSerial[i]=0;
}
while(true) {
if(Serial.available()) {
data = Serial.read(); Serial.write(data); if(data=='\n') {
break;
}
else {
if(data!='\r') {
stringFromSerial[countData] = data;
countData++;
}
}
}
}
if(strcmp(stringFromSerial,"write")==0){
command = 1;
}
else if(strcmp(stringFromSerial,"read")==0){
command = 2;
}
else if(strcmp(stringFromSerial,"clear")==0){
command = 3;
}
else if(strcmp(stringFromSerial,"exit")==0){
command = 4;
}
else{
command = 0;
}
return command;
}
int readValFromSerial() { char stringFromSerial[10]; char data;
int val;
int countData = 0;
for(int i=0;i<1 span="">0;i++) {1>
stringFromSerial[i]=0;
}
while(true) {
if(Serial.available()) {
data = Serial.read(); Serial.write(data); if(data=='\n') {
break;
}
else {
if(data!='\r') { stringFromSerial[countData] = data; countData++;
}
}
}
}
sscanf(stringFromSerial,"%d",&val);
return val;
}
void clearEEPROM() {
for(int i=0;i<5 span="">12;i++) {5>
EEPROM.write(i,0);
}
}
void writeEEPROM() {
int address;
do {
Serial.print("Address : "); address = readValFromSerial(); if(address>512) {
Serial.println("Addres maximal is 512 !, Please type again !");
}
}
while(address>512);
int data;
do {
Serial.print("Data : ");
data = readValFromSerial();
if(data>512) {
Serial.println("Data maximal is 512 !, Please type again !");
}
}
while(data>512);
EEPROM.write(address,data); Serial.println("EEPROM Write Success !");
}
void readEEPROM() {
int address;
do {
Serial.print("Address : ");
address = readValFromSerial();
if(address>512) {
Serial.println("Addres maximal is 512 !, Please type
again !");
}
}
while(address>512);
int data = EEPROM.read(address); Serial.print("Data in Address "); Serial.print(address,DEC);
Serial.print(" :
");
Serial.println(data,DEC);
}
Tidak ada komentar:
Posting Komentar