www.pudn.com > LPC1768_SD_FAT_VCOM.zip > main.cpp, change:2011-05-21,size:2934b


#include "LPC17xx.h" 
#include "delay.h" 
#include "usbSerial.h" 
#include "SD.h" 
#include "ff.h" 
 
 
uint8_t contents[] = "This is a new file written from LPC1768...!!!"; 
/***********LED Blinky Example**********************************************************************/ 
int main() 
{ 
	usbSerialInit(); 
	int i; 
	uint8_t temp[512]; 
	unsigned int fileSize; 
	temp[0] = 0; 
	temp[511] = 0x5A; 
	uint8_t tem; 
 
	uint32_t time; 
 
	FATFS myDrive; 
	FIL myFile; 
 
	f_mount(0,&myDrive); 
	f_open(&myFile,"/A/AB.txt",FA_CREATE_NEW); 
	f_close(&myFile); 
 
	f_open(&myFile,"/A/AB.txt",FA_WRITE); 
	f_write(&myFile,contents,40,&fileSize); 
	f_close(&myFile); 
 
	LPC_GPIO1->FIODIR |= 1 << 29; // P1.29 connected to LED 
	while(1) 
	{ 
		tem = f_mount(0,&myDrive); 
		VCOM_printf("fMount returns %d..\n",tem); 
 
		if(tem == FR_OK) 
		{ 
			tem = f_open(&myFile,"/A/AB.txt",FA_READ); 
 
			if(tem == FR_OK) 
			{ 
				VCOM_printf("File Opened \n"); 
 
				tem = f_read(&myFile,temp,512,&fileSize); 
				if(tem == FR_OK) 
				{ 
					VCOM_printf("Contents are ... \n"); 
					for(i=0;i<fileSize;i++) 
					{ 
						VCOM_putc(temp[i]); 
					} 
					VCOM_printf("\n\n\n\n"); 
				} 
				f_close(&myFile); 
			} 
			VCOM_printf("Speed test on 5 MB file..."); 
			tem = f_open(&myFile,"/A/chal diye.mp3",FA_READ); 
			if(tem == FR_OK) 
			{ 
				VCOM_printf("File Opened for speed test.. \n"); 
 
				fileSize = 512; 
				//start timer 
				LPC_SC->PCONP |= 1<<1; 
				LPC_SC->PCLKSEL0 |= 1<<2; 
				LPC_TIM0->PR = 999999; 
				LPC_TIM0->TCR = 2; 
				LPC_TIM0->TCR = 1; 
 
 
				while(fileSize >= 512) 
				{ 
					tem = f_read(&myFile,temp,512,&fileSize); 
				} 
				time = LPC_TIM0->TC; 
				if(tem == FR_OK) 
				{ 
					VCOM_printf("Time taken = %ld",time); 
					VCOM_printf("\n\n\n\n"); 
				} 
				f_close(&myFile); 
			} 
		} 
	/*	VCOM_printf("sdInit returns %d \n",sdInit()); 
		VCOM_printf("sdWrite returns %d ...",sdWrite(0,temp)); 
		VCOM_printf("sdRead returns %d ...temp[511] = %d\n\n",sdRead(0,temp),temp[511]); 
		VCOM_printf("Resetting...\n"); 
		csHi(); 
		for(i=0;i<80;i++) 
			spiWrite(0xFF); 
/////////////////////////////////////////////// 
		VCOM_printf("Sending CMD0..\n"); 
		csLo(); 
		spiWrite(0x40); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0x95); 
 
		VCOM_printf("Response is ..."); 
		for(i=0;i<10;i++) 
		{ 
			temp = spiWrite(0xFF); 
			VCOM_printf(" %d  ",temp); 
			if(temp == 1) 
				break; 
		} 
		spiWrite(0xff); 
		csHi(); 
//////////////////////////////////////////// 
		VCOM_printf("\nSending CMD1 ..\n"); 
		csLo(); 
		spiWrite(0x41); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0); 
		spiWrite(0xf9); 
 
		VCOM_printf("Response is ..."); 
		for(i=0;i<10;i++) 
		{ 
			temp = spiWrite(0xFF); 
			VCOM_printf(" %d  ",temp); 
		} 
 
		csHi(); 
//////////////////////////////////////////////// 
		VCOM_printf("\n\n\n");*/ 
	} 
	return 0; 
}