Thaimicrotron.com : Home
 
 
Page7
 
 

การใช้งาน LCD โมดูล
ตัวอย่าง การสร้างตัวอักษรลงใน CG-RAM

 
     
  วัตถุประสงค์  
  การสร้างตัวอักษรลงใน CG- RAM  
  การเรียกใช้งานตัวอักษรลงใน CG-RAM  
     
  ดูเพิ่มเติมการใช้งานโมดูล LCD  
 
 
การใช้งาน LCD โมดูลแบบภ 4 bit
 
 
//LCD Custom char Demo
//How to write and read CG-RAM
//#include <16F628.h>
#include <16F648A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP

//LCD Connection Pin
//E(6)         RB0
//RS(4)        RB1
//R/W(5)       GND
//D0-D7(7-14)  RB0-RB7

//Define LCD I/O PIN
//LCD Control
#define  LCD_E              PIN_B0
#define  LCD_RS             PIN_B1
//LCD Data
#define  LCD_D4             PIN_B4
#define  LCD_D5             PIN_B5
#define  LCD_D6             PIN_B6
#define  LCD_D7             PIN_B7

#define  LCD_ROW1           0x00
#define  LCD_ROW2           0x40
#define  LCD_ROW3           0x14
#define  LCD_ROW4           0x54

#define  LCD_EPLUSE_TM  1

//*** START LCD Command ***
#define  LCD_CLEAR_SCREEN   0B00000001
#define  LCD_RETURN_HOME    0B00000010

//Entry mode set (BIT2=1)
#define  LCD_ENTRY_MODE     0B00000100
#define  INCREMENTS_LCD     0B00000010
#define  DECREMENTS_LCD     0B00000000
#define  NO_SCROLL_LCD      0B00000000
#define  SCROLL_LCD         0B00000001

//Display ON/OFF (BIT3=1)
#define  DISPLAY_LCD        0B00001000
#define  LCD_ON             0B00000100
#define  LCD_OFF            0B00000000
#define  LCD_CURSOR_ON      0B00000010
#define  LCD_CURSOR_OFF     0B00000000
#define  LCD_BLINK_ON       0B00000001
#define  LCD_BLINK_OFF      0B00000000

//Scroll Display/Shift Cursor (BIT4=1)
#define  LCD_SCROLL_SHIFT   0B00010000
#define  LCD_SHIFT_CURSOR   0B00000000
#define  LCD_SHIFT_DISPLAY  0B00001000
#define  LCD_SHIFT_LEFT     0B00000000
#define  LCD_SHIFT_RIGHT    0B00000100

//Function set (BIT5=1)
#define  LCD_FUNCTION_SET   0B00100000
#define  LCD4BIT_MODE       0B00000000
#define  LCD8BIT_MODE       0B00010000
#define  LCD_LINE1          0B00000000
#define  LCD_LINE2          0B00001000
#define  LCD_FONT_5X7       0B00000000
#define  LCD_FONT_5X10      0B00000100

//Move To CGRAM Address (BIT6=1)
#define  LCD_CGRAM_ADDRESS  0B01000000

//Move To DDRAM Address (BIT7=1)
#define  LCD_DDRAM_ADDRESS  0B10000000

//*** END LCD Command ***

void InitialChip(void)
{
  setup_comparator(NC_NC_NC_NC);        //Input Digital
  set_tris_b(0B00000000);
   
  output_a(0);
  output_b(0);
}
void LCD_Epluse()
{
  output_bit(LCD_E,1);
  delay_ms(LCD_EPLUSE_TM);
  output_bit(LCD_E,0);
  delay_ms(1);
}

void WriteLCD4Bit(BYTE DAT)
{
  output_bit(LCD_D4,bit_test(DAT,4));
  output_bit(LCD_D5,bit_test(DAT,5));
  output_bit(LCD_D6,bit_test(DAT,6));
  output_bit(LCD_D7,bit_test(DAT,7));
 
  LCD_Epluse();
}
//Write 4 bit Data twice
void WriteLCDData(BYTE DAT)
{
  output_bit(LCD_RS,1);             //Data Mode
  WriteLCD4Bit(DAT);
  swap(DAT);
  WriteLCD4Bit(DAT);
}
//Write 4 bit Command twice
void WriteLCDCmd(BYTE CMD)
{
  output_bit(LCD_RS,0);             //Command Mode 
  WriteLCD4Bit(CMD);
  swap(CMD);
  WriteLCD4Bit(CMD);
}
void InitialLCD(void)
{   
BYTE BITS;

//Prepare LCD Control Logic
  output_bit(LCD_E,0);
  output_bit(LCD_RS,0);             //Command Mode

//Initial LCD send Initial Code 0B00110000 3 time
  WriteLCD4Bit(0B00110000);
  LCD_Epluse();
  WriteLCD4Bit(0B00110000);
  LCD_Epluse();
  WriteLCD4Bit(0B00110000);
  LCD_Epluse();
 //Set as 4 bit
  WriteLCD4Bit(LCD_FUNCTION_SET|LCD4BIT_MODE);
}

//DD-RAM command
void LCDGotoXY(BYTE X,BYTE Y)
{
    X--;                                //Adjust to Count number 1...n
    switch(Y)
    {
      case 1:
           WriteLCDCmd(LCD_ROW1|LCD_DDRAM_ADDRESS+X);         //0x00|0x80
           break;
      case 2:
           WriteLCDCmd(LCD_ROW2|LCD_DDRAM_ADDRESS|+X);     //64|0x80
           break;
    }
}
BYTE const LCD_CUSTOM_CHARS[64] = 
{
  0x1F,0x10,0x17,0x17,0x17,0x17,0x10,0x1F,      //Battery left
  0x1F,0x00,0x1F,0x1F,0x1F,0x1F,0x00,0x1F,      //Battery center
  0x1E,0x02,0x1B,0x19,0x19,0x1B,0x02,0x1E,      //Battery right
  0x00,0x0E,0x11,0x11,0x11,0x0A,0x1B,0x00,      //ohm symbol
  0x02,0x06,0x0E,0x1E,0x0E,0x06,0x02,0x00,      //<
  0x08,0x0C,0x0E,0x0F,0x0E,0x0C,0x08,0x00,      //>
  0x1F,0x11,0x11,0x0A,0x04,0x04,0x04,0x04,      //ant
  0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00       //o
};

main()
{
int i;

    InitialChip();
    delay_ms(200);  
    InitialLCD();
  //Set LCD
    WriteLCDCmd(LCD_FUNCTION_SET|LCD4BIT_MODE|LCD_LINE2|LCD_FONT_5X7);
    WriteLCDCmd(DISPLAY_LCD|LCD_ON|LCD_CURSOR_OFF);
    WriteLCDCmd(LCD_ENTRY_MODE|INCREMENTS_LCD|NO_SCROLL_LCD);
    
   //Write char data in to CG-RAM
    WriteLCDCmd(LCD_CGRAM_ADDRESS);
    for(i=0;i<64;i++) 
      WriteLCDData(LCD_CUSTOM_CHARS[i]);
    
   //Clear screen and move cursor to Line 1 Col1
    WriteLCDCmd(LCD_CLEAR_SCREEN);

   //Wiite Custom char to screen
   //Custom char is address 00h-07h and 08h-0Fh is same
     WriteLCDData(0);
     WriteLCDData(1);
     WriteLCDData(2);
    printf(WriteLCDData," POWER FULL");
    
    LCDGotoXY(1,2);
    printf(WriteLCDData,"TEMP25\x07 10\x03 \x0609");

    while(1);
}
 
     
  ผลการทดลองจะปรากฏดังรูป  
   
     
     
     
     
 
SMITDH EMSOMBAT
 
 
ศมิทธิ์ เอมสมบัติ