
#include "stm8l15x_conf.h"
#include "i2c.h"

void HT1621_comm_out(char data);
//void HT1621_show_variable(char y, char x, char l, char p, float word) ;
void HT1621_S_Write(uint8_t address);
void HT1621_show_variable(char l, char p, float word) ;


#define LCDCS_SetOutput()	GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_Out_PP_Low_Slow);   
#define LCDCS_SetVal()	        GPIO_SetBits(GPIOD, GPIO_Pin_0);  
#define LCDCS_ClrVal()		GPIO_ResetBits(GPIOD, GPIO_Pin_0);

#define LCDWR_SetOutput()	GPIO_Init(GPIOD, GPIO_Pin_1, GPIO_Mode_Out_PP_Low_Slow);   
#define LCDWR_SetVal()	        GPIO_SetBits(GPIOD, GPIO_Pin_1);  
#define LCDWR_ClrVal()		GPIO_ResetBits(GPIOD, GPIO_Pin_1);   

#define LCDDATA_SetOutput()	GPIO_Init(GPIOD, GPIO_Pin_2, GPIO_Mode_Out_PP_Low_Slow);   
#define LCDDATA_SetVal()	GPIO_SetBits(GPIOD, GPIO_Pin_2);  
#define LCDDATA_ClrVal()	GPIO_ResetBits(GPIOD, GPIO_Pin_2);   

char HHB88128_word[10];         
unsigned char HT1621_Buff[5];


void HHB88128(void)
{
         LCD_Menu = 0;
         fPress5 = fPress4;
         
        /************???************/
        if (Range <= 1000)
          HT1621_show_variable(4,0,fPress5); //
        else if(Range <= 10000)
          HT1621_show_variable(5,0,fPress5); //
        else if(Range <= 35000)
          HT1621_show_variable(5,0,fPress5); 

        HT1621_Buff[0] |= 0x08;//pa
        HT1621_S_Write(0);
        _Delay_us(1000);
}


/******************************************************************************
*  y: 癰??獵???ョ?驪??0-7
*  x: 岳??獵???ョ?驪??0-127
* 
* 
*******************************************************************************/
void HT1621_show_variable(char l, char p, float word) 
{
  long press,w;
  char sign;
    
   HT1621_Buff[4] =0;
   press = (long)word;
    //负数处理
     if (word<0)        { sign = 0x08; press = ~(press-1);}
     else               { sign = 0x00;      }
     
    for(int i=0;i<l;i++) 
    { 
         w = press;
        if ((w ==0)&&(i>=(l-1+p-2)))                       HT1621_Buff[i] = 0;  
        else                            {  w = w%10;       HT1621_Buff[i] = HHB88128_word[w];     }

        press /= 10;
    }
    HT1621_Buff[4] |= sign; //负号
    
     /************小数点***********/
      if(p == 0)        HT1621_Buff[1] &= 0xF7;
      else if(p == 1)   HT1621_Buff[1] |= 0x08;
}

void HHB88128_Init(void)
{
    LCDCS_SetOutput(); LCDCS_SetVal();
    LCDWR_SetOutput();	
    LCDDATA_SetOutput();	
    _Delay_us(10000);

    HT1621_comm_out(0x29);//1/3duty 4com
    HT1621_comm_out(0x18);//RC256
   // HT1621_comm_out(0);//SYSDIS
    HT1621_comm_out(5);//WDTDIS
    HT1621_comm_out(1);//SYSEN
    HT1621_comm_out(3);//LCDON
    
             //byte 跟SEG的关系
        //  D  C  B  A  X  G  F  E 
        HHB88128_word[0] = 0xF5 ;  //1111 0101
        HHB88128_word[1] = 0x60 ;  //0110 0000
        HHB88128_word[2] = 0xB6 ;  //1011 0110
        HHB88128_word[3] = 0xF2 ;  //1111 1010
        HHB88128_word[4] = 0x63 ;  //0110 0011
        HHB88128_word[5] = 0xD3 ;  //1011 0011
        HHB88128_word[6] = 0xD7 ;  //1101 0111
        HHB88128_word[7] = 0x70 ;  //0111 0000
        HHB88128_word[8] = 0xF7 ;  //1111 0111
        HHB88128_word[9] = 0xF3 ;  //1111 0011
    
}
/************???************/
void HT1621_comm_out(char data)
{
    char comm = 0x80;
    
    LCDCS_ClrVal();
    for(uint8_t i=0; i<3; i++)
    {
        if(comm & 0x80) {                LCDDATA_SetVal();        }
        else            {                LCDDATA_ClrVal();        }
        comm <<= 1;	      _Delay_ns(1);
        LCDWR_SetVal();	_Delay_ns(1);
        LCDWR_ClrVal();	_Delay_ns(1);
    }
    
    for(uint8_t i=0; i<8; i++)
    {
        if(data & 0x80){                LCDDATA_SetVal();        }
        else           {                LCDDATA_ClrVal();        }
        data <<= 1;		_Delay_ns(1);
        LCDWR_SetVal();	_Delay_ns(1);
        LCDWR_ClrVal();	_Delay_ns(1);
    }
     LCDDATA_ClrVal();   
     LCDWR_SetVal();	_Delay_ns(1);
     LCDWR_ClrVal();	_Delay_ns(1);
    
    LCDCS_SetVal();
    _Delay_ns(5);
}
/************???************/
void HT1621_S_Write(uint8_t address)
{
    uint32_t comm ,data;
    
    comm = 0x140 | address;
    LCDCS_ClrVal();
    for(uint8_t k=0; k<9; k++)
    {
        if(comm & 0x100) {                LCDDATA_SetVal();        }
        else             {                LCDDATA_ClrVal();        }
        comm <<= 1;	      _Delay_ns(1);
        LCDWR_SetVal();	_Delay_ns(1);
        LCDWR_ClrVal();	_Delay_ns(1);
    }
    
    for(uint8_t i=0; i<5; i++)
    {
        data = HT1621_Buff[i];
         for(uint8_t j=0; j<8; j++)
        {
            if(data & 0x80) {                LCDDATA_SetVal();        }
            else            {                LCDDATA_ClrVal();        }
            data <<= 1;		_Delay_ns(1);
            LCDWR_SetVal();	_Delay_ns(1);
            LCDWR_ClrVal();	_Delay_ns(1);
        }
    }
    
   LCDCS_SetVal();
    _Delay_ns(5);
}


void _Delay_us(int32_t dly)
{
        
       // dly <<=2;
	for(long int i=0;i<dly;i++)
	{
		//for(int j=0;j<2;j++)	{	}
        }
}
void _Delay_ns(int32_t dly)
{
	int32_t i;
	if(dly > 0)
	{
		i = dly;
		//dly <<= 2;
		dly += i;
	}
	do
	{
            dly--;
	}while(dly > 0);
}