#include "reg52.h" // #include "delay.h" #include "intrins.h" #include <stdio.h> typedef unsigned int u16; typedef unsigned char u8; typedef unsigned int uint; typedef unsigned char uchar; #define LED_PORT P2 #define SMG_A_DP_PORT P0 #define SMG_PORT P0 sbit LSA = P2 ^ 2; sbit LSB = P2 ^ 3; sbit LSC = P2 ^ 4; u8 gsmg_code[17] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; sbit LED1 = P2 ^ 0; sbit LED2 = P2 ^ 1; sbit LED3 = P2 ^ 2; sbit LED4 = P2 ^ 3; sbit LED5 = P2 ^ 4; sbit LED6 = P2 ^ 5; sbit LED7 = P2 ^ 6; sbit LED8 = P2 ^ 7; sbit KEY1 = P3 ^ 1; sbit KEY2 = P3 ^ 0; sbit KEY3 = P3 ^ 2; sbit KEY4 = P3 ^ 3; unsigned int num = 921; unsigned int time = 0; // #define KEY1_PRESS 1 // #define KEY2_PRESS 2 // #define KEY3_PRESS 3 // #define KEY4_PRESS 4 // #define KEY_NO_PRESS 0 // unsigned char key_scan(unsigned char mode) // { // static unsigned char key = 1; // if (mode) // { // key = 1; // } // if (key == 1 && (KEY1 == 0 || KEY2 == 0 || KEY3 == 0 || KEY4 == 0)) // { // delayms(10); // key = 0; // if (KEY1 == 0) // { // return KEY1_PRESS; // } // else if (KEY2 == 0) // { // return KEY2_PRESS; // } // else if (KEY3 == 0) // { // return KEY3_PRESS; // } // else if (KEY4 == 0) // { // return KEY4_PRESS; // } // } // else if (KEY1 == 1 && KEY2 == 1 && KEY3 == 1 && KEY4 == 1) // { // key = 1; // } // return KEY_NO_PRESS; // } // void main2() // { // unsigned char key = 0; // while (1) // { // key = key_scan(0); // if (key == KEY1_PRESS) // { // LED1 = !LED1; // } // } // } void time0_init(void) { // timer TMOD = 0x01; // TMOD=0000 0001B; TH0 = (65536 - num) / 256; // if num is 921 then 1ms TL0 = (65536 - num) % 256; TR0 = 1; // interrupt // ET0 = 1; // EA = 1; } // void delay10us(u16 ten_us) // { // while (ten_us--) // { // _nop_(); // _nop_(); // _nop_(); // _nop_(); // _nop_(); // _nop_(); // } // } void exit0_init(void) // interrupt 0 { IT0 = 1; // 0 is dianping 1 is xiajiangyan EX0 = 1; EA = 1; } void exit1_init(void) // interrupt 1 { IT1 = 1; // 0 is dianping 1 is xiajiangyan EX1 = 1; EA = 1; } void main() { time0_init(); // exit0_init(); // exit1_init(); while (1) { if (time == 0) { LED1 = 0; LED3 = 0; LED5 = 0; LED7 = 0; } while (TF0 == 0) ; TF0 = 0; time++; if (time == 1000) { LED1 = !LED1; LED3 = !LED3; LED5 = !LED5; LED7 = !LED7; LED2 = !LED2; LED4 = !LED4; LED6 = !LED6; LED8 = !LED8; } if (time == 2000) { LED1 = !LED1; LED3 = !LED3; LED5 = !LED5; LED7 = !LED7; LED2 = !LED2; LED4 = !LED4; LED6 = !LED6; LED8 = !LED8; time = 0; } TH0 = (65536 - num) / 256; TL0 = (65536 - num) % 256; } } void delay2ms(u16 xms) { while (xms--) ; } void exi0() interrupt 0 { delay2ms(1000); if (KEY3 == 0) { LED1 = !LED1; } } void exi1() interrupt 2 { delay2ms(1000); if (KEY4 == 0) { LED2 = !LED2; } } void time0() interrupt 1 { static u16 i = 0; TH0 = 0xfc; TL0 = 0x18; if (i == 0) { LED1 = 0; LED3 = 0; LED5 = 0; LED7 = 0; } i++; if (i == 2000) { i = 0; LED1 = !LED1; LED3 = !LED3; LED5 = !LED5; LED7 = !LED7; LED2 = !LED2; LED4 = !LED4; LED6 = !LED6; LED8 = !LED8; } else if (i == 1000) { LED1 = !LED1; LED3 = !LED3; LED5 = !LED5; LED7 = !LED7; LED2 = !LED2; LED4 = !LED4; LED6 = !LED6; LED8 = !LED8; } }
标签:LED1,P2,LED3,LED,void,PRESS,51,单片机,sbit From: https://www.cnblogs.com/dogingate/p/17567596.html