AVR单片机LCD1602驱动程序

http://tech.ddvip.com   2008年10月10日    社区交流

AVR单片机LCD1602驱动程序。

  //编译器:ICC-AVR v6.31A 日期: 2005-11-24 20:29:57 
  
  //目标芯片 : M16 
  
  //时钟: 8.0000Mhz 
  
  /*------------------------------------------------------------- 
  
  LCD引脚定义 
  
  1---GND 
  
  2---VCC 
  
  3---VO 
  
  4---RS 
  
  5---RW 
  
  6---EN 
  
  7到14--D0-D7 
  
  15--背景灯+ 
  
  16--背景灯- 
  
  -----------------------------------------------------------------*/ 
  
  #include <iom16v.h> 
  
  #include <macros.h> 
  
  /*--------------------------------------------------------------- 
  
  下面是AVR与LCD连接信息 
  
  PA2 ->RS 
  
  PA3 ->EN 
  
  地 ->RW 
  
  PA4 ->D4 
  
  PA5 ->D5 
  
  PA6 ->D6 
  
  PA7 ->D7 
  
  要使用本驱动,改变下面配置信息即可 
  
  -----------------------------------------------------------------*/ 
  
  #define LCD_EN_PORT  PORTA  //以下2个要设为同一个口 
  
  #define LCD_EN_DDR   DDRA 
  
  #define LCD_RS_PORT  PORTA  //以下2个要设为同一个口 
  
  #define LCD_RS_DDR   DDRA 
  
  #define LCD_DATA_PORT PORTA  //以下3个要设为同一个口 
  
  #define LCD_DATA_DDR  DDRA  //一定要用高4位 
  
  #define LCD_DATA_PIN  PINA 
  
  #define LCD_RS     (1<<PA2) //0x04  portA2    out 
  
  #define LCD_EN     (1<<PA3) //0x08  portA3    out 
  
  #define LCD_DATA    ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7)) //0xf0  portA4/5/6/7 out 
  
  /*------------------------------------------------------------------------------ 
  
  函数说明 
  
  ------------------------------------------------------------------------------*/ 
  
  void LCD_init(void); 
  
  void LCD_en_write(void); 
  
  void LCD_write_command(unsigned char command) ; 
  
  void LCD_write_data(unsigned char data); 
  
  void LCD_set_xy (unsigned char x, unsigned char y); 
  
  void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s); 
  
  void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data); 
  
  void delay_nus(unsigned int n); 
  
  void delay_nms(unsigned int n); 
  
  ============================================================================== 
  
  #include "LCD.h" 
  
  void LCD_init(void)     //液晶初始化 
  
{ 
 LCD_DATA_DDR|=LCD_DATA;  //数据口方向为输出 
 LCD_EN_DDR|=LCD_EN;    //设置EN方向为输出 
 LCD_RS_DDR|=LCD_RS;    //设置RS方向为输出 
 LCD_write_command(0x28); 
 LCD_en_write(); 
 delay_nus(40); 
 LCD_write_command(0x28); //4位显示 
 LCD_write_command(0x0c); //显示开 
 LCD_write_command(0x01); //清屏 
 delay_nms(2); 
}
  
  void LCD_en_write(void) //液晶使能 
  
{ 
 LCD_EN_PORT|=LCD_EN; 
 delay_nus(1); 
 LCD_EN_PORT&=~LCD_EN; 
}
  
  void LCD_write_command(unsigned char command) //写指令 
  
{ 
 delay_nus(16); 
 LCD_RS_PORT&=~LCD_RS;    //RS=0 
 LCD_DATA_PORT&=0X0f;     //清高四位 
 LCD_DATA_PORT|=command&0xf0; //写高四位 
 LCD_en_write(); 
 command=command<<4;     //低四位移到高四位 
 LCD_DATA_PORT&=0x0f;     //清高四位 
 LCD_DATA_PORT|=command&0xf0; //写低四位 
 LCD_en_write(); 
  
}
  
  void LCD_write_data(unsigned char data) //写数据 
  
