大家看看哪出错了,麻烦帮下忙,谢谢!!!
#include <reg52.h>
unsigned char a,b,c,d;
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!
ti);
TI = 0;
}
void delay10ms(void) //误差 -0.000000000002us
{
unsigned int a,b;
for(b=249;b>0;b--)
for(a=17;a>0;a--);
}
void main()
{
InitUART();
while(1)
{
if(a==1)
{
a=0;
for(b=0;b<200;b++)//b=0;等于其他数就死机,for循环没走完时有中断也死机
{
SBUF=c;
while(!TI);
TI=0;
delay10ms();
}
/*for(b=0;b<200;b++)//多加这个for循环就死机
{
SBUF=c;
while(!TI);
TI=0;
delay10ms();
}*/
}
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
c=SBUF;
a=1;
//add your code here!
}
else
TI = 0;
}
{
SBUF = c;
while(!TI);
TI = 0;
}
这个调用了也死机,是从单片机精灵上搬来的
TI = 0;
中断服务程序里面这句不要
{
if(RI)
{
RI = 0;
c=SBUF;
a=1;
//add your code here!
}
else
TI = 0;
}
如果这个中断函数会执行的话,至少在//add your code here!这行后面加上:
TI=1;
这样才可以使while(!TI);这个循环跳出来。不然TI永远为0,就是死循环了。
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
c=SBUF;
a=1;
//add your code here!
TI = 1;
}
else
TI = 0;
}
谢谢版主!!!!!去掉以后却实可以运行了
可以读24C01~24C16了
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a,b,c,d,f,k,s;
sbit sda=P2^0;
sbit scl=P2^1;
uchar code table1[]={0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae};
uchar code table2[]={0xa1,0xa3,0xa5,0xa7,0xa9,0xab,0xad,0xaf};
void delay()
{;;}
void delay5ms() //误差 -0.000000000001us
{
uint a,b;
for(b=15;b>0;b--)
for(a=152;a>0;a--);
}
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=11;y>0;y--);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date )
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address )
{
uchar date;
start();
write_byte(s);
respons();
write_byte(address);
respons();
start();
write_byte(f);
respons();
date=read_byte();
stop();
return date;
}
void read_256(uchar j)
{
for(k=0;k<200;k++)
{
b++;
s=table1[j];
f=table2[j];
d=read_add(b);
SBUF=d;
while(!TI);
TI=0;
delay5ms();
}
for(k=56;k>0;k--)
{
b++;
s=table1[j];
f=table2[j];
d=read_add(b);
SBUF=d;
while(!TI);
TI=0;
delay5ms();
}
}
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void main()
{
InitUART();
init();
while(1)
{
if(c==0x01)
{
c=0;
read_256(1);
}
if(c==0x02)
{
c=0;
read_256(2);
}
if(c==0x03)
{
c=0;
read_256(3);
}
if(c==0x04)
{
c=0;
read_256(4);
}
if(c==0x05)
{
c=0;
read_256(5);
}
if(c==0x06)
{
c=0;
read_256(6);
}
if(c==0x07)
{
c=0;
read_256(7);
}
if(c==0x08)
{
c=0;
read_256(8);
}
}
}
void UARTInterrupt(void) interrupt 4
{
if(RI)
{
RI = 0;
c=SBUF;
//add your code here!
}
// else
// TI = 0;
}
一周热门 更多>