اطلاعیه

Collapse
No announcement yet.

آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

    سلام دوستان بنده پروژه ای دارم که هم ds1307 هم ماژول adxl345 و هم sht20 داره
    توی طراحی مدار هم هرکدوم را جدا جدا به پایه های میکرو وصل کردم
    هر سه تا هم از پروتکل i2c استفاده میکنن
    میخواستم بپرسم آیا عملا میشه ازین برد استفاده کرد؟
    چطوری میشه چند پورت i2c تعریف کرد؟ اصلا میشه یا نه؟

    #2
    پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

    سلام بله من انجام دادم
    این کتابخانه روی ds1307 و ads1110 جواب گرفتم



    /**********************************************************




    #include <delay.h>


    #include "i2csoft.h"
    #define uint8_t char


    /*******************************************************************************
    * Function Name : I2C_Configuration
    * Description : configuration
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    #define SCL_HI DDRA.6=0
    #define SCL_LOW DDRA.6=1


    #define SDA_HI DDRA.5=0
    #define SDA_LOW DDRA.5=1




    #define SCL_H SCL_HI
    #define SCL_L SCL_LOW

    #define SDA_H SDA_HI
    #define SDA_L SDA_LOW


    #define SDA_read PINA.5




    void SoftI2CInit(void)
    {
    SDA_L;
    SCL_L;
    }


    /*******************************************************************************
    * Function Name : I2C_delay
    * Description : delay time
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    static void I2C_delay(void)
    {
    int i = 180;
    while(i)
    {
    i--;
    }


    }


    static void delay(void)
    {
    int i = 20;
    while(i)
    {
    i--;
    }


    }
    /*******************************************************************************
    * Function Name : I2C_Start
    * Description : None
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    int SoftI2CStart(void)
    { I2C_delay();
    SDA_H;
    delay();
    SCL_H;
    I2C_delay();


    if( !SDA_read )
    {
    return -1; /* SDA low, bus is busy, exit */
    }


    SDA_L;
    I2C_delay();


    if( SDA_read )
    {
    return -1; /* SDA high, bus error, exit */
    }


    SDA_L;
    I2C_delay();
    return 0;
    }


    /*******************************************************************************
    * Function Name : I2C_Stop
    * Description : None
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    void SoftI2CStop(void)
    {I2C_delay();
    SCL_L;
    I2C_delay();
    SDA_L;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SDA_H;
    I2C_delay();
    }


    /*******************************************************************************
    * Function Name : I2C_Ack
    * Description : None
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    void I2C_Ack(void)
    {
    SCL_L;
    SDA_L;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SCL_L;
    SDA_L;


    }


    /*******************************************************************************
    * Function Name : I2C_NoAck
    * Description : None
    * Input : None
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    void I2C_NoAck(void)
    {
    SCL_L;
    I2C_delay();
    SDA_H;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SCL_L;
    I2C_delay();
    }


    /*******************************************************************************
    * Function Name : I2C_WaitAck
    * Description : None
    * Input : None
    * Output : None
    * Return : Return 0 ACK, Return -1 no ACK
    * Attention : None
    *******************************************************************************/
    int I2C_WaitAck(void)
    {


    SDA_H;
    I2C_delay();
    SCL_H;
    I2C_delay();
    if( !SDA_read )
    {
    SDA_L;
    }
    SCL_L;

    return 0;
    }


    /*******************************************************************************
    * Function Name : I2C_SendByte
    * Description : Send data high to low
    * Input : - SendByte: send data
    * Output : None
    * Return : None
    * Attention : None
    *******************************************************************************/
    void SoftI2CWriteByte(uint8_t SendByte)
    {
    uint8_t i = 8;


    while(i--)
    {
    SCL_L;
    delay();
    delay();
    delay();
    if( SendByte & 0x80 )
    {
    SDA_H;
    }
    else
    {
    SDA_L;
    }
    SendByte <<= 1;
    I2C_delay();
    SCL_H;
    I2C_delay();
    }
    SCL_L;
    I2C_WaitAck();
    }




    /*******************************************************************************
    * Function Name : I2C_ReceiveByte
    * Description : read data high to low
    * Input : None
    * Output : None
    * Return : return data
    * Attention : None
    *******************************************************************************/
    char SoftI2CReadByte(char ACK)
    {
    uint8_t i = 8;
    uint8_t ReceiveByte = 0;

    while(i--)
    {
    ReceiveByte <<= 1;
    SCL_L;
    I2C_delay();
    SDA_H;
    delay();
    SCL_H;
    I2C_delay();
    if( SDA_read )
    {
    ReceiveByte |= 0x01;
    }
    }
    SCL_L;
    if(ACK==1)I2C_Ack();
    else I2C_NoAck();
    return ReceiveByte;
    }






    /*********************************************************************************************************
    END FILE
    *********************************************************************************************************/




    .h

    int SoftI2CStart(void);
    void SoftI2CStop(void);


    دیدگاه


      #3
      پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

      اون قسمت های i2cstart و i2cstop برای چیه؟

      امکانش هست سورس نحوه استفاده برای ds1307 را هم بزارید لطفا؟
      جدیدترین ویرایش توسط bardiya3; ۲۲:۲۴ ۱۳۹۶/۰۹/۱۴.

      دیدگاه


        #4
        پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

        نوشته اصلی توسط bardiya3 نمایش پست ها
        سلام دوستان بنده پروژه ای دارم که هم ds1307 هم ماژول adxl345 و هم sht20 داره
        توی طراحی مدار هم هرکدوم را جدا جدا به پایه های میکرو وصل کردم
        هر سه تا هم از پروتکل i2c استفاده میکنن
        میخواستم بپرسم آیا عملا میشه ازین برد استفاده کرد؟
        چطوری میشه چند پورت i2c تعریف کرد؟ اصلا میشه یا نه؟

        سلام
        شما نیاز به چند i2c ندارید
        خوبی i2c این هست که شما میتونید روی یک خط 127 دستگاه داشته باشین و با آدرس دهی به این دستگاه ها میتونید اونارو کنترل کنید
        پیشنهاد میکنم در مورد نحوه کار i2c یک تحقیق کوچیک انجام بدین
        موفق باشید

        دیدگاه


          #5
          پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

          این هم برنامه ds1307 . کد کد ویژن بود که تغییرش دادم


          #include <bcd.h>








          //unsigned char rtc_read(unsigned char address)
          //{
          //unsigned char date;
          //
          //SoftI2CStart();
          //SoftI2CStart();
          //SoftI2CWriteByte(0xd0);
          //SoftI2CWriteByte(address);
          //SoftI2CStart();
          //SoftI2CWriteByte(0xd1);
          //date=SoftI2CReadByte(0);
          //SoftI2CStop();
          //return date;
          //}




          //void rtc_write(unsigned char address,unsigned char date)
          //{
          //
          //SoftI2CStart();
          //SoftI2CStart();
          //SoftI2CWriteByte(0xd0);
          //SoftI2CWriteByte(address);
          //SoftI2CWriteByte(date);
          //SoftI2CStop();
          //}






          void rtc2_init(unsigned char rs,unsigned char sqwe,unsigned char out)
          {


          rs&=3;
          if (sqwe) rs|=0x10;
          if (out) rs|=0x80;
          SoftI2CStart();
          SoftI2CWriteByte(0xd0);
          SoftI2CWriteByte(7);
          SoftI2CWriteByte(rs);
          SoftI2CStop();
          }










          void rtc2_get_time(unsigned char *hour, unsigned char *min, unsigned char *sec)
          {




          SoftI2CStart();
          SoftI2CWriteByte(0xd0);
          SoftI2CWriteByte(0);
          SoftI2CStop(); // 09022011_1
          SoftI2CStart();
          SoftI2CWriteByte(0xd1);
          *sec=bcd2bin(SoftI2CReadByte(1));
          *min=bcd2bin(SoftI2CReadByte(1));
          *hour=bcd2bin(SoftI2CReadByte(0));
          SoftI2CStop();


          }










          /////////////////////////////////////
          void rtc2_set_time(unsigned char hour,unsigned char min,unsigned char sec)
          {


          //SoftI2CStart();
          SoftI2CStart();
          SoftI2CWriteByte(0xd0);
          SoftI2CWriteByte(0);
          SoftI2CWriteByte(bin2bcd(sec));
          SoftI2CWriteByte(bin2bcd(min));
          SoftI2CWriteByte(bin2bcd(hour));
          SoftI2CStop();
          }






          void rtc2_get_date(unsigned char *date,unsigned char *month,unsigned char *year)
          {


          SoftI2CStart();
          SoftI2CStart();
          SoftI2CWriteByte(0xd0);
          SoftI2CWriteByte(4);
          SoftI2CStart();
          SoftI2CWriteByte(0xd1);
          *date=bcd2bin(SoftI2CReadByte(1));
          *month=bcd2bin(SoftI2CReadByte(1));
          *year=bcd2bin(SoftI2CReadByte(0));
          SoftI2CStop();
          }


          void rtc2_set_date(unsigned char date,unsigned char month,unsigned char year)
          {


          SoftI2CStart();
          SoftI2CStart();
          SoftI2CWriteByte(0xd0);
          SoftI2CWriteByte(4);
          SoftI2CWriteByte(bin2bcd(date));
          SoftI2CWriteByte(bin2bcd(month));
          SoftI2CWriteByte(bin2bcd(year));
          SoftI2CStop();
          }

          دیدگاه


            #6
            پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

            درسته ولی من برد را برای یه پروژه دیگه طراحی کرده بودم
            میخواستم برد را دوباره طراحی نکنم از اول

            دیدگاه


              #7
              پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

              ببخشید این قسمت چرا کامنت شده؟
              کتابخانهi2csoft را از کجا باید بیارم؟
              نوشته اصلی توسط sepehr63 نمایش پست ها
              #include <delay.h>

              #include "i2csoft.h"
              #define* uint8_t* char

              دیدگاه


                #8
                پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

                پست شماره 2 خودش
                i2csoft .c هست
                i2csoft .h هم که یک فایل هست که توش توابع فقط تعریف میشند

                دیدگاه


                  #9
                  پاسخ : آیا میتوان بر روی avr چند پورت i2c ایجاد کرد

                  نوشته اصلی توسط sepehr63 نمایش پست ها
                  پست شماره 2 خودش
                  i2csoft .c هست
                  i2csoft .h هم که یک فایل هست که توش توابع فقط تعریف میشند
                  پس کامنت نباید باشن اون سه خط اول؟

                  دیدگاه

                  لطفا صبر کنید...
                  X