PROJECT 8.3—USB-Based Ambient Pressure Display on the PC
In this project, an ambient atmospheric pressure sensor is connected to a PIC18F4550 microcontroller, and the measured pressure is sent and displayed on a PC every second using a USB link.
An MPX4115A-type pressure sensor is used in this project. This sensor generates an analog voltage proportional to the ambient pressure. The device is available in either a 6-pin or an 8-pin package.
The pin configuration of a 6-pin sensor is:
Figure 8.35 shows the variation of the output voltage of MPX4115A sensor as the pressure varies. We are interested in the range of pressure between 800 and 1100 millibars.
The circuit diagram of the project is shown in Figure 8.37. The sensor output is connected to analog input AN0 of the microcontroller. As in Project 8.2, the USB connector is connected to port pins RC4 and RC5 and the microcontroller is operated from an 8MHz crystal.
The program on the PC is based on Visual Basic, as in the previous projects. A single form is used, as shown in Figure 8.38, to display the pressure in millibars every second.
The microcontroller program listing (named PRESSURE.C) of the project is given in Figure 8.39. At the beginning of the main program the PORTA pins are defined as analog inputs by clearing ADCON1 to 0 and setting port pins as inputs. Then the interrupt registers are set to their default power-on values. Timer interrupt TMR0 is set
to generate an interrupt every 3.3ms to keep the USB bus alive. The USB port of the microcontroller is then enabled, and ADCON2 is initialized by setting the A/D clock frequency to Fosc/64.
An endless loop is formed using a for statement. Inside this loop the pressure sensor data is read into variable Vin and then converted into physical voltage in millivolts and stored in variable mV. The atmospheric pressure is then calculated using Equation (8.4) and stored in variable Pint as a long integer. The mikroC function LongToStr converts this integer into a string in array op. Any leading spaces are removed from this array, and the resulting pressure is stored in a character array called Pressure. The mikroC USB function Hid_Write is then called to send the pressure data to the USB bus as 4- character data. The program then waits for one second, and the above process is repeated forever.
An 8MHz crystal is used to provide clock pulses to the microcontroller. The microcontroller CPU clock and the USB module are operated at 48MHz, and
the clock and configuration register settings are as in the other projects in this chapter.
The PC program, based on Visual Basic, is called PRESSURE. Subroutine OnRead receives the data arriving at the USB port of the PC and then displays it on the screen form. The program does not send any data to the USB bus. The program listing (except the global variable declarations) is given in Figure 8.40.
Figure 8.41 shows a typical output from the Visual Basic program, displaying the atmospheric pressure.
An installable version of the Visual Basic program is provided on the CDROM that comes with this book, in folder PRESSURE.