利用STM32来驱动ZLG7289(最多驱动8位数码管,64个独立键盘),驱动代码如下:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
硬件连接:
PB12-> INT(浮空输入), PB13-> CS(出),PB14-> CLK(出), PB15-> DAT(出+输入浮空)
STM32端口配置:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ZLG7289驱动:
//**************** 7289读写相关操作 (SPI通信程序驱动程序)*******************
void short_delay(void) //确定SPI读写函数中数据延时,不得轻易更改
{
u8 i;
for (i=0;i<250;i++);
}
void send_byte( u8 out_byte)
{
u8 i;
GPIO_ResetBits(GPIOB, GPIO_Pin_13); //7289失能cs=0
for (i=0;i<8;i++)
{
if (out_byte&0x80)
{
GPIO_SetBits(GPIOB, GPIO_Pin_15); // dat="1";
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_15); //dat=0;
}
GPIO_SetBits(GPIOB, GPIO_Pin_14); // clk="1";
short_delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_14); //clk=0;
short_delay();
out_byte=out_byte*2; //实现左移
}
GPIO_ResetBits(GPIOB, GPIO_Pin_15); //dat=0;
}
void write7279(u8 cmd, u8 dta)
{
send_byte (cmd);
send_byte (dta);
GPIO_SetBits(GPIOB, GPIO_Pin_13);
}
u8 read7279(u8 command)
{
u8 temp;
send_byte(command);
temp = receive_byte();
GPIO_SetBits(GPIOB, GPIO_Pin_13); //细分器失能cs=1
return(temp);
}
u8 receive_byte(void)
{
u8 i,j, in_byte;
GPIO_SetBits(GPIOB, GPIO_Pin_15); // dat="1";
short_delay();
for (i=0;i<8;i++)
{
GPIO_SetBits(GPIOB, GPIO_Pin_14); // clk="1";
short_delay();
in_byte=in_byte*2;
j= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
if (j)
{
in_byte=in_byte|0x01;
}
GPIO_ResetBits(GPIOB, GPIO_Pin_14); //clk=0;
short_delay();
}
GPIO_ResetBits(GPIOB, GPIO_Pin_15); //dat=0;
return (in_byte);
}
//********************** 7289驱动数码管程序(这些和具体项目相关)***********//
void DisplayData(u16 disData) //最多显示5位数,取消高位零显示
{
Bit flag = ZERO;
u8 temp;
//第4位显示
temp = disData/10000;
if(temp!=0)
{
flag = ONE;
}
if(flag != 0)
{
write7279(DECODE0+4,temp); //+4使指令码后三位加4,实现数码管位选
}
else
{
write7279(DECODE0+4,0x<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0f); //无显示
}
//第3位显示
temp = disData/1000%10;
if(temp!=0)
{
flag = ONE;
}
if(flag != 0)
{
write7279(DECODE0+3,temp);
}
else
{
write7279(DECODE0+3,0x0f);
}
//第2位显示
temp = disData/100%10;
if(temp!=0)
{
flag = ONE;
}
if(flag != 0)
{
write7279(DECODE0+2,temp);
}
else
{
write7279(DECODE0+2,0x0f);
}
//第1位显示
temp = disData/10%10;
if(temp!=0)
{
flag = ONE;
}
if(flag != 0)
{
write7279(DECODE0+1,temp);
}
else
{
write7279(DECODE0+1,0x0f);
}
//第0位显示
temp = disData%10;
write7279(DECODE0,temp);
}
void DisplayDirection(DIRECTION dir) //显示方向
{
if(dir == RIGHT) //显示"BAC"
{
write7279(DECODE0+4,0x0f); //无显示(清除该位数值)
write7279(DECODE0+3,0x0f); //无显示(清除该位数值)
write7279(DECODE1+2,0x0b); //显B
write7279(DECODE1+1,0x0a); //显A
write7279(DECODE1,0x0c); //显C
}
else //显示"FOR"
{
write7279(DECODE0+4,0x0f); //
write7279(DECODE0+3,0x0f);
write7279(DECODE1+2,0x0f);
write7279(DECODE1+1,0x00);
write7279(DECODE1,0x0a);
}
}
void display_common(void) //状体标示图形显示
{
write7279(UNDECODE+7,0x4e);
write7279(UNDECODE+6,0x48);
write7279(UNDECODE+5,0xf8);
}
void display_steps(void)
{
write7279(UNDECODE+7,0x77);
write7279(UNDECODE+6,0x77);
write7279(UNDECODE+5,0xf7);
}
void display_direction(void)
{
write7279(UNDECODE+7,0x1f);
write7279(UNDECODE+6,0x1f);
write7279(UNDECODE+5,0x9f);
}
void display_position(void)
{
write7279(UNDECODE+7,0x3d);
write7279(UNDECODE+6,0x3d);
write7279(UNDECODE+5,0xbd);
}
void display_subdivision(void)
{
write7279(UNDECODE+7,0x4e);
write7279(UNDECODE+6,0x4e);
write7279(UNDECODE+5,0xce);
}
用户1633888 2013-11-21 22:40
用户377235 2012-10-15 18:22
用户234619 2010-4-25 20:17
用户237194 2009-12-19 23:33
用户377540 2009-11-29 19:09
用户1397278 2009-10-11 19:09
huli184_389376486 2009-9-27 01:07
bitao1983_395643617 2009-5-22 08:54
用户1579481 2009-5-14 16:48
用户1103462 2009-5-14 11:13