Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC 18F452, mmc, FAT system, mikroC

Status
Not open for further replies.

davenjose

Newbie level 4
Joined
Feb 12, 2008
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,322
pic mmc fat

hi there,i am currently on a project entitled Digital Altimeter using SD Card as storage.
i am writing a file (data) to mmc with PIC 18F452 using FAT system and mikroC as compiler.
the data that i am going to write on a mmc is from the reading of our pressure sensor that would be converted to an altitude by the PIC.

i would like to ask some advice on the code for this process (or the code).
i am using mikroC that provides library on mmc, but i am still a beginner and not very confident on my code.

is it possible that i could read or open the file on my mmc with a card reader to a PC after writing data to it?

please help, i really need to finish this project a short time from now.
thank you.
 

hedisurfer

Newbie level 3
Joined
Nov 10, 2009
Messages
3
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
France
Activity points
1,309
Did you succeed in your operation ? Because I want to do the same thing...
 
  • Like
Reactions: dtr

    dtr

    Points: 2
    Helpful Answer Positive Rating

doraemon

Super Moderator
Staff member
Joined
Jun 21, 2009
Messages
1,068
Helped
273
Reputation
554
Reaction score
248
Trophy points
1,343
Location
Japan
Activity points
10,597
Hello,

The subject is a little old, sorry.

Yes of course you can read the SD card on your PC, that's the purpose of the
file system.
I did this implementation today, and I reach an accuracy of 25 cm in altitude.
But I am improving the whole system with a new averaging method, which
might increase the accuracy while being more responsive.

Storing in SD card is not a problem. I store directly in text format so that
the file can be opened with excel or compatible software. The consequence
is that the graphic can be done with Excel's utilities. Well, to be honnest, I don't
use Excel, but "Numbers" on a Mac.

Dora.
 

adm1

Junior Member level 1
Joined
Jan 5, 2010
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,381
hi all, can u pls tell abt the procedure you used to store data from the pic microcontroller into the memory card using mikroc... i am a beginner and i dont knw anything about mmc..!!.. help will be greatly appreciated

hey, doraemon, cud u pls tell how u wrote data into card...and give a sample program if possible... thank you
 

sandy2ing

Newbie level 1
Joined
Mar 11, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
malaysia
Activity points
1,287
Hi all,

I am currently working on part of fyp that I have to use PIC to read file (txt file) which is stored in a SD Card. I use CCS compiler and PIC18f4620. I fail even in trying to open the file :cry:. Anyone can help me in coding?...i would greatly appreciate if you have a complete code in accessing the file stored in SD and read the data from the txt file.

The CCS driver files that i use for MMC are fat.c and mmcsd.c.
Thanks. Hope to get helps from you all. And hope to get replies as soon as possible.As it is urgent. Thanks much.

regards,
sandy
 

pepillo

Advanced Member level 4
Joined
Jan 23, 2006
Messages
114
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Location
Spain
Activity points
2,382
Hi Davenjose,
I am trying to implement a FAT filesystem on PIC18 as well, but I´m struggling with it.
Do you have code examples for PIC18 and FAT? How do you compile everything together? What library are u using?
Thanks and best regards
 

pepillo

Advanced Member level 4
Joined
Jan 23, 2006
Messages
114
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Location
Spain
Activity points
2,382
The Microchip library looks good, is it for free?
What compiler is it for? Have you worked with it before?

Added after 1 hours 16 minutes:

I have downloaded the files and it looks quite interesting, however I don´t know where to start, how to integrate it with my application and my low level routines, make it work on my board, etc.

Has anybody worked with this library before and integrated it into a target application on PIC18?
 

ansarmytheen

Full Member level 3
Joined
Jan 9, 2010
Messages
155
Helped
55
Reputation
110
Reaction score
53
Trophy points
1,308
Location
kerala
Activity points
2,100
hello
It is very simple one.
I put the code here, the code is working one it create a text file name mikroc.txt
Code:
char
 faterror[20] = "FAT16 not found",
 filecontents[50] = "XX MMC/SD FAT16 library by Anton Rieckert\n";

//char filename[14] = "MIKRO00.TXT";          // File names
unsigned short tmp, charecter, loop;
unsigned long i, size;
void main()
{

     PORTB = 0;
     TRISB = 0;
      trisd=0x00; ;
      portd=0x00;
     Usart_Init(9600);
     Delay_100ms();
   //   portd=0xff;
      Delay_ms(3000);
     Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
  //  Spi_Init();
   Mmc_Init(&portC,2);
     // use fat16 quick format instead of init routine if a formatting is needed
     while ((Mmc_Fat_Init(&PORTC, 2)!=0))
      {
         tmp=0;
         while(faterror[tmp])
         Usart_Write(faterror[tmp++]);
         portd=0x0f;
        // Mmc_Fat_Assign("mikroc.txt",0x80);
         Delay_ms(1000);
         portd=0x00;
      }
      for(loop=1;loop<=5;loop++)
      {
           Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
          filename[7]=loop+64;
           portd=0xff;
           Delay_ms(1000);

      //    Mmc_Fat_QuickFormat(&portc,2,"MIKRO00.TXT");
            Mmc_Fat_Assign("mikroc.txt",0x80);

          Mmc_Fat_Rewrite();
          filecontents[0]=loop/10 +48;
          filecontents[1]=loop%10 +48;
          Mmc_Fat_Write(filecontents,41);
          Mmc_Fat_Append();
          Mmc_Fat_Write(filecontents,41);
           portd=0x00;
          Delay_ms(200);

       }

        for(loop=1;loop<=5;loop++)
      {
          filename[7]=loop+64;
          portd=0xff;
          Delay_ms(1000);
          Mmc_Fat_Assign("mikroc.txt",0xA0);
          portd=0x00;
          Mmc_Fat_Append();
          Mmc_Fat_Set_File_Date(2010,5,8,10,loop,0);
          Mmc_Fat_Write("mikroc electronica 2010\r\n",30);
          Mmc_Fat_Append();
          Mmc_Fat_Write(filecontents,41);
          Mmc_Fat_Reset(&size);
          for(i=0;i<=size;i++)
          {
            Mmc_Fat_Read(&charecter);
            Usart_Write(charecter);
            }
          Delay_ms(200);
       }

  }

If you have any doubt you can ask me.
 

recshenoy

Newbie level 2
Joined
Nov 20, 2009
Messages
2
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Bangalore
Activity points
1,288
Thanks ansarmytheen.

But will this code works for FAT16 only rite! what are the modifications needed if i need to run on a FAT32 MMC caed?
 

engmunir

Member level 1
Joined
Sep 24, 2007
Messages
38
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Activity points
1,534
Dear ansarmytheen
can u send me schematic diagram and complete code with included file.
 

isururivi

Newbie level 1
Joined
Jun 2, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
4
I used this code ,It does not successful in the following line
Mmc_Init(&portC,2);


here what is the usege of USART protocol ? can I use this code without using USART or UART ?
 

ansarmytheen

Full Member level 3
Joined
Jan 9, 2010
Messages
155
Helped
55
Reputation
110
Reaction score
53
Trophy points
1,308
Location
kerala
Activity points
2,100
Pleas check MIkroC help ,It contains a circuit diagram which is easy one.

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
char
 faterror[20] = "FAT16 not found",
 filecontents[50] = "XX MMC/SD FAT16 library by Anton Rieckert\n";
 
