link PIC24 with SDcard

49 Views Asked by At

I'm trying to save some data on the SD card that I will write on the PIC24fg64GA002 and I'm having some problems in the coding due to me being new to programming using mplab and programming pic.

Another problem I'm thinking about is that: doesn't the sd card in the spi link have for pins to connect and it is sending 8 bits and the pic works with 16 bits how can I know in the code that it is working.

As I looked on the internet I found that I can work without libraries.

I didn't understand the use of libraries or how to work without them and that the code that I used.

/*
 * File:   main.c
 * 
 *
 * Description:
 *  Example for Run Time Self Programming (RTSP).
 *  This is very limited, useful as a test bench but not much more.
 *
 * Created on December 14, 2022, 9:07 PM
 */

/* Define the system oscillator frequency this code must configure */
#define FSYS (8000000ul)
#define FCY  (FSYS/2ul)

// PIC24FJ128GL305 Configuration Bit Settings

// 'C' source line config statements

// FSEC
#pragma config BWRP = OFF               // Boot Segment Write-Protect bit (Boot Segment may be written)
#pragma config BSS = DISABLED           // Boot Segment Code-Protect Level bits (No Protection (other than BWRP))
#pragma config BSEN = OFF               // Boot Segment Control bit (No Boot Segment)
#pragma config GWRP = OFF               // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = DISABLED           // General Segment Code-Protect Level bits (No Protection (other than GWRP))
#pragma config CWRP = OFF               // Configuration Segment Write-Protect bit (Configuration Segment may be written)
#pragma config CSS = DISABLED           // Configuration Segment Code-Protect Level bits (No Protection (other than CWRP))
#pragma config AIVTDIS = OFF            // Alternate Interrupt Vector Table bit (Disabled AIVT)

// FBSLIM
#pragma config BSLIM = 0x1FFF           // Boot Segment Flash Page Address Limit bits (Enter Hexadecimal value)

// FOSCSEL
#pragma config FNOSC = FRC              // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config PLLMODE = DISABLED       // PLL Mode Selection (No PLL used; PLLEN bit is not available)
#pragma config IESO = ON                // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)

// FOSC/*************************/
#pragma config POSCMOD = NONE           // Primary Oscillator Select (Primary oscillator disabled)
#pragma config I2C1SEL = PRI            // I2C1 Pin Location Select (Use default SCL1/SDA1 pins)
#pragma config IOL1WAY = ON             // IOLOCK Protection (Once IOLOCK is set, cannot be changed)
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = FRCDIV           // Oscillator Select (Fast RC Oscillator with Postscaler (FRCDIV))
#pragma config SOSCSEL = SOSC           // Sec Oscillator Select (Default Secondary Oscillator (SOSC))
#pragma config WUTSEL = LEG             // Wake-up timer Select (Legacy Wake-up Timer)
#pragma config IESO = ON                // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) enabled)

// CONFIG1
#pragma config WDTPS = PS32768          // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128            // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = ON              // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = ON              // Watchdog Timer Enable (Watchdog Timer is enabled)
#pragma config ICS = PGx1               // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1)
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF                // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = ON              // JTAG Port Enable (JTAG port is enabled)


// FPOR/***********************/
#pragma config BOREN = OFF              // Brown Out Enable bit (Brown Out Disabled)
#pragma config LPCFG = OFF              // Low power regulator control (No Retention Sleep)
#pragma config DNVPEN = ENABLE          // Downside Voltage Protection Enable bit (Downside protection enabled using ZPBOR when BOR is inactive)

// FICD
#pragma config ICS = PGD1               // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF             // JTAG Enable bit (JTAG is disabled)

// FDMTIVTL
#pragma config DMTIVTL = 0xFFFF         // Deadman Timer Interval Low Word (Enter Hexadecimal value)

// FDMTIVTH
#pragma config DMTIVTH = 0xFFFF         // Deadman Timer Interval High Word (Enter Hexadecimal value)

