Выбрать главу

 // Write data to the file

 //

 Mmc_Fat_Write("This is MYFILE.TXT.",19);

 //

 // Add more data to the end...

 //

 Mmc_Fat_Append();

 Mmc_Fat_Write(txt,sizeof(txt));

 //

 // Now read the data and send to UART

 //

 Mmc_Fat_Reset(file_size);

 for(i=0; i<file_size; i++) {

  Mmc_Fat_Read(&character);

  Usart_Write(character);

 }

 for(;;); // wait here forever

}

Figure 7.12: Program listing of the project

Table 7.8: Mmc_Fat_Assign file creation flags

Flag Description
0x01 Read only
0x02 Hidden
0x04 System
0x08 Volume label
0x10 Subdirectory
0x20 Archive
0x40 Device (internal use only, never found on disk)
0x80 File creation flag. If file does not exist and this flag is set, a new file with the specified name will be created.

The file is cleared (if it is not already empty) using function call Mmc_Fat_Rewrite, and then the string “This is MYFILE.TXT” is written to the file by calling library function Mmc_Fat_Write. Note that the size of the data to be written must be specified as the second argument of this function call. Then Mmc_Fat_Append is called and the second string “This is the added data…” is appended to the file. Calling function Mmc_Fat_Reset sets the file pointer to the beginning of the file and also returns the size of the file. Finally, a for loop is set up to read each character from the file using the Mmc_Fat_Read function call, and the characters read are sent to the UART with the Usart_Write function call.

A snapshot of the screen with the HyperTerminal running is shown in Figure 7.13.

Figure 7.13: Snapshot of the screen

PROJECT 7.4 — Temperature Logger

This project shows the design of a temperature data logger system. The ambient temperature is read every ten seconds and stored in a file on an SD card. The program is menu-based, and the user is given the option of:

• Sending the saved file contents to a PC

• Saving the temperature readings to a new file on an SD card

• Appending the temperature readings to an existing file on an SD card

The hardware of this project is similar to the one for Project 7.1 (i.e., as shown in Figure 7.8), but here, in addition, the serial input port pin (RC7) is connected to the RS232 connector so data can be received from the PC keyboard. In addition, a LM35DZ-type analog temperature sensor is connected to the microcontroller’s analog input AN0 (pin 2). The new circuit diagram is shown in Figure 7.14.

Figure 7.14: Circuit diagram of the project

The LM35 DZ is a three-pin analog temperature sensor that can measure with 1°C accuracy temperatures between 0°C and +100°C. One pin of the device is connected to the supply (+5V), another pin to the ground, and the third to the analog output. The output voltage of the sensor is directly proportional to the temperature (i.e., Vo=10mV/°C). If, for example, the temperature is 10°C, the output voltage will be 100mV, and if the temperature is 35°C, the output voltage of the sensor will be 350mV.

When the program is started, the following menu is displayed on the PC screen:

TEMPERATURE DATA LOGGER

1. Send temperature data to the PC

2. Save temperature data in a new file

3. Append temperature data to an existing file

Choice?

The user then chooses one of the three options. When an option is completed, the program does not return to the menu. To display the menu again the system has to be restarted.

The program listing of the project is shown in Figure 7.15 (program SD4.C). In this project, a file called TEMPERTRTXT is created on the SD card to store the temperature readings (the library function call will insert the “.” to make the filename “TEMPERTR.TXT”), if it does not already exist.

/**************************************************************

                  TEMPERATURE LOGGER PROJECT

                 ============================

In this project a SD card is connected to PORTC as follows:

CS  RC2

CLK RC3

DO  RC4

DI  RC5

In addition, a MAX232 type RS232 voltage level converter chip

is connected to serial ports RC6 and RC7. Also, a LM35DZ type

analog temperature sensor is connected to analog input AN0 of

the microcontroller.

The program is menu based. The user is given options of either

to send the saved temperature data to the PC, or to read and

save new data on the SD card, or to read temperature data and

append to the existing file. Temperature is read at every 10

seconds.

The temperature is stored in a file called TEMPERTR.TXT

Author: Dogan Ibrahim

Date:   August 2007

File:   SD4.C

**************************************************************/

char filename[] = "TEMPERTRTXT";

unsigned short character;

unsigned long file_size,i,rec_size;

unsigned char ch1,ch2,flag,ret_status,choice;

unsigned char temperature[10],txt[12];

//

// This function sends carriage-return and line-feed to USART

//

void Newline() {

 Usart_Write(0x0D); // Send carriage-return

 Usart_Write(0x0A); // Send line-feed

}

//

// This function sends a space character to USART

//

void Space() {

 Usart_Write(0x20);

}

//

// This function sends a text to serial port