Forum: Mikrocontroller und Digitale Elektronik SD-Karte an PIC18F4620


von Sebastian W. (bin)


Angehängte Dateien:

Lesenswert?

Hallo!
Ich programmiere zur Zeit einen PIC18F4620 und versuche, dass er auf 
eine SD-Karte schreibt.Leider funktioniert die Initialisierung der Karte 
nicht.
Ich bekomm anstatt der 0x01 immer eine 0xBF!Schaltungstechnisch ist 
alles OK.Vielleicht hat jemand dasselbe Problem.
Ach ja ich nutze den Quellcode von Microchip.
Ich habe ihn ein wenig geändert.
Im Anhang findet ihr alle möglichen Datein, die im Projekt drin sind!

: Verschoben durch Moderator
von Lehrmann M. (ubimbo)


Lesenswert?

Sebastian W. schrieb:
> Ich programmiere zur Zeit einen PIC18F4620 und versuche, dass er auf
> eine SD-Karte schreibt.Leider funktioniert die Initialisierung der Karte
> nicht.

Welche Vcc, Takt, Config-Bits ?

Sebastian W. schrieb:
> Schaltungstechnisch ist
> alles OK.

Woher weisst du das denn? Funktioniert's mit einem anderen Code? Wie 
machst du denn den Level-Shift wenn du mit 5V arbeitest?

Schonmal eine andere SD-Karte probiert?

Sebastian W. schrieb:
> Im Anhang findet ihr alle möglichen Datein, die im Projekt drin sind!

Das soll wohl ein Witz sein?! Such gefälligst die wichtigen raus - ich 
hab doch keine Lust mich durch deinen Datenmüll zu wühlen... Also 
sorry... und keine zips. Wir brauchen die *.c und die *.h wenn nötig. 
Wir wollen dein Programm ja nicht kompilieren ...

von heinzhackman (Gast)


Lesenswert?

card-detect angeschlossen?

von Sebastian W. (bin)


Lesenswert?

Also Card Detect ist angeschlossen und es wird auch erkannt, dass eine 
Karte drin ist! Aber wenn ich CMD0 an die Karte sende kommt nichts 
ordentliches zurück.

VCC=2,9V
Bei der Initialisierung hab ich einen Takt von 136 KHz
Da SD-Karten bis 400KHz initialisiert werden können!

Die Schaltung hab ich von Microchip genommen! Und schon 1000mal 
kontrolliert!

von heinzhorst (Gast)


Lesenswert?

Schon mal mit dem Debugger geschaut, wie weit du kommst in der 
Initialisierung? Gehe jetzt mal davon aus, dass deine HardwareProfile.h 
auch in Ordnung ist. Probier mal das Beispielprojekt "MDD File System SD 
Card" aus den Microchip Application Libraries aus.

von Sebastian W. (bin)


Lesenswert?

Also mit dem Debugger komm ich bis in die FSinit(), aber dort dreht er 
er seine Runden auf immer und ewig.Er sagt mir immer 
MEDIA_CANNOT_INITIALIZE...

Ich muss aber nochmal blöd fragen! ich hab ein 8Mhz Quarz am PIC und die 
Config bits sind auf 4xFOSC eingestellt. Also hab ich doch einen 
Systemtakt von 32 MHz oder? Verzeiht mir die Frage!

von heinzhorst (Gast)


Lesenswert?

Nein, dann hast du 8MHz. Den internen Taktteiler nicht vergessen!

von Maik W. (werner01)


Lesenswert?

Also ich sage du hast 32 MHz. Aber jeder Befehl braucht mind. 4 Takte. 
Bei Timern genau das gleiche. Ja und dann kommt man auf 8 MHz 
rischdisch.

von Sebastian W. (Gast)


Lesenswert?

So ihr lieben Leute. Ich habe es jetzt soweit, dass ich Dateien auf 
meiner Sd-Karte erzeugen kann. Allerdings kann ich nicht Daten in diese 
Datein schreiben. Weil:

pointer = FSfopenpgm ("SD-Karte.TXT", "w");

Hier wird die Datei erzeugt.

  if (pointer == NULL)
    while(1);

diese Bedingung ist leider wahr deshalb bleibt er dort stehen.

FSfopenpgm:

FSFILE * FSfopenpgm(const rom char * fileName, const rom char *mode)
{
    char F[13];
    char M[2];
    BYTE count;

    for (count = 0; count < 13; count++)
    {
        F[count] = *(fileName + count);
    }
    for (count = 0; count < 2; count++)
    {
        M[count] = *(mode + count);
    }

    return FSfopen(F, M);
}

FSfopen:
FSFILE * FSfopen( const char * fileName, const char *mode )
{
    FILEOBJ    filePtr;

#ifndef FS_DYNAMIC_MEM
    int      fIndex;
#endif
    BYTE   ModeC;
    WORD    fHandle;
    CETYPE   final;

#ifdef FS_DYNAMIC_MEM
    filePtr = (FILEOBJ) FS_malloc(sizeof(FSFILE));
#else

    filePtr = NULL;

    //Pick available file structure
    for( fIndex = 0; fIndex < FS_MAX_FILES_OPEN; fIndex++ )
    {
        if( gFileSlotOpen[fIndex] )   //this slot is available
        {
            gFileSlotOpen[fIndex] = FALSE;
            filePtr = &gFileArray[fIndex];
            break;
        }
    }

    if( filePtr == NULL )
    {
        FSerrno = CE_TOO_MANY_FILES_OPEN;
        return NULL;      //no file structure slot available
    }
#endif

    //Format the source string.
    if( !FormatFileName(fileName, filePtr->name, 0) )
    {
#ifdef FS_DYNAMIC_MEM
        FS_free( (unsigned char *)filePtr );
#else
        gFileSlotOpen[fIndex] = TRUE;   //put this slot back to the pool
#endif
        FSerrno = CE_INVALID_FILENAME;
        return NULL;   //bad filename
    }

    //Read the mode character
    ModeC = mode[0];

    filePtr->dsk = &gDiskData;
    filePtr->cluster = 0;
    filePtr->ccls    = 0;
    filePtr->entry = 0;
    filePtr->attributes = ATTR_ARCHIVE;

    // start at the current directory
#ifdef ALLOW_DIRS
    filePtr->dirclus    = cwdptr->dirclus;
    filePtr->dirccls    = cwdptr->dirccls;
#else
    filePtr->dirclus = FatRootDirClusterValue;
    filePtr->dirccls = FatRootDirClusterValue;
#endif

    // copy file object over
    FileObjectCopy(&gFileTemp, filePtr);

    // See if the file is found
    if(FILEfind (filePtr, &gFileTemp, LOOK_FOR_MATCHING_ENTRY, 0) == 
CE_GOOD)
    {
        // File is Found
        switch(ModeC)
        {
#ifdef ALLOW_WRITES
            case 'w':
            case 'W':
            {
                // File exists, we want to create a new one, remove it 
first
                fHandle = filePtr->entry;
                final = FILEerase(filePtr, &fHandle, TRUE);

                if (final == CE_GOOD)
                {
                    // now create a new one
                    final = CreateFileEntry (filePtr, &fHandle, 0);

                    if (final == CE_GOOD)
                    {
                        final = FILEopen (filePtr, &fHandle, 'w');

                        if (filePtr->attributes & ATTR_DIRECTORY)
                        {
                            FSerrno = CE_INVALID_ARGUMENT;
                            final = 0xFF;
                        }

                        if (final == CE_GOOD)
                        {
                            final = FSfseek (filePtr, 0, SEEK_END);
                            if (mode[1] == '+')
                                filePtr->flags.read = 1;
                        }
                    }
                }
                break;
            }

            case 'A':
            case 'a':
            {
                if(filePtr->size != 0)
                {
                    fHandle = filePtr->entry;

                    final = FILEopen (filePtr, &fHandle, 'w');

                    if (filePtr->attributes & ATTR_DIRECTORY)
                    {
                        FSerrno = CE_INVALID_ARGUMENT;
                        final = 0xFF;
                    }

                    if (final == CE_GOOD)
                    {
                        final = FSfseek (filePtr, 0, SEEK_END);
                        if (final != CE_GOOD)
                            FSerrno = CE_SEEK_ERROR;
                        else
                            ReadFAT (&gDiskData, filePtr->ccls);
                        if (mode[1] == '+')
                            filePtr->flags.read = 1;
                    }
                }
                else
                {
                    fHandle = filePtr->entry;
                    final = FILEerase(filePtr, &fHandle, TRUE);

                    if (final == CE_GOOD)
                    {
                        // now create a new one
                        final = CreateFileEntry (filePtr, &fHandle, 0);

                        if (final == CE_GOOD)
                        {
                            final = FILEopen (filePtr, &fHandle, 'w');

                            if (filePtr->attributes & ATTR_DIRECTORY)
                            {
                                FSerrno = CE_INVALID_ARGUMENT;
                                final = 0xFF;
                            }

                            if (final == CE_GOOD)
                            {
                                final = FSfseek (filePtr, 0, SEEK_END);
                                if (final != CE_GOOD)
                                    FSerrno = CE_SEEK_ERROR;
                                if (mode[1] == '+')
                                    filePtr->flags.read = 1;
                            }
                        }
                    }
                }
                break;
            }
#endif
            case 'R':
            case 'r':
            {
                fHandle = filePtr->entry;

                final = FILEopen (filePtr, &fHandle, 'r');
#ifdef ALLOW_WRITES
                if ((mode[1] == '+') && !(filePtr->attributes & 
ATTR_DIRECTORY))
                    filePtr->flags.write = 1;
#endif
                break;
            }

            default:
                FSerrno = CE_INVALID_ARGUMENT;
                final = 0xFF;;  //indicate error condition
                break;
        }
    }
    else
    {
#ifdef ALLOW_WRITES
        // the file was not found, reset to the default asked
        FileObjectCopy(filePtr, &gFileTemp);

        // File is not Found
        if(ModeC == 'w' || ModeC == 'W' || ModeC == 'a' || ModeC == 'A')
        {
            // use the user requested name
            fHandle = 0;
            final = CreateFileEntry (filePtr, &fHandle, 0);

            if (final == CE_GOOD)
            {
                final = FILEopen (filePtr, &fHandle, 'w');
                if (filePtr->attributes & ATTR_DIRECTORY)
                {
                    FSerrno = CE_INVALID_ARGUMENT;
                    final = 0xFF;
                }

                if (final == CE_GOOD)
                {
                    final = FSfseek (filePtr, 0, SEEK_END);
                    if (final != CE_GOOD)
                        FSerrno = CE_SEEK_ERROR;
                    if (mode[1] == '+')
                        filePtr->flags.read = 1;
                }
            }
        }
        else
#endif
            final = CE_FILE_NOT_FOUND;
    }

    if (MDD_WriteProtectState())
    {
        filePtr->flags.write = 0;;
    }

#ifdef FS_DYNAMIC_MEM
    if( final != CE_GOOD )
    {
        FS_free( (unsigned char *)filePtr );
        filePtr = NULL;
    }
#else
    if( final != CE_GOOD )
    {
        gFileSlotOpen[fIndex] = TRUE;   //put this slot back to the pool
        filePtr = NULL;
    }
#endif
    else
    {
        FSerrno = CE_GOOD;
    }

    return filePtr;
}

Also, wenn ihr lust habt könntet ihr mir helfen?
Grüße

von Sebastian W. (Gast)


Lesenswert?

So, ich habs geschafft.Das Programm läuft perfekt.
MFG

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.