// FDMTCNTL
#pragma config DMTCNTL = 0xFFFF         // Deadman Timer Instruction Count Low Word (Enter Hexadecimal value)

// FDMTCNTH
#pragma config DMTCNTH = 0xFFFF         // Deadman Timer Instruction Count High Word (Enter Hexadecimal value)

// FMDT
#pragma config DMTDIS = OFF             // Deadman Timer Enable Bit (Dead Man Timer is Disabled and can be enabled by software)

// FDEVOPT1
#pragma config ALTCMPI = DISABLE        // Alternate Comparator Input Enable bit (C2INC and C3INC are on their standard pin locations )
#pragma config TMPRPIN = OFF            // Tamper Pin Enable bit (TMPRN pin function is disabled)
#pragma config SOSCHP = ON              // SOSC High Power Enable bit (valid only when SOSCSEL = 1 (Enable SOSC high power mode (default))
#pragma config ALTI2C1 = ALTI2C1_OFF    // Alternate I2C pin Location (I2C1 Pin mapped to SDA1/SCL1 pins)
#pragma config SMB3EN = SMBUS3          // SM Bus Enable (SMBus 3.0 input levels)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.




/*// CONFIG2
#pragma config POSCMOD = NONE           // Primary Oscillator Select (Primary oscillator disabled)
#pragma config I2C1SEL = PRI            // I2C1 Pin Location Select (Use default SCL1/SDA1 pins)
#pragma config IOL1WAY = ON             // IOLOCK Protection (Once IOLOCK is set, cannot be changed)
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = FRCDIV           // Oscillator Select (Fast RC Oscillator with Postscaler (FRCDIV))
#pragma config SOSCSEL = SOSC           // Sec Oscillator Select (Default Secondary Oscillator (SOSC))
#pragma config WUTSEL = LEG             // Wake-up timer Select (Legacy Wake-up Timer)
#pragma config IESO = ON                // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) enabled)

// CONFIG1
#pragma config WDTPS = PS32768          // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128            // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = ON              // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = ON              // Watchdog Timer Enable (Watchdog Timer is enabled)
#pragma config ICS = PGx1               // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1)
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF                // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = ON              // JTAG Port Enable (JTAG port is enabled)

*/
#include "xc.h"

#include "mcc_generated_files/mcc.h"

#include "ff.h"

FATFS fatFs; /* FatFs work area needed for each volume */

FIL file; /* File object needed for each open file */


short reverseBitsShort(short);


