gravar 10 digitos na eeprom

1,159 views
Skip to first unread message

Sergiolinux

unread,
Sep 25, 2013, 5:54:38 PM9/25/13
to hacker...@googlegroups.com
Olá pessoal, mais um help...rs

Por favor, preciso gravar um número de celular na eeprom do arduino.
Não sei com fazer, terei uma variavel com o numero 999999999 e como faço para dividir isso para poder usar o EEPROM.write?
Auguem pode me ajudar com isso? 

Obrigado.

Sérgio

Felipe Iasi

unread,
Sep 25, 2013, 5:56:43 PM9/25/13
to hacker...@googlegroups.com

Da um bizu...

Enviado via iPhone
--
.--. .- .-. .- .--. --- ... - .- .-. . ... -.-. .-. . ...- .- .--. .- .-. .- .... .- -.-. -.- . .-. ... .--. .- -.-. . ... .--. .- - --. --- --- --. .-.. . --. .-. --- ..- .--. ... -.. --- - -.-. --- --
Regras da Lista: http://garoa.net.br/wiki/Lista:LeiaAntesDeClicarNoSend
Para mais informações sobre o Garoa Hacker Clube acesse http://garoa.net.br
Maiores opções sobre o Google Groups, visite: http://groups.google.com/group/hackerspacesp
.--. .- .-. .- -- .- .. ... .. -. ..-. --- .-. -- .- . ... .- -.-. . ... ... . --- .-- .. -.- ..
Epoch 0 <=> Fundação: 1298244863 s ~ 2.408064*10^52 tP (tempos de Planck)
 

Leandro A. F. Pereira

unread,
Sep 25, 2013, 6:04:08 PM9/25/13
to hacker...@googlegroups.com
2013/9/25 Sergiolinux <sergi...@gmail.com>:
> Não sei com fazer, terei uma variavel com o numero 999999999 e como faço
> para dividir isso para poder usar o EEPROM.write?

As funções de gravar e ler da EEPROM do Arduino só o fazem byte a
byte, então convém escrever funções auxiliares para gravar e ler. Por
exemplo, alguma coisa como isso aqui pode servir:

void escreve_numero(char *n, unsigned pos)
{
for (; *n; n++, pos++)
EEPROM.write(pos, *n);
EEPROM.write(pos, '\0');
}

void le_numero(char *n, unsigned pos)
{
do {
int c = EEPROM.read(pos++);
*n++ = c;
} while (c != '\0');
}

Se espaço estiver a prêmio, dá pra usar um formato melhor que string
na EEPROM. Mas aí dá um pouco mais de trabalho. :)

--
Leandro

Rodrigo 'Skhaz' Delduca

unread,
Sep 25, 2013, 6:05:15 PM9/25/13
to hacker...@googlegroups.com
Você precisa "dividir" a variável em duas partes:

uint16_t telefone = 999999999;

uint8_t primeiraParte = telefone & 0xFF;
uint8_t segundaParte = telefone >> 8;
EEPROM.write(endereco, primeiraParte);
EEPROM.write(endereco, segundaParte);

Para recuperar você precisa fazer o inverso, assim:

uint8_t primeiraParte = EEPROM.read(endereco);
uint8_t segundaParte = EEPROM.read(endereco);
uint16_t telefone = (primeiraParte << 8) + segundaParte;

2013/9/25 Sergiolinux <sergi...@gmail.com>:
> --
> .--. .- .-. .- .--. --- ... - .- .-. . ... -.-. .-. . ...- .- .--. .- .-. .-
> .... .- -.-. -.- . .-. ... .--. .- -.-. . ... .--. .- - --. --- --- --. .-..
> . --. .-. --- ..- .--. ... -.. --- - -.-. --- --
> Regras da Lista: http://garoa.net.br/wiki/Lista:LeiaAntesDeClicarNoSend
> Para mais informações sobre o Garoa Hacker Clube acesse http://garoa.net.br
> Maiores opções sobre o Google Groups, visite:
> http://groups.google.com/group/hackerspacesp
> .--. .- .-. .- -- .- .. ... .. -. ..-. --- .-. -- .- . ... .- -.-. . ... ...
> . --- .-- .. -.- ..
> Epoch 0 <=> Fundação: 1298244863 s ~ 2.408064*10^52 tP (tempos de Planck)
>



