你好!我購買了貴公司的CH451,通過MEGA128控制鍵盤和顯示。鍵盤數(shù)值只能讀到7FH,數(shù)碼管不顯示,或偶爾顯示一下。我的程序如下: #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 //數(shù)據(jù)輸入 #define CH451_DIN_LOW PORTA&=~DIN #define CH451_LOAD_HIGH PORTA|=LOAD //命令加載信號,上升延激活 #define CH451_LOAD_LOW PORTA&=~LOAD #define CH451_DOUT PIND&DOUT //鍵盤中斷和鍵值數(shù)據(jù)輸出 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}; //共陰數(shù)碼管。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寫命令函數(shù) void CH451_WRITECommand(unsigned int Command) { unsigned char i; CH451_LOAD_LOW; //命令輸入開始 for(i=0;i<12;i++) { //CH451_DCLK_LOW; if((Command&0x001)) //輸入低位數(shù)據(jù) CH451_DIN_HIGH; else CH451_DIN_LOW; CH451_DCLK_LOW; Command>>=1; //輸入下一位 CH451_DCLK_HIGH; //上升沿有效 } CH451_LOAD_HIGH; //加載數(shù)據(jù) }
//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; //讀取鍵盤數(shù)值 CH451_WRITECommand(CH451_KEY); //發(fā)送讀取鍵盤數(shù)值命令 for(i=0;i<7;i++) //讀取鍵值 { KeyValue<<=1; if(CH451_DOUT) KeyValue|=1; else KeyValue|=0; } a=KeyValue; b=a; }
//CH451顯示一位數(shù)碼管,DIG選擇數(shù)碼管,Data要顯示的數(shù)字,Point是否顯示小數(shù)點 //Point=1顯示小數(shù)點,Point=0不顯示小數(shù)點 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); }