Êíèãà: Advanced PIC Microcontroller Projects in C

Project Program

Project Program

The program is called SEVEN6.C, and the program listing is given in Figure 6.41. At the beginning of the program PORTC is defined as output and PORTA as input. Then the LCD is configured and the text “VOLTMETER” is displayed on the LCD for two seconds. The A/D is then configured by setting register ADCON1 to 0x80 so the A/D result is right-justified, Vref voltage is set to VDD (+5V), and all PORTA pins are configured as analog inputs.

/**************************************************************
                  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
Author: Dogan Ibrahim
Date:   July 2007
File:   SEVEN6.C
**************************************************************/
//
// Start of MAIN Program. Configure LCD and A/D converter
//
void main() {
 unsigned long Vin, mV;
 unsigned char op[12];
 unsigned char i,j,lcd[5];
 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 = "
  mV = (Vin * 5000) >> 10; // mv = Vin x 5000 / 1024
  LongToStr(mV,op);        // Convert 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
  Delay_ms(1000);          // Wait 1 second
 }
}


Figure 6.41: Program listing

The main program loop starts with a for statement. Inside this loop the LCD is cleared, and analog data is read from channel 0 (pin AN0) using the statement Adc_Read(0). The converted digital data is stored in variable Vin which is declared as an unsigned long. The A/D converter is 10-bits wide and thus there are 1024 steps (0 to 1023) corresponding to the reference voltage of 5000mV. Each step corresponds to 5000mV/1024=4.88mV. Inside the loop, variable Vin is converted into millivolts by multiplying by 5000 and dividing into 1024. The division is done by shifting right by 10 digits. At this point variable mV contains the converted data in millivolts.

Function LongToStr is called to convert mV into a string in character array op. LongToStr converts a long variable into a string having a fixed width of eleven characters. If the resulting string is fewer than eleven characters, the left column of the data is filled with space characters.

The leading blanks are then removed and the data is stored in a variable called lcd. Function Lcd_Out is called to display the data on the LCD starting from column 5 of row 1. For example, if the measured voltage is 1267mV, it is displayed on the LCD as:

mV = 1267

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.622. Çàïðîñîâ Ê ÁÄ/Cache: 2 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç