相關(guān)程序如下: //外部中斷子程序 SIGNAL(SIG_INTERRUPT0) { _delay_ms(10); key_int_flag=1;

}

//向CH452發(fā)出操作命令 void CH452_Write(unsigned short cmd) { unsigned char temp; for(temp = 0;temp < 12;temp++) //送12位命令字 { if(cmd & 0x01) { ch452_sda_set; //低位先傳送,如果低位為1,數(shù)據(jù)端口輸出高電平 } else { ch452_sda_clr; //低位為0,數(shù)據(jù)端口輸出高電平低電平 } ch452_scl_clr; //時鐘端口置低 cmd >>= 1; //數(shù)據(jù)右移一位 ch452_scl_set; //數(shù)據(jù)端口置高,產(chǎn)生一個上升沿,發(fā)送數(shù)據(jù) } ch452_load_clr; //數(shù)據(jù)加載端口產(chǎn)生一個上升沿,加載數(shù)據(jù) _delay_ms(10); ch452_load_set; }

//從CH452讀取按鍵代碼 unsigned char CH452_Read(void) { unsigned char i,j; unsigned char read,key; read=0x07; ch452_load_clr; for(i=0;i<4;i++) { if(read&0x01)ch452_sda_set; else ch452_sda_clr; ch452_scl_clr; read>>=1; ch452_scl_set; } ch452_load_set; for(j=0;j<7;j++) { key<<=1; key|=ch452_int; ch452_scl_clr; ch452_scl_set; } //key_int_flag=0; return(key); }

//主函數(shù) int main(void) //GCC中main文件必須為返回整形值的函數(shù),沒有參數(shù) { _delay_ms(500); CH452_Write(CH452_RESET); //復(fù)位 CH452_Write(CH452_NOP); //空操作 CH452_Write(CH452_CLR_BIT); //段位地址清零 CH452_Write(CH452_SYSON2); //開顯示、鍵盤 CH452_Write(CH452_BCD); //BCD譯碼方式送顯示數(shù)據(jù) PORTB = 0x07; //PB0\PB1\PB2為輸出高電平 DDRB = 0x07; //PB0\PB1\PB2為輸出方向,分別是數(shù)據(jù)、時鐘、數(shù)據(jù)加載線 INI_INT0(); //外部中斷初始化 sei(); //全局中斷允許 while(1) { if(key_int_flag) { keyboard_display(); } while(1); }