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

The steps to calculate the pressure in millibars are:

• Read the output voltage of the pressure sensor using one of the A/D channels of the microcontroller

• Use Equation (8.4) to convert the voltage into pressure in millibars

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

Figure 8.36: Block diagram of the project

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.

Figure 8.37: Circuit diagram of the project

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.

Figure 8.38: Visual Basic form to display pressure

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.

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

             USB BASED ATMOSPHERIC PRESSURE DISPLAY ON PC

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

In this project a PIC18F4550 type microcontroller is connected

to a PC through the USB link.

In addition, a MPX4115A type pressure sensor IC is connected to analog port

AN0 of the microcontroller. The microcontroller reads the atmospheric

pressure and sends it to the PC every second. The PC displays the pressure

on the screen.

A Visual Basic program runs on the PC which reads the pressure from the USB

port and then displays it on a form.

The microcontroller is operated from a 8MHz crystal, but the CPU clock

frequency is increased to 48MHz. Also, the USB module operates with 48MHz.

The pressure is sent to the PC in millibars as a 4 digit integer number.

Author: Dogan Ibrahim

Date:   September 2007

File:   PRESSURE.C

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

#include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HIDlibrary\USBdsc.c"

unsigned char num,i,j;

unsigned long Vin, Pint;

unsigned char op[12], Pressure[4], Read_buffer[4];

float mV,V,Pmb;

//

// Timer interrupt service routine

//

void interrupt() {

 HID_InterruptProc(); // Keep alive

 TMR0L = 100;         // Reload TMR0L

 INTCON.TMR0IF = 0;   // Re-enable TMR0 interrupts

}

//

// Start of MAIN program

//

void main() {

 ADCON1 = 0;   // Set inputs as analog, Ref=+5V

 TRISA = 0xFF; // Set PORT A as inputs

 //

 // Set interrupt registers to power-on defaults

 // Disable all interrupts

 //

 INTCON=0;

 INTCON2=0xF5;

 INTCON3=0xC0;

 RCON.IPEN=0;

 PIE1=0;

 PIE2=0;

 PIR1=0;

 PIR2=0;

 //

 // Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256

 // and load TMR0L to 156 so that the time interval for timer

 // interrupts at 48MHz is 256*156*0.083 = 3.3ms

 //

 // The timer is in 8-bit mode by default

 //

 T0CON = 0x47;      // Prescaler = 256

 TMR0L = 100;       // Timer count is 256-156 = 100

 INTCON.TMR0IE = 1; // Enable T0IE

 T0CON.TMR0ON = 1;  // Turn Timer 0 ON

 INTCON = 0xE0;     // Enable interrupts

 //

 // Enable USB port

 //

 Hid_Enable(&Read_buffer, &Pressure);

 Delay_ms(1000);

 Delay_ms(1000);

 //

 // Configure A/D converter. AN0 is used in this project

 //

 ADCON2 = 0xA6; // A/D clock = Fosc/64, 8TAD

 //

 // Endless loop. Read pressure from the A/D converter,

 // convert into millibars and send to the PC over the

 // USB port every second

 //

 for(;;) // do forever

 {

  Vin = Adc_Read(0);            // Read from channel 0 (AN0)

  mV = (Vin * 5000.0) / 1024.0; // In mv=Vin x 5000/1024

  V = mV / 1000.0;              // Pressure in Volts

  Pmb = (2.0*V + 0.95) / 0.009; // Pressure in mb

  Pint = (int)Pmb;              // As an integer number

  LongToStr(Pint,op);           // Convert to string in "op"

  //

  // Remove leading blanks

  //

  for(j=0; j<4; j++) Pressure[j]=' ';

  j=0;

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

   if(op[i] != ' ') // If a blank

   {

    Pressure[j]=op[i];

    j++;

   }

  }

  //

  // Send pressure (in array Pressure) to the PC

  //

  Hid_Write(&Pressure,4); // Send to USB as 4 characters

  Delay_ms(1000);         // Wait 1 second

 }

 Hid_Disable();

}

Figure 8.39: Microcontroller program of the project

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.