急求51单片机顺、倒计时60秒程序加注释下~好的话可再加财富!谢谢~!

89c51单片机仿真

第1个回答  2010-11-26
#include<reg52.h> // 包含51单片机寄存器定义的头文件
unsigned char code Tab[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
//数码管显示0~9的段码表
unsigned char int_time; //记录中断次数
unsigned char second; //储存秒
/***********************************************************************
函数功能:快速动态扫描延时,延时约0.6毫秒
************************************************************************/
void delay(void)
{
unsigned char i;
for(i=0;i<200;i++)
;
}
/***********************************************************************
函数功能:显示秒
入口参数:k
出口参数:无
************************************************************************/
void DisplaySecond(unsigned char k)
{

P2=0xbf; //P2.6引脚输出低电平, DS6点亮
P0=Tab[k/10]; //显示十位
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
P2=0x7f; //P2.7引脚输出低电平, DS7点亮
P0=Tab[k%10]; //显示个位
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
P2=0xff; //关闭所有数码管

P0=0xff; //显示个位
delay();
delay();
delay();
delay();
delay();
delay();
delay();
delay();
}
void main(void) //主函数
{
TMOD=0x01; //使用定时器T0
TH0=(65536-46083)/256; //将定时器计时时间设定为46083×1.085微秒
//=50000微秒=50毫秒
TL0=(65536-46083)%256;
EA=1; //开启总中断
ET0=1; //定时器T0中断允许
TR0=1; //启动定时器T0开始运行
int_time=0; //中断次数初始化
second=0; //秒初始化
while(1)
{
DisplaySecond(second); //调用秒的显示子程序
}
}
//********************************************************
//函数功能:定时器T0的中断服务程序
//*******************************************************
void interserve(void ) interrupt 1 using 1
{
TR0=0; //关闭定时器T0
int_time ++; //每来一次中断,中断次数int_time自加1
if(int_time==20) //够20次中断,即1秒钟进行一次检测结果采样
{
int_time=0; //中断次数清0
second++; //秒加1
if(second==60)
second =0; //秒等于60就返回0
}
TH0=(65536-46083)/256; //重新给计数器T0赋初值
TL0=(65536-46083)%256;
TR0=1; //启动定时器T0
}
第2个回答  2010-11-28
  ORG 0000H
  START:MOV R0,#30

  MOV DPTR,#TABLE
  LOOP: LCALL DISPLAY
  EDC R0
  CJNE R0,#100,LOOP
  DISPLAY: MOV A,R0
  MOV B,#10
  DIV AB
  MOV R1,A
  MOV R2,B
  MOV R3,#50
  LOOP1: MOV A,R2
  LCALL CHANGE
  CLR P3.1
  LCALL DELAY
  SETB P3.1
  MOV A,R1
  LCALL CHANGE
  CLR P3.0
  LCALL DELAY
  SETB P3.0
  CLR A
  DJNZ R3,LOOP1
  CHANGE: MOVC A,@A+DPTR
  MOV P0,A
  RET

  DELAY: ;10ms延时
  MOV R6,#20
  D1: MOV R7,#248
  DJNZ R7,$
  DJNZ R6,D1
  RET

  TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH

  END
  说明:P0口接数码管 p3.0 p3.1为位选本回答被提问者采纳
第3个回答  2010-11-23
  #include<reg51.h>

  typedef unsigned char uchar;
  typedef unsigned int uint;

  sbit ioResetCountValue = P1^0; //Reset gucInc/DecTimeValue
  sbit ioChooseIncStatus = P1^1; //This Key Tell System it's Inc Order
  sbit ioChooseDecStatus = P1^2; //This Key Tell System it's Dec Order

  #define mTimerValue 50000 //50ms

  uchar data gucIncTimeValue = 0;
  uchar data gucDecTimeValue = 60;
  uchar data guc50ms = 0;
  bit bdata gbIncOder = 1; //ResetValue is 1,means it's Inc Order

  void InitialSys(void) { //System Initial SubFuction
  TMOD = 0x01; //TImer0 is 16bits Timer
  TH0 = (65536 - mTimerValue) / 256;
  TL0 = (65536 - mTimerValue) % 256;
  TR0 = 1; //Do strat Timer0
  EA = 1; //Open Final Interrupt Swith
  ET0 = 1; //Open Timer0 Interrupt Switch
  }

  void IsrTimer0(void) interrupt 1 using 1 { //Timer0 Interrupt Service Routine
  TR0 = 0;
  TH0 = (65536 - mTimerValue) / 256;
  TL0 = (65536 - mTimerValue) % 256;
  TR0 = 1;
  guc50ms++;
  if(guc50ms == 20) { //20 * 50ms is 1s
  guc50ms = 0;
  if(gbIncOrder) {
  gucIncTimeValue++;
  if( gucIncTimeValue == 61 ) //Reset 60s
  gucIncTimerValue = 0;
  }
  else {
  gucDecTimeValue--;
  if( gucDecTimeValue == 255 ) //Reset 60s
  gucIncTimerValue = 60;
  }
  }
  }

  void Main(void) {
  InitialSys();
  while(1) {
  if( !ioChooseIncStatus || !ioChooseDecStatus || !ioResetCountValue) {
  DelayMs(15); //Clear The Key Disturb
  if( !ioChooseIncStatus )
  gbIncOder = 1;
  else if( !ioChooseDecStatus )
  gbIncOder = 0;
  else if( !ioResetCountValue ) {
  gucIncTimeValue = 0;
  gucDecTImeValue = 60;
  }
  }
  }
  }

  什么仿真?本回答被网友采纳