51单片机的串口中断和定时器中断问题~~请教下大虾们
[复制链接]
我现在用的是2个AT89S52,通过接通一个AD0809和MAX481以及LCD1602组成一个显示AD转换数据电路。
我现在的问题是,我定时器中断可以进入但是需要加延时,而且串口中断无法进入。
下面是我编写的程序,这个是小弟刚刚写的第一个程序,希望大家别见笑:
#include
#include
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
sfr WDTRST = 0xA6;
sbit RW = P1^5;
sbit Elcm=P1^4;
sbit ad0=P0^7;
sbit p03=P0^3;
sbit tt=P2^0;
sbit t0=P3^4;
sbit PINEOC=P1^2;
sbit DR=P1^0;
sbit MDR=P1^1;
#define DataPort P0
#define ADDRPort P2
#define Busy 0x80
#define Work_M 0x38//n=1,f=0
#define Cls 0x01
#define Input_M 0x06//i/d=1 ac++ s=0 screen move=off
#define Screen_Off 0x08
#define Disp_M 0x0c//d=1 disp=on c=0 cursor=off
#define Ddram_Al1 0x80;
#define Ddram_Al2 0xc0;//0x80+0x40
#define Wait1us _nop_();
#define Wait2us {_nop_();_nop_();}
#define Wait4us {Wait2us;Wait2us;}
#define Wait8us {Wait4us;Wait4us;}
#define Wait10us {Wait8us;Wait2us;}
unsigned char com,dat;
// uchar xdata ramadd _at_ 0x0200;
unsigned char xdata h1add _at_ 0xff71;
unsigned char xdata h2add _at_ 0xff6f;
unsigned char xdata h3add _at_ 0xff8f;
unsigned char xdata le1 _at_ 0xff10;
unsigned char xdata le2 _at_ 0xff11;
unsigned char xdata opp _at_ 0x0088;
volatile unsigned char xdata AD0 _at_ 0xff40;
unsigned char xdata AD1 _at_ 0xff42;
unsigned char xdata AD2 _at_ 0xff44;
unsigned char xdata AD3 _at_ 0xff46;
unsigned char xdata AD4 _at_ 0xff48;
unsigned char xdata AD5 _at_ 0xff4A;
unsigned char xdata AD6 _at_ 0xff4C;
unsigned char xdata AD7 _at_ 0xff4E;
unsigned char xdata c8251 _at_ 0xff50;
unsigned char xdata d8251 _at_ 0xff51;
unsigned char xdata COM8279 _at_ 0xff00; //8279com
unsigned char xdata DATA8279 _at_ 0xff01; //8279data
unsigned char xdata le1 _at_ 0xff10;
unsigned char xdata le2 _at_ 0xff11;
unsigned char com,dat;
unsigned char count;
unsigned char FREQ;
uchar temp;
unsigned char temp2;
void displcd(void);
uchar dispc[2][16]=
{
' ','W','E','L','C','O','M','E',' ','T','O',' ','S','G','W',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','D','B','M',' ',' ',' '
};
code unsigned char ctab[19]={'0','1','2','3','4','5','6','7','8','9','.','b','c','d','<','>','L','O','W'};
code unsigned char chartab[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
code unsigned char capchartab[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
void Delay(uint i)
{
uint j;
for(;i>0;i--)
{ for(j=1;j>0;j--);}
}
void initlcd(void){
RW=0;
_nop_();
le1=Work_M;
Delay(50);
le1=Screen_Off;
Delay(50);
le1=Cls;
Delay(50);
le1=Input_M;
Delay(50);
le1=Disp_M;
Delay(50);
RW=1;
}
void displcd(void){ //LCD1602
uint i;
RW=0;
_nop_();
le1=Ddram_Al1;
Delay(200);
for (i=0;i<16;i++)
{
le2=dispc[0];
Delay(200);
}
le1=Ddram_Al2;
Delay(20);
for (i=0;i<16;i++)
{
le2=dispc[1];
Delay(20);
// 写字符
}
//le2='0';
// 写字符
RW=1;
}
void WaitADCEnd(void) //ADC0809
{
while(!PINEOC);
}
uint ReadData(void) //ADC0809
{
uint temp;
WaitADCEnd();
Delay(2);
temp=AD0;
return(temp);
}
void main(void)
{
//以下设置中断和定时器;
EA=1; PT0=1;
ET0=1;
IT0=1;IT1=1;
EX0=1;EX1=1;
TCON=0x08;
TMOD=0x20;
//定时器0为产生1ms的中断,定时器1为9600b/s的波特率发生器;
TH0=-2000/256;
TL0=-2000%256;
TL1=0xfd;
TH1=0xfd;
SCON=0x50;
PCON=0x80;
//开定时器
TR1=1;
TR0=1;
TI=1;
while(1){}
}
void intsvr1(void) interrupt 1 //ADC0809
{
uchar temp;
uchar A;
uchar B;
uchar C;
unsigned long int co;
uchar i[8];
//重新装入定时器0的初值;
TH0=-10000/256;
TL0=-10000%256;
FREQ=FREQ+1;
if(FREQ==80)
{
FREQ=0;
AD0=0xff;
temp=ReadData();
A = temp%1000/100;
B = temp%100/10;
C = temp%10;
co=A*100+B*10+C;
/////////////////////////////////////////////调试用2进转10进.
i[0]=co%2;
i[1]=co/2%2;
i[2]=co/2/2%2;
i[3]=co/2/2/2%2;
i[4]=co/2/2/2/2%2;
i[5]=co/2/2/2/2/2%2;
i[6]=co/2/2/2/2/2/2%2;
i[7]=co/2/2/2/2/2/2/2%2;
co=i[0]*128+i[1]*64+i[2]*32+i[3]*16+i[4]*8+i[5]*4+i[6]*2+i[7]*1;
///////////////////////////////////////////////
// A = co%1000/100;
// B = co%100/10;
// C = co%10;
co=500*co/256;
A = co/100;
B = co%100/10;
C = co%10;
// dispc[1][1]=ctab[A];
// dispc[1][2]=ctab[B];
// dispc[1][3]=ctab[C];
dispc[1][1] =ctab[A];
dispc[1][2] =ctab[10];
dispc[1][3] =ctab[B];
dispc[1][4] =ctab[C];
dispc[1][5] =capchartab[21];
initlcd();
displcd();
Delay(30000);
TH0=-1000/256;
TL0=-1000%256;
}
}
void serial () interrupt 4 using 1
{
uchar temp2;
RI=0;
temp2=SBUF; //读取消息
dispc[1][7] = ctab[temp2];
displcd();
SBUF=temp2;
}