1、一个简单的密码开锁程序/-/ File: main.c/ Contents;/ uP control Process/ / Copyright (c) 2006 * CO LTD. All rights reserved./ from: / $Date: 11/10/2006/ $Revision: 1011/-i nclude /-/ Random Macros/-#define min(a,b) (a)(b)?(a):(b)/-/ Constants/-char code Revsion = n uP control Processn Revision:1.0n Data:2006/9/2
2、0n Copyright(c)2006 Wincotime CO LTD. All rights reservedn;int code TABLE132 = D,0x10, 1,0x20, 2,0x40, 3,0x80, 4,0x30, 5,0x50, 6,0x90, 7,0x60, 8,0xA0, 9,0xC0, 0,0xD0, *,0x70, #,0xB0 ; #define CON_SW1 P3_7#define CON_SW2 P3_5#define CON_SW3 P3_4#define CON_SW4 P3_0#define CON_LOCK P3_1/-/ Global Vari
3、ables/-unsigned char csSWH;unsigned char csSWL;unsigned char ccCode;unsigned char cKeyCounter;unsigned long ulKeySampleTime;unsigned long ulOpenLockTime;char cKeyA;char cKeyReg4;char cPSW4;bit bsKeyOk;bit bsLock;bit bsT1ms;/-/ Prototypes/-void TD_Init (void);void TD_InitINT0 (void);void TD_InitTMR0
4、(void);void TD_ProcdutTest(void);void TD_SWSample (void);bit TD_PSWCheck (void);void Delay1ms (void);/-/ Code/-main () TD_Init(); /TD_ProcdutTest(); while(1) / Switch Sample TD_SWSample (); / Lock Control if(TD_PSWCheck () CON_LOCK = 1; ulOpenLockTime = 0; while(ulOpenLockTime10000) else CON_LOCK =
5、0; / End of Main Function/-/ Task Dispatcher hooks/ The following hooks are called by the task dispatcher./-void TD_Init(void) unsigned char i; CON_SW1 = CON_SW2 = CON_SW3 = CON_SW4 = 0; ulKeySampleTime = 0; bsKeyOk = 0; cKeyCounter = 0; for(i=0;i4;i+)cKeyRegi = 0; TD_InitINT0(); TD_InitTMR0();void
6、TD_InitINT0 (void) IT0 = 1; / Configure interrupt 0 for falling edge on /INT0 (P3.2) EX0 = 1; / Enable EX0 Interrupt EA = 1; / Enable Global Interrupt Flagvoid TD_InitTMR0 (void) TH0 = 255 - 200; TMOD = (TMOD & 0xF0) | 0x02; /* Set T/C0 Mode */ ET0 = 1; /* Enable Timer 0 Interrupts */ TR0 = 1; /* St
7、art Timer 0 Running */ EA = 1; /* Global Interrupt Enable */*void TD_ProductTest(void)*/void TD_SWSample(void) unsigned char temp; CON_SW1 = 1; Delay1ms(); temp = P1&0xF; if(temp10)cPSW0=temp+0x30; CON_SW1 = 0; CON_SW2 = 1; Delay1ms(); temp = P1&0xF; if(temp10)cPSW1=temp+0x30; CON_SW2 = 0; CON_SW3 =
8、 1; Delay1ms(); temp = P1&0xF; if(temp10) cPSW2=temp+0x30; CON_SW3 = 0; CON_SW4 = 1; Delay1ms(); temp = P1&0xF; if(temp10)cPSW3=temp+0x30; CON_SW4 = 0;void Delay1ms () bsT1ms = 1; if(bsT1ms) bsT1ms = 1; if(bsT1ms) bsT1ms = 1; if(bsT1ms) bsT1ms = 1; if(bsT1ms) bsT1ms = 1; if(bsT1ms) bsT1ms = 1; if(bs
9、T1ms)bit TD_PSWCheck (void) unsigned char i; bit btemp; if(bsKeyOk) bsKeyOk = 0; cKeyCounter = 0; for(i=0;i4;i+) if(cKeyRegi!=cPSWi) btemp = 0; return 0; else btemp = 1; if(btemp)return 1; else return 0;/-/ MCS51 Interrupt Handlers/ The following functions are called by 51 interrupt jump vector table/-void ISR_INT0 (void) interrupt IE0_VECTOR unsigned char i; ulKeySampleTime = 0; ccCode = 0; ccCode = P1&0xF0; for(i=0;i=4) bsKeyOk = 1; cKeyCounter=0; break; default: if(cKeyCounter150000) / 1s ulKeySampleTime = 0; bsKeyOk = 0; cKeyCounter = 0; for(i=0;i4;i+)cKeyRegi=0; bsT1ms = 0;