software spi ile SD kart kullanımı mümkün mü?

Başlatan Tatala, 21 Ekim 2015, 10:00:54

Tatala

Merhabalar, pic18f4620 ile sd kart kullanmak istiryorum, MikroC ile yapacağım yazılımı. Bulduğum bütün kaynaklar SPI1 protokolü ile bu işlemi yapmış. ben spi1'i başka bir işleme ayırmıştım. bu nedenle sd kart uygulamasını software_spi ile yapmam gerekiyor. bu mümkün mü? mümkünse nasıl yapabilirim? başlangıç için örnek kod ihtiyacım var yardımcı olabilecek olan varsa sevinirim..

Tatala

tekrardan merhabalar, bütün örneklerde olan
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
kodunu yazılımsal spi'e nasıl aktaracam?? bu aktarılırsa olay çözülür mü?

M_B

Alıntı yapılan: Tatala - 21 Ekim 2015, 18:00:36
tekrardan merhabalar, bütün örneklerde olan
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
kodunu yazılımsal spi'e nasıl aktaracam?? bu aktarılırsa olay çözülür mü?
Yazilimsal SPI da pinleri siz belirleyeceksiniz.
İlgili link yardımcı olacaktır.
http://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/software_spi_library.htm
İmkanın sınırlarını görmek için imkansızı denemek lazım.                                                             Fatih Sultan Mehmet

Tatala

/*
 * Project name:
     Mmc_Fat16_Test(Demonstration of usage of Mmc_Fat16 library)
 * Copyright:
     (c) MikroElektronika, 2012
 * Revision History:
     20081218:
       - initial release;
     20111012;
       - revision (JK);
 * Description:
     This project consists of several blocks that demonstrate various aspects of
     usage of the Mmc_Fat16 library. These are:
     - Creation of new file and writing down to it;
     - Opening existing file and re-writing it (writing from start-of-file);
     - Opening existing file and appending data to it (writing from end-of-file);
     - Opening a file and reading data from it (sending it to USART terminal);
     - Creating and modifying several files at once;
     - Reading file contents;
     - Deleting file(s);
     - Creating the swap file (see Help for details);
 * Test configuration:
     MCU:             PIC18F45K22  //değiştirdim kodları PIC18F4620 de derleniyor.
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
     Dev.Board:       EasyPIC7
                      http://www.mikroe.com/easypic/
     Oscillator:      HS-PLL, 32.00000 MHz
     Ext. Modules:    ac:MMC-SD_Board on PORTC
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     - Make sure that MMC card is properly formatted (to FAT16 or just FAT)
       before testing it on this example.
     - Connect RS232 cable and turn on RS232 switches SW1.1 and SW2.1 (board specific).
     - This example expects MMC card to be inserted before reset, otherwise,
       the FAT_ERROR message is displayed.
 */

// MMC module connections

/*sbit Mmc_Chip_Select           at LATC0_bit;  // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC0_bit;*/



sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;

sbit SoftSpi_SDI at LATD0_bit;
sbit SoftSpi_SDO at RD1_bit;
sbit SoftSpi_CLK at LATC3_bit;

sbit SoftSpi_SDI_Direction at TRISD0_bit;
sbit SoftSpi_SDO_Direction at TRISD1_bit;
sbit SoftSpi_CLK_Direction at TRISC3_bit;


 sbit Mmc_Chip_Select           at LATC2_bit;  // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISC2_bit;


// eof MMC module connections

const LINE_LEN = 43;
char err_txt[20]       = "FAT16 not found";
char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckert\n";
char           filename[14] = "MIKRO00x.TXT";          // File names
unsigned short loop, loop2;
unsigned long  i, size;

// UART1 write text and new line (carriage return + line feed)
   ///////////////*****************************///////////////////////

/*void UART1_Write_Line(char *uart_text) {
  UART1_Write_Text(uart_text);
  UART1_Write(13);
  UART1_Write(10);
}*/

    void Lcd_Write_Line(char *lcd_text)
    {
    Lcd_Out(1,1,lcd_text);
             delay_ms(50);
    }
// Creates new file and writes some data to it
void M_Create_New_File() {
  filename[7] = 'A';
  Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
  Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one
  Mmc_Fat_Rewrite();                        // To clear file and start with new data
  for(loop = 1; loop <= 99; loop++) {
    Lcd_Out(1,1,'.');
    delay_ms(10);
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1);   // write data to the assigned file
  }
}

// Creates many new files and writes data to them
void M_Create_Multiple_Files() {
  for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
    Lcd_Out(2,1,loop2); 
    delay_ms(10);                 // signal the progress
    filename[7] = loop2;                 // set filename
    Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
    Mmc_Fat_Assign(&filename, 0xA0);     // find existing file or create a new one
    Mmc_Fat_Rewrite();                   // To clear file and start with new data
    for(loop = 1; loop <= 44; loop++) {
      file_contents[0] = loop / 10 + 48;
      file_contents[1] = loop % 10 + 48;
      Mmc_Fat_Write(file_contents, LINE_LEN-1);  // write data to the assigned file
    }
  }
}

// Opens an existing file and rewrites it
void M_Open_File_Rewrite() {
  filename[7] = 'C';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Rewrite();
  for(loop = 1; loop <= 55; loop++) {
    file_contents[0] = loop / 10 + 48;
    file_contents[1] = loop % 10 + 48;
    Mmc_Fat_Write(file_contents, LINE_LEN-1);    // write data to the assigned file
  }
}

// Opens an existing file and appends data to it
//               (and alters the date/time stamp)
void M_Open_File_Append() {
   filename[7] = 'B';
   Mmc_Fat_Assign(&filename, 0);
   Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 20, 0);
   Mmc_Fat_Append();                                    // Prepare file for append
   Mmc_Fat_Write(" for mikroElektronika 2010\n", 27);   // Write data to assigned file
}

// Opens an existing file, reads data from it and puts it to UART
void M_Open_File_Read() {
  char character;

  filename[7] = 'B';
  Mmc_Fat_Assign(&filename, 0);
  Mmc_Fat_Reset(&size);            // To read file, procedure returns size of file
  for (i = 1; i <= size; i++) {
    Mmc_Fat_Read(&character);
   // UART1_Write(character);        // Write data to UART
   Lcd_Out(3,1,character);
   delay_ms(10);
  }
}

// Deletes a file. If file doesn't exist, it will first be created
// and then deleted.
void M_Delete_File() {
  filename[7] = 'F';
  Mmc_Fat_Assign(filename, 0);
  Mmc_Fat_Delete();
}

// Tests whether file exists, and if so sends its creation date
// and file size via UART
void M_Test_File_Exist() {
  unsigned long  fsize;
  unsigned int   year;
  unsigned short month, day, hour, minute;
  unsigned char  outstr[12];

  filename[7] = 'B';       //uncomment this line to search for file that DOES exists
//  filename[7] = 'F';       //uncomment this line to search for file that DOES NOT exist
  if (Mmc_Fat_Assign(filename, 0)) {
    //--- file has been found - get its create date
    Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
    Lcd_Out(1,1," created: ");
    WordToStr(year, outstr);
    Lcd_Out(2,1,outstr);
    ByteToStr(month, outstr);
    Lcd_Out(2,5,outstr);
    WordToStr(day, outstr);
    Lcd_Out(2,9,outstr);
    WordToStr(hour, outstr);
    Lcd_Out(2,12,outstr);
    WordToStr(minute, outstr);
    Lcd_Out(2,15,outstr);

    //--- file has been found - get its modified date
    Mmc_Fat_Get_File_Date_Modified(&year, &month, &day, &hour, &minute);
    Lcd_Out(3,1," modified: ");
    WordToStr(year, outstr);
    Lcd_Out(4,15,outstr);
    ByteToStr(month, outstr);
    Lcd_Out(4,15,outstr);
    WordToStr(day, outstr);
    Lcd_Out(4,15,outstr);
    WordToStr(hour, outstr);
    Lcd_Out(4,15,outstr);
    WordToStr(minute, outstr);
    Lcd_Out(4,15,outstr);

    //--- get file size
    fsize = Mmc_Fat_Get_File_Size();
    LongToStr((signed long)fsize, outstr);
    Lcd_Write_Line(outstr);
  }
  else {
    //--- file was not found - signal it
    Lcd_Out(2,10,0x55);
    Delay_ms(1000);
    Lcd_Out(3,10,0x55);
  }
}


// Tries to create a swap file, whose size will be at least 100
// sectors (see Help for details)
void M_Create_Swap_File() {
  unsigned int i;

  size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20);   // see help on this function for details

  for(i=0; i<512; i++)
    f16_sector.fSect[i] = i;

  if (size) {
    LongToStr((signed long)size, err_txt);
    Lcd_Write_Line(err_txt);

    for(i=0; i<5000; i++) {
      Mmc_Write_Sector(size++, f16_sector.fSect);
      Lcd_Out(1,1,'.');
    }
  }
}

