I have tried out the MikroC example from folder: ExtraBoard/MMC.
My simulation Virtual Terminal get weird result with/without SD card inserted as shown below:
Notes:
- Library works with PIC18 family only;
- The library uses the SPI module for communication. User must initialize
SPI module before using the SPI Graphic Lcd Library.
- For MCUs with two SPI modules it is possible to initialize both of them and
then switch by using the SPI_Set_Active() routine.
- Routines for file handling can be used only with FAT16 file system.
- Library functions create and read files from the root directory only;
- Library functions populate both FAT1 and FAT2 tables when writing to files,
but the file data is being read from the FAT1 table only; i.e. there is no
recovery if FAT1 table is corrupted.
Anyone here know how to create image file for storing and writing file ?
I searched internet source, they all recommended this "dd".
This is link, chrysocome.net - dd for windows
Anyone know how to use this ?
Concerning the Proteus' Virtual Terminal, each character will be transmitted as it is typed into the terminal.
[COLOR="#FF0000"]Hello World MMC init FAILED Test End MMC init OK ? H? Hello World MMC init FAILED Test End MMC init OK ? H? Hello World MMC init FAILED Test End MMC init OK ? H? Hello World MMC init FAILED Test End MMC init OK ? H? Hello World MMC init F[/COLOR]
// MMC module connections
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
// LCD 16x2 connections
// Lcd pinout settings
sbit 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 direction
sbit 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
const 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] = "MIKRO00x.TXT"; // File names
unsigned short loop, loop2;
unsigned long i, size;
char Buffer[512];
// 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_Write("Hello World",255);
}
// 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;
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);
M_Create_New_File();
Lcd_Out(1,1,"Test End");
}
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)
}
}
// 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
[COLOR="#FF0000"]Mmc_Fat_Rewrite(); // Initialize File Pointer to Clear File[/COLOR]
[COLOR="#FF0000"] Mmc_Fat_Write("Hello World\r\n",13);[/COLOR]
}
You need to initialize the file pointer by using either Mmc_Fat_Append() or Mmc_Fat_Rewrite():
Code:// 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 [COLOR="#FF0000"]Mmc_Fat_Rewrite(); // Initialize File Pointer to Clear File[/COLOR] [COLOR="#FF0000"] Mmc_Fat_Write("Hello World\r\n",13);[/COLOR] }
Also the second parameter of Mmc_Fat_Write() should be adjusted to the actual length of the string, 13, after I added an return and newline.
Let me know if this takes care of the issue.
BigDog
You could use a counter variable which you increment each time a character is added to a character buffer, however you would need to limit the number of received characters before writing the buffer to the MMC. This would allow you to continue using Mmc_Fat_Write(char *fdata, unsigned data_len) with the data_len being the counter.
Another alternative would be to simply write a single character at a time by inserting Mmc_Fat_Write(receivechar, 1) in the appropriate position of your UART receiving routine.
You would of course need to initialize the file pointer using the Mmc_Fat_Append() in the UART receiving routine.
BigDog
Ok,
Set the buffer length to nine as well as the data_len argument of Mmc_Fat_Write(receivechar, 9).
BigDog
// Define necessary definitions
char filename[14] = "MIKRO00x.TXT"; // File names
unsigned char uart_rd;
unsigned char data;
unsigned char temp[10];
unsigned char number =0;
// MMC module connections
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
// LCD 16x2 connections
// Lcd pinout settings
sbit 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 direction
sbit 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)
}
}
Hmm, no people can suggest idea to solve it ?
Well, back to basic FAT16 writing, how'd I write new line data in text file for each input ?
I did search around, they mentioned about "ASCII" for new line and tab.
However I have tried with following:
Mmc_Fat_Write("%12",2) // %12 for new line
Mmc_Fat_write("%12",3) // %12 for new line
// 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(); // Initialize File Pointer to Clear File
Mmc_Fat_Write("Hello World[COLOR="#FF0000"]\r\n[/COLOR]",13);
}
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,[COLOR="#FF0000"]number[/COLOR]); // Use your counter instead of hardcoding the value 9
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Test End");
number =0;
}
unsigned char uart_rd;
unsigned char data;
unsigned char temp[10];
unsigned char N = 0;
// LCD define
// Lcd pinout settings
sbit 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 direction
sbit 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;
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISB = 0;
PORTB = 0xFF;
UART1_Init(9600);
Delay_ms(100);
Lcd_Init();
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
Delay_ms(100);
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[N] = uart_rd;
N = N +1;
if (N == 9) // temp storing string
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_out(1,1,temp);
N=0;
}
}
} while(1);
}
// Define necessary definitions
char filename[14] = "MIKRO00x.TXT"; // File names
unsigned char uart_rd;
unsigned char data;
unsigned char temp[10];
unsigned char number =0;
// MMC module connections
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
// LCD 16x2 connections
// Lcd pinout settings
sbit 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 direction
sbit 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;
CMCON = 0x07;
TRISC = 0x80;
TRISB = 0;
UART1_Init(9600);
Delay_ms(1000);
Lcd_Init();
Lcd_Out(1,1,"Testing");
// 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 == 12) // 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,number);
Mmc_Fat_Write("\r\n",2);
number =0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Test End");
}
}
} 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)
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?