首页 > 代码库 > LCD显示--HT1621b芯片驱动程序

LCD显示--HT1621b芯片驱动程序

HT1621b芯片驱动程序

CS : 片选输入接一上拉电阻当/CS 为高电平读写HT1621的数据和命令无效串行接口电路复位当/CS 为低电平和作为输入时读写HT1621的数据和命令有效.

WR : WRITE脉冲输入接一上拉电阻在/WR 信号的上升沿 DATA线上的数据写到HT1621.

DATA : 外接上拉电阻的串行数据输入/输出.

/* Includes ------------------------------------------------------------------*/
#include "drv_ht1621.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define HT1621_CS_PORT            GPIOC
#define HT1621_CS_PIN             GPIO_PIN_7
#define Ht1621CsHigh              (HT1621_CS_PORT->ODR |= (u8)(HT1621_CS_PIN))
#define Ht1621CsLow               (HT1621_CS_PORT->ODR &= (u8)(~HT1621_CS_PIN))

#define HT1621_WR_PORT            GPIOC
#define HT1621_WR_PIN             GPIO_PIN_6
#define Ht1621WrHigh              (HT1621_WR_PORT->ODR |= (u8)(HT1621_WR_PIN))
#define Ht1621WrLow               (HT1621_WR_PORT->ODR &= (u8)(~HT1621_WR_PIN))

#define HT1621_DATA_PORT          GPIOC
#define HT1621_DATA_PIN           GPIO_PIN_5
#define Ht1621DataHigh            (HT1621_DATA_PORT->ODR |= (u8)(HT1621_DATA_PIN))
#define Ht1621DataLow             (HT1621_DATA_PORT->ODR &= (u8)(~HT1621_DATA_PIN))


#define LedPwmDuty(A)             TIM2->CCR2H = (u8)((A)>> 8);TIM2->CCR2L = (u8)(A); 
#define LedOn()                   TIM2->CCER1 |= TIM2_CCER1_CC2E  
#define LedOff()                  TIM2->CCER1 &= (~TIM2_CCER1_CC2E)  

#define  COMMAND_CODE                0x80  //命令码
#define  WRITE_DATA_CODE             0xa0  //写命令
#define  READ_DATA_CODE              0xc0  //读命令
#define  DISPLAY_BEGIN_ADDR          (0X0F << 2) //显示起始地址
#define  SYS_EN                      0x02
#define  RC_256K                     0x30
#define  BIAS_13                     0x52
#define  TONE_ON                     0x12  //打开声音输出
#define  TONE_OFF                    0x10
#define  TONE_2K                     0xc0
#define  TONE_4K                     0x80
#define  LCD_ON                      0x06

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/*******************************************************************************
函数:LCD背光PWM
参数: 
返回值:无
*******************************************************************************/
void TIM2_Configuration(void)
{
   TIM2_DeInit();
  
   TIM2_TimeBaseInit(TIM2_PRESCALER_1,  
                     640);                                                      //25khz
  
   TIM2_OC2Init(TIM2_OCMODE_PWM1, 
                TIM2_OUTPUTSTATE_ENABLE, 
                500, 
                TIM2_OCPOLARITY_LOW);
   
   TIM2_Cmd(ENABLE);
}

/*******************************************************************************
函数:延时函数 ms 
参数: 
返回值:无
*******************************************************************************/
void Ht1621_Delay(uint8_t ms)
{
   unsigned short jj;
   for (jj = 1; jj <= ms; jj++)
   {
      nop();
   }
}

/*******************************************************************************
函数:HT1621初始化
参数: 
返回值:无
*******************************************************************************/
void Ht1621_Init()
{  
   GPIO_Init(HT1621_CS_PORT, HT1621_CS_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);         //初始化端口
   GPIO_Init(HT1621_WR_PORT, HT1621_WR_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);
   GPIO_Init(HT1621_DATA_PORT, HT1621_DATA_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);
   
   Ht1621_WriteCommand(SYS_EN);   //SYS_EN 打开系统振荡器
   Ht1621_Delay(5);
   
   Ht1621_WriteCommand(RC_256K);   //RC_256K 系统时钟源 片内RC振荡器
   Ht1621_Delay(5);
   
   Ht1621_WriteCommand(BIAS_13);  //BIAS13 4个公共口 
   Ht1621_Delay(5);
   
   Ht1621_WriteCommand(TONE_4K);  //TONE_2KC0  TONE_4K80 
   Ht1621_Delay(5);
   
   Ht1621_WriteCommand(LCD_ON);   //LCD_ON 打开LCD偏压发生器
   Ht1621_Delay(5);
}


/*******************************************************************************
函数:HT1621 写一字节数据
参数: 写数据的为位数 和 数据
返回值:无
*******************************************************************************/
void Ht1621_WriteNBit(uint8_t wData , uint8_t BitCnt)
{
   uint8_t i;
   
   for(i=0; i< BitCnt; i++)
   {
      Ht1621WrLow;
      Ht1621_Delay(2);
      
      if((wData << i) & 0x80)
      {
         Ht1621DataHigh;
      }
      else 
      {
         Ht1621DataLow;
      }
      Ht1621_Delay(2);
      
      Ht1621WrHigh;
      Ht1621_Delay(2);
   }
}

/*******************************************************************************
函数:HT1621 写数据操作(连续写  3位写操作命令+6位地址+ ...数据)
参数: 写数据指针
返回值:无
*******************************************************************************/
void Ht1621_WriteData(const uint8_t *wDataBuf)
{
   uint8_t i;
      
   Ht1621CsLow;
   Ht1621_Delay(2);
      
   Ht1621_WriteNBit(WRITE_DATA_CODE, 3);
   Ht1621_WriteNBit(DISPLAY_BEGIN_ADDR, 6);            //TODO: 20140820  hzg  注意要对应原理图上的起始地址
   
   for(i=0; i<10; i++)
   {
    Ht1621_WriteNBit(wDataBuf[i], 8);  
   }
   
   Ht1621CsHigh;
   Ht1621_Delay(2);
}

/*******************************************************************************
函数:HT1621 写命令操作(12位命令数据)
参数: 命令值
返回值:无
*******************************************************************************/
void Ht1621_WriteCommand(uint8_t Cmd)
{
   Ht1621CsLow;
   Ht1621_Delay(2);
   
   Ht1621_WriteNBit(COMMAND_CODE, 4);       
   Ht1621_WriteNBit(Cmd, 8);
   
   Ht1621CsHigh;
   Ht1621_Delay(2);
}

/*******************************************************************************
函数:HT1621 蜂鸣器输出控制(12位命令数据)
参数: 开关标志
返回值:无
*******************************************************************************/
void Ht1621_BuzzerControl(uint8_t RunFlag)
{
   (RunFlag == 0)?Ht1621_WriteCommand(TONE_OFF):Ht1621_WriteCommand(TONE_ON);
}