工业控制 | 能源技术 | 汽车电子 | 通信网络 | 安防监控 | 智能电网 | 移动手持 | 无线技术 | 家用电器 | 数字广播 | 消费电子 | 应用软件 | 其他方案

电路设计->微机单片机电路图->单片机综合电路图->12864液晶(AVR_Mega128)的头文件

12864液晶(AVR_Mega128)的头文件

发布人:fanxiaoxi 时间:2023-11-21 收藏

  大家使用时把这段代码保存成.h的头文件,在主程序中包含这个头文件就行
  根据你们液晶电路更改下面的端口宏定义即可
  //CPU:ATmega128; 时钟频率为16MHz
  //编译环境为ICCAVR
  //头文件
  #include iom128v.h>
  #include string.h>
  #includeavrdef.h>
  //数据端口定义
  #define Dat_Port_Write PORTA
  #define Dat_Port_Read PINA
  //控制端口及相应的位
  #define RS_RW_EN_Control PORTG
  #define LCM_RS 0
  #define LCM_RW 1
  #define LCM_EN 2
  #define uchar unsigned char
  #define uint unsigned int
  //延时函数
  void Delay_Ms(unsigned int ms)
  {
  for(;ms>1;ms--);
  }
  //写数据
  void Write_Data_LCM(unsigned char WDLCM)
  {
  Read_Status_LCM(); //检测忙
  Delay_Ms(100);
  RS_RW_EN_Control|=BIT(LCM_RS); //RS=1
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_RW); //RW=0
  Delay_Ms(100);
  RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
  Delay_Ms(100);
  Dat_Port_Write=WDLCM; //输出数据
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
  Delay_Ms(100);
  }
  //写指令
  void Write_Command_LCM(unsigned char WCLCM)
  {
  Read_Status_LCM(); //根据需要检测忙
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_RS); //RS=0
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_RW); //RW=0
  Delay_Ms(100);
  RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
  Delay_Ms(100);
  Dat_Port_Write=WCLCM; //输出指令
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
  Delay_Ms(100);
  }
  //读状态:检测忙
  void Read_Status_LCM(void)
  {
  uchar temp;
  uchar flag = 1;
  while(flag==1)
  {
  DDRA=0x00; //端口A改为输入
  Dat_Port_Write = 0xff;
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_RS); //RS=0
  Delay_Ms(100);
  RS_RW_EN_Control|=BIT(LCM_RW); //RW=1
  Delay_Ms(100);
  RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
  Delay_Ms(100);
  temp = Dat_Port_Read; //读端口A
  Delay_Ms(100);
  DDRA=0xff; //端口A改为
  Delay_Ms(100);
  RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
  Delay_Ms(100);
  if(temp>>7==0)
  flag = 0;
  }
  }
  //LCM初始化
  void LCM_Init(void)
  {
  Write_Command_LCM(0x38); //三次显示模式设置,不检测忙信号
  Delay_Ms(1000);
  Write_Command_LCM(0x38);
  Delay_Ms(1000);
  Write_Command_LCM(0x38);
  Delay_Ms(1000);
  Write_Command_LCM(0x38); //显示模式设置,开始要求每次检测忙信号
  Write_Command_LCM(0x08); //关闭显示
  Write_Command_LCM(0x01); //显示清屏
  Write_Command_LCM(0x06); //显示光标移动设置
  Write_Command_LCM(0x0C); //显示开及光标设置
  }
  /


关键词:

评论

技术专区