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

📄 1602c_drive.txt

📁 1602驱动程序
💻 TXT
字号:
LCD1602驱动函数
 2008-04-23 08:59:42   来自: Applex (上海)

  /******************************************************** 
  * 函数库说明:LCD1602驱动函数 * 
  * 版本: v1.0 * 
  * 作者: Applex * 
  * 日期: 2008年4月20日 * 
  * 编译环境: AVRGCC+AVRSTUDIO * 
  ********************************************************/ 
  #include <avr/io.h> 
  #include <util/delay.h> 
  #include <avr/wdt.h> 
  #include <stdint.h> 
  #define LCD_DATA PORTB 
   
  /*指令选择函数,未采用位段宏定义*/ 
  //PC7接EN 
  void LCD_EN(uint8_t cmd) 
  { 
   if(cmd==0) 
   { 
   PORTC &= ~_BV(PC7); 
   } 
   else if(cmd==1) 
   { 
   PORTC |= _BV(PC7); 
   } 
  } 
  //PC6接RW 
  void LCD_RW(uint8_t cmd) 
  { 
   if(cmd==0) 
   { 
   PORTC &= ~_BV(PC6); 
   } 
   else if(cmd==1) 
   { 
   PORTC |= _BV(PC6); 
   } 
  } 
  //PC5接RS 
  void LCD_RS(uint8_t cmd) 
  { 
   if(cmd==0) 
   { 
   PORTC &= ~_BV(PC5); 
   } 
   else if(cmd==1) 
   { 
   PORTC |= _BV(PC5); 
   } 
  } 
  //函数:毫秒级延时程序 
  void DelayMs(uint16_t ms) 
  { 
   while(--ms) 
   { 
   _delay_ms(1); 
   } 
  } 
  //函数:使能高脉冲函数; 
  void Enable_Pulse() 
  { 
   LCD_EN(1); 
   DelayMs(5); 
   LCD_EN(0); 
  } 
  //函数:写指令函数; 
  //实现:RS=L,RW=L,D0~D7=指令码,E=高脉冲 
  void LCD_Command(uint8_t cmd) 
  { 
   LCD_RS(0); 
   LCD_RW(0); 
   LCD_DATA=cmd; 
   Enable_Pulse(); 
  } 
  //函数:写数据函数; 
  //实现:RS=H,RW=L,D0~D7=数据,E=高脉冲 
  void LCD_Data(uint8_t data) 
  { 
   LCD_RS(1); 
   LCD_RW(0); 
   LCD_DATA=data; 
   Enable_Pulse(); 
  } 
  //函数:写地址函数 
  //参数:line=0-1;column=0-15; 
  void LCD_Address(uint8_t Line,uint8_t Column) 
  { 
   uint8_t address; 
   
   if (Line == 0) 
   { 
   address = 0x80 + Column; 
   } 
   else 
   { 
   address = 0xc0 + Column; 
   } 
   
   LCD_Command(address); 
  } 
  //函数:写字符串程序 
  //实现:先写地址,再写数据 
  void LCD_String(uint8_t Line,uint8_t Column,char *s) 
  { 
   LCD_Address(Line,Column); //写地址 
   while (*s) 
   { 
   LCD_Data(*s); 
   s++; 
   } 
  } 
   
  //函数:初始化函数 
  void LCD_Init(void) 
  { 
   DelayMs(15); 
   LCD_Command(0x38); 
   DelayMs(5); 
   LCD_Command(0x38); 
   DelayMs(5); 
   
   LCD_Command(0x38); 
   DelayMs(5); 
   LCD_Command(0x08); 
   DelayMs(5); 
   LCD_Command(0x01); 
   DelayMs(5); 
   LCD_Command(0x06); 
   DelayMs(5); 
   LCD_Command(0x0c); 
   DelayMs(5); 
  } 
   
  int main(void) 
  { 
   DDRB=0xFF; 
   DDRC=0xFF; 
   
   LCD_Init(); 
   LCD_String(0,3,"Test Ok!"); 
   LCD_String(1,9,"Applex"); 
   while(1) 
   return 0; 
  } 

 

⌨️ 快捷键说明

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