--
http://www.nullonerror.org/
-- flipping bits whilst updating pixels

Sergiolinux

unread,
Sep 25, 2013, 6:07:06 PM9/25/13
to hacker...@googlegroups.com
O Felipe, valeu mesmo, mas já passei por ai.
Para escrever e ler tá tranquilo...o negócio é:

como gravar uma variavel = 999999999

?

Ai que pegou pra mim...

Abraços.

Sergiolinux

unread,
Sep 25, 2013, 6:18:42 PM9/25/13
to hacker...@googlegroups.com
Rodrigo, tentei aqui e deu erro na linha..."uint8_t primeiraParte =  EEPROM.read(0); "

o que estou fazendo de errado?

#include <EEPROM.h>

uint16_t telefone = 999999999; 

void setup()
{
Serial.begin(9600);

uint8_t primeiraParte = telefone & 0xFF;
uint8_t segundaParte = telefone >> 8;
EEPROM.write(0, primeiraParte); 
EEPROM.write(10, segundaParte); 

uint8_t primeiraParte =  EEPROM.read(0); 
uint8_t segundaParte =  EEPROM.read(10); 
uint16_t telefone = (primeiraParte << 8) + segundaParte;

Serial.println(telefone);

}

void loop() { }

------------------------

Leandro A. F. Pereira

unread,
Sep 25, 2013, 6:21:27 PM9/25/13
to hacker...@googlegroups.com
2013/9/25 Rodrigo 'Skhaz' Delduca <rodrigo...@gmail.com>:
> uint16_t telefone = 999999999;

Um inteiro de 16 bits sem sinal vai guardar valores de 0 a 65535.

--
Leandro

Sergiolinux

unread,
Sep 25, 2013, 6:25:12 PM9/25/13
to hacker...@googlegroups.com, lea...@tia.mat.br
Então tenho que dividir em 3 partes?

Leandro A. F. Pereira

unread,
Sep 25, 2013, 6:27:08 PM9/25/13
to hacker...@googlegroups.com
2013/9/25 Sergiolinux <sergi...@gmail.com>:
> Então tenho que dividir em 3 partes?

Ou gravar em string mesmo; veja meu email anterior.

--
Leandro

Rodrigo 'Skhaz' Delduca

unread,
Sep 25, 2013, 6:38:32 PM9/25/13
to hacker...@googlegroups.com, lea...@tia.mat.br
Perdão, o Leandro tem razão, deveria ser uint32_t, corrigido o outro problema

http://codebender.cc/sketch:19473



2013/9/25 Sergiolinux <sergi...@gmail.com>:

Sergiolinux

unread,
Sep 26, 2013, 9:54:39 AM9/26/13
to hacker...@googlegroups.com, lea...@tia.mat.br
Rodrigo, muito obrigado (obrigado a todos).
Estou testando o código mas o número que ele retorna no Serial.print(telefone) é:

4294953471

O que acontece?
Posso abusar mais um pouco?
Pode me explicar essas linhas?
byte a = (telefone >> 24) & 0xff;

Obrigado mesmo.

Abraços.

Sergiolinux

unread,
Sep 26, 2013, 12:12:13 PM9/26/13
to hacker...@googlegroups.com, lea...@tia.mat.br
Rodrigo, pesquisei aqui,,,,

o numero 999999999 é igual ao binario 111011100110101100100111111111
então vc lê até o bit 24 depois até o 18 ... certo?
oque é - & 0xff   ?

então ele grana na o seguinte:
111011 10011010 11001001 11111111
59          154        201       255

ok?

