Software
The firmware for PIC12F1822 is developed in C and compiled with mikroElektronika’s mikroC Pro for PIC compiler. The compiler does provide an ADC library but that uses the external supply voltage as a reference for A/D conversion. In order to configure the FVR module to generate a fixed 2.048V for A/D conversion, you have to write your own code for ADC operation. The ADC sample is taken every 2 sec and is sent to the PC through USB-UART module as two bytes. The complete source code is provided below with comments. It can be compiled with the demo version of mikroC Pro for PIC compiler. Make sure that you select the internal clock source at 4.0 MHz from Project->Edit window.
Download the complete mikroC code
As I mentioned earlier, the PC application is developed using the Processing programming language. Processing is an open-source software development environment designed for simplifying the process of creating digital images, animations and interactive graphical applications. It is free to download and operates on Mac, Windows, and Linux platforms. I have written a simple application here that receives the 10-bit ADC sample from the serial port, converts it to temperature, and display it on the computer screen. The Processing serial library allows for easily reading and writing data to and from the serial ports.
You should import the Processing Serial library first before accessing the serial port. This can be done by,
import processing.serial.*;
Next, you can open a serial port as
PIC_Board = new Serial(this, "COM6", 9600);
In my case, the USB-UART module appear as COM6, you should find the right COM number to make it work for you, which you can find out from the Device Manager tool in Windows.
Once the two bytes of ADC data are received serially, the actual temperature is retrieved by applying the sensor specific conversion factors. For MCP9701A, the conversion equation would be,
temp = MS_Byte*256 + LS_Byte;
tempC = (2*temp - 400)/19.5; // Factor 2 corresponds to VREF = 2.048 V
tempF = ((tempC*9)/5) + 32;
A clickable Start/Stop button is also provided on the display window. The Processing Mouse functions are used to detect a mouse press over the button. When the Start is pressed, data logging begins and the label on the button turns into ‘Stop’. If Stop is pressed, the data logging is paused. The temperature samples are recorded along with the date and time stamp (from PC) into an ASCII file. Every time the Start is pressed, the program creates a new ASCII log file. The name of the file contains the current system date and time so that there won’t be any overwriting of files. However, the data are temporarily stored into the PC’s RAM and are transferred to the ASCII file on the hard drive only after pressing the Stop button.
The Processing source code and exported applications can be downloaded from the following link.
Download the Processing source code