Một Số Bài Toán Ứng Dụng Cho Trình biên dịch Keil C pptx 1: chương trình ma trận 4x4 ra led P1, nút ấn P2 //PROJECT : DEM LED 7 DOAN //FILE : VD1.c //DATE : 24/02/2014 //PROCESSOR : AT89C51 //AUTHOR : HPT //****************************************** #include <REGX51.H> #define LOOP 10 unsigned char LED_ARRAY[16] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; unsigned char KEY_MATRIX[4]={0xEF,0xDF,0xBF,0x7F}; //cho lan luot cac hang bang 0 roi kiem tra cot //KHAI BAO CAC HAM char SCAN_KEY(void); void DISPLAY(unsigned char key); void delay(unsigned int time); //NOI DUNG CAC HAM char SCAN_KEY(void) { unsigned char KEY_NUM; //bien tra ve gia tri ban phim tu 0-15 unsigned char x,y; //2 bien quet hang va cot for(x=0;x<4;x++) { P2=KEY_MATRIX[x]; for(y=0;y<LOOP;y++) { if(P2_0==0) KEY_NUM=0+4*x; if(P2_1==0) KEY_NUM=1+4*x; if(P2_2==0) KEY_NUM=2+4*x; if(P2_3==0) KEY_NUM=3+4*x; } } return(KEY_NUM); } void DISPLAY(unsigned char key) { switch(key) { case 0: {P1 = ~LED_ARRAY[0];break;} case 1: {P1 = ~LED_ARRAY[1];break;} case 2: {P1 = ~LED_ARRAY[2];break;} case 3: {P1 = ~LED_ARRAY[3];break;} case 4: {P1 = ~LED_ARRAY[4];break;} case 5: {P1 = ~LED_ARRAY[5];break;} case 6: {P1 = ~LED_ARRAY[6];break;} case 7: {P1 = ~LED_ARRAY[7];break;} case 8: {P1 = ~LED_ARRAY[8];break;} case 9: {P1 = ~LED_ARRAY[9];break;} case 10: {P1 = ~LED_ARRAY[10];break;} case 11: {P1 = ~LED_ARRAY[11];break;} case 12: {P1 = ~LED_ARRAY[12];break;} case 13: {P1 = ~LED_ARRAY[13];break;} case 14: {P1 = ~LED_ARRAY[14];break;} case 15: {P1 = ~LED_ARRAY[15];break;} } } //KHAI BAO HAM void delay(unsigned int time) { unsigned int i; for(i=0;i<time;i++) { ; } } /*KHAI BAO MANG QUET PHIM*/ //CT CHINH void main(void) { unsigned char a; while(1) { a=SCAN_KEY(); DISPLAY(a); } } 2: LED7 đoạn chạy từ 0-9, có 3 nút ấn P3_0 nút ấn lên, nút ấn P3_1 nút ấn xuống và nút ấn P3_2 là RESET: #include <REGX51.H> #define ON 0 #define OFF 1 sbit TANG=P3^0; sbit GIAM=P3^1; sbit RESET=P3^2; unsigned char DEM=0; unsigned char LED_ARRAY [10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //KHAI BAO void delay(unsigned int time); void display(void); void read_key(void); // viet tuong minh ca ham con void delay(unsigned int time) { unsigned int i; for (i=0;i<time;i++) {;} } void display(void) { P2 = ~LED_ARRAY[DEM]; } void read_key(void) { if(TANG==0) { delay(5000); while(TANG==0); delay(5000); DEM++; if(DEM==10) DEM=0; } ; {if (GIAM==0) { delay(5000); while(GIAM==0); delay(5000); if(DEM==0) DEM=10; DEM ; } ; { if (RESET==0) { while(RESET==0); delay(5000); DEM=0; } } } } // CHUONG TRINH CHINH void main (void) { while(1) { read_key(); display(); } } 3: LED7 đoạn có nút P3_0 đếm lên: #include <REGX51.H> #define ON 0 #define OFF 1 sbit BUTTON = P3^0; unsigned char DEM=0; unsigned char LED_ARRAY[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void delay(unsigned int time) ; void display(void); void read_key(void); void delay(unsigned int time) { unsigned int i; for(i=0;i<time;i++) {;} } void display(void) { P2 = ~LED_ARRAY[DEM]; } void read_key(void) { if(BUTTON==ON) { delay(5000); while(BUTTON==ON); //cho nha nut delay(50000); DEM++; if(DEM==10) DEM=0; } } //chuong trinh chinh void main(void) { while(1) { read_key(); display(); } } 4: 8LED sáng: #include <REGX51.H> void delay (unsigned int time) { unsigned int i; for ( i=0; i<time; i++) { ; } } void main (void) { unsigned char LED=0x01; while (1) { P2=~LED; LED=LED<<1; if(LED==0x00) LED=0x01; delay (65000); } } . Một Số Bài Toán Ứng Dụng Cho Trình biên dịch Keil C pptx 1: chương trình ma trận 4x4 ra led P1, nút ấn P2 //PROJECT : DEM LED 7 DOAN //FILE : VD1 .c //DATE : 24/02/2014 //PROCESSOR : AT8 9C5 1 //AUTHOR. KEY_MATRIX[4]={0xEF,0xDF,0xBF,0x7F}; / /cho lan luot cac hang bang 0 roi kiem tra cot //KHAI BAO CAC HAM char SCAN_KEY(void); void DISPLAY(unsigned char key); void delay(unsigned int time); //NOI DUNG CAC HAM char SCAN_KEY(void) { unsigned. DISPLAY(unsigned char key) { switch(key) { case 0: {P1 = ~LED_ARRAY[0];break;} case 1: {P1 = ~LED_ARRAY[1];break;} case 2: {P1 = ~LED_ARRAY[2];break;} case 3: {P1 = ~LED_ARRAY[3];break;} case 4: {P1