Книга: Advanced PIC Microcontroller Projects in C

A More Accurate Display

A More Accurate Display

The voltage displayed in Figure 6.41 is not very accurate, since integer arithmetic has been performed in the calculation and the voltage is calculated by multiplying the A/D output by 5000 and then dividing the result by 1024 using integer division. Although the multiplication is accurate, the accuracy of the measurement is lost when the number is divided by 1024. A more accurate result can be obtained by scaling the number before it is displayed, as follows.

First, multiply the number Vin by a factor to remove the integer division. For example, since 5000/1024 = 4.88, we can multiply Vin by 488. For the display, we can calculate the integer part of the result by dividing the number into 100, and then the fractional part can be calculated as the remainder. The integer part and the fractional part can be displayed with a decimal point in between. This technique has been implemented in program SEVEN7.C as shown in Figure 6.42. In this program variables Vdec and Vfrac store the integer and the fractional parts of the number respectively. The decimal part is then converted into a string using function LongToStr and leading blanks are removed. The parts of the fractional number are called ch1 and ch2. These are converted into characters by adding 48 (i.e., character “0”) and then displayed at the next cursor positions using the LCD command Lcd_Chr_Cp.

/**************************************************************
                  VOLTMETER WITH LCD DISPLAY
                 ============================
In this project an LCD is connected to PORTC. Also, input port
AN0 is used as analog input. Voltage to be measured is applied
to AN0. The microcontroller reads the analog voltage, converts
into digital, and then displays on the LCD.
Analog input range is 0 to 5V. A PIC18F452 type microcontroller
is used in this project, operated with a 4MHz resonator.
Analog data is read using the Adc_Read built-in function. This
function uses the internal RC clock for A/D timing.
The LCD is connected to the microcontroller as follows:
Microcontroller LCD
     RC7        D7
     RC6        D6
     RC5        D5
     RC4        D4
     RC3        Enable
     RC2        RS
This program displays more accurate results than program SEVEN6.C.
The voltage is displayed as follows:
     mV = nnnn.mm
Author: Dogan Ibrahim
Date:   July 2007
File:   SEVEN7.C
**************************************************************/
//
// Start of MAIN Program. Configure LCD and A/D converter
//
void main() {
 unsigned long Vin, mV,Vdec,Vfrac;
 unsigned char op[12];
 unsigned char i,j,lcd[5],ch1,ch2;
 TRISC = 0;    // PORTC are outputs (LCD)
 TRISA = 0xFF; // PORTA  is input
 //
 // Configure LCD
 //
 Lcd_Init(&PORTC); // LCD is connected to PORTC
 Lcd_Cmd(LCD_CLEAR);
 Lcd_Out(1,1, "VOLTMETER");
 Delay_ms(2000);
 //
 // Configure A/D converter. AN0 is used in this project
 //
 ADCON1 = 0x80; // Use AN0 and Vref=+5V
 //
 // Program loop
 //
 for(;;) // Endless loop
 {
  Lcd_Cmd(LCD_CLEAR);
  Vin = Adc_Read(0);     // Read from channel 0 (AN0)
  Lcd_Out(1,1, "mV = "); // Display "mV = "
  Vin = 488*Vin;         // Scale up the result
  Vdec = Vin / 100;      // Decimal part
  Vfrac = Vin % 100;     // Fractional part
  LongToStr(Vdec,op);    // Convert Vdec to string in "op"
  //
  // Remove leading blanks
  //
  j=0;
  for(i=0;i<=11;i++) {
   if (op[i] != ' ') // If a blank
   {
    lcd[j]=op[i];
    j++;
   }
  }
  //
  // Display result on LCD
  //
  Lcd_Out(1,6,lcd);   // Output to LCD
  Lcd_Out_Cp(".");    // Display "."
  ch1 = Vfrac / 10;   // Calculate fractional part
  ch2 = Vfrac % 10;   // Calculate fractional part
  Lcd_Chr_Cp(48+ch1); // Display fractional part
  Lcd_Chr_Cp(48+ch2); // Display fractional part
  Delay_ms(1000);     // Wait 1 second
 }
}


Figure 6.42: A more accurate program

We could also calculate and display more accurate results by using floating point arithmetic, but since this uses huge amounts of memory it should be avoided if possible.

Оглавление книги


Генерация: 1.466. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз