//stc8H2K08U  LCDʾ


include <COMM/STC8H.h>					 //MCUͷļ
#include "intrins.h"					 //nopָͷļ 
#include "stdio.h"
#define uchar unsigned char                                 //ͺ궨
#define uint unsigned int                                 //ͺ궨
	

//TM1621d׼1/3

/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_׼1/3*/
#define nop _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); //궨
/******************TM1621Dģ*********************/
#define SYSDIS   0x00    //ϵͳLCDƫѹ        
#define SYSEN    0x02    //ϵͳ      
#define LCDOFF   0x04    //LCDƫѹ
#define LCDON    0x06    //LCDƫѹ                                                         
#define RC       0x30    //ڲRC      
#define BIAS     0x52    //1/3ƫѹ 4       
#define WDTDIS   0x0a    //ֹŹ
#define WDTEN    0x0e    //Ź
/*ڲLCDƫѹҲʾʾLCD ؿŹ ʺ//1/3ƫѹ 4  */

/********************ƶ˿Ҫʵ**********************/
sbit CS=P3^7;
sbit WRITE=P3^6;
sbit DATA=P3^5;

/********************˵ûõTM1621Dٷ*************************/
uchar code Smg[16]={0x05,0xff,0xc7,0xE5,0x6C,0xAD,0xaf,0xE0,0xef,0xed,0xee,0x2f,0x8b,0x67,0x8f,0x8e};  //0~F
uchar code Tab0[16]={0x08,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //
/********************ʱ*************************/
void delay_nms(uint n)
{
   uint i;
   while(n--)
     for(i=0;i<500;i++);
}
/********************Ӹλд*************************/
void Write_Data_H(uchar Data, uchar Cnt)           //DataĸcntλдTM1621Dλǰ
{
uchar i;
for(i=0;i<Cnt;i++)
{
  WRITE=0;
	
  if(Data&0x80)                                   //λ
    DATA=1;               
  else
    DATA=0;
  nop;

  WRITE=1;
  nop;
  Data<<=1;
}
WRITE=0;
DATA=0;
}
/********************ӵλд*************************/
void Write_Data_L(uchar Data,uchar Cnt) //Data ĵcntλдTM1621Dλǰ
{
unsigned char i;
for(i=0;i<Cnt;i++)
{
   WRITE=0;
	        nop;
   if(Data&0x01)                                 //ӵλ
     DATA=1;  
   else
     DATA=0;
        nop;

        WRITE=1;
	         nop;
        Data>>=1;
}
  WRITE=0;
  DATA=0;  
}

/********************д*************************/
void WriteCmd(uchar Cmd)
{
CS=0;
nop;
Write_Data_H(0x80,4);     //д־100
Write_Data_H(Cmd,8);      //д
CS=1;
nop;
}

/*********ַָдݣʵд4λ************/
void WriteOneData(uchar Addr, uchar Data)
{
	
Addr=Addr<<2;
CS=0;
nop;
Write_Data_H(0xa0,3);     //дݱ־101
	
Write_Data_H(Addr,6);  //дַ
Write_Data_L(Data,4);     //д
nop;	
CS=1;
nop;
}

/*********д뷽ʽÿΪ8λдݣÿȥ************/
/*
void WriteAllData(uchar Addr,uchar *p,uchar cnt)
{
uchar i;
CS=0;
Write_Data_H(0xa0,3);          //дݱ־101
Write_Data_H(Addr<<2,6);        //дַ
for(i=0;i<cnt;i++)                //д
{
   Write_Data_L(*p,8);            
   p++;
}
CS=1;
nop;
}
*/
/*******************TM1621Dʼ**********************/
void TM1621D_init()
{
CS=1;
WRITE=1;
DATA=1;
nop;
delay_nms(1);                        
WriteCmd(BIAS);                 //1/3ƫѹ 4
WriteCmd(RC);                         //ڲRC	
WriteCmd(SYSDIS);                 //ϵͳLCDƫѹ
WriteCmd(WDTDIS);								 //ֹŹ
WriteCmd(SYSEN);                 //ϵͳ
WriteCmd(LCDON);                 //LCDƫѹ
}

/*************TM16211621DӦȫղǶԵ*****************/
void Clear1621()
{
uchar i;
for(i=0;i<32;i++) //9-21
{
WriteOneData(i, 0x00);
}
}
/*************TM1621ȫ ûȫʾвԵĵطҪӵ*****************/
/*
void quanping1621()
{
uchar i;
for(i=9;i<22;i++) //9-21
{
WriteOneData(i, 0x0f);
}
}
*/
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_׼1/3*/

//TM1621d׼1/3

//ʾlcd_xsbl
unsigned int lcd_xsbl=0;//lcdʾ
static void lcd_1(unsigned long xsbl_lcd)//1λλ(λ)
{
				 if(xsbl_lcd / 100 == 0)
    {
        WriteOneData(0x09,0x0b);	WriteOneData(0x0e,0x0e);  	            // 0
    }
    else if(xsbl_lcd / 100== 1)
    {
        WriteOneData(0x09,0x00);	WriteOneData(0x0e,0x06);  	            // 1
    }
    else if(xsbl_lcd / 100== 2)
    {
        WriteOneData(0x09,0x07);	WriteOneData(0x0e,0x0c);  	            // 2
    }   
    else if(xsbl_lcd / 100== 3)
    {
        WriteOneData(0x09,0x05);	WriteOneData(0x0e,0x0e);  	            // 3
    } 
    else if(xsbl_lcd / 100== 4)
    {
        WriteOneData(0x09,0x0c);	WriteOneData(0x0e,0x06);  	            // 4
    }
    else if(xsbl_lcd / 100== 5)
    {
        WriteOneData(0x09,0x0d);	WriteOneData(0x0e,0x0a);  	            // 5
    }
    else if(xsbl_lcd / 100== 6)
    {
        WriteOneData(0x09,0x0f);	WriteOneData(0x0e,0x0a);  	            // 6
    }
    else if(xsbl_lcd / 100== 7)
    {
        WriteOneData(0x09,0x00);	WriteOneData(0x0e,0x0e);                // 7
    }
    else if(xsbl_lcd / 100== 8)
    {
        WriteOneData(0x09,0x0f);	WriteOneData(0x0e,0x0e);  	            // 8
    }
    else if(xsbl_lcd / 100== 9)
    {
				WriteOneData(0x09,0x0d);	WriteOneData(0x0e,0x0e); 	      	      // 9
    }
}

static void lcd_2(unsigned long xsbl_lcd)//2λʮλ(λ)
{
				 if(xsbl_lcd % 100/10== 0)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0b);  	            // 0
    }
    else if(xsbl_lcd % 100/10== 1)
    {
        WriteOneData(0x0c,0x06);	WriteOneData(0x0d,0x00);  	            // 1
    }
    else if(xsbl_lcd % 100/10== 2)
    {
        WriteOneData(0x0c,0x0c);	WriteOneData(0x0d,0x07);  	            // 2
    }   
    else if(xsbl_lcd % 100/10== 3)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x05);  	            // 3
    } 
    else if(xsbl_lcd % 100/10== 4)
    {
        WriteOneData(0x0c,0x06);	WriteOneData(0x0d,0x0c);  	            // 4
    }
    else if(xsbl_lcd % 100/10== 5)
    {
        WriteOneData(0x0c,0x0a);	WriteOneData(0x0d,0x0d);  	            // 5
    }
    else if(xsbl_lcd % 100/10== 6)
    {
        WriteOneData(0x0c,0x0a);	WriteOneData(0x0d,0x0f);  	            // 6
    }
    else if(xsbl_lcd % 100/10== 7)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x00);  	            // 7
    }
    else if(xsbl_lcd % 100/10== 8)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0f);  	            // 8
    }
    else if(xsbl_lcd % 100/10== 9)
    {
        WriteOneData(0x0c,0x0e);	WriteOneData(0x0d,0x0d);  	            // 0
    }
}

