您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > HD7279芯片程序
/************************************************************************************//**//*SAGETECH.PROPRIETARY*//**//*COPYRIGHT(c)2005BYSAGETECH.CHINA.*//*--ALLRIGHTSRESERVED--*//**//*FileName:HD7279Atest.c*//*Author:sagestar*//*Created:2005*//*Modified:no*//*Revision:V1.0*//************************************************************************************//*程式作用:测试SageTech.研发板(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/*定义头文档*//************************************************************************************/typedefunsignedcharBYTE;/*自定义字节类型*//************************************************************************************/#defineSet_Bit(BIT)(BIT=1)/*定义置1函数*/#defineClear_Bit(BIT)(BIT=0)/*定义清0函数*//************************************************************************************/voidLed_On(BYTE);/*定义点亮LED灯函数*/voidLed_Off(BYTE);/*定义熄灭LED灯函数*/voidWrite_Hd7279(BYTE,BYTE);/*定义HD7279写函数*/BYTERead_Hd7279(BYTE);/*定义HD7279读函数*/voidSend_Byte(BYTE);/*定义HD7279发送字节函数*/BYTEReceive_Byte(void);/*定义HD7279接收字节函数*/voidShort_Delay(void);/*定义短延时函数*/voidLong_Delay(void);/*定义长延时函数*/voidMcu_Init(void);/*定义MCU初始化函数*/voidDelay_200_mS(void);/*定义200ms延时函数*//************************************************************************************/BYTEKeyboard_Out;/*定义键值变量*/bitKeyboard_Flag;/*定义按键标志*//************************************************************************************/sbitHd7279_Key=P3^2;/*定义HD7279中断硬件连接*/sbitHd7279_Clk=P2^1;/*定义HD7279时钟硬件连接*/sbitHd7279_Data=P2^0;/*定义HD7279数据硬件连接*//************************************************************************************/voidmain(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);}elseif(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);}elseif(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);}elseif(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();}}}/***********************************************************************************/voidDelay_200_mS(void)/*500ms延时函数*/{BYTEi,j;i=200;while(i--){j=228;while(j--);}}/************************************************************************************/voidMcu_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);}/************************************************************************************/voidint1_Hd7279key(void)interrupt0/*外部中断0函数*/{Keyboard_Out=Read_Hd7279(0x15);/*读取键值*/if(Keyboard_Out=0x04){Set_Bit(Keyboard_Flag);/*配置键标志*/}}/************************************************************************************/voidShort_Delay(void)/*短延时函数*/{BYTEi;for(i=0;i0x08;i++);}/************************************************************************************/voidLong_Delay(void)/*长延时函数*/{BYTEi;for(i=0;i0x30;i++);}/************************************************************************************/voidLed_On(BYTELed_Num)/*点亮LED灯函数*/{Write_Hd7279(0xe0,Led_Num);}/************************************************************************************/voidLed_Off(BYTELed_Num)/*熄灭LED灯函数*/{Write_Hd7279(0xc0,Led_Num);}/************************************************************************************/voidWrite_Hd7279(BYTECommand,BYTEData)/*HD7279写函数*/{Send_Byte(Command);Send_Byte(Data);}/************************************************************************************/BYTERead_Hd7279(BYTECommand)/*HD7279读函数*/{Send_Byte(Command);return(Receive_Byte());}/************************************************************************************/voidSend_Byte(BYTEData_Out)/*HD7279发送字节函数*/{BYTEi;Long_Delay();for(i=0;i8;i++){if(Data_Out&0x80)Set_Bit(Hd7279_Data);elseClear_Bit(Hd7279_Data);Set_Bit(Hd7279_Clk);Short_Delay();Clear_Bit(Hd7279_Clk);Short_Delay();Data_Out=Data_Out1;}Clear_Bit(Hd7279_Data);}/************************************************************************************/BYTEReceive_Byte(void)/*HD7279接收字节函数*/{BYTEi,Data_In;Set_Bit(Hd7279_Data);Long_Delay();for(i=0;i8;i++){Set_Bit(Hd7279_Clk);Short_Delay();Data_In=Data_In1;if(Hd7279_Data)Data_In=Data_In|0x01;Clear_Bit(Hd7279_Clk);Short_Delay();}Clear_Bit(Hd7279_Data);return(Data_In);}/************************************
本文标题:HD7279芯片程序
链接地址:https://www.777doc.com/doc-6298533 .html