Agora quem faria o inverso, seria essa linha certo?
 uint32_t telefone =  (a << 24) | (b << 16) | (c << 8) | d;

O que acontece que o resultado sai 4294953471 ?

Estou colocando a baixo o seu código com os prints que coloquei para eu começar a intender...

#include <EEPROM.h>
 
 
void setup()
{
  Serial.begin(9600);
  
  { // Escreve 
    uint32_t telefone = 999999999; 
 
 
    Serial.println(telefone);
 
    byte a = (telefone >> 24) & 0xff;
    byte b = (telefone >> 16) & 0xff;
    byte c = (telefone >> 8) & 0xff;
    byte d = (telefone) & 0xff;
 
    Serial.print("---so o a em BIN: ");
    Serial.println(a, BIN);
    Serial.print("---so o b em BIN: ");
    Serial.println(b, BIN);
    Serial.print("---so o c em BIN: ");
    Serial.println(c, BIN);
    Serial.print("---so o d em BIN: ");
    Serial.println(d, BIN);
    Serial.print("---TODOS em BIN: ");
    Serial.print(a, BIN);Serial.print(b, BIN);Serial.print(c, BIN);Serial.println(d, BIN);

    EEPROM.write(0, a); 
    Serial.print("---byt a: ");
    Serial.println(a);
    EEPROM.write(1, b);
    Serial.print("---byt b: ");
    Serial.println(b);
    EEPROM.write(2, c); 
    Serial.print("---byt c: ");
    Serial.println(c);
    EEPROM.write(3, d); 
    Serial.print("---byt d: ");
    Serial.println(d);    
   
    Serial.print("--- TODOS em DECIMAL: ");
    Serial.print(a);Serial.print(b);Serial.print(c);Serial.println(d);
    
  }
 
  { // Lê
  
      Serial.println("-------- LENDO e imprimindo em BINARIO");
  
    byte a =  EEPROM.read(0); 
    byte b =  EEPROM.read(1); 
    byte c =  EEPROM.read(2); 
    byte d =  EEPROM.read(3); 
 
    Serial.print("---byt a: ");
    Serial.println(a,BIN);
    Serial.print("---byt b: ");
    Serial.println(b,BIN);
    Serial.print("---byt c: ");
    Serial.println(c,BIN);
    Serial.print("---byt d: ");
    Serial.println(d,BIN);
 
    uint32_t telefone =  (a << 24) | (b << 16) | (c << 8) | d;
  
    Serial.print("--- Numero de telefone final");
    Serial.println(telefone);
  }
}
 
void loop() { }

Abraços

Sergiolinux

unread,
Oct 1, 2013, 9:10:39 AM10/1/13
to hacker...@googlegroups.com, lea...@tia.mat.br
Pessoal, tá quase finalizado, utilizei as funções do Leandro e ficou assim: (abaixo)
Só falta ao ler a eeprom juntar os valores em uma variável...alguem sabe como fazer isso?

Obrigado..

#-----inicio código

#include <EEPROM.h>

int c;

char *numero= "11999999999";

void setup(){
  Serial.begin(9600);
  escreve_numero(numero,1);
  int a = 1;
  int b = 12;  
  ler(a,b);
//  le_numero(0,1);

}

void escreve_numero(char *n, unsigned pos) 
    for (; *n; n++, pos++) {
      Serial.print(pos);Serial.print(" : ");Serial.println(*n);
      EEPROM.write(pos, *n); 
}
    EEPROM.write(pos, '\0'); 
    Serial.println("------");


void ler(int ini, int fim) 
    for (int x=ini; x<fim; x++) {
      Serial.print(x);Serial.print(" : ");
      char c = EEPROM.read(x);
      Serial.println(c);
    }


/*
void le_numero(char *n, unsigned pos) 
    do { 
       int c = EEPROM.read(pos++); 
       *n++ = c; 
    } while (c != '\0'); 
*/

void loop(){}



#------fim código
Reply all
Reply to author
Forward
0 new messages