你好!我购买了贵公司的CH451,通过MEGA128控制键盘和显示。键盘数值只能读到7FH,数码管不显示,或偶尔显示一下。我的程序如下: #include #include #include #define DCLK (1<#define DIN (1<#define LOAD (1<#define DOUT (1<#define CH451_DCLK_HIGH PORTA|=DCLK //时钟信号 #define CH451_DCLK_LOW PORTA&=~DCLK #define CH451_DIN_HIGH PORTA|=DIN //数据输入 #define CH451_DIN_LOW PORTA&=~DIN #define CH451_LOAD_HIGH PORTA|=LOAD //命令加载信号,上升延激活 #define CH451_LOAD_LOW PORTA&=~LOAD #define CH451_DOUT PIND&DOUT //键盘中断和键值数据输出 unsigned char KeyValue;
const unsigned char DSYCode[18]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71, 0x00}; //共阴数码管。0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F,全部熄灭 const unsigned char DigCode[8]={CH451_DIG0,CH451_DIG1,CH451_DIG2,CH451_DIG3,CH451_DIG4,CH451_DIG5,CH451_DIG6,CH451_DIG7};
//CH451写命令函数 void CH451_WRITECommand(unsigned int Command) { unsigned char i; CH451_LOAD_LOW; //命令输入开始 for(i=0;i<12;i++) { //CH451_DCLK_LOW; if((Command&0x001)) //输入低位数据 CH451_DIN_HIGH; else CH451_DIN_LOW; CH451_DCLK_LOW; Command>>=1; //输入下一位 CH451_DCLK_HIGH; //上升沿有效 } CH451_LOAD_HIGH; //加载数据 }
//CH451初始化 void CH451_Init() {DDRA|=DCLK|DIN|LOAD; PORTA|=DCLK|DIN|LOAD; //CH451_DIN_HIGH; CH451_DIN_LOW; //使能串口 // PORTA&=~(1< CH451_DIN_HIGH; //PORTA|=(1< CH451_WRITECommand(CH451_REST); //复位CH451 CH451_WRITECommand(CH451_WKDDK); //开键盘显示 DDRD&=~DOUT; //INT0(PD0)为输入方向,用于键盘中断 PORTD|=DOUT; //INT0(PD0)为高电平,用于下降沿中断 EICRA=0x20; //INT0下降沿中断 EIMSK=0x01; //开INT0 SEI(); }
//CH451通过INT0读取键值 /*unsigned char CH451_GetKeyValue(void) { unsigned char i; unsigned char GetKeyValue=0x00; // }*/
#pragma interrupt_handler int0_isr:2 void int0_isr(void) { unsigned char i,a,b; unsigned char KeyValue=0x00; //读取键盘数值 CH451_WRITECommand(CH451_KEY); //发送读取键盘数值命令 for(i=0;i<7;i++) //读取键值 { KeyValue<<=1; if(CH451_DOUT) KeyValue|=1; else KeyValue|=0; } a=KeyValue; b=a; }
//CH451显示一位数码管,DIG选择数码管,Data要显示的数字,Point是否显示小数点 //Point=1显示小数点,Point=0不显示小数点 void CH451_Display(unsigned char DIG,unsigned char Data,unsigned char Point) { if(Point) CH451_WRITECommand(DigCode[DIG]|(DSYCode[Data]|0x80)); else CH451_WRITECommand(DigCode[DIG]|DSYCode[Data]); }
void main(void) {CH451_Init(); //CH451_WRITECommand(CH451_WKDDK); while(1) { CH451_Display(2,7,0);} //while(1); }