关于秒表的一个程序,但是数码管只显示一个0,求各位帮忙看一下。
单片机源码:
- #include<reg51.h>
- sbit K1 = P1^0;
- sbit K2 = P1^1;
- sbit K3 = P1^2;
- sbit K4 = P1^3;
- unsigned char code LedChar[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- unsigned char LedBuff[] = {0x00,0x00,0x00,0x00,0x00,0x00};
- unsigned char KeySta[] = {1,1,1,1};
- bit StopwatchRunning = 0;
- bit StopwatchRefresh = 1;
- unsigned char DecimalPart = 0;
- unsigned int IntegerPart = 0;
- unsigned char T0RH = 0;
- unsigned char T0RL = 0;
- void ConfigTimer0(unsigned int ms);
- void StopwatchDisplay();
- void KeyDriver();
- void main()
- {
- EA = 1;
- P1 = 0xFE;
- ConfigTimer0(2);
- while(1)
- {
- if(StopwatchRefresh)
- {
- StopwatchRefresh = 0;
- StopwatchDisplay();
- }
- KeyDriver();
- }
- }
- void ConfigTimer0(unsigned int ms)
- {
- unsigned long tmp;
-
- tmp = 11059200 / 12;
- tmp = (tmp*ms) /1000;
- tmp = 65536 -tmp;
- tmp = tmp + 18;
- T0RH = (unsigned char)(tmp>>8);
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- }
- void StopwatchDisplay()
- {
- signed char i;
- unsigned char buf[6];
- LedBuff[0] = LedChar[DecimalPart % 10];
- LedBuff[1] = LedChar[DecimalPart / 10];
- buf[0] = IntegerPart % 10;
- buf[1] = (IntegerPart / 10) % 10;
- buf[2] = (IntegerPart / 100) % 10;
- buf[3] = (IntegerPart / 1000) % 10;
- for(i = 3;i>=1;i--)
- {
- if(buf[i] == 0)[/i][i]
- LedBuff[i+2] = 0x00;
- else
- break;
- }
- for(;i>=0;i--)
- {
- LedBuff[i+2] = LedChar[buf[i]];[/i][i]
- }
- LedBuff[2] &= 0x80;
- }
- void StopwatchAction()
- {
- if(StopwatchRunning)
- StopwatchRunning = 0;
- else
- StopwatchRunning = 1;
- }
- void StopwatchReset()
- {
- StopwatchRunning = 0;
- DecimalPart = 0;
- IntegerPart = 0;
- StopwatchRefresh = 1;
- }
- void KeyDriver()
- {
- unsigned char i;
- static unsigned char backup[4] = {1,1,1,1};
- for(i=0;i<4;i++)
- {
- if(backup[i] != KeySta[i])[/i][/i][i][i]
- {
- if(backup[i] != 0)[/i][i]
- {
- if(i == 1)
- StopwatchReset();
- else if(i == 2)
- StopwatchReset();
- }
- }
- backup[i] = KeySta[i];[/i][/i][i][i]
- }
- }
- void KeyScan()
- {
- unsigned char i;
- static unsigned char keybuf[] = {0x00,0x00,0x00,0x00};
- keybuf[0] = (keybuf[0]<<1) | K1;
- keybuf[1] = (keybuf[1]<<1) | K2;
- keybuf[2] = (keybuf[2]<<1) | K3;
- keybuf[3] = (keybuf[3]<<1) | K4;
- for(i = 0;i<4;i++)
- {
- if(keybuf[i] == 0x00)[/i][i]
- {
- KeySta[i] = 0;[/i][i]
- }
- else if(keybuf[i] == 0xff)[/i][i]
- {
- KeySta[i] =1;[/i][i]
- }
- }
- }
- void LedScan()
- {
- static unsigned char i = 0;
- P0 =0X00;
- P2 = (P2 &0xF8) | i;
- P0 = LedBuff[i];[/i][i]
- if(i<5)
- i++;
- else
- i = 0;
- }
- void StopwatchCount()
- {
- if(StopwatchRunning)
- {
- DecimalPart++;
- if(DecimalPart >= 100)
- {
- DecimalPart = 0;
- IntegerPart++;
- if(IntegerPart >= 10000)
- {
- IntegerPart = 0;
- }
- }
- StopwatchRefresh = 1;
- }
- }
- void InterruptTimer0() interrupt 1
- {
- static unsigned char tmr10ms = 0;
- TH0 = T0RH;
- TL0 = T0RL;
- LedScan();
- KeyScan();
- tmr10ms++;
- if(tmr10ms >= 5)
- {
- tmr10ms = 0;
- StopwatchCount();
- }
- }
复制代码
|