首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > 单片机 >

抢救,如果主函数中用定时初始化T0_init()来实现初始化,程序不能程序,初始化直接放在主函数中程序就可以执

2013-03-01 
急救,如果主函数中用定时初始化T0_init()来实现初始化,程序不能程序,初始化直接放在主函数中程序就可以执

急救,如果主函数中用定时初始化T0_init()来实现初始化,程序不能程序,初始化直接放在主函数中程序就可以执
第一种情况:主函数中用定时初始化T0_init()来实现初始化,程序不能实现显示0-60
#include <reg51.h>
#define uint  unsigned int
#define uchar unsigned char
sbit P15=P1^5;
sbit P20=P2^0;
sbit P21=P2^1;
uint  Count=0,Second=0;
uchar code   TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
uchar dis[]={1,2};
 
 void T0_init()
  { 
   TMOD=0x01;
   TH0=(65536-50000);
   TL0=(65536-50000);
   IE=0x82;
   TR0=1;
   }
   
 void  delay(uint x)
  { uint i,j;
   for(i=0;i<x;i++)
    for(j=0;j<120;j++);
  }

 void Time0(  ) interrupt  1
    {
   TH0=(65536-50000);
   TL0=(65536-50000);
   if(++Count==20)
{Count=0;
 Second++;
 if(Second==60)
  Second=0;
}
//dis[0]=Second/10;
//dis[1]=Second%10;
}


/* 显示函数*/
void display()
{
  uchar i,temp=0x01;  

  dis[1]=Second/10;   //取计数值的整数位
  dis[0]=Second%10;   //取计数值的余数位
       
  for(i=0;i<2;i++)           
{
P1=TAB[dis[i]];  
P2=temp;                     
delay(5);                      
P1=0xff;                   
P2=0xff;
temp<<=1;                     
}
   
}

 main()
 {
   void T0_init();
   while(1)
   {
    display();
   }
 }

第二种情况:初始化直接放在主函数中程序可以执行,显示0-60#include <reg51.h>
#define uint  unsigned int
#define uchar unsigned char
sbit P15=P1^5;
sbit P20=P2^0;
sbit P21=P2^1;
uint  Count=0,Second=0;
uchar code   TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98};
uchar dis[]={1,2};
 

 void  delay(uint x)
  { uint i,j;
   for(i=0;i<x;i++)
    for(j=0;j<120;j++);
  }

 void Time0(  ) interrupt  1
    {
   TH0=(65536-50000);
   TL0=(65536-50000);
   if(++Count==20)
{Count=0;
 Second++;
 if(Second==60)
  Second=0;
}
}

/* 显示函数*/
void display()
{
  uchar i,temp=0x01;  

  dis[0]=Second/10;   //取计数值的整数位
  dis[1]=Second%10;   //取计数值的余数位
       
  for(i=0;i<2;i++)           


{
P1=TAB[dis[i]];  
P2=temp;                     
delay(5);                      
P1=0xff;                   
P2=0xff;
temp<<=1;                     
}
}

  main()
 {
   TMOD=0x01;
   TH0=(65536-50000);
   TL0=(65536-50000);
   IE=0x82;
   TR0=1;
   while(1)
   {
    display();
   }
 }


定时器初始化函数
[解决办法]
main()
  {
    void T0_init();//应该为 T0_init();
    while(1)
    {
     display();
    }
  }
=================
建议有良好的风格:
void T0(void)
{
   。。。。。。;
}
[解决办法]
void T0_init(void)
{
    ......;
}
void dispaly(void)
{
    ......;
}
[解决办法]
main中
void T0_init();
改成T0_init();

[解决办法]
那也可以放在 Main() 的最前面啊,这样也不错。
反正单片机的 Main 里一般都有 while(1)。

热点排行