Writing and Reading Hex Values to a Text file using FatFs
This line makes a pointer from a random value on the stack, asking for a (Hard-) fault:

f_write(&amp;MyFile,Buff,sizeof(Buff),(void*)byteswritten); 


Correct code would look like this:

UINT byteswritten;
f_write(&amp;MyFile,Buff,sizeof(Buff),&amp;byteswritten);


Note that f_write() writes data in binary to the file, not in hexadecimal.

That f_printf() line should give you a compiler warning like "make pointer from an integer without a cast", because it expects a string (pointer to char) as second argument.

Corrected line for hexadecimal output:

f_printf(&amp;MyFile," %X",Buff[i]);