char filename[14] = "MIKRO00.TXT";          // File names
unsigned short tmp, charecter, loop;
unsigned long i, size;
void main()
{
 
     PORTB = 0;
     TRISB = 0;
      trisd=0x00; ;
      portd=0x00;
     Usart_Init(9600);
     Delay_100ms();
   //   portd=0xff;
      Delay_ms(3000);
     Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
  //  Spi_Init();
   Mmc_Init(&portC,2);
     // use fat16 quick format instead of init routine if a formatting is needed
     while ((Mmc_Fat_Init(&PORTC, 2)!=0))
      {
         tmp=0;
         while(faterror[tmp])
         Usart_Write(faterror[tmp++]);
         portd=0x0f;
        // Mmc_Fat_Assign("mikroc.txt",0x80);
         Delay_ms(1000);
         portd=0x00;
      }
      for(loop=1;loop<=5;loop++)
      {
           Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
          filename[7]=loop+64;
           portd=0xff;
           Delay_ms(1000);
 
      //    Mmc_Fat_QuickFormat(&portc,2,"MIKRO00.TXT");
            Mmc_Fat_Assign("mikroc.txt",0x80);
 
          Mmc_Fat_Rewrite();
          filecontents[0]=loop/10 +48;
          filecontents[1]=loop%10 +48;
          Mmc_Fat_Write(filecontents,41);
          Mmc_Fat_Append();
          Mmc_Fat_Write(filecontents,41);
           portd=0x00;
          Delay_ms(200);
 
       }
 
        for(loop=1;loop<=5;loop++)
      {
          filename[7]=loop+64;
          portd=0xff;
          Delay_ms(1000);
          Mmc_Fat_Assign("mikroc.txt",0xA0);
          portd=0x00;
          Mmc_Fat_Append();
          Mmc_Fat_Set_File_Date(2010,5,8,10,loop,0);
          Mmc_Fat_Write("mikroc electronica 2010\r\n",30);
          Mmc_Fat_Append();
          Mmc_Fat_Write(filecontents,41);
          Mmc_Fat_Reset(&size);
          for(i=0;i<=size;i++)
          {
            Mmc_Fat_Read(&charecter);
            Usart_Write(charecter);
            }
          Delay_ms(200);
       }
 
  }



- - - Updated - - -


Code C - [expand]
1
unsigned char Mmc_Init(unsigned short *port, unsigned short pin);

 
Last edited by a moderator:

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,428
Helped
290
Reputation
580
Reaction score
275
Trophy points
1,363
Location
France 01800
Activity points
10,020
hello,

I tried successfully "petite FAT 32 " on a SDcard 2Go
preformatted in FAT 32 with a PC computer...
my main problem was no card detection on my SDcard Breadboard ,
so i goes over this test (no Card presence test possible..because no hardware for that)
and i can write or read file on SDcard.

Written in C18 MPLAB.. for Pic18F46K22

all details HERE

i will try soon to translate in MikroC.. because now i have a licence and can go over the 2K MikroC limit..
 

hemnath

Advanced Member level 2
Joined
Jun 24, 2012
Messages
691
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Activity points
6,514
This is the basic code to save the temperature on SD card. Written in MikroC. And simulated in Proteus.

HTML:
#define BUFFER_SIZE 15

//Memory Card Chip Select Connection
sfr sbit Mmc_Chip_Select at RB2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISB2_bit;
//
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_RW at RD1_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

unsigned char filename[] = "Temp.TXT";
unsigned char error;
unsigned char i;
unsigned int adc_value;
unsigned char Temperature_Log[BUFFER_SIZE];
void main()
{
 unsigned char ones,tens,hundreds;
 unsigned int temp;

 TRISD = 0x00;
 Lcd_Init();
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Cmd(_LCD_CLEAR);
 Delay_ms(100);
 Lcd_Out(1,1," TEMP. RECORDER");
 Lcd_Out(2,1,"  BY HEMNATH  ");
 Delay_ms(1000);

 ADCON0 = 0b00000001;    //A/D Converter Power Up
 ADCON1 = 0x00;          //All Analog Channels
 ADCON2 = 0b10111110;    //Right Justfied and Slowest Clock for better accuracy
 ADC_Init();

 SPI1_Init();
 SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
 Delay_us(10);

 error = MMC_Init();
 while(error == 1)
 {
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Out(1,1," CARD NOT FOUND");
  error = MMC_Init();
 }

 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1," CARD DETECTED!");
 Lcd_Out(2,1,"CARD INITIALIZED");
 Delay_ms(1000);

 MMC_Fat_Init();
 SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
 
 Mmc_Fat_Delete();
 Mmc_Fat_Assign(&filename,0xA0);

 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1,"TEMPERATURE:");

 while(1)
 {
  adc_value = ADC_Read(0);      //Read Temperature from ADC
  adc_value = adc_value*488;
  adc_value = adc_value/10;

  temp = adc_value/100;

  Temperature_Log[0] = 'T';
  Temperature_Log[1] = 'E';
  Temperature_Log[2] = 'M';
  Temperature_Log[3] = 'P';
  Temperature_Log[4] = '=';

  ones = temp%10;
  temp = temp/10;
  tens = temp%10;
  temp = temp/10;
  hundreds = temp%10;

  ones = ones|0x30;
  tens = tens|0x30;
  hundreds = hundreds|0x30;

  Temperature_Log[5] = hundreds;
  Temperature_Log[6] = tens;
  Temperature_Log[7] = ones;
  Temperature_Log[8] = '.';

  Lcd_Chr(2,1,hundreds);
  Lcd_Chr(2,2,tens);
  Lcd_Chr(2,3,ones);

  ones = adc_value%10;
  adc_value = adc_value/10;
  tens = adc_value%10;
  ones = ones|0x30;
  tens = tens|0x30;

  Temperature_Log[9] = tens;
  Temperature_Log[10] = ones;
  Temperature_Log[11] = ' ';
  Temperature_Log[12] = 'C';
  Temperature_Log[13] = '\r';
  Temperature_Log[14] = '\n';

  Lcd_Chr(2,4,'.');
  Lcd_Chr(2,5,tens);
  Lcd_Chr(2,6,ones);
  Lcd_Chr(2,7,223);
  Lcd_Chr(2,8,'C');
  Mmc_Fat_Append();
  Mmc_Fat_Write(Temperature_Log,15);
  Delay_ms(300);
 }
}
 

Attachments

  • working.JPG
    working.JPG
    81.1 KB · Views: 16
  • TEMP.TXT
    1,005 bytes · Views: 7

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,428
Helped
290
Reputation
580
Reaction score
275
Trophy points
1,363
Location
France 01800
Activity points
10,020
hello,


Thank's for sharing your solution ..but

you wrote ,in another post

It works good. and I have tested in proteus. Not in hardware.

is it always true ?

Plenty of hobyst do not use Proteus ..because of his high cost..
so only work in real world..
The advantage is .. we can be confident if it's work , and get a result.
not the same in simulation.

With MikroC ,we must trust with a complete library without to know details
and if it doesn't work .. we are "Chocolat " ( What about to do ?)

Even this, i will try also by myself your solution, in real world , and give here a feedback
within any days..
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,286
Helped
14,228
Reputation
28,717
Reaction score
12,923
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,606
With MikroC ,we must trust with a complete library without to know details
and if it doesn't work .. we are "Chocolat " ( What about to do ?)
I believe that most people using mikroC do it for the comfort of the built-in libraries. On the other hand, it's a full featured C compiler, you are free to use other libraries, e.g. popular FATFS.
 

paulfjujo

Advanced Member level 4
Joined
Jun 9, 2008
Messages
1,428
Helped
290
Reputation
580
Reaction score
275
Trophy points
1,363
Location
France 01800
Activity points
10,020
I believe that most people using mikroC do it for the comfort of the built-in libraries

I agree with you, it's the main raison i payed for, to get a MikroC Licence (without 2K limit)..

But Comfort means also less basis knowledge for the user..
Fortunatly i started with ASM compiler , then C18 MPLAB ..
and wanted only one compiler for all range of 8bits PIC family. MikroC is one solution of many others.

and because i fund MPLABX too heavy to handle..too many directorie and files ..
even it could be very performant for big project..
(it is my own opinion..not shared by all , of course ! )

i don't forget also, that a compiler is never 100% free of bug, with a so large panel of PIC12..16..18.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top