这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » I2C,SWITCH 我写的I2C 的双机通信的程序,但是SWITCH 语句就是

共2条 1/1 1 跳转至

I2C,SWITCH 我写的I2C 的双机通信的程序,但是SWITCH 语句就是执行不对:

院士
2006-09-17 18:14:16     打赏
I2C,SWITCH 我写的I2C 的双机通信的程序,但是SWITCH 语句就是执行不对:



关键词: SWITCH     写的     双机     通信     程序     但是     语句         

院士
2006-12-22 22:43:00     打赏
2楼
问 请大家帮忙看看这个奇怪的问题:
   我写的I2C 的双机通信的程序,但是SWITCH 语句就是执行不对:

   当switch(0) 或 switch(1)时,程序确跑到case 4 去执行;
   当switch(2)及其后时,程序才执行正确;

我用的PIC16F877A 的片子,PICC8.05  的
C编译器,请大家帮忙看看
   
void sServiceI2c(void)
{
    bI2cState=0;//////
    switch(bI2cState)
    {
        case 0:        
            /*generate i2c bus start condition*/
            bI2cState++;
            /* Initiate START condition*/
            SEN=1;
            break;
        

        case 1:        
            /*generate i2c address write (r/w=0)*/
            bI2cState++;
            SSPBUF=(bSlaveAddres<<1);
            break;
        

        case 2:
            /*test acknowledge after address and data write*/
            if((SSPCON2&&(1<<ACKSTAT))==1)
            {
                MBitSet(fI2c,AckError);                
                bI2cState=0;
                PEN=1;  // Generate STOP condition
            }
            else
            {
                if(bWrCnt>=15)
                {
                    bI2cState++;
                }
                SSPBUF=I2cTxBuff[bWrCnt];
                bWrCnt++;                
            }
            break;

        case 3:
            /*generate i2c bus stop condition*/
            if((SSPCON2&&(1<<ACKSTAT))==1)
            {
                bI2cState=0;
                MBitSet(fI2c,AckError);    
            }
            else
            {
                bI2cState++;
            }
            PEN=1;        
            break;

        case 4:
            /*generate i2c bus start condition*/
            bI2cState++;
            SEN=1;
            break;

        case 5:
            /*generate i2c address write (r/w=1)*/
            bI2cState++;
            SSPBUF=(bSlaveAddres<<1);
            break;

        case 6:
            /*test acknowledge after address write*/
            if((SSPCON2&&(1<<ACKSTAT))==1)
            {
                MBitSet(fI2c,AckError);
                bI2cState=0;
                PEN=1;
            }
            else
            {
                bI2cState++;        
                RCEN=1;
            }
            break;

        case 7:
            /*read slave i2c*/
            if(bRdCnt>=16)
            {                /*send not acknowledge*/
                bI2cState++;
                I2cRxBuff[bRdCnt]=SSPBUF;
                ACKDT=1;
                ACKEN=1;
            }
            else
            {                /*send acknowledge*/
                I2cRxBuff[bRdCnt]=SSPBUF;
                bRdCnt++;
                ACKDT=0;
                ACKEN=1;
                do
                 {
                      ;
                 }
                   while((SSPCON2&&(1<<ACKEN))==1);          // Wait for ack cycle finished
                RCEN=1;
                SSPIF=0;    
            }
            break;

        case 8:
            /*generate i2c stop condition */
            bI2cState++;
            SSPIE=0;
            PEN=1;

            bI2cState=0;
            MBitClr(fI2c,RwDone);
            break;
        
        default:;            
    }
}
1: 到底哪里有错啊?难道大家没有碰到过吗? 2: 大家帮帮忙啊大家帮帮忙啊


    TKS

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]