ht1621驱动数码管

ht1621驱动数码管显示0-9 a-z

我的数码管引脚如下图

ht1621b.c

#include "ht1621b.h"

static void ht1621_send_high_order_data(u8 data, u8 len)
{
	u8 i;

	for (i=0; i<len; i++)
	{
        HT1621_WR_CLR();
		delay(10);
		if((data&0x80) == 0)
		{
			HT1621_DATA_CLR();
		}
		else
		{
			HT1621_DATA_SET();
		}
		HT1621_WR_SET();
		delay(10);

		data <<= 1;
	}
}

static void ht1621_send_low_order_data(u8 data, u8 len)
{
	u8 i;

	for (i=0; i<len; i++)
	{
        HT1621_WR_CLR();
		delay(10);
		if((data&0x01) == 0)
		{
			HT1621_DATA_CLR();
		}
		else
		{
			HT1621_DATA_SET();
		}
		HT1621_WR_SET();
		delay(10);

		data >>= 1;
	}
}

static void ht1621_send_cmd(u8 command)
{
	HT1621_CS_CLR();
	ht1621_send_high_order_data(0x80, 4);
	ht1621_send_high_order_data(command, 8);
	HT1621_CS_SET();
	delay(10);
}

static void ht1621_write(u8 addr, u8 data)
{
	HT1621_CS_CLR();
	ht1621_send_high_order_data(0xA0, 3);
	ht1621_send_high_order_data(addr<<2, 6);
	ht1621_send_low_order_data(data, 4);
	HT1621_CS_SET();
	delay(10);
}

static void ht1621_write_all(u8 addr, u8 *p, u8 len)
{
	u8 i;

	HT1621_CS_CLR();
	ht1621_send_high_order_data(0xA0, 3);
	ht1621_send_high_order_data(addr<<2, 6);

	for (i=0; i<len; i++, p++)
	{
		ht1621_send_low_order_data(*p, 8);
	}
	HT1621_CS_SET();
}

/* 清屏 */
static void ht1621_all_display()
{
    u8 i;

    HT1621_CS_CLR();

	ht1621_send_high_order_data(0xA0, 3);
	ht1621_send_high_order_data(0x00<<2, 6);
    for(i=0;i<32;i++)
    {
        ht1621_send_low_order_data(0x00,8);
    }

    HT1621_CS_SET();
}

void ht1621_init()
{
	printf(">>>>ht1621_init");
	HT1621_CS_INIT();
	HT1621_WR_INIT();
	HT1621_DATA_INIT();

    HT1621_CS_SET();
    HT1621_WR_SET();
    HT1621_DATA_SET();
    delay_2ms(50);
    ht1621_send_cmd(HT_BISA_COM);
    ht1621_send_cmd(HT_RCOSC);
    ht1621_send_cmd(HT_SYS_EN);
    ht1621_send_cmd(HT_LCD_ON);

	ht1621_all_display();//clear
	/* 测试引脚是否被其他地方占用 */
//	  HT1621_DATA_SET();
//	  HT1621_WR_SET();
//	  HT1621_CS_SET();

//	  HT1621_DATA_CLR();
//	  HT1621_WR_CLR();
//	  HT1621_CS_CLR();

    display_test();
}
#if 0
void set_lcd_on()
{
	ht1621_send_cmd(HT_LCD_ON);
}

void set_lcd_off()
{
	ht1621_send_cmd(HT_LCD_OFF);
}
#endif

//==============================================================================
static u8 Buffer[5];

#define CHAR_A_OFFSET	10
#define MASK_STRING		0x7F
#define MAX_STRING_LEN	4

/* 存放字模 */
static const DISP_TYPE DispCharSet[] =
{
	NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9,
	CHR_A, CHR_B, CHR_C, CHR_D, CHR_E, CHR_F, CHR_G, CHR_H, CHR_I, CHR_J,
	CHR_K, CHR_L, CHR_M, CHR_N, CHR_O, CHR_P, CHR_Q, CHR_R, CHR_S, CHR_T,
	CHR_U, CHR_V, CHR_W, CHR_X, CHR_Y, CHR_Z,
};

/* 用来把字模转换格式,转成显示驱动芯片的格式 */
static const DISP_TYPE buf_char[4][2] = {
    {(1<<0), (1<<4)},
    {(1<<1), (1<<5)},
    {(1<<2), (1<<6)},
    {(1<<3), (1<<7)},
};

/* 指定位置显示一个字符 */
bool Led7DrawChar(DISP_TYPE Char, uint8_t DisCharPos)
{
    uint8_t i;
	DISP_TYPE DispVal;

	if(DisCharPos >= MAX_STRING_LEN)
	{
		return FALSE;
	}

    if((Char <= 'Z') && (Char >= 'A'))
	{
		DispVal = DispCharSet[Char - 'A' + CHAR_A_OFFSET];
	}
	else if((Char <= 'z') && (Char >= 'a'))
	{
		DispVal = DispCharSet[Char - 'a' + CHAR_A_OFFSET];
	}
	else if((Char <= '9') && (Char >= '0'))
	{
		DispVal = DispCharSet[Char - '0'];
	}
	else if((Char <= 9) && (Char >= 0))
	{
		DispVal = DispCharSet[Char];
	}
	else if(Char == '-')
	{
		DispVal = CHR_;
	}
	else
	{
		DispVal = CHR_NUL;
	}

    //printf("==>%02X\n", DispVal);

    for(i = 0; i < 7; i++) //写入数据
    {
        if (DispVal & 0x01)
        {
            Buffer[i/2] |= buf_char[DisCharPos][i%2];
            //printf("1==>%02X\n", buf_char[DisCharPos][i%2]);
        }
        else
        {
            Buffer[i/2] &= ~buf_char[DisCharPos][i%2];
            //printf("0==>%02X\n", ~buf_char[DisCharPos][i%2]);
        }
        DispVal >>= 1;
    }

    //printf_buf(Buffer, 5);

	return TRUE;
}

/* 显示字符串 */
void Led7DrawStr(char* Str)
{
	uint8_t CharCnt = 0;
	while(*Str)
	{
		if(!Led7DrawChar(*Str++, CharCnt++))
		{
			break;
		}
	}
}

/* 发送显存到驱动芯片 */
void Led7SendBuffer(void)
{
    ht1621_write_all(0, Buffer, 5);
}
#if 1
void display_test(void)
{
//    u8 a = (1<<1);
//    a |= (1<<0);
//
//    /* 测试显示数字1 */
//    ht1621_write(0, a);
//    ht1621_write(1, a);
//    ht1621_write(3, a);
//    ht1621_write(4, a);
//    ht1621_write(6, a);

    Led7DrawChar(1, 0);
    Led7DrawChar(2, 1);
    Led7DrawChar(3, 2);
    Led7DrawChar(4, 3);
    Led7SendBuffer();
    delay_2ms(500);
    Led7DrawStr("ABCD");
    Led7SendBuffer();

    ht1621_write(7, 0x01);
}
#endif

ht1621b.h

#ifndef _HT1621B_H
#define _HT1621B_H

#include "includes.h"

#define HT_BISA_COM			0x52		//(1<<1) | (0<<3) | (1<<4) | (0<<5) | (1<<6) | (0<<7) | (0<<8) | (0<<9) | (0<<10) | (1<<11)
#define HT_LCD_OFF			0x04		//(0<<1) | (1<<2) | (0<<4) | (0<<5) | (0<<6) | (0<<7) | (0<<8) | (0<<9) | (0<<10) | (1<<11)
#define HT_LCD_ON			0x06		//(1<<1) | (1<<2) | (0<<4) | (0<<5) | (0<<6) | (0<<7) | (0<<8) | (0<<9) | (0<<10) | (1<<11)
#define HT_WRITE_CMD		0x80		//(0<<0) | (0<<1) | (1<<2)  | (0<<3) | (1<<4) | (0<<5) | (1<<6) | (0<<7) | (1<<8)
#define HT_WRITE_DATA		0xA0
#define HT_SYS_EN			0x02
#define HT_SYS_DIS			0x00
#define HT_RCOSC			0x30

//#define  HT1621_CS_SET()        do{JL_PORTB->PU |= BIT(8);JL_PORTB->DIR &= ~BIT(8);JL_PORTB->OUT |= BIT(8);}while(0)
//#define  HT1621_CS_CLR()        do{JL_PORTB->PU |= BIT(8);JL_PORTB->DIR &= ~BIT(8);JL_PORTB->OUT &= ~BIT(8);}while(0)
//
//#define  HT1621_WR_SET()        do{JL_PORTB->PU |= BIT(9);JL_PORTB->DIR &= ~BIT(9);JL_PORTB->OUT |= BIT(9);}while(0)
//#define  HT1621_WR_CLR()        do{JL_PORTB->PU |= BIT(9);JL_PORTB->DIR &= ~BIT(9);JL_PORTB->OUT &= ~BIT(9);}while(0)
//
//#define  HT1621_DATA_SET()      do{JL_PORTB->PU |= BIT(10);JL_PORTB->DIR &= ~BIT(10);JL_PORTB->OUT |= BIT(10);}while(0)
//#define  HT1621_DATA_CLR()      do{JL_PORTB->PU |= BIT(10);JL_PORTB->DIR &= ~BIT(10);JL_PORTB->OUT &= ~BIT(10);}while(0)

/* PB8 */
#define HT1621_CS_INIT() \
    do { \
        JL_PORTB->DIR &= ~BIT(8); \
        JL_PORTB->DIE |= BIT(8); \
        JL_PORTB->PU &= ~BIT(8); \
        JL_PORTB->PD &= ~BIT(8); \
    } while(0)
#define HT1621_CS_SET()     (JL_PORTB->OUT |= BIT(8))
#define HT1621_CS_CLR()     (JL_PORTB->OUT &= ~BIT(8))
/* PB9 */
#define HT1621_WR_INIT() \
    do { \
        JL_PORTB->DIR &= ~BIT(9); \
        JL_PORTB->DIE |= BIT(9); \
        JL_PORTB->PU &= ~BIT(9); \
        JL_PORTB->PD &= ~BIT(9); \
    } while(0)
