51单片机-定时器-proteus仿真
这是代码
#include "reg51.h" #define u16 unsigned int #define u8 unsigned char #define FOSC 11059200L #define BAUD 9600 #define T0MS (65536 - 1 * FOSC / 12 / 1000) #define T1MS (65536 - 1 * FOSC / 12 / 1000) sbit kaiguan = P1 ^ 0; u8 code leddat[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x81, 0x82, 0x84, 0x88, 0x90, 0xa0, 0xc0, 0xc1, 0xc2, 0xc4, 0xc8, 0xd0, 0xe0, 0xe1, 0xe2, 0xe4, 0xe8, 0xf0, 0xf1, 0xf2, 0xf4, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0x00, 0xff, 0x00}; // gong yin u8 code gongyin_smg[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; // gong yang u8 code gongyang_smg[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e}; // string u8 code str[] = {0x76, 0x79, 0x38, 0x38, 0x3f}; // hello // u8 code smg_pos[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; u8 code smg_pos_2[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07}; u8 num = 0; u8 kaiguan_flag = 0; u16 timer0_count = 0; u16 timer1_count = 0; u8 timer0_num = 0; u8 timer1_num = 0; // void initUART(void) // { // SCON = 0x50;//8bit data // } void delayms(u16 n) { u16 i = 0, j = 0; for (i = 0; i < n; i++) { for (j = 0; j < 120; j++) ; } } void multi_smg() { u8 i; for (i = 0; i < sizeof(str) / sizeof(str[0]); i++) { P2 = ~smg_pos[i]; P3 = smg_pos_2[i]; P1 = str[i]; delayms(5); } } void key_scan() { // u8 i; if (kaiguan == 0 && kaiguan_flag == 0) { kaiguan_flag = 1; // delayms(100); // if (kaiguan == 0) // { // P3 = gongyin_smg[num]; // num++; // if (num == 16) // { // num = 0; // } // } // delayms(100); } if (kaiguan_flag == 1 && kaiguan == 1) { P3 = gongyin_smg[num]; num++; kaiguan_flag = 0; if (num == 10) { num = 0; } } } void key_scan2() { u8 row, col; P2 = 0xf0; if (P2 != 0xf0) { delayms(20); row = P2; P2 = 0x0f; if (P2 != 0x0f) { col = P2; } } if (col == 0x0e) { switch (row) { case 0xe0: P3 = gongyin_smg[1]; P1 = gongyin_smg[1]; break; case 0xd0: P3 = gongyin_smg[2]; P1 = gongyin_smg[1]; break; case 0xb0: P3 = gongyin_smg[3]; P1 = gongyin_smg[1]; break; case 0x70: P3 = gongyin_smg[4]; P1 = gongyin_smg[1]; break; } } if (col == 0x0d) { switch (row) { case 0xe0: P3 = gongyin_smg[1]; P1 = gongyin_smg[2]; break; case 0xd0: P3 = gongyin_smg[2]; P1 = gongyin_smg[2]; break; case 0xb0: P3 = gongyin_smg[3]; P1 = gongyin_smg[2]; break; case 0x70: P3 = gongyin_smg[4]; P1 = gongyin_smg[2]; break; } } if (col == 0x0b) { switch (row) { case 0xe0: P3 = gongyin_smg[1]; P1 = gongyin_smg[3]; break; case 0xd0: P3 = gongyin_smg[2]; P1 = gongyin_smg[3]; break; case 0xb0: P3 = gongyin_smg[3]; P1 = gongyin_smg[3]; break; case 0x70: P3 = gongyin_smg[4]; P1 = gongyin_smg[3]; break; } } if (col == 0x07) { switch (row) { case 0xe0: P3 = gongyin_smg[1]; P1 = gongyin_smg[4]; break; case 0xd0: P3 = gongyin_smg[2]; P1 = gongyin_smg[4]; break; case 0xb0: P3 = gongyin_smg[3]; P1 = gongyin_smg[4]; break; case 0x70: P3 = gongyin_smg[4]; P1 = gongyin_smg[4]; break; } } // if (row + col == 0xee) // { // P3 = gongyin_smg[1]; // } } void liushuideng2() { u8 i = 0; for (i = 0; i < sizeof(leddat) / sizeof(leddat[0]); i++) { P2 = ~leddat[i]; delayms(100); } } void shumaguan() { u8 i; for (i = 0; i < sizeof(gongyin_smg) / sizeof(gongyin_smg[0]); i++) { P2 = gongyin_smg[i]; delayms(100); P3 = gongyang_smg[i]; delayms(100); P1 = gongyin_smg[i]; delayms(100); } } void liushuideng() { u8 i = 0; for (i = 0; i < 8; i++) { P2 = ~0x01 << i; P1 = 0x01 << i; delayms(100); } } void initTimer0() { TMOD = 0x01; TH0 = T0MS / 256; TL0 = T0MS % 256; TF0 = 0; ET0 = 1; // EA = 1; TR0 = 1; } void initTimer1() { TMOD = 0x10; TH1 = T1MS / 256; TL1 = T1MS % 256; TF1 = 0; ET1 = 1; // EA = 1; TR1 = 1; } // 0 out interrupt 0 // 1 timer interrupt 0 // 2 out interrupt 1 // 3 timer interrupt 1 // 4 serial interrupt void initTime() { EA = 1; initTimer0(); initTimer1(); } void initUART(void) { ES = 1; } void main() { initTime(); while (1) ; // { // // // liushuideng2(); // // // shumaguan(); // // // multi_smg(); // // // key_scan(); // // // key_scan2(); // // // initUART(); // // // led = 0; // // // delayms(100); // // // led = 1; // // // delayms(100); // } } void Timer0_ISR(void) interrupt 1 using 2 { timer0_count++; if (timer0_count == 1000) { timer0_num++; P3 = gongyin_smg[timer0_num]; if (timer0_num == 9) { timer0_num = 0; } timer0_count = 0; } TH0 = T0MS / 256; TL0 = T0MS % 256; TF0 = 0; } void Timer1_ISR(void) interrupt 3 using 3 { timer1_count++; if (timer1_count == 1000) { timer1_num++; P1 = gongyin_smg[timer1_num]; if (timer1_num == 9) { timer1_num = 0; } timer1_count = 0; } TH1 = T1MS / 256; TL1 = T1MS % 256; TF1 = 0; }
这是proteus,通过计时器将P3连接的共阴极数码管点亮,从1-9显示,当然代码里还包含了矩阵键盘,单个按键的代码,只要检测按键按起状态,代替了之前的delay方法来进行消抖