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

📄 lcd._h

📁 本程序是基于avr单片机的atmega48的控制16*4点阵型液晶。其中main文件调用LCD.c和LCD.h文件。用户可以直接把工程中的LCD.c和LCD.h文件放到自己的程序目录下即可。
💻 _H
字号:

#include <iom48v.h>
#include <macros.h>

#define LCD_DATA_PORT  PORTB
#define LCD_DATA_DDR   DDRB
#define LCD_EN_PORT  PORTD
#define LCD_RS_PORT  PORTD
#define LCD_EN  0x10
#define LCD_RS  0x08

void LCD_init         (void);
void LCD_en_write     (void);
void LCD_write_char   (unsigned command,unsigned data);
void LCD_set_xy       (int x, int y);
void LCD_write_string (int X,int Y,unsigned char *s);
void LCD_display_char (int X,int Y,unsigned char data);
void delay_nus        (unsigned int n);
void delay_nms        (unsigned int n);


void LCD_init(void)       //液晶初始化
{
 delay_nms(2);
 LCD_write_char(0x38,0);  //8位显示

  delay_nms(6);
 LCD_write_char(0x38,0);  //8位显示

  delay_nms(2);
 LCD_write_char(0x38,0);  //8位显示

 LCD_write_char(0x38,0);  //8位显示
  delay_nms(2);
 LCD_write_char(0x01,0);  //8位显示
  delay_nms(2);
 LCD_write_char(0x08,0);  //8位显示
  delay_nms(1);
 
 LCD_write_char(0x06,0);  //8位显示
 delay_nms(1);
 
 
 LCD_write_char(0x0c,0);  //显示开
 //delay_nms(100);
 LCD_write_char(0x01,0);  //清屏
 //delay_nms(3);
}

void LCD_write_string(int X,int Y,unsigned char *s)
  {
    LCD_set_xy( X, Y ); //写地址
    
    while (*s)  // 写显示字符
      {
        LCD_write_char( 0, *s );
	    s ++;
      }
  }


void LCD_set_xy( int x, int y )  //写地址函数
  {
    
    unsigned char address;
	delay_nus(100);
	address=x&0x0f;
    switch(y)
      {
      	case 0:
			address+= 0x00;          //0x00 - 0x0f
			break;
		case 1:
			address+= 0x40;          //0x40 - 0x4f
			break;
		case 2:
			address+= 0x10;          //0x10 - 0x1f
			break;
		case 3:
			address+= 0x50;          //0x50 - 0x5f
			break;
		default :
			break;
      }
	address=address|0x80;
    LCD_write_char( address, 0 );
	//delay_nms(2);
  }


void LCD_en_write(void)  //液晶使能
{
  LCD_EN_PORT|=LCD_EN;
  delay_nus(1);
  LCD_EN_PORT&=~LCD_EN;
}
void LCD_display_char(int x,int y,unsigned char data)
{
   LCD_set_xy(x,y);  //写地址函数
   LCD_write_char(0,data);
}

void LCD_write_char(unsigned command,unsigned data) // 写数据
{

 delay_nus(16);
 if(command==0)
 {
  LCD_RS_PORT|=LCD_RS;  //RS=1
  LCD_EN_PORT&=~LCD_EN;
  LCD_DATA_PORT=data;
  LCD_EN_PORT|=LCD_EN;
  
  delay_nus(50);
  LCD_EN_PORT&=~LCD_EN;
 }
 else
 {
  LCD_RS_PORT&=~LCD_RS;   //RS=0
   LCD_EN_PORT&=~LCD_EN;
  LCD_DATA_PORT=command;
  LCD_EN_PORT|=LCD_EN;
  
  delay_nus(50);
  LCD_EN_PORT&=~LCD_EN;
  }
}

void port_init(void)
{
 PORTB = 0x1B;
 DDRB  = 0xFF;
 PORTC = 0x03; //m103 output only
 DDRC  = 0x03;
 PORTD = 0x00;
 DDRD  = 0x18;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 port_init();
 //all peripherals are now initialized
}

/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void)                 //1us延时函数
  {
   asm("nop");
  }

void delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1us();
  }
  
void delay_1ms(void)                 //1ms延时函数
  {
   unsigned int i;
   for (i=0;i<114;i++);
  }
  
void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
   

⌨️ 快捷键说明

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