[STM32F7] IIC总线的程序到底哪儿出了问题

[复制链接]
1163|10
 楼主| xxrs 发表于 2021-7-3 15:39 | 显示全部楼层 |阅读模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
uchar a;

//define 函数
void delay();
void start();
void response();
uchar read_byte();
void write_byte(uchar dat);
void init();
void stop();
void delay1(uint z);
void delay()
{
uint i,j;
for(i=0;i<1;i++)
  for(j=0;j<1;j++);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();

}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void response()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<255))i++;
scl=0;//destroy the condition of response, to avoid response.
delay();

}
uchar read_byte()
{
uchar i,j,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_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
  temp=temp<<1;
  scl=0;//写byte时把时钟拉低
  delay();
  sda=CY;// spill registor
  delay();
  scl=1;
  delay();
   
}
scl=0;
delay();
sda=1;
delay();
}
void init()//bus initation     
{
scl=1;
delay();
sda=1;
delay();
}
void main()
{
init();
start();
write_byte(0xa0);
response();
write_byte(3);
response();
write_byte(0xfe);
response();
stop();
delay1(100);
start();
write_byte(0xa0);
response();
write_byte(3);
response();
start();
write_byte(0xa1);
response();
a=read_byte();
P1=a;
response();
stop();
while(1);

}
void delay1(uint z){
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<100;j++);
}
 楼主| xxrs 发表于 2021-7-3 15:40 | 显示全部楼层
已经知道了,延迟时间出了问题。 按理说我的延迟时间是超过4.7微秒的,理应也是可以的。
zyf部长 发表于 2021-7-3 15:42 | 显示全部楼层
这个可以发到单片机论坛板块
heweibig 发表于 2021-7-3 15:44 | 显示全部楼层
楼主怎么就不能说说哪里犯错了呢
wuhany 发表于 2021-7-3 17:13 | 显示全部楼层
iic是一个很娇气的东西
zhanghqi 发表于 2021-7-3 17:15 | 显示全部楼层
希望楼主告知到底哪里的问题
 楼主| xxrs 发表于 2021-7-3 17:17 | 显示全部楼层
搞定了,呵呵,犯了低级错误…多谢各位
pengf 发表于 2021-7-3 17:19 | 显示全部楼层
楼主怎么就不能说说哪里犯错了呢
jiajs 发表于 2021-7-3 17:21 | 显示全部楼层

iic是一个很娇气的东西
zwll 发表于 2021-7-3 17:23 | 显示全部楼层
希望楼主告知到底哪里的问题
 楼主| xxrs 发表于 2021-7-3 17:25 | 显示全部楼层
搞定了,呵呵,犯了低级错误……结贴啦,多谢各位
您需要登录后才可以回帖 登录 | 注册

本版积分规则

405

主题

7415

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部