// Main. Uncomment the function(s) to test the desired operation(s)
void main() {
  //#define COMPLETE_EXAMPLE         // comment this line to make simpler/smaller example
    trisa=1;            // A portunu giriş olarak ayarladık
     trisb=0;            // B portunu çıkış olarak ayarladık
     portb=0;            // B portunu temizledik
     trisc=0;
     portc=0;
     trisd=0b11111110;

     ADCON0=0b00000000;        // PIC ın analog giriş ayarları yapıldı
     ADCON1=0b00001110;        // Analog giriş kanalları seçildi
     ADCON2=0b00000000;
  // Initialize UART1 module
 /*UART1_Init(19200);
  Delay_ms(10);*/
  
     LCD_Init();                  // LCD kütüphanesini tanımladık
       LCD_CMD(_LCD_CURSOR_OFF); // CURSOR'u kapattık
       LCD_CMD(_LCD_CLEAR);      // LCD yi temizledik
       delay_ms(10);

  Lcd_Write_Line("PIC-Started"); // PIC present report
       Soft_Spi_Init();
  // Initialize SPI1 module
  //SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

  // use fat16 quick format instead of init routine if a formatting is needed
  if (Mmc_Fat_Init() == 0) {
    // reinitialize spi at higher speed
    
  //  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
   

   
    //--- Test start
    Lcd_Write_Line("Test Start.");
    //--- Test routines. Uncomment them one-by-one to test certain features
    M_Create_New_File();
    #ifdef COMPLETE_EXAMPLE
      M_Create_Multiple_Files();
      M_Open_File_Rewrite();
      M_Open_File_Append();
      M_Open_File_Read();
      M_Delete_File();
      M_Test_File_Exist();
      M_Create_Swap_File();
    #endif
    Lcd_Write_Line("Test End.");

  }
  else {
    Lcd_Write_Line(err_txt); // Note: Mmc_Fat_Init tries to initialize a card more than once.
                               //       If card is not present, initialization may last longer (depending on clock speed)
  }
}


pinleri tabiki belirledim. bu kodlar mikrochipin örnekleri içinde vardı. uart ile görüyordu ne olduğunu uartları lcd'ye çevirdim. saçmalamış olabilirim:) ama saçma da olsa bağlantı yapılmasına aykırı değil lcd outputları. ben ekranda sadece "not found" yazısını görebiliyorum. burada da spi1 ile yapılmış haberleşme. onu soft_spi'a nasıl döndürcem?  :-\ :-\

RaMu

Kodlar mikroelektronikanın, microchipin değil.

Denediğin örnek hakkında kısa açıklama ve isisde çalışır hali:
https://www.picproje.org/index.php?topic=54274.0

Orada yanlış söylemişim, spi init kısmı pic in  spi modülünü kuruyor.

Yazılımsal spi için:

sd kart için tanımlanmış pinler
soft_spi şeklinde değiştirilecek
sbit Chip_Select at RC0_bit;
sbit SoftSpi_CLK at RC3_bit;
sbit SoftSpi_SDI at RC4_bit;
sbit SoftSpi_SDO at RC5_bit;

sbit Chip_Select_Direction at TRISC0_bit;
sbit SoftSpi_CLK_Direction at TRISC3_bit;
sbit SoftSpi_SDI_Direction at TRISC4_bit;
sbit SoftSpi_SDO_Direction at TRISC5_bit;



spi_init, spi_write ve spi_read
bulunan her satır
Soft_SPI_Init
Soft_SPI_Read
Soft_SPI_Write
ile değiştirilecek.

İngilizce durumu nedir?
M_B nin paylaştığı linki anlayabiliyor musun?
Sorularınıza hızlı cevap alın: http://www.picproje.org/index.php/topic,57135.0.html

Tatala

Cevap için çok teşekkür ederim, söylediğiniz değişikleri yukarıda verdiğim kodda yapmıştım. paylaştığım kod söylediğiniz gibi mikroelektronika'nın içindeydi. orada da normal spi ile yapılmış ve pinler işlemcinin spi bacaklarına bağlanmış. sorf_spi ve spi kütüphanelerinin help kısımlarını okudum anladım. sd kart ile yapılan bütün örneklerde pinler pic'in spi bacaklarına bağlanmış. yukarıda paylaştığım kodda ben uart ile pc'de değilde lcd ile devre üzerinde görmek istedim ne olup bittiğini. örnek uart'a göre diye uartlı kısımları /*......*/ içine aldığim için karışık gözükebilir. spi_init olması gereken yerde soft_spi_init yazıyor, SPI1_Init_Advanced yazan yerler "//" içinde, soft_spi_write yazmadım bunun yerine orjinal kodda kullanılan Mmc_Fat_write komutu var. bunu şuan farkettim :) bunları değiştirip deneyeyim. :)