⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab1.c

📁 使用PIC24 16位单片机 读写SD卡 支持FAT32
💻 C
字号:
// Lab1.c

#include "sdmmc.h"
#include "fileio.h"

int main (void)
{
	WORD 	i;

	// Address of the root directory
	DWORD	ROOTaddress = 0x0000020B;

	// Address of the FAT
	DWORD	FATaddress	= 0x0000003B;
	
	// Each file had at least one cluster assigned to it
	// When we create the file, we will have to make sure the
	// cluster that we assign is empty
	// Otherwise, we could get unexpected data in our file
	DWORD	DATAClusterAddress	= 0x0000022C;	// The address of the first sector of the file

	// Initialize the first entry in the root directory buffer 
	// to the values we want for our file
	BYTE	ROOTbuffer[513] = {0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x20, 0x20, 0x20,  // File name
								0x54, 0x58, 0x54,		// File extension-	 offset 8
								0x20, 		// File attributes-				 offset 11
								0x00,		// Reserved-					 offset 12
								0xB2,		// Create time (ms)-			 offset 13
								0x78, 0x72,		// Create time-				 offset 14
								0xB0, 0x32,		// Create date-				 offset 16
								0xB0, 0x32,		// Last access date-		 offset 18
								0x00, 0x00,		// first cluster high-		 offset 20
								0x79, 0x72,		// Write time-				 offset 22
								0xB0, 0x32,		// Write date-				 offset 24
								0x00, 0x00,		// First cluster low-		 offset 26
								0x00, 0x00, 0x00, 0x00};  // File size-		 offset 28

	// Initialize the clusters in the file allocation buffer to
	// point to the cluster we want to use
	BYTE	FATbuffer[513] = {0xF8, 0xFF,		// Reserved cluster 
								0xFF, 0xFF, 	// Reserved cluster
								0x00, 0x00, 	// empty cluster
								0xF8, 0xFF};	// The cluster for our file

	// The text we want to write
	BYTE	DATAbuffer[513] = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x77, 0x6F, 
								0x72, 0x6C, 0x64, 0x21};

	// A buffer for clearing the sectors of the cluster we are using
	BYTE	EMPTYbuffer[513];


	TRISA = 0x00;

	// Initialize the other stuff in the root directory buffer to 0
	for (i = 32; i < 513; i++)
	{
		ROOTbuffer[i] = 0x00;
	}
	
	// Initialize the other stuff in the file allocation table buffer to 0
	for (i = 8; i < 513; i++)
	{
		FATbuffer[i] = 0x00;
	}

	// Initialize the other stuff in the data buffer to 0
	for (i = 13; i < 513; i++)
	{
		DATAbuffer[i] = 0x00;
	}

	// Initialize the empty buffer to 0
	for (i = 0; i < 513; i++)
	{
		EMPTYbuffer[i] = 0x00;
	}





	if (!FAT16Init())
	{
		// If the card didn't initialize, you'll get 0xAA on the LEDs
		LATA = 0xAA & 0xF3;
		LATD = (0xAA & 0x0C) ? (((0xAA & 0x0C) == 0x0C) ? 0xC0 : ((~((0xAA & 0x0C) << 4)) & 0xC0)) : 0x00;
						
		while (1);
	}



/***********************************************************************************
* 1. Write the initial root directory entry data to the root directory
***********************************************************************************/
	
/**********************************************************************************/


/***********************************************************************************
* 2. Write the FAT data to the FAT
***********************************************************************************/
	
/**********************************************************************************/



/***********************************************************************************
* 3. Clear the data space that the data will be written to
***********************************************************************************/
	
/**********************************************************************************/



	ROOTbuffer[26] = 0x03;	// Update the root directory entry buffer
							// Our file is in cluster 3


/***********************************************************************************
* 4. Write the updated root directory data to the root directory
***********************************************************************************/
	
/**********************************************************************************/


/***********************************************************************************
* 5. Write the data to the date cluster
***********************************************************************************/
	
/**********************************************************************************/


	ROOTbuffer[22] = 0x7A;	// Increment the write time so we know we 
							// did something. Since we don't have a 
							// real time clock, there is no real
							// way to update this with the actual time
	ROOTbuffer[28] = 0x0D;	// Update our file size with the number of bytes 
							// we put in our file.  In this case 0x0D bytes


/***********************************************************************************
* 6. Write the updated root directory date to the root directory
***********************************************************************************/
	
/**********************************************************************************/
	

	// If you got this far, all the lights will turn on
	LATA = 0xF3;
	LATD = 0xC0;
						
	while(1);	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -