Sie sind auf Seite 1von 12

Project 7.

9—SD Card-Based Temperature Data


Logger
In this project, the design of a temperature data logger system is
described. The ambient temperature is read every 10s, and 100
records are stored on an SD card, or the contents of an already saved
file are sent to the PC. The program is menu based, and the user is
given the options of (Figure 7.84)
• Send saved temperature readings on the SD card to a PC.
• Save temperature readings in a new file on SD card.
• Append the temperature readings to an existing file on SD card.

FIGURE 7.82 Circuit Diagram of the XC8 Version of the Project.


FIGURE 7.83 MPLAB XC8 Program.

Block Diagram of the Project.


FIGURE 7.84

The block diagram of the project is shown in Figure 7.83.


HARDWARE DESCRIPTION
The circuit diagram of the project is shown in Figure 7.85. An SD
card is connected to the microcontroller. Additionally, the UART
pins (RX6 and RX7) are connected to an RS232 connector via a
MAX232 voltage level translator chip. The temperature is sensed via
the LM35DZ-type analog temperature sensor, connected to AN0
pin.

Circuit Diagram of the Project.


FIGURE 7.85

LM35DZ is a three-pin analog temperature sensor that can measure


temperature with a 1°C accuracy in the interval 0 to +100°C. One pin
of the device is connected to the supply (+5V), the other pin to the
ground and the third pin is the analog output. The output voltage of
the sensor is directly proportional to the temperature, that is,
Vo=10mV/°C. If, for example, the temperature is 10°C, the output
voltage will be 100mV. Similarly, if the temperature is 35°C, the
output voltage of the sensor will be 350mV.

Project Program
MIKEOC PRO FOR PIC
When the program is started, the following menu will be 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 is then expected to choose the required option. At the end
of an option, the program does not return to the menu and the
system should be restarted to display the menu again.
The mikroC Pro for PIC program listing of the project is shown
in Figure 7.86 (MIKROC-SD2.C). In this project, a file called
“TEMPERTR.TXT” is created on the SD card to store the
temperature readings.
The following functions are created at the beginning of the program,
before the main program:
Newline: This function sends a carriage return and a line feed to the
UART so that the cursor moves to the next line.
Get_Temperature: This function 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 degrees
Celsius. The decimal part of the temperature found 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 onto the SD card.
mikroC Pro for PIC Program.
FIGURE 7.86

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 (between 1 and 3).
• If the choice=1, then open the saved temperature file, read the
temperature records, and send them to the PC.
• If the choice=2, then create a new temperature file, get new
temperature readings every 10s, and store 100 records in the file.
• If the choice=3, then assign to the temperature file, get new
temperature readings every 10s, and append them to the existing
temperature file. Hundred records are appended to the file.
• If the choice is not 1–3, display an error message on the screen.
The menu options are described below in more detail:
Option 1: The program attempts to open an existing temperature file
with name TEMPERTR.TXT (notice here that Mmc_Fat_Assign
function is used. We could have used the Mmc_Fat_Open function
instead). If the file does not exist, the error messages: ”File does not
exist...” and “Try again...” are displayed on the screen. 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. Mmc_Fat_Reset function is called to set the file pointer to
the beginning of the file and also to return the size of the file in
bytes. Then a forloop is formed, temperature records are read from
the card 1 byte at a time using function Mmc_Fat_Read, and these
records are sent to the PC screen. 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, a new file is created, 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 into 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 is shown on the PC screen
to indicate that the program is actually working. This process is
repeated after a 10-s delay, until 100 records are written to the file.
After this time, the main menu is displayed again.
Option 3: This option is very similar to Option 2. The only difference
is that here a new file is not created, but the existing temperature file
is opened in the append mode, and 100 records are written to the
file. If the file does not exist, then an error message is displayed on
the PC screen.
Saving Temperature Records on the SD Card with Option 2.
FIGURE 7.87

Default: If the user entry is a number outside 1–3, then this option
runs and displays the error message “Wrong choice…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 a terminal emulation software (e.g. Hyperterm or mikroC
Pro for PIC built-in terminal emulator—USART Terminal). Set
communication parameters to 2400 baud, 8 data bits, 1 stop bit, no
parity bit, and no flow control. Figure 7.87 shows a snapshot of the
PC screen when Option 2 is selected to save temperature records in
a new file. Note that the current record numbers are displayed on
the screen as they are written to the SD card.
Figure 7.88 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.
Finally, Figure 7.89 shows a screen snapshot when option 3 is
selected to append the temperature readings to the existing file.
MPLAB XC8
The MPLAB XC8 version of the program is left as an exercise to the
reader.

FIGURE 7.88 Displaying the Records on the PC Screen with Option 1.

Das könnte Ihnen auch gefallen