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

📄 lcd.c

📁 扫描键盘加LCD显示
💻 C
📖 第 1 页 / 共 3 页
字号:


#include <reg51.h>
#include <intrins.h>
#include <stdio.h>


#define uchar unsigned char 
#define uint unsigned int


////////////////////////////////////////////////////
// ASCII characters 宋体 12 (8*16)
uchar code ascii[96][16];


//----------------------------------------------
// Variables
//----------------------------------------------
/*
//dip-switch
sbit cs1 = P1^3;
sbit cs2 = P1^4;
sbit di = P1^0;
sbit rw = P1^1;
sbit en = P1^2;
*/

//keyboard
sbit cs1 = P1^4;   // High active
sbit cs2 = P1^3;
sbit di = P1^7;
sbit rw = P1^6;
sbit en = P1^5;

sfr lcddata=0xA0;  // P2
uchar addr[] = {0,0};    // x,y address x:0~7;y:0~127

sbit flag = P3^7;

//-----------------------------------
// Status regester
#define Busy 0x80
#define OnOff 0x20
#define Reset 0x10

//-----------------------------------
// Command word
#define	 DISPON		0x3f
#define	 DISPOFF	0x3e
#define	 DISPFIRST	0xc0

//----------------------------------------------
// Functions
//----------------------------------------------
void InitLCD(void);                     // Initialize the system

uchar read(bit m);                      // Read a data from LCD
                                        // m=0 indicate instruction, m=1 indicate data
void write(uchar c,bit m);              // Write a data to LCD
                                        // m=0 indicate instruction, m=1 indicate data
void set_addr(uchar x,uchar y);         // Set the x and y address. x:(0-7) y:(0-127)

uchar test_status(uchar ref);           // Test the status of LCD
                                        // 0x80-busy:0x20-on/off:0x10-reset
void printc(uchar cha);                 // Display a character on LCD
                                        // Seting the address is necessary before calling.
void prints(uchar *cha);                // Display a string on LCD. The lenth is less than 18.
                                        // Seting the address is necessary before calling.
uchar select_ic(uchar y);               // If y address is larger than 63 then shift to IC2

extern uchar log2(uchar);

void print(void);

void clear(void);

void init(void);

extern uchar scan(void);


//----------------------------
// Clear the screen
void clear(void)
{
 for (addr[0]=0;addr[0]<8;addr[0]++)
   for (addr[1]=0;addr[1]<128;addr[1]++)
     {
      if ((addr[1] == 64) | (addr[1] == 0))
        set_addr(addr[0],addr[1]);
      write(0x00,1);
     }
}

/*------------------------------------------------
// Caculates the 2 logarithm for
// the unsigned char number "Unm"
// Note: the return value is the reality log2(n)+1
unsigned char log2(unsigned char Power)
{
 unsigned char Index=0;
 while (Power)
   {
    Power >>= 1;
    Index++;
   }
 return (Index-1);
}*/


//----------------------------------------------
// Display a string on LCD
void prints(uchar *cha)
{
 while (*cha != '\0')
   {
//    if (*cha == '\n')// Test for CR
//      { 
//       set_addr(addr[0]+2,0) // Call process of control character
//      }
    printc(*cha);
    cha++;
   };
}


//----------------------------------------------
// Display a character on LCD
void printc(uchar cha)
{
 uchar i,cursor = addr[1]; 

 // Caculate the first address of font
 cha -= 0x20;

 // Output upper page
 for (i=0;i<8;i++)
   {
    if (cursor == 64)
      set_addr(addr[0],cursor);
    write (ascii[cha][i*2],1);
      cursor++;
   }; 


 cursor = addr[1];
 addr[0] += 1;
 set_addr(addr[0],addr[1]);

 // Output lower page
 for (i=0;i<8;i++)
   {
    if (cursor == 64)
      set_addr(addr[0],cursor);
    write (ascii[cha][i*2+1],1);
    cursor++;
   };
 

 addr[0] -= 1;                           // Initialize the x to the up page
 addr[1] += 8;
 if (addr[1] > 120)
   {
    addr[0] += 2;                      // Next line
    addr[1] -= 120;
   };
 set_addr(addr[0],addr[1]);              // Point next location
}


/*----------------------------------------------
// Test the status of LCD
uchar test_status(uchar ref)
{
 uchar temp = read(0);
 temp &= ref;
 return(temp);
}*/


//----------------------------------------------
// Select ic of display control
uchar select_ic(uchar y)
{
 if (y > 63) 
   {
    // Select IC2
    cs1 = 0;
    cs2 = 1;
    y -= 64;
   }
 else
   {
    // Select IC1
    cs1 = 1;
    cs2 = 0;
   };
 return(y);
}


//----------------------------------------------
// Set the x and y address
void set_addr(uchar x,uchar y)
{
 addr[0] = x;
 addr[1] = y; 
 y = select_ic(y);       // While y>63 shift to IC2 to control the display 
 // Process x address data, 1011 1AAA(binary) indicate x address
 x |= 0xF8;              // 1111 1000
 x &= 0xBF;              // 1011 1111 

 // Process y address data, 01AA AAAA(binary) indicate y address
 y |= 0xC0;              // 1100 0000 
 y &= 0x7F;              // 0111 1111
 
 // Output x address
 write(x,0);
 // Output y address 
 write(y,0);
}

//---------------------------------
// Initialize the system
void init(void)
{ 
 SCON = 0x50;                       // Setup serial port control register
                                    // Mode 1: 8-bit uart var. baud rate
                                    // REN: enable receiver 

 PCON &= 0x7F;                      // Clear SMOD bit in power ctrl reg
                                    // This bit doubles the baud rate
}

//--------------------------
// InitLCD
void InitLCD(void)
{
 uint TimeOut = 0;
 while (TimeOut < 20000)
   TimeOut++;

 cs1 = 1;
 cs2 = 0;
 write(DISPOFF,0);
 cs1 = 0;
 cs2 = 1;
 write(DISPOFF,0);
 
 cs1 = 1;
 cs2 = 0;
 write(DISPON,0);
 cs1 = 0;
 cs2 = 1;
 write(DISPON,0);
 
 cs1 = 1;
 cs2 = 0;
 write(DISPFIRST,0);
 cs1 = 0;
 cs2 = 1;
 write(DISPFIRST,0);
 
 clear();

 TimeOut = 0;
 while (TimeOut < 20000)
   TimeOut++;
 set_addr(0,0);
}

//---------------------------
// Write data
void write(uchar cha,bit m)
{
 while (read(0) & Busy);
 di = m;
 rw = 0;
 lcddata = cha;
 en = 1;
 _nop_();
 en = 0;
}

//---------------------------
// Read data
uchar read(bit m)
{
 uchar rcha;
 di = m;
 rw = 1;
 lcddata = 0xff;
 en = 1;
 _nop_();
 rcha = lcddata;
 en = 0;
 return rcha;
}


//---------------------------------------------
void main(void)
{
 uchar disp[64];
 uint keypress;      
 // Code of constructing the string which to be displayed on LCD
 init();
 InitLCD();
 while (1)
   {
    keypress = scan();
    sprintf(disp,"%d",keypress);// Constructe the string 
                         // which will be displayed later.
    clear();
    set_addr(5,0);
    prints(disp);    // Display the string on screen
   }
}


/**************************************************/
/*----点阵转换时间:2004/11/20   7:56:23----*/
uchar code ascii[96][16] =
/*---8x16--*/
{
/*---   ---*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- ! ---*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x33,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- " ---*/
{0x00,0x00,0x10,0x00,0x0C,0x00,0x06,0x00,0x10,0x00,0x0C,0x00,0x06,0x00,0x00,0x00},


/*--- # ---*/
{0x40,0x04,0xC0,0x3F,0x78,0x04,0x40,0x04,0xC0,0x3F,0x78,0x04,0x40,0x04,0x00,0x00},


/*--- $ ---*/
{0x00,0x00,0x70,0x18,0x88,0x20,0xFC,0xFF,0x08,0x21,0x30,0x1E,0x00,0x00,0x00,0x00},


/*--- % ---*/
{0xF0,0x00,0x08,0x21,0xF0,0x1C,0x00,0x03,0xE0,0x1E,0x18,0x21,0x00,0x1E,0x00,0x00},


/*--- & ---*/
{0x00,0x1E,0xF0,0x21,0x08,0x23,0x88,0x24,0x70,0x19,0x00,0x27,0x00,0x21,0x00,0x10},


/*--- ' ---*/
{0x10,0x00,0x16,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- ( ---*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x07,0x18,0x18,0x04,0x20,0x02,0x40,0x00,0x00},


/*--- ) ---*/
{0x00,0x00,0x02,0x40,0x04,0x20,0x18,0x18,0xE0,0x07,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- * ---*/
{0x40,0x02,0x40,0x02,0x80,0x01,0xF0,0x0F,0x80,0x01,0x40,0x02,0x40,0x02,0x00,0x00},


/*--- + ---*/
{0x00,0x01,0x00,0x01,0x00,0x01,0xF0,0x1F,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00},


/*--- , ---*/
{0x00,0x80,0x00,0xB0,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- - ---*/
{0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},


/*--- . ---*/
{0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},


/*--- / ---*/
{0x00,0x00,0x00,0x60,0x00,0x18,0x00,0x06,0x80,0x01,0x60,0x00,0x18,0x00,0x04,0x00},

⌨️ 快捷键说明

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