|
試著寫了單片機(jī)程序,但不知如何加人啟動(dòng)和停止,定時(shí)器TR0為什么不能被其它函數(shù)調(diào)用呢?
#include <REGX52.H>
#include<intrins.h>
#include"Delay.h" //延時(shí)函數(shù)申明
unsigned char tem_key=0; //按鍵+次數(shù)
unsigned char tem_min;//1分
unsigned char tem; //中間變量
unsigned char timing_time; //定時(shí)時(shí)間
sbit key=P1^0; //按鍵定義
sbit LED=P1^1; //定時(shí)工作指示
unsigned char smgxs[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//數(shù)碼管編碼
void Key() //按鍵函數(shù)
{
if(key==0)
{
Delay(20);
while(key==0);
Delay(20);
tem_key++;
if(tem_key>=10) //0-9值循環(huán)
{
tem_key=0;
}
tem=tem_key;
timing_time=(tem*60); //顯示值*60分鐘
}
}
void T0Init(void) //50毫秒@11.0592MHz
{
TMOD=0x01; //設(shè)置定時(shí)器模式
TL0 = 0x00; //設(shè)置定時(shí)初始值
TH0 = 0x4C; //設(shè)置定時(shí)初始值
TF0 = 0; //清除TF0標(biāo)志
ET0=1;
EA=1;
TR0 = 1; //T0開始計(jì)時(shí)
}
void Timer0_Rountiue() interrupt 1 //中斷程序
{
unsigned char tem_ms;
unsigned int tem_s; //1秒
TL0 = 0x00; //重裝初始值
TH0 = 0x4C; //重裝初始值
tem_ms++;
if(tem_ms==20)
{
tem_s++; //秒
tem_ms=0;
LED=~LED;
}
if(tem_s==3600)
{
tem_min++; //分
tem_s=0;
}
}
void main()
{
T0Init(); //T0初始化
while(1)
{
Key();
P0=smgxs[tem_key]; //顯示送P0
if(timing_time==tem_min) //時(shí)間到
{
tem_min=0; //時(shí)間到清零
//執(zhí)行程序。。。
}
}
}
75行加上定時(shí)器沒起作用,開機(jī)就執(zhí)行程序了,是什么原因?
|
|