//[LCD.c]LCD驱动-----------------------------------------------------------*
//本文件包含:                                                              *
//    (1)LCDcommand:执行LCD命令                                            *
//    (2)LCDinit:初始化LCD(HD44780)                                        *
//    (3)LCDshow:在LCD(HD44780)显示屏上显示数据                            *
//硬件连接:                                                                *
//    在SD-HC08实验板上,有与LCD连接的两排插孔:                             *
//    (1)LCD若接上排插孔:                                                  *
//       LCD的数据线(DB0-7),控制线RS,R/W,E分别与MCU的PTB0-7,PTC0-2连接     *
//    (2)LCD若接下排插孔:                                                  *
//       可自行定义LCD与MCU的连接                                          *
//-------------------------------------------------------------------------*

#include "LCD.h"    //该头文件包含寄存器及相关位定义

//LCDshow:在LCD(HD44780)显示屏上显示数据-----------------------------------*
//功能:在LCD(HD44780)显示屏上显示32个数据                                  *
//参数:str 待显示数据的首地址                                              *
//返回:无                                                                  *
//内部调用:LCDinit,LCDcommand                                              *
//-------------------------------------------------------------------------*
void LCDshow(uint8 str[])
{
    uint8 i,cmd2;
    //1. LCD初始化
    LCDinit();
 	//2. 显示第1行16个字符
 	//2.1设置显示首地址
 
    AW60_LCDctrl=( AW60_LCDctrl
               & ~AW60_LCDctrl_RS
               & ~AW60_LCDctrl_RW);
    cmd2=(0
         |AW60_LCDdata_PTAD7);
    LCDcommand(cmd2); 
    //2.2写16个数据到DD RAM  
    AW60_LCDctrl=( AW60_LCDctrl
               | AW60_LCDctrl_RS
               & ~AW60_LCDctrl_RW);  
    for (i = 0;i < 16;i++)     //将要显示在第1行上的16个数据逐个写入DD RAM中
    {
        LCDcommand(str[i]);
    }      
    //3. 显示第2行16个字符
   	//3.1设置显示首地址
  
  	AW60_LCDctrl=( AW60_LCDctrl
               & ~AW60_LCDctrl_RS
               & ~AW60_LCDctrl_RW);
    cmd2=(0
         |AW60_LCDdata_PTAD7
         |AW60_LCDdata_PTAD6);
    LCDcommand(cmd2);  
	//3.2再写16个数据到DD RAM

	AW60_LCDctrl=( AW60_LCDctrl
               | AW60_LCDctrl_RS
               & ~AW60_LCDctrl_RW);   
    for (i = 16;i < 32;i++)    //将要显示在第2行上的16个数据逐个写入DD RAM中
	{
	    LCDcommand(str[i]);
    }
}
//LCDinit:初始化LCD(HD44780)-----------------------------------------------*
//功能:初始化LCD(HD44780),设置显示方式,输入方式,并清屏                     *
//参数:无                                                                  *
//返回:无                                                                  *
//内部调用:LCDcommand                                                      *
//-------------------------------------------------------------------------*
void LCDinit(void)
{
    uint16 i;
    uint8 cmd1; 
    //定义数据口(PTD0-7)为输出
    
    AW60_LCDdataD  = (0
             |AW60_LCDdataD_PTADD7
             |AW60_LCDdataD_PTADD6
             |AW60_LCDdataD_PTADD5
             |AW60_LCDdataD_PTADD4
             |AW60_LCDdataD_PTADD3
             |AW60_LCDdataD_PTADD2
             |AW60_LCDdataD_PTADD1
             |AW60_LCDdataD_PTADD0);  
    //定义控制口(PTC0-2)为输出
   
    AW60_LCDctrlD =(AW60_LCDctrlD
             |AW60_LCDctrl_E
             |AW60_LCDctrl_RW
             |AW60_LCDctrl_RS);
    AW60_LCDctrl =(AW60_LCDctrl
             & ~AW60_LCDctrl_RS 
             & ~AW60_LCDctrl_RW);  
    //1. 功能设置
    cmd1=(0
         |AW60_LCDdata_PTAD5
         |AW60_LCDdata_PTAD4
         |AW60_LCDdata_PTAD3);
    LCDcommand(cmd1); //5*7点阵模式, 2行显示,8位数据总线
    //2. 显示开关控制
    cmd1=(0
        |AW60_LCDdata_PTAD3);
    LCDcommand(cmd1); // 不闪烁, 关光标显示,关显示
    //3. 清屏
    //3.1清DD RAM内容,光标回原位,清AC
    cmd1=(0
        |AW60_LCDdata_PTAD0);
    LCDcommand(cmd1); 
    //3.2等待清屏完毕,时间 > 1.6ms
    for(i = 0; i< 40000; i++)
        asm("NOP");
    //4.输入方式设置
    cmd1=(0
        |AW60_LCDdata_PTAD2
        |AW60_LCDdata_PTAD1);
    LCDcommand(cmd1); // 显示不移动,光标左移(A = 1), 数据读写操作后,AC自动增1
    //5.光标或画面移位设置
    cmd1=(0
        |AW60_LCDdata_PTAD2
        |AW60_LCDdata_PTAD4);
    LCDcommand(cmd1);//光标右移一个字符位,AC自动加1
    //6. 显示开关控制
    cmd1=(0
        |AW60_LCDdata_PTAD2
        |AW60_LCDdata_PTAD3);
    LCDcommand(cmd1);  //不闪烁, 关光标显示,开显示
    
}

//LCDcommand:执行给定的LCD命令---------------------------------------------*
//功能:执行给定的LCD命令,并延时                                            *
//参数:cmd:待执行的命令                                                    *
//返回:无                                                                  *
//-------------------------------------------------------------------------*
void LCDcommand(uint8 cmd)
{    
    uint16 i;
    //1.等待 > 40us
    for(i = 0; i < 1000; i++)
       asm("NOP"); 
    //2.数据送到LCD的数据线上
    AW60_LCDdata  = cmd;
    //3.给出E信号的下降沿,使数据写入LCD
    AW60_LCDctrl=(AW60_LCDctrl
              |AW60_LCDctrl_E);
    asm("NOP");
    asm("NOP");
    asm("NOP");
    AW60_LCDctrl=(AW60_LCDctrl
              & ~AW60_LCDctrl_E);
    //4.等待 > 40us
    for(i = 0; i < 1000; i++)
       asm("NOP"); 
   
}
