Created
January 17, 2020 03:01
-
-
Save rescurib/04c149ca3e43df5525c8abfe35514960 to your computer and use it in GitHub Desktop.
Ejemplo de escritura a una archivo en formato FAT32 hacia una memoria SD utililizando un PIC18F4550
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*.............................................................. | |
Ejemplo de escritura simple a una memoria SD con un PIC18F4550. | |
Tarjeta de desarollo: Miuva | |
Compilador: MikroC Pro for PIC | |
Autor: Rodolfo E. Escobar U. | |
Fecha: Enero 2020 | |
..............................................................*/ | |
#include "__Lib_FAT32.h" | |
// -- Variable globales -- | |
char tempStr[6]; | |
char nl[] = "\r\n"; | |
char sep[] = ","; | |
signed short err; | |
char FileName[13]; | |
extern __HANDLE fileHandle[]; | |
__HANDLE fileHandle[4]; | |
// Selector de Chip de la tarjeta | |
sbit Mmc_Chip_Select at LATC2_bit; | |
sbit Mmc_Chip_Select_Direction at TRISC2_bit; | |
void main(){ | |
int i,k; | |
ADCON1 |= 0x07; | |
CMCON |= 7; | |
//--- Led de referencia --- | |
TRISE.F1 = 0; | |
LATE.F1 = 1; | |
//-------------------------- | |
TRISB = 0x00; | |
PORTB = 0X00; | |
//------ Inicializar el modulo SPI (modo lento) ---------- | |
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64,_SPI_DATA_SAMPLE_MIDDLE, | |
_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); | |
//-------------------------------------------------------- | |
//----------- Inicialización de tarjeta SD --------------- | |
err = FAT32_Init(); | |
//err = FAT32_format("dev0"); // Formatear tarjeta (opcional) | |
if (err < 0){ | |
LATE.F0 = 0; | |
while(err < 0){ // Reintentar cada segundo... | |
err = FAT32_Init(); | |
Delay_ms(1000); | |
} | |
} | |
LATE.F0 = 1; | |
//------------------------------------------------------- | |
//------ Inicialización SPI a velocidad de trabajo ------ | |
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, | |
_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); | |
//------------------------------------------------------- | |
//----------- Escritura de un archivo ------------------- | |
fileHandle[0] = FAT32_Open("FILE1.TXT", FILE_WRITE); | |
if (fileHandle[0] < 0) while(1); | |
LATE.F1 = 0; | |
//Cabecera: | |
err = FAT32_Write(fileHandle[0],"fecha,temperatura", 18); | |
err = FAT32_Write(fileHandle[0],nl,2); | |
// Estampa de tiempo (en una aplición real debe ser tomada de un RTC) | |
err = FAT32_Write(fileHandle[0],"01/03/2020 11:34:01", 19); | |
err = FAT32_Write(fileHandle[0],sep,1); | |
// Dato de temperatura (simulado) | |
IntToStr(578, tempStr); // Conversión de entero a cadena | |
err = FAT32_Write(fileHandle[0],tempStr, 6); | |
err = FAT32_Write(fileHandle[0],nl,2); | |
LATE.F1 = 1; | |
err = FAT32_Close(fileHandle[0]); // Cerrar archivo | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment