⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ks0108_12864lcd.c

📁 ks0108 avr单片机1284显示
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------*-
   
   Source Name    :  KS0108_12864LCD.C (v1.00)  

  ---------------------------------------------------------------------------- 
   COPYRIGHT 
   ---------	

   Created date  :  2008.03.01
   Created By    :  HOCHIEN
   Modified by   :
   Modified Date :
   Version       :  2.0  
   Description   :  Simple KS0108 based 128 * 64 pixels LCD
                    demonstration program. 
					   
   LCD LIBRARY CODE
   
   Designed for SuperLoop operation, 
   in this case for a 64 * 128 pixels matrix LCD display.
   
   * * * This program is from : www.ICEworksop.com * * *

-*----------------------------------------------------------------------------*/

#include "Main.H"
#include "Port.H"

#include "KS0108_12864LCD.H"

// ------ Public variable declarations ---------------------------------------

// See 'FONT6x8.C' for details
extern flash uInt8 FONT6x8[][6];

// See 'Chinese_Text.C' for details
extern flash uInt8 CN_WELCOME[][32];
extern flash uInt8 CN_EMBEDDED[][32];

// See 'Image_Test.C' for details
extern flash uInt8 Image_Test[];

// ------ Private constants --------------------------------------------------

#define DISPLAY_ON   0x3F  // 0011 1111
#define DISPLAY_OFF  0x3E  // 0011 1110
#define DISPLAY_STARTLINE   0xC0  // 1100 0000
#define DISPLAY_PAGE_SET    0xB8  // 1011 1000
#define DISPLAY_COLUMN_SET  0x40  // 0100 0000
#define LCD_BUSY 0x80 

#define INVERSE (1)
#define NORMAL  (0)
#define D_DATA  (1)
#define COMMAND (0)

static flash uInt8 WELCOME[] = "Hello, EmbeddedWorld!";
static flash uInt8 WEBSITE[] = "*www.ICEworkshop.com*";

// ------ Private function prototypes ----------------------------------------

static void Delay_1ms(void);
static void Clear_Screen(uInt8);
static void LCD_Send_Byte(uInt8, uInt8, uInt8);
static void LCD_Set_XY(uInt8, uInt8);
static void LCD_Disp_a_Char(uInt8, uInt8, uInt8, uInt8);
static void LCD_Disp_String(uInt8, uInt8, flash uInt8 *,  uInt8);
static void LCD_Disp_Chinese_String (uInt8, uInt8, uInt8, uInt8, uInt8, uInt8,
                                     flash uInt8 (*)[32], uInt8);
static void LCD_Disp_an_Image(uInt8, uInt8, flash uInt8 *, uInt8, uInt8, uInt8);
static void LCD_Draw_a_Dot(uInt8, uInt8, uInt8);

/*----------------------------------------------------------------------------*-

   Delay_1ms()

   Software delay * approx * 1 millisecond @ 1MHz system clock.

-*----------------------------------------------------------------------------*/
#pragma ctask Delay_1ms
static void Delay_1ms(void)
   {
   uInt8 i;
   for (i=0;i<143;i++);
   }

/*----------------------------------------------------------------------------*-

   KS0108_12864LCD_Init()

   Set up LCD controller KS0108 before use it.

-*----------------------------------------------------------------------------*/
void KS0108_12864LCD_Init(void)
   {
   // Set up the ports, see 'Port.H' for port pins details
   LCD_DATA_PORT_DIR  = OUTPUT; // LED data PORT is an output
   LCD_INSTR_PORT_DIR = OUTPUT; // LED instruction PORT is an output
   LCD_DATA_PORT = 0x00;
   
   // Toggle reset
   LCD_INSTR_PORT |= LCD_RST;
   Delay_1ms();
   
   LCD_INSTR_PORT &= ~LCD_RST;
   Delay_1ms();
   
   LCD_INSTR_PORT |=  LCD_RST;
   Delay_1ms();
   
   // Setup column select and initialize display
   LCD_INSTR_PORT |=  LCD_CS1;
   LCD_INSTR_PORT &= ~LCD_CS2;

   LCD_Send_Byte(DISPLAY_OFF, COMMAND, NORMAL); // Display OFF
   LCD_Send_Byte(DISPLAY_STARTLINE, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_PAGE_SET, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_COLUMN_SET, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_ON, COMMAND, NORMAL); // Display ON
   
   LCD_INSTR_PORT &= ~LCD_CS1;
   LCD_INSTR_PORT |=  LCD_CS2;

   LCD_Send_Byte(DISPLAY_OFF, COMMAND, NORMAL); // Display OFF
   LCD_Send_Byte(DISPLAY_STARTLINE, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_PAGE_SET, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_COLUMN_SET, COMMAND, NORMAL);
   LCD_Send_Byte(DISPLAY_ON, COMMAND, NORMAL); // Display ON
   
   Clear_Screen(NORMAL);
   }
   
