
#ifndef _LCD_C_
#define _LCD_C_
/*********************************************************************************************************************/
#include "include/ca51f_config.h"		
#include "include/ca51f2sfr.h"
#include "include/ca51f2xsfr.h"
#include "include/gpiodef_f2.h"
#include "include/system_clock.h"
#include "include/delay.h"
#include "include/lcd_led.h"
/*********************************************************************************************************************/
	
void LCD_Init(void)
{
	unsigned char i;
		
	//初始化LCD驱动引脚
	P00F = P00_COM0_SETTING;
	P01F = P01_COM1_SETTING;
	P02F = P02_COM2_SETTING;
	P03F = P03_COM3_SETTING;	 
	   
	P57F = P57_SEG0_SETTING;
	P34F = P34_SEG1_SETTING; 
	P35F = P35_SEG2_SETTING;  
	P56F = P56_SEG3_SETTING; 
	P50F = P50_SEG4_SETTING;
	P51F = P51_SEG5_SETTING;
	P52F = P52_SEG6_SETTING; 
	P53F = P53_SEG7_SETTING;
	P54F = P54_SEG8_SETTING;	
	
	//LCD RAM清零
	for(i = 0; i < 9; i++)
	{
		INDEX = i;
		LXDAT = 0;
	}

#if (RTC_CLK_SELECT == IRCL)
	CKCON |= ILCKE;							//使能IRCL
#elif (RTC_CLK_SELECT == XOSCL)	
	P72F = P72_XOSCL_IN_SETTING;			//设置P72为晶振引脚
	P71F = P71_XOSCL_OUT_SETTING;			//设置P71为晶振引脚
	CKCON |= XLCKE;							//使能XSOCL
	while(!(CKCON & XLSTA));				//等待XSOCL稳定
#endif

	LXDIVH = 0;								//设置LCD时钟分频，目标帧频率为64HZ
	LXDIVL = 0;	
	LXCAD  = 0;
	LXCFG =	 DMOD(DMOD_130ua) | BIAS(BIAS_1_3) | LDRV(LDRV_7);			//设置LCD驱动电流、偏压(bias)、辉度
#if (RTC_CLK_SELECT == IRCL)	
	LXCON =  LEN(LEN_IRCL) | LMOD(LMOD_lcd);	 						//设置LCD时钟源为IRCL，选择LCD模式
#else
	LXCON =  LEN(LEN_XOSCL) | LMOD(LMOD_lcd);	 						//设置LCD时钟源为XOSCL，选择LCD模式
#endif
	
	//设置LCD全显
	for(i = 0; i < 9; i++)
	{
		INDEX = i;
		LXDAT = 0xFF;
	}
}

//若产品在进入休眠状态前需关闭LCD显示，请在关闭LCD功能前，将所设置的COM脚和SEG脚设置为输出模式并输出低电平。此举可有效避免在关闭LCD功能时，LCD屏幕出现拖影现象。
void LCD_Off(void)
{
	P00 = 0;
	P01 = 0;
	P02 = 0;
	P03 = 0;	 
       
	P57 = 0;
	P34 = 0; 
	P35 = 0;  
	P56 = 0; 
	P50 = 0;
	P51 = 0;
	P52 = 0; 
	P53 = 0;
	P54 = 0;
	
	P00F = OUTPUT;
	P01F = OUTPUT;
	P02F = OUTPUT;
	P03F = OUTPUT;	 
       
	P57F = OUTPUT;
	P34F = OUTPUT; 
	P35F = OUTPUT;  
	P56F = OUTPUT; 
	P50F = OUTPUT;
	P51F = OUTPUT;
	P52F = OUTPUT; 
	P53F = OUTPUT;
	P54F = OUTPUT;
	
	LXCON = 0;						//关闭LCD功能
}

#endif