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

📄 lcd1602_4.c

📁 LCD1602的4位和8位并行传输程序
💻 C
字号:
/*==============================================================
2007-8-9
SMC1602A(16*2)模拟口线接线方式
采用四线数据线
连接线图如下:
---------------------------------------------------
|LCD-----51   | LCD------51 |
---------------------------------------------------
|DB4-----P0.4 | RS-------P0.0 |
|DB5-----P0.5 | RW-------P0.1 |
|DB6-----P0.6 | EN-------P0.2 |
|DB7-----P0.7 | VLCD接20K变阻器到GND|
---------------------------------------------------

[注:AT89S51使用12M晶振]
=============================================================*/
#include <reg51.h>
#include <string.h>
//#include <intrins.h>     //_nop_;
//#include<absacc.h>
//#include<math.h>


//-------------------宏定义数据类型-------------------------//

#define uchar   unsigned char
#define uint    unsigned int
#define ulong   unsigned long

//----------------------引脚定义----------------------------//

#define  LCD_Data  P0

sbit LCD1602_RS = P0^0;
sbit LCD1602_RW = P0^1;
sbit LCD1602_EN = P0^2;

//--------------------全局变量定义--------------------------//

uchar code ASCII[]={"0123456789ABCDEFn"};

//----------------------延时t ms----------------------------//
void delayt_ms(uint t)
{
    uint i;
    while(t--)
    {
        for (i=0;i<125;i++);  	// 对于12M时钟,约延时1ms
    }
}

//-----------------------延时n us--------------------------//
void delayn_us(uint n)
{
    uint i;
    for(i=0;i<n;i++)
    {
        ;//_nop_();              	// 对于12M时钟,约延时1us
    }
}


//--------------------读指令-------------------------//
void LCD_en_command(uchar command)
{

    LCD1602_RS=0;
    LCD1602_RW=0;
    LCD1602_EN=0;

    LCD_Data |=(command & 0xf0);
    LCD_Data &=(command & 0xf0) | 0x0f;

    LCD1602_EN=1;
    delayn_us(10);
    LCD1602_EN=0;

    LCD_Data |=(command & 0x0f)<<4;
    LCD_Data &=(command & 0x0f)<<4 | 0x0f;

    LCD1602_EN=1;
    delayn_us(10);
    LCD1602_EN=0;
}


//--------------------读数据---------------------------//
void LCD_en_dat(uchar dat)
{

    LCD1602_RS=1;
    LCD1602_RW=0;
    LCD1602_EN=0;

    LCD_Data |=(dat & 0xf0);
    LCD_Data &=(dat & 0xf0) | 0x0f;

    LCD1602_EN=1;
    delayn_us(10);
    LCD1602_EN=0;

    LCD_Data |=(dat & 0x0f)<<4;
    LCD_Data &=(dat & 0x0f)<<4 | 0x0f;

    LCD1602_EN=1;
    delayn_us(10);
    LCD1602_EN=0;
}


//-----------------------设置显示的起始坐标---------------//
void LCD_set_xy( uchar x, uchar y )
{
    unsigned char address;
    if (y ==0)
        address = 0x80 + x;
    else
        address = 0xC0 + x;
    LCD_en_command(address);
}



//---------------------------显示单个字符------------------//
void LCD_write_char( uchar x,uchar y,uchar dat)
{
    LCD_set_xy( x, y );
    LCD_en_dat(dat);
}



//-------------------显示一串字符----------------------//
void LCD_write_string(uchar X,uchar Y,uchar *s)
{
    LCD_set_xy( X, Y );             //set address
    while (*s)                      // write character
    {
      LCD_en_dat(*s);
      s ++;
    }
}
//----------------------LCD初始化--------------------//
void LCD_init(void)
{
    LCD_en_command(0x01);
    delayt_ms(10);
    LCD_en_command(0x01);
    delayt_ms(10);
    LCD_en_command(0x28);
    delayt_ms(10);
    LCD_en_command(0x28);
    delayt_ms(10);
    LCD_en_command(0x28);
    delayt_ms(10);
    LCD_en_command(0x0C);
    delayt_ms(10);
    LCD_en_command(0x06);
    delayt_ms(10);
    LCD_en_command(0x01);
    delayt_ms(10);
}

/*调用参考****
*******************************************************************************************
*****************************************************************************************/
void main(void)
{
          delayt_ms(40);
          //interruptInit();
	  LCD_init(); //LCM初始化
           //LCD_write_string(0,0 ,"       ");
          //Delayms();
          /*delayt_ms(500);
	  LCD_write_string(0,0 ,"waiting      ");
	  delayt_ms(500);
          LCD_write_string(0,0 ,"       ");
	  delayt_ms(500);
          LCD_write_string(0,0 ,"welcoming      ");
	  delayt_ms(500);
          LCD_write_string(0,0 ,"               ");
	  delayt_ms(500);
          LCD_init(); //LCM初始化   */
	  //while(1)
	  //{
	         //LCD_write_string(0,1 ,"Motor      ");
                 delayt_ms(40);
                 //LCD_write_string(0,1,"zhao  sdfjj");
                 LCD_write_string(0,1,ASCII);
                 LCD_write_char(10,0,ASCII[0]);
	   //}
           while(1);

}

⌨️ 快捷键说明

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