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

📄 1602b_lcd.c

📁 本设计以ATMEL公司生产的AT90S8515作为控制核心
💻 C
字号:
// 1602B_LCD.c
//

/**********************************************************
/* TC1602B LCD DISPLAY
/* LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
                   入命令
/* LCD第一行显示寄存器地址:0X80-0X8F
/* LCD第二行显示寄存器地址:0XC0-0XCF
/* 光标命令:
/* LCD_write_char(0x0e,0);    //光标开
/* LCD_write_char(0x0d,0);    //光标所在字符闪烁  
/* LCD_write_char(0x0c,0);    //光标关  
**********************************************************/
#include <io8515v.h>
#include <macros.h>
#include "1602LCD.h"

void LCD_init(void) 
{ 
  LCD_CTRL_DDR|=LCD_RS | LCD_EN | LCD_RW;
  LCD_DATA_DDR|=LCD_DATA;
  delay_nms(100); 
  LCD_write_char(0x28,0); 					    //功能设置    
  delay_nus(100); 
  LCD_write_char(0x06,0);      					//显示光标移动设置
  delay_nus(100);
  LCD_write_char(0x01,0);      					//显示清屏
  delay_nms(2);
  LCD_write_char(0x0C,0); 						//显示开 
  delay_nus(100);
}
 
void LCD_en_write(void)        //EN端产生一个高电平脉冲,写LCD
  {
    LCD_EN_PORT |= LCD_EN;
    delay_nus(5);
    LCD_EN_PORT &= ~LCD_EN;
  }

/****************************************************************
/* LCD_write_char    : 英文字符串显示函数
/* 输入参数:*s      :英文字符串指针?
             X、Y    : 显示字符串的位置,X:0-15,Y:0-1
                     LCD第一行显示寄存器地址:0X80-0X8F
                     LCD第一行显示寄存器地址:0XC0-0XCF 		
*****************************************************************/
void LCD_write_char(unsigned command,unsigned data)
  {
    unsigned command_temp,data_temp;        
    
	command_temp = command;
    data_temp = data;
    LCD_wait_Ready();
    LCD_RW_PORT &= ~LCD_RW;             //RW=0
    if (command == 0)
      {
   	 LCD_RS_PORT |= LCD_RS;             //RS=1 
   	 LCD_DATA_PORT &= 0X0F;
	 LCD_DATA_PORT |= data_temp&0xf0;   //send high 4bit
      }
    else 
      {
	 LCD_RS_PORT &= ~LCD_RS;            //RS=0
	 LCD_DATA_PORT &= 0X0F;
     LCD_DATA_PORT |= command_temp&0xf0;//send high 4bit
      }
    LCD_en_write();
    command_temp=command_temp << 4;     //send low 4bit
    data_temp=data_temp       << 4;
    LCD_DATA_PORT &= 0X0F;
    if (command==0)
      LCD_DATA_PORT |= data_temp&0xf0;
    else
      LCD_DATA_PORT |= command_temp&0xf0;
    LCD_en_write();
    LCD_RW_PORT |= LCD_RW;
    LCD_RS_PORT ^= LCD_RS;    
  }

void LCD_wait_Ready(void)                  //等待LCD空闲
  {
    LCD_DATA_DDR &= ~0x80;                 //PB7 I/O口方向设置为输入
    LCD_RW_PORT |= LCD_RW;                 //RW=1
    LCD_RS_PORT &= ~LCD_RS;                //RS=0
    LCD_EN_PORT |= LCD_EN;                 //EN=1
    while (!( LCD_DATA_PIN&0x80 ) == 0);   //RW=1,读PD7,为0表示空闲;
    LCD_EN_PORT &= ~LCD_EN;                //EN=0
    LCD_DATA_DDR |= 0xf0;
  }

/**********************************************************
/* LCD_set_xy        : 设置LCD显示的起始位置
/* 输入参数:x、y    : 显示字符串的位置,X:0-15,Y:0-1
                     LCD第一行显示寄存器地址:0X80-0X8F
                     LCD第一行显示寄存器地址:0XC0-0XCF
***********************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
  {
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else 
       address = 0xC0 + x;
    LCD_write_char( address, 0 );
  }
/**********************************************************
/* LCD_write_string  : 英文字符串显示函数
/* 输入参数:*s      :英文字符串指针;
/*           X、Y    : 显示字符串的位置
***********************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
  {
    LCD_set_xy( X, Y );
    while (*s) 
      {
        LCD_write_char( 0, *s );
	    s ++;
      }
  }
/**********************************************************
/* LCD_write         : 英文字符显示函数
/* 输入参数:data    :英文字符
/*           X、Y    : 显示字符的位置
***********************************************************/  
void LCD_write(unsigned char X, unsigned char Y,unsigned data) 
  {
   LCD_set_xy( X, Y );
   LCD_write_char(0, data);
  } 
/**********************************************************
/* LCD_clr           : 清屏函数
***********************************************************/    
void LCD_clr(void) 
  {
   LCD_write_char(0x01,0);
   delay_nms(2);
  } 
/**********************************************************
/* LCD_display
/* 在LCD具体位置开始,显示一段长度不超过5的数值,如54321
/* x——列值
/* y——行值
/* data_length——数值长度<11
/* data ——数值内容
***********************************************************/
void LCD_display(unsigned char x,unsigned char y,unsigned char data_length,unsigned long int data)
  {
   unsigned char i,temp;
   for(i=0;i<data_length;i++)
     {
      temp=data%10;
      LCD_write(data_length+x-i-1,y,'0'+temp);
      data=data/10; 
     }
  }

⌨️ 快捷键说明

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