键盘显示控制芯片HD7279子程序
/*键盘显示控制芯片HD7279子程序(C)
佚名 2005-08-14 00:00:00 robotsy.com */
/************************************************************************************/
/*程序作用:测试
/*程序功能:按下号键显示、、、,按下号键显示c、d、e、f,按下号键显示、、、 */ /* 按下号键显示、、a、b, */
/************************************************************************************/
#include /*定义头文件*/
/************************************************************************************/
typedef unsigned char BYTE; /*自定义字节类型*/
unsigned int opt1,opt2,result; /*自定义字节类型*/
/************************************************************************************/
#define Set_Bit(BIT) (BIT = 1) /*定义置函数*/
#define Clear_Bit(BIT) (BIT = 0) /*定义清函数*/
#define HD7279_TEST 0xbf //测试
#define HD7279_RLC 0xa3 //循环左移
#define HD7279_RRC 0xa2 //循环右移
#define HD7279_RL 0xa1 //左移
#define HD7279_RR 0xa0 //右移
#define HD7279_DECODE0 0x80 //译码方式
#define HD7279_DECODE1 0xc8 //译码方式
#define HD7279_UNDECODE 0x90 //译码方式: 不译码
#define HD7279_HIDE 0x98 //消隐
#define HD7279_FLASH 0x88 //闪烁
#define HD7279_SEGON 0xe0 //段亮
#define HD7279_SEGOFF 0xc0 //段灭
#define HD7279_READ 0x15 //读
/************************************************************************************/
void Led_On(BYTE); /*定义点亮LED灯函数*/
void Led_Off(BYTE); /*定义熄灭LED灯函数*/
void Write_HD7279(BYTE,BYTE); /*定义HD7279写函数*/
BYTE Read_HD7279(BYTE); /*定义HD7279读函数*/
void Send_Byte(BYTE); /*定义HD7279发送字节函数*/
BYTE Receive_Byte(void); /*定义HD7279接收字节函数*/
void Short_Delay(void); /*定义短延时函数*/
void Long_Delay(void); /*定义长延时函数*/
void Mcu_Init(void); /*定义MCU初始化函数*/
void Delay_200_mS(void); /*定义ms延时函数*/
/************************************************************************************/
BYTE Keyboard_Out; /*定义键值变量*/
bit Keyboard_Flag; /*定义按键标志*/
/************************************************************************************/
sbit HD7279_Key=P3^3; /*定义HD7279中断硬件连接--->INT1*/
sbit HD7279_Clk=P1^0; /*定义HD7279时钟硬件连接*/
sbit HD7279_Data=P1^1; /*定义HD7279数据硬件连接*/
sbit HD7279_CS=P2^6;
/*参照MCUdesign原理图
/************************************************************************************/
void main(void) /*主函数*/
{
Mcu_Init(); /*MCU初始化*/
opt1=13;
opt2=34;
result=opt1+opt2;
while(1)
{
if(Keyboard_Flag)
{
Clear_Bit(Keyboard_Flag);
if(Keyboard_Out==0) /*0号键处理*/
{
}
else if(Keyboard_Out==1) /*1号键处理*/
{ /*显示、、、*/
Write_HD7279(0xc9,0x03);
Write_HD7279(0xca,0x02);
Write_HD7279(0xcb,0x01);
Write_HD7279(0xcc,0x00);
}
else if(Keyboard_Out==2) /*2号键处理*/
{ /*显示c、d、e、f*/
Write_HD7279(0xc9,0x0f);
Write_HD7279(0xca,0x0e);
Write_HD7279(0xcb,0x0d);
Write_HD7279(0xcc,0x0c);
}
else if(Keyboard_Out==3) /*3号键处理*/
{ /*显示、、、*/
Write_HD7279(0xc9,0x07);
Write_HD7279(0xca,0x06);
Write_HD7279(0xcb,0x05);
Write_HD7279(0xcc,0x04);
}
else /*4号键处理*/
{ /*显示、、a、b*/
Write_HD7279(0xc9,0x0b);
Write_HD7279(0xca,0x0a);
Write_HD7279(0xcb,0x09);
Write_HD7279(0xcc,0x08);
}
Delay_200_mS();
}
}
}
/***********************************************************************************/
void Delay_200_mS(void) /*500ms延时函数*/
{
BYTE i,j;
i=200;
while (i--)
{
j=228;
while(j--);
}
}
/************************************************************************************/
void Mcu_Init(void) /*MCU初始化函数*/
{
Set_Bit(EX1); /*外部中断使能*/
Set_Bit(IT1); /*外部中断下降沿触发*/
Set_Bit(EA); /*中断使能*/
HD7279_CS=0;
Send_Byte(0xa4); /*HD7279复位*/
Write_HD7279(0x98,0x0f); /*HD7279启动消隐功能(高位消隐),能提高灯的亮度*/
Write_HD7279(0xc8,0x03); /*上电显示、、、,方式译码:0xc8,显示位置xc8+(0~7) */
Write_HD7279(0xc9,0x04);
Write_HD7279(0xca,0x05);
Write_HD7279(0xcb,0x06);
}
/************************************************************************************/
void int1_HD7279key(void) interrupt 2 /*外部中断函数*/
{
Keyboard_Out=Read_HD7279(0x15); /*读取键值*/
if(Keyboard_Out<=0x04)
{
Set_Bit(Keyboard_Flag); /*设置键标志*/
}
}
/************************************************************************************/
void Short_Delay(void) /*短延时函数*/
{
BYTE i;
for(i=0;i<0x08;i++);
}
/************************************************************************************/
void Long_Delay(void) /*长延时函数*/
{
BYTE i;
for(i=0;i<0x30;i++);
}
/************************************************************************************/
void Write_HD7279(BYTE Command,BYTE Data1) /*HD7279写函数*/
{
Send_Byte(Command);
Send_Byte(Data1);
}
/************************************************************************************/
BYTE Read_HD7279(BYTE Command) /*HD7279读函数*/
{
Send_Byte(Command);
return(Receive_Byte());
}
/************************************************************************************/
void Send_Byte(BYTE Data_Out) /*HD7279发送字节函数*/
{
BYTE i;
Long_Delay();
for(i=0;i<8;i++)
{
if(Data_Out&0x80)
Set_Bit(HD7279_Data);
else Clear_Bit(HD7279_Data);
Set_Bit(HD7279_Clk);
Short_Delay(); //8us
Clear_Bit(HD7279_Clk);
Short_Delay();
Data_Out=Data_Out<<1;
}
Clear_Bit(HD7279_Data);
}
/************************************************************************************/
BYTE Receive_Byte(void) /*HD7279接收字节函数*/
{
BYTE i,Data_In;
Set_Bit(HD7279_Data);
Long_Delay();
for(i=0;i<8;i++)
{
Set_Bit(HD7279_Clk);
Short_Delay();
Data_In=Data_In<<1;
if(HD7279_Data)
Data_In=Data_In|0x01;
Clear_Bit(HD7279_Clk);
Short_Delay();
}
Clear_Bit(HD7279_Data);
return(Data_In);
}
/************************************************************************************/