當前位置:編程學習大全網 - 編程語言 - 幫忙寫個51c語言從0-9999自動加,按k1鍵1次停再壹次走,k2歸0,盡量簡單的。

幫忙寫個51c語言從0-9999自動加,按k1鍵1次停再壹次走,k2歸0,盡量簡單的。

//12M晶振。4位***陰數碼管,P0 段碼,P1.0~P1.3 位控, P1.4開始/暫停 P1.5復位 

//顯示 0000~9999

//2012 02 13

#include <reg51.h>

#define uchar unsigned char

sbit P10=P1^0;

sbit P11=P1^1;

sbit P12=P1^2;

sbit P13=P1^3;

sbit KS=P1^4; //開始/暫停

sbit KR=P1^5; //復位

uchar time,xs,ge,shi,bai,qian;

uchar code tab[ ]= {

   0x3F,/*0*/

0x06,/*1*/

0x5B,/*2*/

0x4F,/*3*/

0x66,/*4*/

0x6D,/*5*/

0x7D,/*6*/

0x07,/*7*/

0x7F,/*8*/

0x6F,/*9*/

};

void delay_ms(unsigned int ms) //1ms延時

{

uchar a;

while(ms--)

for(a=123;a>0;a--);

}

void display();

void T0intinit( ) //定時器T0初始化

{

TMOD=0x01;

TH0=(65536-50000)/256; //50ms定時

TL0=(65536-50000)%256;

EA=1;

ET0=1;

TR0=1;

}

void main()

{

T0intinit( );

while(1)

{

display( );

while(!KS) //開始/暫停 鍵

{

display( );

if(KS)

TR0=!TR0;

}

while(!KR) //清零鍵

{

display( );

if(KR)

TR0=0;

qian=0;

bai=0;

shi=0;

ge=0;

}

if(bai==10)

{

bai=0;

qian++;

}

if(qian==10)

{

qian=0;

TR0=0;

}

}

}

void display( )

{

P13=0;

P0=tab[ge];

delay_ms(1);

P13=1;

P12=0;

P0=tab[shi];

delay_ms(1);

P12=1;

P11=0;

P0=tab[bai];

delay_ms(1);

P11=1;

P10=0;

P0=tab[qian];

delay_ms(1);

P10=1;

}

void T0int( ) interrupt 1 //定時器T0中斷 方式1

{

uchar t;

TH0=(65536-50000)/256; //重裝50ms定時常數

TL0=(65536-50000)%256;

t++;

if(t==10)

{

t=0;

ge++;

if(ge==10)

{

ge=0;

shi++;

}

if(shi==10)

{

shi=0;

bai++;

}

}

}

  • 上一篇:什麽是物聯網,物聯網的應用有哪些?
  • 下一篇:求MATLAB代碼
  • copyright 2024編程學習大全網