如何连续传输数据?

时间:2018-08-08 05:23:07

标签: c

芯片:C8051F321和PCF85176(LCD驱动器)

我正在尝试从C8051F321(MASTER)到PCF85176(SLAVE)进行通信,并且原始代码仅发送一个字节,如下所示:

  

开始→从地址→数据→确认→停止

但是我试图使其像

  

开始→从地址→命令→ACK→数据→ACK ...→ACK ..→ACK ..→   停止

它总是像我的代码一样没有效果

如何纠正我的代码?

感谢您的帮助〜

void SMBus_ISR (void) interrupt 7
{
    bit FAIL = 0;                       // Used by the ISR to flag failed
                                        // transfers
    static bit ADDR_SEND = 0;           // Used by the ISR to flag failed    
    unsigned char a;                    // transmissions as slave addresses

    if(ARBLOST == 0)
    {
        switch (SMB0CN & 0xF0)          // Status vector
        {
        // Master Transmitter/Receiver: START condition transmitted.
        case SMB_MTSTA:
            SMB0DAT = TARGET;           // Load address of the target slave
            SMB0DAT &= 0xFE;            // Clear the LSB of the address for the
                                        // R/W bit
            SMB0DAT |= SMB_RW;          // Load R/W bit
            STA = 0;                    // Manually clear START bit
            ADDR_SEND = 1;                        
            break;

        // Master Transmitter: Data byte 
        case SMB_MTDB:
            if (ACK)                    // Slave Address or Data Byte
            {                           // Acknowledged?
                if (ADDR_SEND)
                {                                      
                    ADDR_SEND = 0;      // Next byte is not a slave address              
                    if(SMB_DATA_OUT==0xF0)
                    {                                       
                        SMB0DAT = SMB_DATA_OUT;
                        ADDR_SEND = 0;
                    }
                    else
                    {                                   
                        SMB0DAT = SMB_Command_OUT;
                        ADDR_SEND = 1;                                    
                    }                                           
                }
                else                    // If previous byte was not a slave address,
                {                               
                    STO =1;             // Set STO to terminate transfer
                    SMB_BUSY =0;        // And free SMBus interface
                }
            }
            else
            {
                STO = 1;                // Set STO to terminate transfer
                STA = 1;                // By a START
                NUM_ERRORS++;           // Indicate error
            }                   
        break;

        //Master Receiver:byte received
        case SMB_MRDB:
            SMB_DATA_IN =SMB0DAT;       // Store received byte
            SMB_BUSY = 0;               // Free SMBus interface
            ACK = 0;                    // Send NACK to indicate last byte
                                        // of this transfer
            STO = 1;                    // Send STOP to terminate transfer
            break;

        default:
            FAIL = 1;                   // Indicate failed transfer
                                        // and handle at end of ISR 
            break;
        }//end switch
    }
    else
    {
         //ARBLOST = 1,error occurred...abort transmission
         FAIL = 1;
    } //end ARBLOST if

    if(FAIL)                            // If the transfer failed,
    {
         SMB0CF &= ~0x80;               // Reset communication
         SMB0CF |= 0x80;
         STA = 0;
         STO = 0;
         ACK = 0;
         SMB_BUSY = 0;                  // Free SMBus               
         FAIL = 0;
         NUM_ERRORS++;                  // Indicate an error occurred
    }
    SI = 0;                             //Clear interrupt flag
}

0 个答案:

没有答案