int main (void)
 { 
   // Write your code here
    AD1PCFG = 0xFFFF; //Pin for all channels configured in Digital mode
   TRISB = 0;
//    TRISC = 0xFF00;
    TRISA = 0xFF00;
   PORTBbits.RB3 = 1;
    //TRISB = 0;
/*    To set or clear IOLOCK, a specific command sequence must be executed:
    1. Write 46h to OSCCON<7:0>.
    2. Write 57h to OSCCON<7:0>.
    3. Clear (or set) IOLOCK as a single operation.*/
    //*************************************
// Unlock Registers
//*************************************

asm volatile ( "MOV #OSCCON, w1 \n"
               "MOV #0x46, w2 \n"
               "MOV #0x57, w3 \n"
               "MOV.b w2, [w1] \n"
               "MOV.b w3, [w1] \n"
               "BCLR OSCCON,#6");
//__builtin_write_OSCCONL(0x46);
//__builtin_write_OSCCONL(0x57);

RPINR20 = 0x0200;// SDI ----> RP0; SCK1OUT----> RP2

RPOR0 = 0x0700;   // SDO1---> RP1
RPOR1 = 0x0008;   // SCK1OUT----> RP2(SS1OUT ----> RP3) SS1OUT ---> simple GPIO
PORTBbits.RB3 = 1;
/*
 To set up the SPI module for the Standard Master mode of operation:
1.If using interrupts:
    a) Clear the SPIxIF bit in the respective IFSxregister.
    b) Set the SPIxIE bit in the respective IECx register.
    c) Write the SPIxIP bits in the respective IPCxregister to set the interrupt priority.
2.Write the desired settings to the SPIxCON1 and SPIxCON2 registers with MSTEN(SPIxCON1<5>) = 1.
3.Clear the SPIROV bit (SPIxSTAT<6>).
4.Enable SPI operation by setting the SPIEN bit (SPIxSTAT<15>).
5.Write the data to be transmitted to the SPIxBUF register. Transmission (and reception) will start
as soon as data is written to the SPIxBUFregister.

 */
IFS0bits.SPI1IF = 0;
IEC0bits.SPI1IE = 0;
IPC2bits.SPI1IP0 = 1; // SPI1 priority 7 (highest)
IPC2bits.SPI1IP1 = 1;
IPC2bits.SPI1IP2 = 1;
SPI1CON1bits.MSTEN =1;
SPI1CON1 = 0x06FF;
SPI1CON1bits.SSEN = 0;
SPI2CON2 = 0;
 SPI1STATbits.SPIROV = 0;
 SPI1STATbits.SPIEN = 1;
 SPI1STATbits.SPITBF = 0;
 // calibrating +-12V
 asm volatile ( "MOV #SPI1BUF, w1 \n"
                 "MOV _range, w2 \n"
                 "BCLR PORTB,#3 \n"
                 "MOV w2, [w1]");
           //PORTBbits.RB3 = 0;
           //SPI1BUF = range; 
 while(SPI1STATbits.SPITBF){};
 PORTBbits.RB3 = 1;
 ad = SPI1BUF;

 IEC0bits.SPI1IE = 1;
 PORTBbits.RB8 =1;
  // start 1st conversion
   asm volatile ( "MOV #SPI1BUF, w1 \n"
                 "MOV _CH0, w2 \n"  //CH1
                 "BCLR PORTB,#3 \n"
                 "MOV w2, [w1]");
        //PORTBbits.RB3 = 0; cs¨¨¨¨¨|____
        //SPI1BUF = CH1;
 
   while (1);{}
   return 0;
 }   



void __attribute__((__interrupt__, __no_auto_psv__)) _SPI1Interrupt(void)
{
 /*asm volatile ("BSET PORTB,#3");
 
 IFS0bits.SPI1IF = 0;  
 ad = SPI1BUF;
 PORTAbits.RA0 = ad>>13;
 ad=ad<<3;  
 ad = ad & 0xFFF8;
 PORTB = (PORTB & 0x000F) | ad;
 
 while(SPI1STATbits.SPITBF){};*/
 printf("\r\n============= Start Interfacing with SD Card =============\r\n");
            FRESULT stat = f_mount(&fatFs, "", 1);
            if (stat == FR_OK) {    /* Mount SD */             
            printf("SD Card Mount Successful\r\n");
            SD_LED_SetHigh();
             if (f_open(&file, "Sd_test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE) == FR_OK) {    /* Open or create a file */            
            printf("Create a file successful\r\n");            
                         if ((file.fsize != 0) && (f_lseek(&file, file.fsize) != FR_OK)) goto endSD;    /* Jump to the end of the file */
                         f_write(&file, "Hello Guys!\r\n", 13, &bw);
            f_write(&file, "This is text message written to SD card. For more information, please visit www.circuitdigest.com\r\n", 99, &bw);
                         endSD: f_close(&file);                                                                                    /* Close the file */
            SD_LED_SetLow();
             }else{
            printf("Create a file unsuccessful\r\n");
            SD_LED_SetLow();
            }
    }else{
            printf("SD Card Mount UnSuccessful\r\n ------ FRESULT %d ------\r\n");
            }
            printf("\r\n============= Finish Interfacing with SD Card ============= \r\n\r\n\r\n\r\n");
 
 asm volatile ( "MOV #SPI1BUF, w1 \n"
                 "MOV _CH0, w2 \n" //CH1
                 "BCLR PORTB,#3 \n"
                 "MOV w2, [w1]");
 }
 

enter image description here

0

There are 0 best solutions below