I have done basic testing for UART and MMC fat16 separately which basically works.
When I combined both coding, the result doesn't show correct result such like invalid/strange characters.
Can you guys suggest opinion ?
Sorry about lengthy coding.
Code: Select all
// 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)
}
}