02 我们直接根据LCD demo 这个代码来讲一下LCD的原理以及操作?
//
/**
* Title: LCD DEMO for use in Lab 2 tutorial
* C File lcddemo.c
* Platform: PICmicro PIC16F877A @ 4 Mhz
* Written by: GBM
*
* Date: 01/10/2018
*
* Function: A sample c file to show how to use the LCD display onboard
* the PIC DIP-40 board.
*
*/
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h> // Include Standard I/O header file
#include "ee302lcd.h" // Include LCD header file. This file must be in same
// directory as source main source file.
#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate the delay functions, __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000
#endif
void setup(void); // Declare setup function
void loop(void); // Declare loop function
void data2LCD(void); // Declare data to LCD function
void lcdTitle(void); // Declare title to LCD function
void clear_outString(void); // Declare outString clear function
//Main program
void main(void)
{
setup(); // Call initialisation
lcdTitle(); // Call LCDTitle
//Superloop
for(;;)
{
loop(); // Write data to LCD
}
}
void setup(void)
{
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
TRISB=0x03; // Set PORTB bit 0 and 1 as inputs
}
void loop(void)
{
data2LCD();
}
void data2LCD(void)
{
if (SW1 == CLOSED) // If SW1 closed then
{
Lcd8_Clear(); //clear LCD display
clear_outString(); //clear outString array
sprintf(gOutString,"Int Value1 is"); //define string as "Int Value is"
Lcd8_Write_String(gOutString); //print string to LCD
Lcd8_Set_Cursor(2,1); //select line 2 of LCD
sprintf(gOutString," %d",VALUE1); //define intvalue as a char in outString
Lcd8_Write_String(gOutString); //print string to LCD
}
if (SW2 == CLOSED) // If SW2 closed then
{
// Modulus used to help convert integer to ASCII character
Lcd8_Clear(); //clear LCD display
clear_outString(); //clear outString array
sprintf(gOutString,"Int Value2 is"); //define string as "Int Value is"
Lcd8_Write_String(gOutString); //print string to LCD
Lcd8_Set_Cursor(2,7); //select line 2 of LCD
clear_outString(); //clear outString array
gOutString[0] = (VALUE2/100)%10 + 48; // add 48 (0x30) to remainder to offset to character in Ascii table.
gOutString[1] = (VALUE2/10)%10 + 48;
gOutString[2] = (VALUE2/1)%10 + 48;
Lcd8_Write_String(gOutString);
}
}
void lcdTitle(void)
{
Lcd8_Write_String("LCD demo"); // print "LCD Demo" on line 1 of LCD
Lcd8_Set_Cursor(2,1); // select line 2
Lcd8_Write_String("LCD demo"); // print "LCD Demo" on line 2 of LCD
}
void clear_outString(void)
{
int i;
for (i=0; i<16; i++)
{
gOutString = 0x00;
}
}