/**********************************************************/
#include<reg52.h>
unsigned char PWM=0x7f ; //賦初值
char code reserve [3] _at_ 0x3b; //保留0x3b開始的3個字節
unsigned char t;
unsigned char TT;
unsigned char count=0;
unsigned char flag;
sbit BEEP =P3^7 ; //蜂鳴器
/*********************************************************
延時子程序
*********************************************************/
void delayms(unsigned char ms)
{
unsigned char i ;
while(ms--)
{
for(i=0 ; i<114 ; i++) ;
}
}
/*************T1定時器的初始化程序***********************/
void init2_main()
{
SCON = 0x50; //設定串口工作方式1,接收使能
PCON = 0x00; //波特率不倍增
TMOD = 0x20; //定時器1工作于8位自動重載模式, 用于產生波特率
EA = 1;
TL1 = 0xfd;
TH1 = 0xfd; //波特率9600
TR1 = 1;
}
/*******T2定時器初始化程序********************************/
void init_main()
{
RCAP2H=0XFF;
RCAP2L=0x66;
TH2=RCAP2H;
TL2=RCAP2L;
T2CON=0;
IE=0xb0;
EA=1;
ET2=1;
}
/*********************************************************/
void main()
{
t=10;
TT=255;
BEEP = 1;
ES = 1;
init_main();
TMOD=0x01 ;
TH0=0xff ;
TL0=0x66 ;
EA=1;
ET0=1;
init2_main();
delayms(100);
while(1)
{
if(t>TT) {BEEP=!BEEP;P0=0xff;}
if(flag==1){t=PWM;}
if(flag==2){TT=PWM;}
}
}
/*********************************************************
定時器0中斷服務程序
*********************************************************/
void timer0() interrupt 1
{
if(PWM!=0x02)
{t=PWM;
flag=1;}
TT=TT;
TH0=0xff ;
TL0=0x66 ;
count++;
if(count<=t){P0=0x00;}
if(count>t) {P0=0xFF;}
if(count>TT){count=0;}
}
/*********************************************************
定時器2中斷服務程序
*********************************************************/
void timer1() interrupt 5
{ TF2=0;
if(PWM!=0X01){TT=PWM;flag=2;}
RCAP2H=0xff;
RCAP2L=0x66 ;
t=t;
count++;
if(count<=t){P0=0x00;}
if(count>t) {P0=0Xff;}
if(count>=TT){count=0;}
}
/*********************串口中斷服務程序************************************/
void mm() interrupt 4
{
RI=0;
PWM=SBUF;
if(PWM==0x01)
{
TR2=0;
TR0=1;
flag=0;
}
if(PWM==0x02)
{
TR0=0;
TR2=1;
TT=PWM;
flag=0;
}
}
當while(1)中的TT或者t改變時,中斷函數1中的T(TT仍然為255)T和中斷5中的t(t仍然為10)都不會發生相應的變化。請問有什么辦法可以解決這個問題。謝謝了!!!
|