bias 发表于 2022-11-25 16:58:56

STC8H硬件I2C如何实现I2C地址扫描

想通过wait()里面超时来判断,怎么都会有中断标志,就算写错地址也会有,有什么好的办法扫描呢
模拟的在检查ACK那里判断是可行的,一直这样用的

void I2C_Init(void)
{       
        P_SW2 |= 0x80;                        //使能访问XFR, I2C功能寄存器访问需要打开这个
       
        I2CCFG= 0xFE;                        //使能I2C,主机模式,MSSPEED=2x62+4=128,I2C总线速度=40M/2/128=156.25K
        I2CMSST = 0x00;
}

bit I2C_Wait(void)
{       
        unsigned long TimeOut = 200000L;        //40*1000*5,5ms
        while(TimeOut--)
        {
                if (I2CMSST & 0x40)
                {
                        I2CMSST &= ~0x40;                        //清中断标志
                        return TRUE;
                }
        }
        return FALSE;
}

//启动I2C总线
void I2C_Start(void)
{
        I2CMSCR = 0x01;                                                //发送START命令,如果用中断则是0x81
        if( !I2C_Wait() )
                UART_PutString_NoISR("I2C Start Fail!\r\n");
               
}

//主机发送一个字节
void I2C_SendByte(unsigned char SendByte)
{
        I2CTXD= SendByte;                                        //写数据到数据缓冲区
        I2CMSCR = 0x02;                                                //发送SEND 命令,,如果用中断则是0x81
        I2C_Wait();
}

//主机接受从机的应答信号
bit I2C_RecvAck(void)                                        //返回为true有ACK,false无ACK
{
        I2CMSCR = 0x03;                                                //发送读ACK 命令
        if( I2C_Wait() )
        {
                return TRUE;
        }
        else
        {
                UART_PutString_NoISR("I2C RecvAck Fail!\r\n");
                return FALSE;
        }
}

//主机读取一个字节
unsigned char I2C_ReceiveByte(void)
{
        I2CMSCR = 0x04;                                                //发送RECV 命令
        I2C_Wait();
        return I2CRXD;
}


//主机给从机发送应答信号
void I2C_SendAck( void)
{
        I2CMSST = 0x00;                                         //设置ACK 信号
        I2CMSCR = 0x05;                                         //发送ACK 命令
        I2C_Wait();
}

void I2C_SendNoAck( void)
{
        I2CMSST = 0x01;                                         //设置NAK 信号
        I2CMSCR = 0x05;                                                //发送ACK 命令
        I2C_Wait();
}

//停止I2C总线
void I2C_Stop(void)
{
        I2CMSCR = 0x06;                                         //发送STOP 命令
        I2C_Wait();
}

void I2C_Scan_SlaveAddress(void)
{
        u8 i;
        for(i=0;i<=0x7F;i++)        //7位SlaveAddress
        {
                I2C_Start();
                I2C_SendByte((i<<1) | 0);        //I2C从地址+写标志
                if ( I2C_RecvAck() )
                {
//                        UART_PutString("\r\nFound Device Address:");
//                        UART_DebugDat(&i,1);
//                        UART_PutString("\r\n");

                        UART_PutString_NoISR("\r\nFound Device Address:");
                        UART_PutChar_NoISR(i);
                        UART_PutString("\r\n");

                }
                else
                {
                        I2C_Stop();
                }
        }
}




给我全部列出来:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:
Found Device Address:!
Found Device Address:"
Found Device Address:#
Found Device Address:$
Found Device Address:%
Found Device Address:&
Found Device Address:'
Found Device Address:(
Found Device Address:)
Found Device Address:*
Found Device Address:+
Found Device Address:,
Found Device Address:-
Found Device Address:.
Found Device Address:/
Found Device Address:0
Found Device Address:1
Found Device Address:2
Found Device Address:3
Found Device Address:4
Found Device Address:5
Found Device Address:6
Found Device Address:7
Found Device Address:8
Found Device Address:9
Found Device Address::
Found Device Address:;
Found Device Address:<
Found Device Address:=
Found Device Address:>
Found Device Address:?
Found Device Address:@
Found Device Address:A
Found Device Address:B
Found Device Address:C
Found Device Address:D
Found Device Address:E
Found Device Address:F
Found Device Address:G
Found Device Address:H
Found Device Address:I
Found Device Address:J
Found Device Address:K
Found Device Address:L
Found Device Address:M
Found Device Address:N
Found Device Address:O
Found Device Address:P
Found Device Address:Q
Found Device Address:R
Found Device Address:S
Found Device Address:T
Found Device Address:U
Found Device Address:V
Found Device Address:W
Found Device Address:X
Found Device Address:Y
Found Device Address:Z
Found Device Address:[
Found Device Address:\
Found Device Address:]
Found Device Address:^
Found Device Address:_
Found Device Address:`
Found Device Address:a
Found Device Address:b
Found Device Address:c
Found Device Address:d
Found Device Address:e
Found Device Address:f
Found Device Address:g
Found Device Address:h
Found Device Address:i
Found Device Address:j
Found Device Address:k
Found Device Address:l
Found Device Address:m
Found Device Address:n
Found Device Address:o
Found Device Address:p
Found Device Address:q
Found Device Address:r
Found Device Address:s
Found Device Address:t
Found Device Address:u
Found Device Address:v
Found Device Address:w
Found Device Address:x
Found Device Address:y
Found Device Address:z
Found Device Address:{
Found Device Address:|
Found Device Address:}
Found Device Address:~
Found Device Address:

国学芯用 发表于 2022-11-25 17:04:15

STC8H系列 范例程序:https://www.stcai.com/filedownload/618387

bias 发表于 2022-11-25 21:26:38

国学芯用 发表于 2022-11-25 17:04
STC8H系列 范例程序:https://www.stcai.com/filedownload/618387
(引用自2楼)

这个和规格书中的例程没区别啊

程序是按照规格书写的,不是说程序的问题。

是如何实现I2C 地址扫描

angler12 发表于 2022-11-28 17:46:39

仅供参考: https://github.com/mozilla-b2g/i2c-tools/blob/master/tools/i2cdetect.c , https://github.com/mcauser/i2cdetect

bias 发表于 2022-11-29 12:34:02

angler12 发表于 2022-11-28 17:46
仅供参考: https://github.com/mozilla-b2g/i2c-tools/blob/master/tools/i2cdetect.c , https://github.co ...
(引用自4楼)

谢谢,问题已经解决了,根据STC官方的提示:

正确的做法是在中断中判断MSACKI这个位来判断读取的是ACK还是NAK
如果是ACK,则表示当前地址有效,否则如果是NAK,则地址无效

小李非刀 发表于 2022-12-9 17:17:21

发了I2C地址后根据应答来判断。从机均能收到起始条件中断、地址中断,但地址不对则非应答。
页: [1]
查看完整版本: STC8H硬件I2C如何实现I2C地址扫描