The mikroC compiler assumes by default that the LCD is connected to the microcontroller as follows:
LCD Microcontroller port
D7 Bit 7 of the port
D6 Bit 6 of the port
D5 Bit 5 of the port
D4 Bit 4 of the port
E Bit 3 of the port
RS Bit 2 of the port
where port is the port name specified using the Lcd_Init statement.
For example, we can use the statement Lcd_Init(&PORTB) if the LCD is connected to PORTB with the default connection.
It is also possible to connect the LCD differently, using the command Lcd_Config to define the connection.
Figure 6.38 shows the block diagram of the project. The microcontroller reads the analog voltage, converts it to digital, formats it, and then displays it on the LCD.
Figure 6.38: Block diagram of the project
The circuit diagram of the project is shown in Figure 6.39. The voltage to be measured (between 0 and 5V) is applied to port AN0 where this port is configured as an analog input in software. The LCD is connected to PORTC of the microcontroller as in the default four-wire connection. A potentiometer is used to adjust the contrast of the LCD display.
Figure 6.39: Circuit diagram of the project
The PDL of the project is shown in Figure 6.40. At the beginning of the program PORTC is configured as output and PORTA is configured as input. Then the LCD and the A/D converter are configured. The program then enters an endless loop where analog input voltage is converted to digital and displayed on the LCD. The process is repeated every second.
START
Configure PORTC as outputs
Configure PORTA as input
Configure the LCD
Configure the A/D converter
DO FOREVER
Read analog data (voltage) from channel 0
Format the data
Display the data (voltage)
Wait one second
ENDDO
END
Figure 6.40: PDL of the project
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
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.
Читать дальше