ballack_linux 发表于 2013-11-15 23:05
好 待会我搭个仿真环境试试
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char uint8;
sbit sda=P3^6;
sbit scl=P3^7;
void delay_ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay()
{;;}
void init()
{
scl=1;
delay();
sda=1;
delay();
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void reply()
{
uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<200))i++;
scl=0;
delay();
}
void unreply()
{
sda=1;
delay();
scl=1;
delay();
scl=0;
delay();
}
void write_byte(uchar dat)
{
uchar i;
scl=0;
delay();
for(i=0;i<8;i++)
{
if(dat&0x80)
sda=1;
else sda=0;
dat<<=1;
scl=1;
delay();
scl=0;
delay();
}
//sda=1;
scl = 1;
delay();
}
uchar read_byte()
{
uchar i,num;
/* scl=0;
delay();
sda=1;
delay();*/
for(i=0;i<8;i++)
{
scl=1;
delay();
num<<=1;
if(sda)
{
num++;
}
scl=0;
delay();
}
return num;
}
void UART_init(void)
{
SCON = 0X50;
TMOD &= 0X0F;
TMOD |= 0X20;
TH1 = 0XFD;
TL1 = 0XFD;
TR1 = 1;
}
void UART_send_byte(uint8 dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void main()
{
UART_init();
init();
start();
write_byte(0xa0);
reply();
write_byte(0xff);
reply();
write_byte(0xaa);
reply();
stop();
delay_ms(10);
start();
write_byte(0xa0);
reply();
write_byte(0xff);
reply();
start();
write_byte(0xa1);
reply();
UART_send_byte(read_byte());
unreply();
stop();
while(1);
}
哈哈!!! 我把你那个程序修改了下 调试成功!!!发送回串口的终于是AA 了 而且按一下复位键只发送一个 但是还是有点不懂刚才那个程序为啥一次发送四个字节!!!你的结果咋样了???
|