Forum: Mikrocontroller und Digitale Elektronik EEPROM write/read für PIC18F27Q84


von Chris (chris_dd)


Lesenswert?

Hallo,

ich wollte in einem Projekt einen Marker setzen, der auch einen 
Stromreset übersteht, dafür wollte ich in den EEPROM schreiben mit:

DATAEE_WriteByte(0x2, 0xAB);

Dazu gehört dann eine IF clause, bei der das gleiche Byte wieder 
ausgelesen wird:

if (DATAEE_ReadByte(0x2)==0xAB)

Das funktioniert bei mir nicht. Mein Wissen ist aber auch sehr begrenzt. 
Hat jemand eine Idee woran es liegen könnte?

Danke schonmal vorab.

Chris

von Franko P. (sgssn)


Lesenswert?

Hallo
wie sieht denn die ganze Routine dazu aus? Und wo hast du das her? Mit 
MCC erzeugt oder was?
https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ProductDocuments/DataSheets/PIC18F27-47-57Q84-Microcontroller-Data-Sheet-DS40002213.pdf

Seite 106 steht, wie man da ein Word ins EEPROM schreibt.
Gruß

: Bearbeitet durch User
von Chris (chris_dd)


Lesenswert?

Danke für die Rückmeldung.

Der Teil zum Schreiben sieht so aus:

switch(funcRes)
        {
            case STATE_RUNNING:
                break;
            case STATE_ERROR:
                InitNextStage(SEQ_CONNECTION_REQUEST);
                break;
            case STATE_OK:
                DATAEE_WriteByte(0x2, 0xAB);
                InitNextStage(SEQ_IDLE);
                res = 1;
                break;}

Der zum Auslesen so:

if (emul_init_seq==0 && ig_rise_count>=1)
    {
        if (DATAEE_ReadByte(0x2)==0xAB)
        emul_init_seq = 1;
        if (emul_init_seq==2)
        {
            emul_init_seq = 0; // reset flag for sending init sequence
            ig_rise_count = 0;
            coding_message_type = 0;
            InitSendMsgSeq();
        }
        else
        emul_init_seq = codingSequence(&rcvdMsg, msgCnt?1:0);

Ja, stammt aus MCC und einem Bsp. aus der memory.h

von Franko P. (sgssn)


Lesenswert?

Ok, und wie sehen die Funktion DATAEE_WriteByte() und DATAEE_ReadByte() 
aus?

Beinhalten sie die befehle wie im Datenblatt beschrieben?

von Mario M. (thelonging)


Lesenswert?

Chris schrieb:
> case STATE_OK:
> DATAEE_WriteByte(0x2, 0xAB);

Wie oft kommt das Programm da durch? Nicht dass die Zelle schon kaputt 
geschrieben wurde.

von Chris (chris_dd)


Lesenswert?

Die Funktionen sind durch MCC generiert:

void DATAEE_WriteByte(uint16_t bAdd, uint8_t bData)
{
    uint8_t GIEBitValue = INTCON0bits.GIE;

    //Set NVMADR with the target word address (0x380000 - 0x3803FF)
    NVMADRU = 0x38;
    NVMADRH = (uint8_t) ((bAdd & 0xFF00) >> 8);
    NVMADRL = (uint8_t) (bAdd & 0x00FF);

    //Load NVMDATL with desired byte
    NVMDATL = bData;

    //Set the NVMCMD control bits for DFM Byte Write operation
    NVMCON1bits.NVMCMD = 0b011;

    //Disable all interrupts
    INTCON0bits.GIE = 0;

    //Perform the unlock sequence and start Page Erase
    NVMLOCK = 0x55;
    NVMLOCK = 0xAA;

    //Start DFM write and wait for the operation to complete
    NVMCON0bits.GO = 1;
    while (NVMCON0bits.GO);

    //Restore all the interrupts
    INTCON0bits.GIE = GIEBitValue;

    //Set the NVMCMD control bits for Word Read operation to avoid 
accidental writes
    NVMCON1bits.NVMCMD = 0b000;
}

uint8_t DATAEE_ReadByte(uint16_t bAdd)
{
    //Set NVMADR with the target word address (0x380000 - 0x3803FF)
    NVMADRU = 0x38;
    NVMADRH = (uint8_t) ((bAdd & 0xFF00) >> 8);
    NVMADRL = (uint8_t) (bAdd & 0x00FF);

    //Set the NVMCMD control bits for DFM Byte Read operation
    NVMCON1bits.NVMCMD = 0b000;
    NVMCON0bits.GO = 1;

    return NVMDATL;
}

Die Zelle sollte erst wenige Male beschrieben worden sein.

von Andras H. (kyrk)


Lesenswert?

Simulierst du oder debuggst du?

Im Simulator tun manche Sachen nicht. Ich glaube der Eeprom ist genau 
soetwas. Sonst nach dem schreiben den Inhalt mit debugger zurücklesen.

von Franko P. (sgssn)


Lesenswert?

Die Lesefunktionist ja nicht ganz korrekt:

while (NVMCON0bits.GO);

das fehlt.

von Chris (chris_dd)


Lesenswert?

Ist in realer Umgebung und ich vergleich gleich noch Datasheeet und 
generierte Funktion.

Danke schonmal für die Hilfe.

: Bearbeitet durch User
von Chris (chris_dd)


Lesenswert?

Hab es jetzt so geändert. Geht aber leider immer noch nicht:

uint8_t DATAEE_ReadByte(uint16_t bAdd)
{
    //Set NVMADR with the target word address (0x380000 - 0x3803FF)
    NVMADRU = 0x38;
    NVMADRH = (uint8_t) ((bAdd & 0xFF00) >> 8);
    NVMADRL = (uint8_t) (bAdd & 0x00FF);

    //Set the NVMCMD control bits for DFM Byte Read operation
    NVMCON1bits.NVMCMD = 0b000;
    NVMCON0bits.GO = 1;
    while (NVMCON0bits.GO);

    return NVMDATL;
}

von Franko P. (sgssn)


Lesenswert?

Die Funktion liefert ja irgendwas zurück. Und was liefert die 
Read-Funktion zurück? ff, 0? und lässt sich das wiederholen?

Was steht in der CONFIG für Code protection (CP) und EEPROM Write 
Protection (WRTD) drin?

: Bearbeitet durch User
von Chris (chris_dd)


Lesenswert?

Was im EEPROM steht, kann ich aktuell leider nicht testen.

In der Config steht CP aktiv und WRTD nicht aktiv.

von Franko P. (sgssn)


Lesenswert?

Dann schmeiss mal das CP raus. Codeprotection behindert auch das 
Schreiben vom EEPROM, wenn ich das im datenblatt richtig lese.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.