Code:
#include <REGX52.H> #include <stdio.h> #define roletrai P1^4 #define rolephai P1^2 #define dctrai P1^5 #define dcphai P1^3 unsigned char dem1=0,dem2=0; unsigned char XT,XP ; void delayms(int ms) //ham tao tre khoang 1ms { int i,j; for(i=0;i<ms;i++) for(j=0;j<85;j++){} } void khoitao(void) { EA=0; TMOD=0x02; TL0=0x9B; TH0=0x9B; ET0=1; EA=1; TR0=1; } void timer0(void)interrupt 1 { TR0=0; TF0=0; dem1++; dem2++; if(dem1<=XP) { dcphai=1; } else { P1^3=0; } if(dem1>10) dem1=0; if(dem2<=XT) { P1^5=1; } else { P1^5=0; } if(dem2>9) dem2=0; TR0=1; } void stop(void) { XP=0; XT=0; } void main(void) { khoitao(); P0=0xB00000000; roletrai=1; rolephai=1; delayms(50); XP=10; XT=9; delayms(500); while(1) { if (P0==0xB00011000) //lech ph { XP=10; XT=9; } if (P0==0xB00010000) { XP=10; XT=8; } if (P0==0xB00110000) { XP=10; XT=7; } if ( P0==0xB00100000) { XP=10; XT=6; } if(P0==0xB01100000) { XP=10; XT=5; } if (P0==0xB01000000) { XP=8; XT=5; } if(P0==0xB11000000) { XP=7; XT=4; } if (P0==0xB00001000)//lech tr { XP=9; XT=9; } if (P0==0xB00001100) { XP=8; XT=9; } if ( P0==0xB00000100) { XP=7; XT=9; } if(P0==0xB00000110) { XP=6; XT=9; } if (P0==0xB00000010) { XP=5; XT=7; } if(P0==0xB00000011) { XP=4; XT=6; } } }