Jump to content
ELFORUM - Forumul electronistilor

PIC si SD card in MikroC


Recommended Posts

Folosesc un PIC18F252 cu care vreau sa scriu e un SD card.

 

Am crezut ca va fi simplu de scris in MikroC numai ca nu a fost asa.

 

Programul tipic in MikroC pentru card arata asa:

sbit Mmc_Chip_Select at LATB2_bit;sbit Mmc_Chip_Select_Direction at TRISB2_bit;char *text="SD CARD......";void main(){SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);error= Mmc_Init();essage);}

Si ma rog creez un fisire (New file) care nu  vrut de loc sa apara :rade:

 

BUN,

Am zis asa, el in primele doua linii stie doar PORTB2 care e chip select sau CS.

 

Pai si restul conexiunilor pe care le-am facut oarecum aletor cum le stie ?

 

De unde stie pe ce pin e Data In sau Clock ?

 

Sa ma uit .LST poate.......

 

Edited by Mondan
Link to comment
  • Replies 19
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

documentatia pic-ului spune ca protocolul SPI foloseste pinii RC3/RC4/RC5 pentru SCL/SDA/SDO

Ar mai fi o problema. Pe doi pini din cei amintiti am conetat un ceas DS1307.

 

  I2C_Wr(0xD0);               // I2C write address

 

Cum fac conexiunile ?

Hai sa zic la Clock e input, as putea sa il folosesc pentru ambele.

 

Dar cu datele ? Sa lega ambele fire pe aceeasi intrare ?

Nu prea marege. 

Link to comment

daca ai chip select la ambele, banuiesc ca le poti controla cate unul pe rand, activezi si dezactivezi SPI sau I2C (ai doar un device?), in functie de ce CS ai activ, in mod normal nu ar trebui sa deranjeze bus-ul comun, dar daca nu merge poti face oricare din protocoale soft pe ce pini doresti.

Edited by core
Link to comment

MikroC are "Software_I2C" merge exact la fell ca I2C dar ca poti seta orice pin doresti din PIC. Eu pe ala il utilizez pentru ca PIC16F690 desi are hardware I2C din nu stiu ce cauza MikroC nu are librarie hardware pentru el si am fost prea lenes sa studiez datasheet ul sa vad cum ar trebui utilizat I2C ul din registrii. 

Link to comment

Am atastat fieiserul DSN Proteus,

Codul arata asa:

 ////////////////////////////////MMC///////////////////////////////////////   // MMC module connectionssbit Mmc_Chip_Select           at LATB3_bit;  // for writing to output pin always use latch (PIC18 family)sbit Mmc_Chip_Select_Direction at TRISB3_bit;// eof MMC module connectionsconst LINE_LEN = 43;char err_txt[20]       = "FAT16 not found";char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckertn";char           filename[14] = "Record.TXT";          // File nameschar      mmc_fat_label[]="Label MMC";unsigned short loop, loop2;unsigned long  i, size;char           Buffer[512];     /////////////////////////////////////////////////////////////////////////sbit LCD_RS at LATC2_bit;//// R/W=PORTC,0      -> tinut in zerosbit LCD_EN at LATC0_bit;sbit LCD_D4 at LATC1_bit;sbit LCD_D5 at LATB0_bit;sbit LCD_D6 at LATC6_bit;sbit LCD_D7 at LATC7_bit;sbit LCD_RS_Direction at TRISC2_bit;sbit LCD_EN_Direction at TRISC0_bit;sbit LCD_D4_Direction at TRISC1_bit;sbit LCD_D5_Direction at TRISB0_bit;sbit LCD_D6_Direction at TRISC6_bit;sbit LCD_D7_Direction at TRISC7_bit;unsigned char sec, min1, hr, week_day, day, mn, year;char *txt, tnum[4];     char txtV[]="  . V";//char i;                              // Loop variableunsigned int temp_res;unsigned int temp_res1;double volt_res;//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor://  18S20: 9  (default setting; can be 9,10,11,or 12)//  18B20: 12const unsigned short TEMP_RESOLUTION = 12;char *text_semn = " ";char *text = "000.0";unsigned temp;bit flag;    const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;  char temp_whole;  unsigned int temp_fraction;  long timeres;  ////////////////////////////////////////////////////////////////////////  //  //                      Main  //  /////////////////////////////////////////////////////////////////////////void main(){  PORTA=0x00;  LATA=0x00;  ADCON1=0x02; //toate pe digital  TRISA=0XFF;  //PORTA=0XFF;  //LATA=0xFF;  TRISC=0;  TRISB=0xFF; /// pe PORTB.0 e senzorul de temperatira    ResetAdr:        timeres=0; ///////////////////////// LCD /////////////////////////////////////////// //////////////////////////// Senzorul de temperatura  //////////////////////////           Lcd_Init();                                    // Initialize LCD                Delay_ms(150);  Lcd_Cmd(_LCD_CLEAR);  Lcd_Cmd(_LCD_CURSOR_OFF);  Lcd_Out(1, 1, "  Card MMC   ");        Delay_ms(500);  ////////////////////////////  MMC //////////////////////////////////////////////         SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);          //if (!Mmc_Fat_QuickFormat(&mmc_fat_label)) {;};                 Lcd_Out(1, 1, "Initializare SPI");       Delay_ms(500);                Mmc_Fat_Rewrite();                        // To clear file and start with new data  Lcd_Out(1, 1, "Fisier creat ");       Delay_ms(500);              Mmc_Fat_QuickFormat(&mmc_fat_label);       Lcd_Out(1, 1, "Format OK");       Delay_ms(500);                       Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info  Lcd_Out(1, 1, "Set Data ");       Delay_ms(500);  Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one  Lcd_Out(1, 1, "Search existent ");       Delay_ms(500);   Mmc_Fat_Rewrite();                        // To clear file and start with new data  Lcd_Out(1, 1, "Fisier creat ");       Delay_ms(500);  for(loop = 1; loop <= 99; loop++) {    //UART1_Write('.');    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  }      Lcd_Out(1, 1, "Fisier creat ");       Delay_ms(500);    ///////////////////////////////////////////////////////////////////////////////        loop:        goto loop;}

Sunt multe definitii in plus in cod, ele au ramas de la altele, nu asta ma intereseaza.

 

Nu creaaza fisierul, nu scrie in el.

 

Am sa pun si o poza pentru cine vrea sa verifice fara sa desarce fisierul .DSN

post-183833-0-70824200-1431503379_thumb.jpg

Si cu scuze pentru care stiti, am sa lamuresc. HEX-ul trebui indicat in Proteus. Vi-l pot pune daca aveti nevoie de el, dar nu cred ca e cazul.

pentru card se foloseste un programul numit WinImage:

Posted Image

 

Se creaza o imagine de tipul *.ima si apoi se da rename cu *.mmc

Cred ca puteti pune si un fisier text gol cu nume *.mmc, dar n-am testat varianta asta.

 

Merge doar pana la initializare SPI. 

 

Daca veti sa sugesti cu totul alt cod postati coldul aici. Nu ma deranjeaza daca il modific radical.

 

Pare a nu fi comunicatie SPI. Cardul nu raspunde  pe DateOut al lui. Dar nu am gasit greseala.

 

Poate MikroC nu e chiar mare lucru,

Dar pentru probleme de tipul asta mi se pare foarte bun. Ai un montaj de facut, vrei sa sa il faci mai repede si gata.

 

 

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

A.. si inca ceva. Exemplul din MikroC compilat imi da erori.

PIC18F.DSN.zip

Edited by Mondan
Link to comment

Nu a mers nici dupa asta nici dupa ce am adaugat si UART1_init(19200).

 

====================================================================

Am luat-o de la 0 si am reprodus un montaj de pe alt forum. Link la forum pe PM.

Codul este, probail il gasiti si la serach:

Repet Nu este al meu, este reprodus de pe alt forum unde initiatorul afirma ca merge si totul e in regula.

// Define necessary definitionschar           filename[14] = "MIKRO00x.TXT";          // File namesunsigned char uart_rd;unsigned char data;unsigned char temp[10];unsigned char number =0;// MMC module connectionssbit 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// LCD 16x2 connections// Lcd pinout settingssbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D7 at RB3_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D4 at RB0_bit;// Pin directionsbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D7_Direction at TRISB3_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D4_Direction at TRISB0_bit;// End of LCD 16x2 connections// 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;  Mmc_Fat_Write("Hello World",12);} */// Main. Uncomment the function(s) to test the desired operation(s)void main() {  ADCON1 |= 0x0F;                  // Configure AN pins as digital  CMCON  |= 7;  TRISB = 0;                    // Turn off comparators  PortB = 0xFF;  UART1_Init(19200);  Delay_ms(100);  Lcd_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) {    Lcd_Cmd(_LCD_CLEAR);    Lcd_Out(1,1,"MMC init OK");    // reinitialize spi at higher speed    SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);   do {   if (UART1_Data_Ready()){              //<------ Check if a character has been received before reading       uart_rd = UART1_Read();     // read the received data,       UART1_Write(uart_rd);       temp[number] = uart_rd;       number = number +1;       if (number == 9) // temp storing  string       {          Lcd_Cmd(_LCD_CLEAR);          Lcd_cmd(_LCD_RETURN_HOME);          Lcd_out(1,1,temp);          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;          Mmc_Fat_Write(temp,9);          Lcd_Cmd(_LCD_CLEAR);          Lcd_Out(1,1,"Test End");          number =0;        }       }    } while(1);  }  else {    Lcd_Cmd(_LCD_CLEAR);    Lcd_Out(1,1,"MMC init FAILED"); // 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)  }}

Fisierul .DSM are unele corecturi. La divizorul rezistiv spre MMC.

 

Deci, am reprodus identic de pe un alt forum si nu merge. Da eroare la initializare card.

 

PIC18F.DSN.zip

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines