键盘显示控制芯片HD7279子程序(C)

2005年08月14日 00:00:00 来源: robotsy.com 网友评论 点击查看
  • /************************************************************************************//*          *//*      S A G E   T E C H 
/************************************************************************************/
/*          */
/*      S A G E   T E C H .   P R O P R I E T A R Y             */
/*     */
/*      COPYRIGHT (c)   2005 BY SAGE TECH. CHINA.     */
/*            --  ALL RIGHTS RESERVED  --             */
/*     */
/* File Name:        HD7279Atest.c     */
/*  Author:           sagestar     */
/*  Created:          2005     */
/*  Modified:   no          */
/*  Revision:         V1.0     */
/************************************************************************************/
/*程序作用:测试Sage Tech.开发板(MCP300)中hd7279键盘显示芯片是否能正常工作     */
/*程序功能:按下1号键显示0、1、2、3,按下2号键显示c、d、e、f,按下3号键显示4、5、6、7       */
/*    按下4号键显示8、9、a、b,按下0号键点亮4个LED灯     */
/************************************************************************************/
#include <reg51.h> /*定义头文件*/
/************************************************************************************/
typedef unsigned char BYTE; /*自定义字节类型*/
/************************************************************************************/
#define Set_Bit(BIT)          (BIT = 1) /*定义置1函数*/
#define Clear_Bit(BIT)          (BIT = 0) /*定义清0函数*/
/************************************************************************************/
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); /*定义200ms延时函数*/
/************************************************************************************/
BYTE Keyboard_Out; /*定义键值变量*/
bit Keyboard_Flag; /*定义按键标志*/
/************************************************************************************/
sbit Hd7279_Key=P3^2; /*定义HD7279中断硬件连接*/
sbit Hd7279_Clk=P2^1; /*定义HD7279时钟硬件连接*/
sbit Hd7279_Data=P2^0; /*定义HD7279数据硬件连接*/
/************************************************************************************/
void main(void) /*主函数*/
{
Mcu_Init(); /*MCU初始化*/
while(1)
{
if(Keyboard_Flag)
{
Clear_Bit(Keyboard_Flag);
if(Keyboard_Out==0)  /*0号键处理*/
{
Led_On(0x00);
Led_On(0x01);
Led_On(0x02);
Led_On(0x03);
}
else if(Keyboard_Out==1) /*1号键处理*/
{ /*显示0、1、2、3*/
Write_Hd7279(0xc9,0x03);
Write_Hd7279(0xca,0x02);
Write_Hd7279(0xcb,0x01);
Write_Hd7279(0xcc,0x00);                                                                                                                                                                                                                                 Led_Off(0x02);
}
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);                                                                                                                                                                                                                                 Led_Off(0x02);
}
else if(Keyboard_Out==3) /*3号键处理*/
{ /*显示4、5、6、7*/
Write_Hd7279(0xc9,0x07);
Write_Hd7279(0xca,0x06);
Write_Hd7279(0xcb,0x05);
Write_Hd7279(0xcc,0x04);                                                                                                                                                                                                                                 Led_Off(0x02);
}
else  /*4号键处理*/
{ /*显示8、9、a、b*/
Write_Hd7279(0xc9,0x0b);
Write_Hd7279(0xca,0x0a);
Write_Hd7279(0xcb,0x09);
Write_Hd7279(0xcc,0x08);                                                                                                                                                                                                                                 Led_Off(0x02);
}
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(EX0); /*外部中断0使能*/
Set_Bit(IT0); /*外部中断0下降沿触发*/
Set_Bit(EA); /*中断使能*/
Send_Byte(0xa4); /*HD7279复位*/
Write_Hd7279(0x98,0x1f); /*HD7279启动消隐功能,能提高灯的亮度*/
Write_Hd7279(0xc9,0x03); /*上电显示6、5、4、3*/
Write_Hd7279(0xca,0x04);
Write_Hd7279(0xcb,0x05);
Write_Hd7279(0xcc,0x06);
}
/************************************************************************************/
void int1_Hd7279key(void) interrupt 0 /*外部中断0函数*/
{        
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 Led_On(BYTE Led_Num) /*点亮LED灯函数*/
{
Write_Hd7279(0xe0,Led_Num);
}
/************************************************************************************/
void Led_Off(BYTE Led_Num) /*熄灭LED灯函数*/
{
Write_Hd7279(0xc0,Led_Num);
}
/************************************************************************************/
void Write_Hd7279(BYTE Command,BYTE Data) /*HD7279写函数*/

Send_Byte(Command);
   Send_Byte(Data);
}
/************************************************************************************/
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();
     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);
}
/************************************************************************************/ 
/*END*/

【已有位网友发表了看法,点击查看。】
关于本文章 的
匿名
用户名: 密码: 验证码:
      机器视觉在线,机器视觉专家