Advanced PIC18 Projects—SD Card Projects:Temperature Logger

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.

Advanced PIC18 Projects—SD Card Projects-0118

The LM35 DZ is a three-pin analog temperature sensor that can measure with 1oC accuracy temperatures between 0oC and þ100oC. 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/oC). If, for example, the temperature is 10oC, the output voltage will be 100mV, and if the temperature is 35oC, 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.

The following functions are created at the beginning of the program, before the main program:

Newline sends a carriage return and a line feed to the UART so the cursor moves to the next line.

Text_To_Usart receives a text string as its argument and sends it to the UART to display on the PC screen.

Get_Temperature starts the A/D conversion and receives the converted data into a variable called Vin. The voltage corresponding to this value is then calculated in millivolts and divided by 10 to find the actual measured temperature in oC. The decimal part of the found temperature is then converted into string form using function LongToStr. The leading spaces are removed from this string, and the resulting string is stored in character array temperature. Then the fractional parts of the measured temperature, a carriage return, and a line feed are added to this character array, which is later written to the SD card.

The following operations are performed inside the main program:

• Initialize the UART to 2400 baud

• Initialize the SPI bus

• Initialize the FAT file system

• Display menu on the PC screen

• Get a choice from the user (1, 2, or 3)

• If the choice ¼ 1, assign the temperature file, read the temperature records, and display them on the PC screen

Advanced PIC18 Projects—SD Card Projects-0119

Advanced PIC18 Projects—SD Card Projects-0120Advanced PIC18 Projects—SD Card Projects-0121

Advanced PIC18 Projects—SD Card Projects-0122Advanced PIC18 Projects—SD Card Projects-0123Advanced PIC18 Projects—SD Card Projects-0124

• If the choice ¼ 2, create a new temperature file, get new temperature readings every ten seconds, and store them in the file

• If the choice ¼ 3, assign to the temperature file, get new temperature readings every ten seconds, and append them to the existing temperature file

• If the choice is not 1, 2, or 3, display an error message on the screen The menu options are described here in more detail:

Option 1: The program attempts to assign the existing temperature file. If the file does not exist, the error messages “File does not exist.. .No saved data.. .” and “Restart the program and save data to the file.. .” are displayed on the screen, and the user is expected to restart the program. If, on the other hand, the temperature file already exists, then the message: “Sending saved data to the PC.. .” is displayed on the PC screen. Function Mmc_Fat_Reset is called to set the file pointer to the beginning of the file and also return the size of the file in bytes. Then a for loop is formed, temperature records are read from the card one byte at a time using function Mmc_Fat_Read, and these records are sent to the PC screen using function Usart_Write. At the end of the data the message “End of data.. .” is sent to the PC screen.

Option 2: In this option, the message “Saving data in a NEW file.. .” is sent to the PC screen, and a new file is created using function Mmc_Fat_Assign with the create flag set to 0x80. The message “TEMPERATURE DATA – SAVED EVERY 10 SECONDS” is written on the first line of the file using function Mmc_Fat_Write. Then, a for loop is formed, the SD card is set to file append mode by calling function Mmc_Fat_Append, and a new temperature reading is obtained by calling function Get_Temperature. The temperature is then written to the SD card. Also, the current record number appears on the PC screen to indicate that the program is actually working. This process is repeated after a ten-second delay.

Option 3: This option is very similar to Option 2, except that a new file is not created but rather the existing temperature file is opened in read mode. If the file does not exist, then an error message is displayed on the PC screen.

Default: If the user entry is a number other than 1, 2, or 3, then this option runs and displays the error message “Wrong choice.. .Restart the program and try again.. .” on the PC screen.

The project can be tested by connecting the output of the microcontroller to the serial port of a PC (e.g., COM1) and then running the HyperTerminal terminal emulation software. Set the communications parameters to 2400 baud, 8 data bits, 1 stop bit, and no parity bit. Figure 7.16 shows a snapshot of the PC screen when Option 2 is selected to save the temperature records in a new file. Notice that the current record numbers are displayed on the screen as they are written to the SD card.

Figure 7.17 shows a screen snapshot where Option 1 is selected to read the temperature records from the SD card and display them on the PC screen.

Advanced PIC18 Projects—SD Card Projects-0125Advanced PIC18 Projects—SD Card Projects-0126

Leave a comment

Your email address will not be published. Required fields are marked *