串口进不了中断,求助大侠!!

2020-02-02 09:36发布

#include<STC12C2052AD.h>         //库文件
#include <I2Ctongxin.h>
#include <chuanxingtongxin.h>
#define uchar unsigned char//宏定义无符号字符型
#define uint unsigned int  //宏定义无符号整型
static int  value;

sbit sda=P3^2;
sbit scl=P3^3;
//uchar tcnt=0,write=1;
void delay1()
{ ;; }
/*void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}*/
void start()  //开始信号
{
sda=1;
delay1();
scl=1;
delay1();
sda=0;
delay1();
}
void stop()   //停止
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
delay1();
}
void respons()  //应答
{
uchar i;
scl=1;
delay1();
while((sda==1)&&(i<250))i++;
scl=0;
delay1();
}
void initIC()
{
sda=1;
delay1();
scl=1;
delay1();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay1();
sda=CY;
delay1();
scl=1;
delay1();
}
scl=0;
delay1();
sda=1;
delay1();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay1();
sda=1;
delay1();
for(i=0;i<8;i++)
{
scl=1;
delay1();
k=(k<<1)|sda;
scl=0;
delay1();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0x4c);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0x42);
respons();
write_byte(address);
respons();
start();
write_byte(0x43);
respons();
date=read_byte();
stop();
return date;
}


void ICtongxin()
{
//TMOD=0x01;  //定时器工作在方式1
//ET0=1;
//EA=1;
//TH0=(65536-50000)/256; //对TH0 TL0赋值
//TL0=(65536-50000)%256; //使定时器0.05秒中断一次
//TR0=1;                 //开始计时

//if(write==1)       //判断计时器是否计时一秒
//{
//write=0;              //清零
//    P1=sec;
value=read_add(0xfe);    //在24c02的地址2中写入数据sec
//}

}

uchar flag;
uchar sec;
uchar table[]="I get ";


void InitUARTchuan(void)
{
TMOD &= 0x0f;
TMOD |= 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
PCON = 0x80;
EA = 1;
ES = 1;
TR1 = 1;
}


void SendOneByte(void)
{


SBUF=value;
while(!TI);
TI=0;
// ES=1;
// flag=0;
}


void UARTchuanInterrupt(void) interrupt 4
{
uchar i;
if(RI)
{
RI = 0;
sec=SBUF;   
ES=0;
for(i=0;i<6;i++)
{
SBUF =table[i];
while(!TI);
TI = 0;
}
SBUF=sec;
while(!TI);
TI=0;
ES=1;
}
else
TI = 0;
}


void main()
{
//P3M0 = P3M1 = 0x00;
InitUARTchuan();//串口初始化
//Timer0Init();
initIC();//IC初始化
P1=0xff;
while(1)
{
ICtongxin();
SendOneByte();
}
}串口助手向单片机发送数据时,返回不了I get *,可能是进不了中断,但是把SendOneByte();去掉后就可以了,哪位大侠帮忙看看是啥问题啊!!!
0条回答

一周热门 更多>