12864(CVAVR程序)

注:本人博文整理至CSDN以方便查阅,源博文位于EDN


#include<mega16.h>
#include<delay.h>


#define uchar unsigned char


#define DB PORTB
#define RS PORTD.5
#define RW PORTD.6
#define E  PORTD.7


uchar check_busy(void);      
void Write_Command(uchar cmd);//写命令    
void Write_Data(uchar datacode);//写数据  
void Initial_LCD();  //初始化  
void Location_XY(uchar x,uchar y);//设置光标位置    
void Write_String(uchar *asc);  //写字符串 
void Write_String_atXY(uchar x,uchar y,uchar string[]);//在(x,y)处写字符串


uchar str1[]="欢迎光临";
uchar str2[]="知己相惜";
uchar str3[]="携手共度";
uchar str4[]="GOOD LUCK";


void main()
{
  Initial_LCD();
  while(1)
  {
    Write_String_atXY(1,0,str1); 
    delay_ms(100);
    Write_String_atXY(2,1,str2);
    delay_ms(100);
    Write_String_atXY(3,2,str3);
    delay_ms(100);
    Write_String_atXY(3,3,str4);
    delay_ms(100);
    Write_Command(0x01);
    delay_ms(100); 
  }
}


/**************************************************************
功能:检查是否LCD忙碌
***************************************************************/
uchar check_busy(void) 
{
    char r;
    DDRB = 0x00;       //端口B设为输入方式
    E="0";RS=0;RW=1;     //E=0(致能),RS=0(命令),RW=1(读)
    delay_us(2);       //液晶延时子程序
    E="1";
    delay_us(2);       //液晶延时子程序
    r = PINB & 0x80;   //读取lcd_data第八位
    E="0";
    DDRB="0xff";         //端口B设为输出方式
    return r;          //读取结果返回
}



/****************************************************************
功能:写命令到LCM
参数:cmdcode是要写入的命令
****************************************************************/
void Write_Command(uchar cmdcode)
{
  while(check_busy());
  RS="0";
  RW="0";
  E="0";
  delay_us(1);delay_us(1);
  E="1";
  DB="cmdcode";
  delay_us(1);delay_us(1);delay_us(1);
  E="0";
}


/****************************************************************
功能:写数据到LCM
参数:datacode是要写入的数据
****************************************************************/
void Write_Data(uchar datacode)
{
  while(check_busy());
  RS="1";
  RW="0";
  E="0";
  delay_us(1);delay_us(1);
  E="1";
  DB="datacode";
  delay_us(1);delay_us(1);delay_us(1);
  E="0";
}


/****************************************************************
功能:初始化LCD
****************************************************************/
void Initial_LCD(void) 
{    
    DDRB = 0xff;       //端口B设为输出方式
    DDRD = 0xff;       //端口D设为输出方式
    Write_Command(0x38); //
    Write_Command(0x38); //
    Write_Command(0x38); //
    Write_Command(0x38); //
    Write_Command(0x08); //令显示器off
    Write_Command(0x01); //清除显示器
    Write_Command(0x06); //令LCD每接收到1Byte数据后,AC自动加1
    Write_Command(0x0C); //令光标,0x0c=不显示,0x0d=显示闪动.
}


/****************************************************************
功能:写一串字符到LCM
参数:datacode是要写入的数据
****************************************************************/
void Write_String(unsigned char *asc)
{
  while((*asc) != 0)   //判断字是否结束
    {
        Write_Data(*asc); //向lcd写入字符串
        asc++;           //移下一个字符
    }
}


/*==============设置坐标====================*/
void Location_XY(uchar x,uchar y)
{
  switch(y)
  {
    case 0:
      y="0x80";break;
    case 1:
      y="0x90";break;
    case 2:
     y="0x88";break;
    case 3:
      y="0x98";break;
    default:
      y="0x80";
  }
  x="x"&0x07;
  Write_Command(x+y);
}


/***********************************************************************
功能:在(x,y)处显示字符串string
************************************************************************/
void Write_String_atXY(uchar x,uchar y,uchar string[])
{
  Location_XY(x,y);
  Write_String(string);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值