static void lcd_3(unsigned long xsbl_lcd)//3λλ(λ)
{
    if(xsbl_lcd % 10 == 0)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0b);  	            // 0
    }
    else if(xsbl_lcd % 10 == 1)
    {
        WriteOneData(0x0a,0x06);	WriteOneData(0x0b,0x00);  	            // 1
    }
    else if(xsbl_lcd % 10 == 2)
    {
        WriteOneData(0x0a,0x0c);	WriteOneData(0x0b,0x07);  	            // 2
    }   
    else if(xsbl_lcd % 10 == 3)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x05);  	            // 3
    } 
    else if(xsbl_lcd % 10 == 4)
    {
        WriteOneData(0x0a,0x06);	WriteOneData(0x0b,0x0c);  	            // 4
    }
    else if(xsbl_lcd % 10 == 5)
    {
        WriteOneData(0x0a,0x0a);	WriteOneData(0x0b,0x0d);  	            // 5
    }
    else if(xsbl_lcd % 10 == 6)
    {
        WriteOneData(0x0a,0x0a);	WriteOneData(0x0b,0x0f);  	            // 6
    }
    else if(xsbl_lcd % 10 == 7)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x00);  	            // 7
    }
    else if(xsbl_lcd % 10 == 8)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0f);  	            // 8
    }
    else if(xsbl_lcd % 10 == 9)
    {
        WriteOneData(0x0a,0x0e);	WriteOneData(0x0b,0x0d);  	            // 0
    }
}
	
//ʾlcd_xsbl


void Delay1000ms(void)	//@11.0592MHz
{
	unsigned char data i, j, k;

	_nop_();
	_nop_();
	i = 2;
	j = 67;
	k = 183;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void main()
{
//main
//TM1621dӦò2/3
	{
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_Ӧò2/3*/
    P3M0 = 0x00; P3M1 = 0x00; 

		


TM1621D_init();                //ʼظʹѭҪĻ
delay_nms(1);
	Clear1621();//LCDԴ
	
//quanping1621();  //ӦÿȫʾŶ
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_Ӧò2/3*/
	}
	
	
	
	

	
while(1)
{//while
	Delay1000ms();
	Clear1621();
/*
	lcd_xsbl=lcd_xsbl+1;
	
	if (lcd_xsbl>=999)
		lcd_xsbl=0;
	

	*/
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_ʾ3/3*/
lcd_1(123);
lcd_2(123);
lcd_3(123);
/*tm1621dLCDʾ-޹صLCDʾܵŻвP20 21 22  P34 35 ڸTM1621D 1621D_ʾ3/3*/
	
}//while
	
}