stm32 采用的是4线SPI方式连接。操作命令是12位的数据,采用的是io模拟的方式驱动。试过使用硬件SPI驱动,但是命令只支持12位,硬件SPI发送的是8位或者16位,是驱动不起来的。
常用指令如下,
/* CH451和CH452的常用命令码 */
#define CH452_NOP 0x0000 // 空操作
#define CH452_RESET 0x0201 // 复位
#define CH452_LEVEL 0x0100 // 加载光柱值,需另加7位数据
#define CH452_CLR_BIT 0x0180 // 段位清0,需另加6位数据
#define CH452_SET_BIT 0x01C0 // 段位置1,需另加6位数据
#define CH452_SLEEP 0x0202 // 进入睡眠状态
#define CH452_LEFTMOV 0x0300 // 设置移动方式-左移
#define CH452_LEFTCYC 0x0301 // 设置移动方式-左循环
#define CH452_RIGHTMOV 0x0302 // 设置移动方式-右移
#define CH452_RIGHTCYC 0x0303 // 设置移动方式-右循环
#define CH452_SELF_BCD 0x0380 // 自定义BCD码,需另加7位数据
#define CH452_SYSOFF 0x0400 // 关闭显示、关闭键盘
#define CH452_SYSON1 0x0401 // 开启显示
#define CH452_SYSON2 0x0403 // 开启显示、键盘
#define CH452_SYSON2W 0x0423 // 开启显示、键盘, 真正2线接口
#define CH452_NO_BCD 0x0500 // 设置默认显示方式,可另加3位扫描极限
#define CH452_BCD 0x0580 // 设置BCD译码方式,可另加3位扫描极限
#define CH452_TWINKLE 0x0600 // 设置闪烁控制,需另加8位数据
#define CH452_GET_KEY 0x0700 // 获取按键,返回按键代码
#define CH452_DIG0 0x0800 // 数码管位0显示,需另加8位数据
#define CH452_DIG1 0x0900 // 数码管位1显示,需另加8位数据
#define CH452_DIG2 0x0a00 // 数码管位2显示,需另加8位数据
#define CH452_DIG3 0x0b00 // 数码管位3显示,需另加8位数据
#define CH452_DIG4 0x0c00 // 数码管位4显示,需另加8位数据
#define CH452_DIG5 0x0d00 // 数码管位5显示,需另加8位数据
#define CH452_DIG6 0x0e00 // 数码管位6显示,需另加8位数据
#define CH452_DIG7 0x0f00 // 数码管位7显示,需另加8位数据
// BCD译码方式下的字符
// '0' - '9' 对应 0x00 - 0x09
#define CH452_BCD_SPACE 0x10 //' '
#define CH452_BCD_PLUS 0x11 //'+'
#define CH452_BCD_MINUS 0x12 //'-'
#define CH452_BCD_EQU 0x13 //'='
#define CH452_BCD_LEFT 0x14 //'['
#define CH452_BCD_RIGHT 0x15 //']'
#define CH452_BCD_UNDER 0x16 //'_'
#define CH452_BCD_CH_H 0x17 //'H'
#define CH452_BCD_CH_L 0x18 //'L'
#define CH452_BCD_CH_P 0x19 //'P'
#define CH452_BCD_DOT 0x1A //'.'
#define CH452_BCD_SELF 0x1E //自定义
#define CH452_BCD_TEST 0x88 //'-'
#define CH452_BCD_DOT_X 0x80 //'-'
BCD模式写数据步骤:
1.开启显示 ,写CH452_SYSON1 0x0401。
2.如果采用BCD译码模式,设置BCD模式,写CH452_BCD 0x0580。
3.写0-7数码管 地址命令|8位数据,如 0x0800|0x05, 写数码管0字符‘5’。
如:写'4', ch452_write_cmd(CH452_DIG0|0x04)。
写时序:
void ch452_write_cmd(uint16_t cmd)
{
uint8_t i=0;
for(i=0;i<12;i++)
{
if(cmd & 0x1)
CH452_DIN_H;
else
CH452_DIN_L;
CH452_SCK_L;
//ch452_delay();
CH452_SCK_H;
cmd >>= 1;
}
CH452_LOAD_L;
//ch452_delay();
CH452_LOAD_H;
}
如果是不使用BCD译码模式,8位数据需要写原始位数据。bit7 - bit0位分别对应数码管段
SEG7 - SEG0。
如要写字符 ‘2 .’ ,就要点亮 A,B, G, E, D, DP, 数据 11011011 = 0xDB。
以下采用不译码模式写代码。
代码要求:输入字符 'space' 0 1 2 3 4 5 6 7 8 9 A b c d E F U P - + = .,小数点 1显示 0不显示。
' '空格相当于清除,不显示。
// 'space' 0 1 2 3 4 5 6 7 8 9 A b c d E F U P - + = .
uint8_t charBCD[24] =
{
0x00, //'space'
0x80, //.
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x77, //A
0x7C, //b
0x58, //c
0x5E, //d
0x79, //E
0x71, //F
0x3E, //U
0x73, //P
0x40, //-
0x46, //+
0x48, //=
};
uint16_t chBCD[8] = {CH452_DIG0,CH452_DIG1,CH452_DIG2,CH452_DIG3,CH452_DIG4,CH452_DIG5,CH452_DIG6,CH452_DIG7};
void Ch452ASetCharacter(uint8_t byChannel, char cCh, uint8_t bDP)
{
uint8_t isDP=0;
if(! (byChannel<8))
return;
if(cCh == ' ')
ch452_write_cmd(chBCD[byChannel] | charBCD[0]);
if(bDP)
isDP = 0x80;
if(cCh >= '0' && cCh <= '9')
{
uint8_t index = cCh - 48;
ch452_write_cmd(chBCD[byChannel] | (charBCD[index+2]|isDP));
}
if(cCh >= 'A' && cCh <= 'F')
{
uint8_t index = cCh - 65;
ch452_write_cmd(chBCD[byChannel] | (charBCD[index+12]|isDP));
}
if(cCh >= 'a' && cCh <= 'f')
{
uint8_t index = cCh - 97;
ch452_write_cmd(chBCD[byChannel] | (charBCD[index+12]|isDP));
}
if(cCh == 'U' || cCh == 'u')
ch452_write_cmd(chBCD[byChannel] | (charBCD[18]|isDP));
if(cCh == 'P' || cCh == 'p')
ch452_write_cmd(chBCD[byChannel] | (charBCD[19]|isDP));
if(cCh == '-')
ch452_write_cmd(chBCD[byChannel] | (charBCD[20]|isDP));
if(cCh == '+')
ch452_write_cmd(chBCD[byChannel] | (charBCD[21]|isDP));
if(cCh == '=')
ch452_write_cmd(chBCD[byChannel] | (charBCD[22]|isDP));
}
测试代码,循环显示所有数据字符。
uint16_t delays = 1000;
void CH452_display_Test(void)
{
uint8_t i=0;
Ch452ASetCharacter(1, '0', 1);
HAL_Delay(delays);
Ch452ASetCharacter(2, '1', 1);
HAL_Delay(delays);
Ch452ASetCharacter(3, '2', 1);
HAL_Delay(delays);
Ch452ASetCharacter(4, '3', 1);
HAL_Delay(delays);
Ch452ASetCharacter(5, '4', 1);
HAL_Delay(delays);
Ch452ASetCharacter(6, '5', 1);
HAL_Delay(delays);
Ch452ASetCharacter(7, '6', 1);
HAL_Delay(delays);
for(i=1;i<8;i++)
{
Ch452ASetCharacter(i, ' ', 0);
}
HAL_Delay(delays);
Ch452ASetCharacter(1, '7', 1);
HAL_Delay(delays);
Ch452ASetCharacter(2, '8', 1);
HAL_Delay(delays);
Ch452ASetCharacter(3, '9', 1);
HAL_Delay(delays);
Ch452ASetCharacter(4, 'A', 1);
HAL_Delay(delays);
Ch452ASetCharacter(5, 'B', 1);
HAL_Delay(delays);
Ch452ASetCharacter(6, 'C', 1);
HAL_Delay(delays);
Ch452ASetCharacter(7, 'D', 1);
HAL_Delay(delays);
for(i=1;i<8;i++)
{
Ch452ASetCharacter(i, ' ', 0);
}
HAL_Delay(delays);
Ch452ASetCharacter(1, 'E', 1);
HAL_Delay(delays);
Ch452ASetCharacter(2, 'F', 1);
HAL_Delay(delays);
Ch452ASetCharacter(3, 'U', 1);
HAL_Delay(delays);
Ch452ASetCharacter(4, 'P', 1);
HAL_Delay(delays);
Ch452ASetCharacter(5, '-', 1);
HAL_Delay(delays);
Ch452ASetCharacter(6, '+', 1);
HAL_Delay(delays);
Ch452ASetCharacter(7, '=', 1);
HAL_Delay(delays);
}