#define HT1621_WR_SET()     (JL_PORTB->OUT |= BIT(9))
#define HT1621_WR_CLR()     (JL_PORTB->OUT &= ~BIT(9))
/* PB10 */
#define HT1621_DATA_INIT() \
    do { \
        JL_PORTB->DIR &= ~BIT(10); \
        JL_PORTB->DIE |= BIT(10); \
        JL_PORTB->PU &= ~BIT(10); \
        JL_PORTB->PD &= ~BIT(10); \
    } while(0)
#define HT1621_DATA_SET()     (JL_PORTB->OUT |= BIT(10))
#define HT1621_DATA_CLR()     (JL_PORTB->OUT &= ~BIT(10))



#define DISP_TYPE	uint8_t
//SEG定义
#define SEGA_BITNO	0
#define SEGB_BITNO	1
#define SEGC_BITNO	2
#define SEGD_BITNO	3
#define SEGE_BITNO	4
#define SEGF_BITNO	5
#define SEGG_BITNO	6
#define SEGH_BITNO	7

#define	SEG_A	(1 << SEGA_BITNO)
#define	SEG_B	(1 << SEGB_BITNO)
#define	SEG_C	(1 << SEGC_BITNO)
#define	SEG_D	(1 << SEGD_BITNO)
#define	SEG_E	(1 << SEGE_BITNO)
#define	SEG_F	(1 << SEGF_BITNO)
#define	SEG_G	(1 << SEGG_BITNO)
#define	SEG_H	(1 << SEGH_BITNO)

#define	NUM_0	(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F)
#define	NUM_1	(SEG_B | SEG_C)
#define	NUM_2	(SEG_A | SEG_B | SEG_D | SEG_E | SEG_G)
#define	NUM_3	(SEG_A | SEG_B | SEG_C | SEG_D | SEG_G)
#define	NUM_4	(SEG_B | SEG_C | SEG_F | SEG_G)
#define	NUM_5	(SEG_A | SEG_C | SEG_D | SEG_F | SEG_G)
#define	NUM_6	(SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G)
#define	NUM_7	(SEG_A | SEG_B | SEG_C)
#define	NUM_8	(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G)
#define	NUM_9	(SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G)

#define	CHR_A	(SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G)
#define	CHR_B	(SEG_C | SEG_D | SEG_E | SEG_F | SEG_G)
#define	CHR_C	(SEG_A | SEG_D | SEG_E | SEG_F)
#define	CHR_D	(SEG_B | SEG_C | SEG_D | SEG_E | SEG_G)

#define	CHR_E	(SEG_A | SEG_D | SEG_E | SEG_F | SEG_G)
#define	CHR_F	(SEG_A | SEG_E | SEG_F | SEG_G)
#define	CHR_G	(SEG_A | SEG_C | SEG_D | SEG_E | SEG_F)
#define	CHR_H	(SEG_B | SEG_C | SEG_E | SEG_F | SEG_G)
//#define	CHR_H	(SEG_C | SEG_E | SEG_F | SEG_G)			//为了与X(显示位H)区别开,H显示位'h'
#define	CHR_I	(SEG_E | SEG_F)
#define	CHR_J	(SEG_B | SEG_C | SEG_D)
#define	CHR_K	(SEG_D | SEG_E | SEG_F | SEG_G)
#define	CHR_L	(SEG_D | SEG_E | SEG_F)
#define	CHR_M	(SEG_A | SEG_B | SEG_E | SEG_F)
#define	CHR_N	(SEG_A | SEG_B | SEG_C | SEG_E | SEG_F)
#define	CHR_O	(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F)
#define	CHR_P	(SEG_A | SEG_B | SEG_E | SEG_F | SEG_G)
#define	CHR_Q	(SEG_A | SEG_B | SEG_C | SEG_F | SEG_G)
#define	CHR_R	(SEG_E | SEG_F | SEG_G)
#define	CHR_S	(SEG_A | SEG_C | SEG_D | SEG_F | SEG_G)
#define	CHR_T	(SEG_D | SEG_E | SEG_F | SEG_G)
#define	CHR_U	(SEG_B | SEG_C | SEG_D | SEG_E | SEG_F)
#define	CHR_V	(SEG_C | SEG_D | SEG_E)
#define	CHR_W	(SEG_C | SEG_D | SEG_E | SEG_F)
#define	CHR_X	(SEG_B | SEG_C | SEG_E | SEG_F | SEG_G)
#define	CHR_Y	(SEG_B | SEG_C | SEG_D | SEG_F | SEG_G)
#define	CHR_Z	(SEG_A | SEG_D)
#define	CHR_	(SEG_G)
#define	CHR_NUL 0

bool Led7DrawChar(DISP_TYPE Char, uint8_t DisCharPos);
void Led7DrawStr(char* Str);
void Led7SendBuffer(void);

void ht1621_init(void);
void display_test(void);

#endif

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值