今天写了另外一款HT1621的驱动,开发环境用的IAR。
代码:
/******************************************************************************
* Include files
******************************************************************************/
#include "gpio.h"
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
typedef unsigned char uchar;
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
unsigned int counter;
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
unsigned int tmp;
unsigned int n1, n2, n3, n4;
unsigned char Ht1621Tab[]={0x00,0x00,0x00,0x00};
unsigned char DispTab[]={0x7B,0x12,0x67,0x57,0x1E,0x5D,0x7D,0x13,0x7F,0x5F,0x69,0x45,0x3f, 0x7a, 0x3e};
// 0 1 2 3 4 5 6 7 8 9 C 三 A V H
#define BIAS 0x52 //0b1000 0101 0010 1/3duty 4com
#define SYSDIS 0X00 //0b1000 0000 0000 关振系统荡器和LCD偏压发生器
#define SYSEN 0X02 //0b1000 0000 0010 打开系统振荡器
#define LCDOFF 0X04 //0b1000 0000 0100 关LCD偏压
#define LCDON 0X06 //0b1000 0000 0110 打开LCD偏压
#define XTAL 0x28 //0b1000 0010 1000 外部接时钟
#define RC256 0X30 //0b1000 0011 0000 内部时钟
#define WDTDIS1 0X0A //0b1000 0000 1010 禁止看门狗
////////////////////////////////////////////////////////////////////////////////
void Ht1621_CS_0() { Gpio_ClrIO(GpioPortB, GpioPin5);} // define P2.2 ---> CS
void Ht1621_CS_1() { Gpio_SetIO(GpioPortB, GpioPin5);}
void Ht1621_WR_0() { Gpio_ClrIO(GpioPortB, GpioPin7);} // define P2.1 ---> WR
void Ht1621_WR_1() { Gpio_SetIO(GpioPortB, GpioPin7);}
void Ht1621_DO_0() { Gpio_ClrIO(GpioPortB, GpioPin8);} // define P2.0 ---> DAT
void Ht1621_DO_1() { Gpio_SetIO(GpioPortB, GpioPin8);}
//-----------------------------------------------------------------------------------------
//函数名称:Delay()
//功 能:延时子程序
//-----------------------------------------------------------------------------------------
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
//-----------------------------------------------------------------------------------------
//函数名称:Delayms()
//-----------------------------------------------------------------------------------------
void Delayms(unsigned int ims)
{
unsigned int i,j;
for(i=0;i<ims;i++)
for(j=0;j<65;j++) { Delay(1); }
}
//-----------------------------------------------------------------------------------------
//Ht1621Wr_Data()
//-----------------------------------------------------------------------------------------
void Ht1621Wr_Data(unsigned char Data,unsigned char cnt)
{
unsigned char i;
for (i=0;i<cnt;i++)
{
Ht1621_WR_0();
if((Data & 0x80)==0x80) {Ht1621_DO_1();}
else {Ht1621_DO_0();}
Ht1621_WR_1();
Data<<=1;
}
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrCmd(uchar Cmd)
//-----------------------------------------------------------------------------------------
void Ht1621WrCmd(unsigned char Cmd)
{
Ht1621_CS_0();
Ht1621Wr_Data(0x80,4); //写入命令标志100
Ht1621Wr_Data(Cmd,8); //写入命令数据
Ht1621_CS_1();
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrOneData(uchar Addr,uchar Data)
//-----------------------------------------------------------------------------------------
void Ht1621WrOneData(unsigned char Addr,unsigned char Data)
{
Ht1621_CS_0();
Ht1621Wr_Data(0xa0,3); //写入数据标志101
Ht1621Wr_Data(Addr<<2,6); //写入地址数据
Ht1621Wr_Data(Data,4); //写入数据的前四位 7 6 5 4
Ht1621_CS_1();
}
//-----------------------------------------------------------------------------------------
//void Ht1621WrAllData()
//-----------------------------------------------------------------------------------------
void Ht1621WrAllData(unsigned char Addr,unsigned char *p,unsigned char cnt)
{
unsigned char i;
Ht1621_CS_0();
Ht1621Wr_Data(0xa0,3); //写入数据标志101
Ht1621Wr_Data(Addr<<2,6); //写入地址数据
for (i=0;i<cnt;i++)
{
Ht1621Wr_Data(*p,8); //写入数据
p++;
}
Ht1621_CS_1();
}
//-----------------------------------------------------------------------------------------
//void Ht1621_Init(void)
//-----------------------------------------------------------------------------------------
void Ht1621_Init(void)
{
Ht1621WrCmd(BIAS);
Ht1621WrCmd(RC256); //使用内部振荡器
//Ht1621WrCmd(XTAL); //使用外部振荡器
Ht1621WrCmd(SYSDIS);
Ht1621WrCmd(WDTDIS1);
Ht1621WrCmd(SYSEN);
Ht1621WrCmd(LCDON);
}
//-----------------------------------------------------------------------------------------
//DISPLAY
//-----------------------------------------------------------------------------------------
void Display(void)
{
unsigned char com;
com = 0;
if((DispTab[n1]&0x01) == 0x01){ com = com + 0x10;}
if((DispTab[n2]&0x01) == 0x01){ com = com + 0x20;}
if((DispTab[n3]&0x01) == 0x01){ com = com + 0x40;}
if((DispTab[n4]&0x01) == 0x01){ com = com + 0x80;}
Ht1621WrOneData(0 , com);
com = 0;
if((DispTab[n1]&0x02) == 0x02){ com = com + 0x10;}
if((DispTab[n2]&0x02) == 0x02){ com = com + 0x20;}
if((DispTab[n3]&0x02) == 0x02){ com = com + 0x40;}
if((DispTab[n4]&0x02) == 0x02){ com = com + 0x80;}
Ht1621WrOneData(1 , com);
com = 0;
if((DispTab[n1]&0x10) == 0x10){ com = com + 0x10;}
if((DispTab[n2]&0x10) == 0x10){ com = com + 0x20;}
if((DispTab[n3]&0x10) == 0x10){ com = com + 0x40;}
if((DispTab[n4]&0x10) == 0x10){ com = com + 0x80;}
Ht1621WrOneData(2 , com);
com = 0;
if((DispTab[n1]&0x40) == 0x40){ com = com + 0x10;}
if((DispTab[n2]&0x40) == 0x40){ com = com + 0x20;}
if((DispTab[n3]&0x40) == 0x40){ com = com + 0x40;}
if((DispTab[n4]&0x40) == 0x40){ com = com + 0x80;}
Ht1621WrOneData(3 , com);
com = 0;
if((DispTab[n1]&0x20) == 0x20){ com = com + 0x10;}
if((DispTab[n2]&0x20) == 0x20){ com = com + 0x20;}
if((DispTab[n3]&0x20) == 0x20){ com = com + 0x40;}
if((DispTab[n4]&0x20) == 0x20){ com = com + 0x80;}
Ht1621WrOneData(4 , com);
com = 0;
if((DispTab[n1]&0x08) == 0x08){ com = com + 0x10;}
if((DispTab[n2]&0x08) == 0x08){ com = com + 0x20;}
if((DispTab[n3]&0x08) == 0x08){ com = com + 0x40;}
if((DispTab[n4]&0x08) == 0x08){ com = com + 0x80;}
Ht1621WrOneData(5 , com);
com = 0;
if((DispTab[n1]&0x04) == 0x04){ com = com + 0x10;}
if((DispTab[n2]&0x04) == 0x04){ com = com + 0x20;}
if((DispTab[n3]&0x04) == 0x04){ com = com + 0x40;}
if((DispTab[n4]&0x04) == 0x04){ com = com + 0x80;}
Ht1621WrOneData(6 , com);
}
//-----------------------------------------------------------------------------------------
//Display_lcd_dot
//-----------------------------------------------------------------------------------------
void Display_lcd_dot(void)
{
Ht1621WrOneData(7 , 0x50);
}
//-----------------------------------------------------------------------------------------
//convertor()
//-----------------------------------------------------------------------------------------
void data_convertor(unsigned long adc_value)
{
tmp=adc_value; //adc
n4=tmp/100;
tmp=tmp%100;
n3=tmp/10;
tmp=tmp%10;
n2=tmp;
// 4
n1=10; //display "C"
}
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
******************************************************************************
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/
int32_t main(void)
{
stc_gpio_config_t pstcGpioCfg;
counter = 235;
///< 打开GPIO外设时钟门控
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
///< 端口方向配置->输出
pstcGpioCfg.enDir = GpioDirOut;
///< 端口驱动能力配置->高驱动能力
pstcGpioCfg.enDrv = GpioDrvH;
///< 端口上下拉配置->无上下拉
pstcGpioCfg.enPuPd = GpioNoPuPd;
///< 端口开漏输出配置->开漏输出关闭
pstcGpioCfg.enOD = GpioOdDisable;
///< 端口输入/输出值寄存器总线控制模式配置->AHB
pstcGpioCfg.enCtrlMode = GpioAHB;
///< GPIO IO PD05初始化(PD05在STK上外接LED)
Gpio_Init(GpioPortD, GpioPin5, &pstcGpioCfg);
Gpio_Init(GpioPortB, GpioPin5, &pstcGpioCfg);
Gpio_Init(GpioPortB, GpioPin7, &pstcGpioCfg);
Gpio_Init(GpioPortB, GpioPin8, &pstcGpioCfg);
Ht1621_Init(); //上电初始化LCD
Delay(100); //延时一段时间
Ht1621WrAllData(0,Ht1621Tab,16);// Clear LCD display
Ht1621_Init();
while(1)
{
data_convertor(counter);
Display();
Display_lcd_dot();
Delayms(1000);
counter++;
}
}
效果图:
|
共2人点赞
|