I allready Closed the file handler, but it does not write any info into the file.
Code: Select all
#define BUFFER_SIZE 27
#include "__Lib_FAT32.h"
//Memory Card Chip Select Connection
sfr sbit Mmc_Chip_Select at LATB2_bit; //was RB2_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISB2_bit;
//
// Software I2C connections
sbit Soft_I2C_Scl at RB3_bit;
sbit Soft_I2C_Sda at RB4_bit;
sbit Soft_I2C_Scl_Direction at TRISB3_bit;
sbit Soft_I2C_Sda_Direction at TRISB4_bit;
// End Software I2C connections
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_RW at RD1_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
unsigned char filename[] = "TEMP.TXT";
//unsigned char error;
unsigned char i;
unsigned char *WriteBuffer[100];
unsigned int adc_value;
unsigned char Temperature_Log[BUFFER_SIZE];
char seconds, minutes, hours, day, month, year; // Global date/time variables
char prev_minutes=0;
int err;
short fhandle;
__HANDLE fileHandle[4];
__SECTOR sc;
unsigned char BCD2UpperCh(unsigned char bcd)
{
unsigned char temp;
temp = bcd >> 4;
temp = temp | 0x30;
return(temp);
}
unsigned char BCD2LowerCh(unsigned char bcd)
{
unsigned char temp;
temp = bcd & 0x0F; //Making the Upper 4-bits
temp = temp | 0x30;
return(temp);
}
unsigned char rtc1307_Read(unsigned char address)
{
unsigned char temp;
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
temp = Soft_I2C_Read(0);
Soft_I2C_Stop();
return temp;
}
void Display_Time() {
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_FIRST_ROW);
Lcd_Out(1, 1,"DATE:"); // Prepare and output static text on LCD
Lcd_Chr(1, 7,BCD2UpperCh(day));
Lcd_Chr(1, 8,BCD2LowerCh(day));
Lcd_Chr(1, 9,'/');
Lcd_Chr(1, 10,BCD2UpperCh(month));
Lcd_Chr(1,11,BCD2LowerCh(month));
Lcd_Chr(1,12,'/');
Lcd_Chr(1,13, BCD2UpperCh(year));
Lcd_Chr(1,14,BCD2LowerCh(year));
Lcd_Out(2, 1,"TIME:");
Lcd_Chr(2, 7,BCD2UpperCh(hours));
Lcd_Chr(2, 8,BCD2LowerCh(hours));
Lcd_Chr(2, 9,':');
Lcd_Chr(2, 10,BCD2UpperCh(minutes));
Lcd_Chr(2,11,BCD2LowerCh(minutes));
Lcd_Chr(2,12,':');
Lcd_Chr(2,13,BCD2UpperCh(seconds));
Lcd_Chr(2,14,BCD2LowerCh(seconds));
}
void main()
{
unsigned char ones,tens,hundreds,fractional;
unsigned int temp;
TRISD = 0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(100);
Lcd_Out(1,1," TEMP. RECORDER");
Lcd_Out(2,1," EMBEDDED LAB");
Delay_ms(1000);
ADCON0 = 0b00000001; //A/D Converter Power Up
ADCON1 = 0x0E;
ADCON2 = 0b10111110; //Right Justfied and Slowest Clock for better accuracy
ADC_Init();
while(err == 1)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1," CARD NOT FOUND");
err = MMC_Init();
}
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1," CARD DETECTED!");
Lcd_Out(2,1,"CARD INITIALIZED");
Delay_ms(1000);
SPI1_Init();
err = FAT32_Init();
if (FAT32_Exists("SWAP.TXT"))
{
Lcd_Out(1,1,"Deleting existing swap file...");
FAT32_Delete("SWAP.TXT");
Lcd_Out(2,1,"Done");
}
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
fileHandle[0]=FAT32_Open("data.txt",FILE_APPEND);//write mode. If will create data.txt if it doesn't exist.
err = FAT32_Close(fileHandle[0]);
FAT32_MakeDir("ronde1"); //Dit werkt
fileHandle[0]=FAT32_Open("data.txt",FILE_APPEND);//write mode. If will create data.txt if it doesn't exist.
fileHandle[1] = FAT32_MakeSwap("SWAP.TXT", 500, &sc); // if swap file creation was successful,
if (fileHandle[0] < 0) while(1);
err = FAT32_Close(fileHandle[1]);
err = FAT32_Close(fileHandle[0]);
while(1)
{
adc_value = ADC_Read(0); //Read Temperature from ADC
adc_value = adc_value*488;
adc_value = adc_value/10;
Temperature_Log[0] = 'T';
Temperature_Log[1] = 'E';
Temperature_Log[2] = 'M';
Temperature_Log[3] = 'P';
Temperature_Log[4] = '=';
temp = adc_value/100;
ones = temp%10;
temp = temp/10;
tens = temp%10;
temp = temp/10;
hundreds = temp%10;
ones = ones|0x30;
tens = tens|0x30;
hundreds = hundreds|0x30;
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"TEMPERATURE:");
Lcd_Chr(2,1,hundreds);
Lcd_Chr(2,2,tens);
Lcd_Chr(2,3,ones);
Temperature_Log[5] = hundreds;
Temperature_Log[6] = tens;
Temperature_Log[7] = ones;
Temperature_Log[8] = '.';
adc_value = adc_value/10;
fractional = adc_value%10;
fractional |= 0x30;
Lcd_Chr(2,4,'.');
Lcd_Chr(2,5,fractional);
Lcd_Chr(2,6,'C');
Temperature_Log[9] = tens;
Temperature_Log[10] = ones;
Temperature_Log[11] = ' ';
Temperature_Log[12] = 'C';
Temperature_Log[13] = '\r';
Temperature_Log[14] = '\n';
FAT32_Dev_Write_Sector(sc++, Temperature_Log);
fileHandle[0]=FAT32_Open("data.txt",FILE_APPEND);//write mode. If will create data.txt if it doesn't exist.
FAT32_Write(fileHandle[0],"TL",strlen("TL"));
FAT32_Close(fileHandle[0]);
Lcd_Out(2,9,err);
Delay_ms(1000);
}
}
// Code modified by Admin, comment lines removed for better code clarity