/*----------------------------------------------------------------------------*-

   PCD8544_LCD_Update()        

   This function updates data on the LCD panel (if it requires update).

-*----------------------------------------------------------------------------*/
void KS0108_12864LCD_Update(void)
   {
   uInt16 i, j;
   
   LCD_Disp_an_Image(0, 0, Image_Test, 64, 128, NORMAL);
   for(i = 0; i < 1000; i++)
	  for(j = 0; j < 400; j++);

   Clear_Screen(NORMAL);  
   LCD_Disp_String(0, 1, WELCOME, NORMAL); 
   LCD_Disp_String(7, 1, WEBSITE, NORMAL); 
   LCD_Disp_Chinese_String(2, 32, 16, 4, 0, 0, CN_WELCOME, NORMAL);
   LCD_Disp_Chinese_String(4, 24, 16, 5, 0, 0, CN_EMBEDDED, NORMAL);     
   for(i = 0; i < 1000; i++)
	  for(j = 0; j < 400; j++);
	  
   LCD_Disp_an_Image(0, 0, Image_Test, 64, 128, INVERSE);
   for(i = 0; i < 1000; i++)
	  for(j = 0; j < 400; j++);

   Clear_Screen(INVERSE);  
   LCD_Disp_String(0, 1, WELCOME, INVERSE); 
   LCD_Disp_String(7, 1, WEBSITE, INVERSE); 
   LCD_Disp_Chinese_String(2, 32, 16, 4, 0, 0, CN_WELCOME, INVERSE);
   LCD_Disp_Chinese_String(4, 24, 16, 5, 0, 0, CN_EMBEDDED, INVERSE); 
   for(i = 0; i < 1000; i++)
	  for(j = 0; j < 400; j++); 
   }

/*----------------------------------------------------------------------------*-

   Clear_Screen()

   This function clears the LCD screen.

-*----------------------------------------------------------------------------*/    
static void Clear_Screen(uInt8 Vedio_Mode)
   {   
   uInt8 Page, Column;
   
   for (Page = 0; Page < 8; Page++) // Clear left side
      {
      LCD_INSTR_PORT |=  LCD_CS1; // Select left side
      LCD_INSTR_PORT &= ~LCD_CS2;

      LCD_Send_Byte(DISPLAY_PAGE_SET + Page, COMMAND, NORMAL); // Sweep pages
      LCD_Send_Byte(DISPLAY_COLUMN_SET, COMMAND, NORMAL);
	  
      for (Column = 0; Column < 128; Column++)
         { 
		 // Clear right side
         if(Column == 64)
            {
            LCD_INSTR_PORT &= ~LCD_CS1; // Select right side
            LCD_INSTR_PORT |=  LCD_CS2;
			
			// Sweep pages
            LCD_Send_Byte(DISPLAY_PAGE_SET + Page, COMMAND, NORMAL); 
            LCD_Send_Byte(DISPLAY_COLUMN_SET, COMMAND, NORMAL);
            }
		 // For each set of 8 pixels, write 0x00 or 0xFF(depend on Vedio_Mode)
         LCD_Send_Byte(0x00, D_DATA, Vedio_Mode); 
         }
      }
   }

/*----------------------------------------------------------------------------*-

   LCD_Read_Busy()    
 
   Check out if the LCD is in operation or ready for writing data.
			   
-*----------------------------------------------------------------------------*/
static void LCD_Read_Busy(void)
   {
   uInt16 LCD_Timeout; // Software timeout 
   
   LCD_DATA_PORT = 0xFF;  
   LCD_DATA_PORT_DIR = INPUT; // Set data PORT direction 
 
   LCD_INSTR_PORT &= ~LCD_DI; // Set instruction
   LCD_INSTR_PORT |=  LCD_RW; // Set read

   LCD_INSTR_PORT |=  LCD_EN;
   asm("nop");	
   asm("nop");
    
   LCD_INSTR_PORT &= ~LCD_EN; // Toggle enable
   asm("nop");	
   asm("nop");
   
   // See if busy with software timeout       
   while((LCD_BUSY & LCD_DATA_PORT_VL) && ++LCD_Timeout)  
      {
      LCD_INSTR_PORT &= ~LCD_EN; // Toggle enable   		
      asm("nop");	
	  asm("nop");
	  
      LCD_INSTR_PORT |=  LCD_EN;
      asm("nop");	
	  asm("nop");
      }
   LCD_DATA_PORT_DIR = OUTPUT; // Set data PORT direction back to an output
   LCD_INSTR_PORT &= ~LCD_EN;
   }     
   
/*----------------------------------------------------------------------------*-

   LCD_Send_Byte()

   This function writes a byte to the LCD panel.

   Parameters: 
   ---------------------------------------------------------------------------
   Data      : The byte to be written to the LCD
    
   DATA_FLAG : If DATA_FLAG == 1, a data byte is sent
               If DATA_FLAG == 0, a command byte is sent
			  
   IVERSE    : If IVERSE == 1, inverse video mode			   
               If IVERSE == 0, normal video mode
			  
-*----------------------------------------------------------------------------*/
static void LCD_Send_Byte(uInt8 DATA, uInt8 DATA_FLAG, uInt8 IVERSE)
   { 
   // LCD_Read_Busy();
     
   LCD_INSTR_PORT |=  LCD_EN;
   asm("nop");	
   asm("nop");
   
   if (DATA_FLAG)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -