Sunday, June 26, 2016

Memory Card Interfacing with PIC Micro-Controller

Memory card (also called a flash memory card) is a solid-state electronic data storage device. Memory cards save the stored data even after the memory device is disconnected from its power source. This ability to retain data is the key
for flash memory card applications, for example, in digital cameras, where the saved pictures are not lost after the memory card is removed from the camera.


For reading and writing to memory cards, one can use out of two protocols and they are:

  • SD Card Protocol
  • SPI Protocol
SD Card protocol is little bit complex and SPI Protocol is easy as SPI Modules can be found in almost every controller.

Two Activate the SPI Protocol mode of Memory Card just give a low pulse on the Chip Select pin of the Memory Card.

Have a look at the schematic diagram below, to interface memory card with PIC Micro-Controller.
Memory Card Interfacing with PIC Micro-Controller
The Memory Card uses SPI Protocol for reading and writing data to it, apart from that, if you want that a Operating system like Windows, Linux etc can see the contents of the card easily then you have create a FAT table in your memory card, which it self is a very complex process and and writing everything from scratch is itself a project of at-least of one year.
But it didn't means that we are going to take 1 year to complete this project, we will just take few hours.
How? With mikroC Compiler, this compiler has in built library for Memory Card so we have to just use the functions, which are very easy to use and learn.

In this project we are going to create a log in memory card of the temperature sensed by Micro-Controller.
Have a look at the schematic diagram below, which shows how i wired up the things, make sure that when making a real hardware dont forget to make a voltage divider when interfacing memory card and pic micro-controller, as 5V on memory card pins will damage that.

Schematic Diagram
Code is as follow:
 #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," EMBEDDED LAB");  
  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_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);  
  }  
 }  

The Log created in memory card when seen in windows explorer is as follow:
Log Data
Video Demonstration:



Creating Image for Memory Card in Simulation Environment (Proteus):


Code Update:
In this project, time and date information are added, so that one can use the log created in memory card for some visual representation with respect to time and date.
A Software is also developed in MATLAB, which takes the log and then plot the Temperature With Respect to time. Have a look at the following video for demonstration.



A Software is also developed in LabVIEW (Similar to the one shown above in MATLAB), which takes the log file as input and then plot the Temperature With Respect to time.


To Download Initial Release Click Here.
To Download the Updated Release with Time and Date addition, Click Here

18 comments:

  1. Thanks a lot! These instructions helped me in my own little project (Ethernet). First time on MikroC and loving it. PS: That final increment with MatLab: Magnifique!

    ReplyDelete
  2. Hi... I need your help in my project...There is some error and its not working correctly plz can you help me i shall be very thank full to you..

    ReplyDelete
    Replies
    1. What is the problem you are facing. If you share what is the problem then i will try to help you.
      One thing i want to say that, this project will not work when you use 48MHz Frequency, as Memory Card will not initialize.
      So what you can do is that, operate your PIC at 20Mhz.
      Mail us matlab.academy@gmail.com

      Delete
  3. Hi @A Khattak
    What is the problem you are facing. If you share what is the problem then i will try to help you.
    One thing i want to say that, this project will not work when you use 48MHz Frequency, as Memory Card will not initialize.
    So what you can do is that, operate your PIC at 20Mhz.
    Mail us matlab.academy@gmail.com

    ReplyDelete
  4. Hi...!!!
    Very Good information on this blog can you help me where is Buy 16GB Memory Card

    ReplyDelete
    Replies
    1. Thanks.
      You can buy it from the nearest mobile store.
      If you are from India, you can order it online from Amazon or Flipkart or Snapdeal
      http://amzn.to/2eRicdP
      But i think you are not, so either buy locally or from online.

      Delete
  5. Hello, this project is awesome, thank you very much for sharing it.

    I am doing a Project inspired on this one, and I would be glad if you could give me more details on the crystal that you used. At home I just have 12MHz and 10MHz, can it use one of those? How do I change the settings on the MikroC or in the code to use one of these?

    I am really grateful already.

    ReplyDelete
    Replies
    1. Thanks for your words, i am glad that you like it and found helpful.

      Now coming to your connection, if you want to use the same project then i would suggest you to use the 8MHz External Oscillator, but as you are saying that you have 10MHz crystal with you, then you can proceed with them as well, without any problem, you don't have to edit the configuration of the PIC as 8MHz and 10 MHz and 12MHz oscillator needs HS configuration setting, so that won't change.
      Just make sure to edit the Frequency Value in mikroC project.
      Thanks

      Delete
  6. Please share steps of building above mentioned code in mikroc because when we build this file we got hex file.But when hex file loaded to the pic we got only lcd light up on running..nothing else...so please share if there is any library to add or something like that...

    ReplyDelete
  7. please share the steps of building the above mentioned code in mikroc,because when we build the program we got the hex file,but when we loaded the hex file to the pic only the lcd lit up nothing else.So please share if there is any library to be added in mikroc or something like that..........

    ReplyDelete
    Replies
    1. The most common problem is setting up PIC Configuration.
      Leave the MikroC Memory Card Library, in your case even the LCD is nor working.
      As per Code, in any case you must get
      " TEMP. RECORDER"
      " EMBEDDED LAB"
      the above display on LCD, but if you are not getting this, then the problem is different.
      Try to debug, first test the simple led blink program and make sure that the delay you entered matches with the real time delay between led blinks.
      The most common problem individual faced in the above project is that they use PLL for configuring PIC in this project, and that will create problem.
      But to me, your case is different, please debug and find out the problem, atleast LCD should work.

      Delete
    2. Could you show me my error?

      0 434 Demo Limit Demo Limit
      0 102 Finished (with errors): 14 Sep 2017, 09:25:56 MyProject.mcppi

      Thank you very much!

      Delete
  8. could you help me fix it:

    0 434 Demo Limit Demo Limit
    0 102 Finished (with errors): 14 Sep 2017, 09:25:56 MyProject.mcppi

    ReplyDelete
  9. Code Size Increased.
    You need to buy or get the license for pro version for your compiler.

    ReplyDelete
  10. hi i used ur code in proteus with the schematic u gave
    i got a way lot of errors...the errors are below...i just tried to implement the above project of interfacing temperature sensor nd sd card through pic ...SM 38 : Illegal opcode (char)
    Warning[207] ..\MAIN

    ReplyDelete
    Replies
    1. I don't see anything due to which your project is not working.
      In proteus you will get some warnings and that is fine.
      Are you using my proteus project, which is not working, or you have done some changes.
      Mail be everything at matlab.academy@gmail.com
      I can check the proteus one my pc.

      Delete