void usart_sendstring (char *s);
void usart_sendchar(char chr);
void usart_init(void);
sbit Mmc_Chip_Select at LATB4_bit;
sbit Mmc_Chip_Select_Direction at TRISB4_bit;
unsigned long size;
char character[10];
unsigned char i;
char err;
short fhandle;
void main()
{
usart_init();
usart_sendstring("start");
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
err=Mmc_Fat_Init();
if (err == 0)
{
usart_sendstring("sd card initialized successfully");
}
else if (err != 0)
{
usart_sendstring("error in initializing sd card");
usart_sendchar(err);
}
Mmc_Fat_Assign("MIKRO007.TXT", 0xA0);
Mmc_Fat_Append();
fhandle = Mmc_Fat_Open("MIKRO007.TXT.TXT", FILE_WRITE, 0x01);
Mmc_Fat_Write("Hello world", 255);
Mmc_Fat_Reset(&size);
for (i=0; i<11; i++);
{
Mmc_Fat_Read(&character[i]);
usart_sendstring(character);
}
Mmc_Fat_Close();
}
void usart_init(void)
{
TXSTA |= (1<<5); // Transmit Enable bit
TXSTA |= (1<<2); // baudrate generator to high speed
TRISC |=(1<<7); // congifure rx and tx
TRISC &=~(1<<6);
RCSTA |= (1<<7); // serial enable bit
RCSTA |= (1<<4); // Enabling the reception by setting bit CREN
SPBRG = 129; // baudrate 9600
}
void usart_sendchar(char chr)
{
while (!(TXSTA &(1<<1)));
TXREG =chr;
}
void usart_sendstring (char *s)
{
while (*s)
{
usart_sendchar(*s);
s++;
}
}