Single-Chip Microcomputer: Controls digital addition and subtraction by pressing buttons (using interruptions) (implemented in C)
Purpose:
Interrupt is used to control the addition and subtraction of digital tubes by pressing the keys 8 and C. After 15, the digital tubes are recycled to 0, and the value remains unchanged after 0.
The Code contains detailed notes:
# Include
Sbit WEI = P2 ^ 7; sbit DUAN = P2 ^ 6; void delay (int t) {while (t --);} unsigned char code dofly_DuanMa [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; // display the field code value 0 ~ 9 unsigned char code dofly_WeiMa [] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f}; // the corresponding Digital tubes are lit, that is, the location code unsigned char KeyScan (void) // The Keyboard Scan function, using the row-level scanning {unsigned char Val; P3 = 0xf0; // The height of the four positions, lower four digits lower if (P3! = 0xf0) // indicates that a key is pressed {// DelayMs (10); // if (P3! = 0xf0) {// indicates that the first line if (P3! = 0xfe) {Val = P3 & 0xf0; Val + = 0x0e; while (P3! = 0xfe); while (P3! = 0xfe); return Val;} P3 = 0xfd; // checks the second row if (P3! = 0xfd) {Val = P3 & 0xf0; Val + = 0x0d; while (P3! = 0xfd); while (P3! = 0xfd); return Val;} P3 = 0xfb; // check the third row if (P3! = 0xfb) {Val = P3 & 0xf0; Val + = 0x0b; while (P3! = 0xfb); while (P3! = 0xfb); return Val;} P3 = 0xf7; // check the fourth row if (P3! = 0xf7) {Val = P3 & 0xf0; Val + = 0x07; while (P3! = 0xf7); while (P3! = 0xf7); return Val ;}}return 0xff ;}/ * ---------------------------------------------------- the key value processing function, returns the sweep key value ready */unsigned char KeyPro (void) {switch (KeyScan () {case 0xee: return 0; break; // press the corresponding key to display the corresponding code value case 0xde: return 1; break; // 1 case 0xbe: return 2; break; // 2 case 0x7e: return 3; break; // 3 case 0xed: return 4; break; // 4 case 0xdd: return 5; break; // 5 case 0xbd: return 6; break; // 6 case 0x7d: return 7; break; // 7 case 0xeb: return 8; break; // 8 case 0xdb: return 9; break; // 9 case 0xbb: return 10; break; // a case 0x7b: return 11; break; // B case 0xe7: return 12; break; // c case 0xd7: return 13; break; // d case 0xb7: return 14; break; // e case 0x77: return 15; break; // f default: return 0xff; break;} // The first parameter I indicates the number of digital tubes to be controlled, and n indicates the number void Screen1 (int I, int n) to be displayed) {int high; P0 = 0; DUAN = 1; DUAN = 0; high = n/10; WEI = 1; P0 = dofly_WeiMa [I]; WEI = 0; DUAN = 1; P0 = dofly_DuanMa [high]; DUAN = 0;} void Screen2 (int I, int n) {int lower; P0 = 0; DUAN = 1; DUAN = 0; lower = n % 10; WEI = 1; P0 = dofly_WeiMa [I]; WEI = 0; DUAN = 1; P0 = dofly_DuanMa [lower]; DUAN = 0;} int num; int Key_push; int flag; int main () {num = 0; WEI = 0; DUAN = 0; EA = 1; EX0 = 1; IT0 = 0; EX1 = 1; IT1 = 0; flag = 0; while (1) {if (flag & num> 9) {Screen1 (6, num ); flag = 0;} else {Screen2 (7, num); flag = 1;} Key_push = KeyPro ();} return 0;} // void inter_sub () interrupt 0 using 1 {if (Key_push = 8) {if (num> 0) num --;} Key_push = 0;} void inter_add () interrupt 2 using 1 {if (Key_push = 12) {num ++; if (num = 16) num = 0;} Key_push = 0 ;}