{ 
 delay_nus(16); 
 LCD_RS_PORT|=LCD_RS;    //RS=1 
 LCD_DATA_PORT&=0X0f;    //清高四位 
 LCD_DATA_PORT|=data&0xf0; //写高四位 
 LCD_en_write(); 
 data=data<<4;        //低四位移到高四位 
 LCD_DATA_PORT&=0X0f;    //清高四位 
 LCD_DATA_PORT|=data&0xf0;  //写低四位 
 LCD_en_write(); 
}
  
  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_command( address); 
}
  
  void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1 
  
{ 
  LCD_set_xy( X, Y ); //写地址   
  while (*s) // 写显示字符 
  { 
   LCD_write_data( *s ); 
   s ++; 
  } 
    
} 
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1 
{ 
 LCD_set_xy( X, Y ); //写地址 
 LCD_write_data( data); 
  
}
======================================================================= 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<1140;i++); 
 } 
  
void delay_nms(unsigned int n)    //N ms延时函数 
 { 
  unsigned int i=0; 
  for (i=0;i<n;i++) 
  delay_1ms(); 
 }
  
  ========================================================================= 
  
  #include "LCD.h" 
  
void init_devices(void) 
{ 
 CLI(); //disable all interrupts 
 LCD_init(); 
 MCUCR = 0x00; 
 GICR = 0x00; 
 TIMSK = 0x00; //timer interrupt sources 
 SEI(); //re-enable interrupts 
} 
void main(void) 
{ 
  init_devices(); 
  LCD_write_string(1,0,"Hi!!"); 
  for(;;) 
  { 
  LCD_write_string(0,1,"archeng504"); 
  LCD_write_char(6,0,’8’);  
  } 
 
}
  
  =======以下是显示自定义字符的程序=============
  
  1602中还可以自定义8个字符图案 
  
  图案规格5*8 
  
  下面是一个向上的箭头↑ 
  
  0 0 0| 0 0 1 0 0  // 0x04 
  
  | 
  
  0 0 0| 0 1 1 1 0  // 0x0e 
  
  | 
  
  0 0 0| 1 0 1 0 1  // 0x15 
  
  | 
  
  0 0 0| 0 0 1 0 0  // 0x04 
  
  | 
  
  0 0 0| 0 0 1 0 0  // 0x04 
  
  | 
  
  0 0 0| 0 0 1 0 0  //0x04 
  
  | 
  
  0 0 0| 0 0 1 0 0  // 0x04 
  
  | 
  
  0 0 0| 0 0 0 0 0  // 0x00 
  
  unsigned char data[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x00};//↑ 
  
  unsigned char data1[8]={0x04,0x04,0x04,0x04,0x15,0x0e,0x04,0x00};//↓ 
  
  void my_signs(unsigned char ascii,unsigned char *z)//0x00 
  
{ 
 unsigned char address=0,i,temp=0;   //ascii为与要定义的图案对应的ascii码值(0x00到 
 temp=(ascii&0x07)<<3;        //0x007间任选) 
  for(i=0;i<8;i++) 
  {  
   address = 0x40 +temp+ i; 
   LCD_write_command( address ); 
   delay_nus(1); 
   LCD_write_data ( *(z++)); 
   delay_nus(1); 
  } 
} 
  
  //建立图案 
  
  void LCD_init(void)    //液晶初始化 
  
{ 
LCD_DATA_DDR|=LCD_DATA;  // 数据为输出 
LCD_EN_DDR|=LCD_EN;  //设置RS.EN方向 
LCD_RS_DDR|=LCD_RS; 
LCD_write_command(0x28); //4位显示 
LCD_en_write(); 
delay_nms(15); 
LCD_write_command(0x28); //4位显示 
LCD_write_command(0x0c); //显示开 
my_signs(0x00,data);//↑ //建立图案 
my_signs(0x01,data1);//↓ 
LCD_write_command(0x01); //清屏 
} 
  
  显示自定义图案 
  
  LCD_write_char(9,0,0x00);//↑ 
  
  LCD_write_char(9,0,0x01);//↓

来源:东哥单片机    责编:豆豆技术应用

正在加载评论...