Thông báo

Collapse
No announcement yet.

Nối mạng AVR bằng I2C

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Nối mạng AVR bằng I2C

    Trước, e có thấy bác nào đó nói đến vấn đề này rồi nhưng diễn đàn bị lỗi, e tìm lại k thấy bài đó đâu nữa. Bác nào làm vấn đề này rùi thì giải đáp giùm e với:

    E định nối mạng các Chip AVR với nhau bằng I2C: 1 Master+vài Slave. (dùng atmega8)
    Down AppNote trên atmel về : cả master driver lẫn slave driver. Ví dụ này nó viết cho IAR compiler -> e chuyển sang CodeVision (hầu như k phải viết thêm j cả).
    Chạy thử -> kết quả là: phần Master (M) truyền cho các Slave(S) hoạt động đúng nhưng chế độ S truyền lại cho M thì k thực hiện được. Thằng M toàn báo lỗi 0x48 (đã truyền SLA+R nhưng nhận được NACK). Các driver này viết dựa trên sự kiện ngắt của TWI. E cũng đã mất mấy đêm soi kĩ datasheet và AppNote của nó rùi mà chưa phát hiện ra lỗi. Mong các bác có kinh nghiệm chỉ giáo cho e với.

    Thanks!

    Vote hộ tớ với. Thanks!

  • #2
    Slave: http://atmel.com/dyn/resources/prod_...nts/AVR311.zip
    Master: http://atmel.com/dyn/resources/prod_...nts/AVR315.zip

    Trên đây là 2 link mã nguồn cho driver TWI. Mã nguồn hoàn toàn đúng. E đã soi ra vấn đề rùi.

    Vote hộ tớ với. Thanks!

    Comment


    • #3
      Mình cũng đang nghiên cứu nối thử I2C, nhưng mình chưa soi ra vấn đề dùng codevision với phần mã nguồn của Atmel. Bạn có thể hướng dẫn cụ thể được không ? Cần điều chỉnh mã nguồn như thế nào cho phù hợp.
      Co cong mai sat co ngay nen kim !

      Comment


      • #4
        Mình định upload bản Test của mình lên diễn đàn nhưng k được.

        Phần khai báo bạn nên chuyển như sau (cả Master lẫn Slave):

        union TWI_UstatusReg // Status byte holding flags.
        {
        /* unsigned char all;
        struct
        {
        unsigned char lastTransOK:1;
        unsigned char unusedBits:7;
        };*/
        unsigned char lastTransOK:1;
        unsigned char unusedBits:7;//*/
        };

        extern union TWI_UstatusReg TWI_statusReg;

        Phần trong dấu /*...*/ là khai báo cũ của Atmel.
        Compile lại để xem phần nào còn lỗi để chình lại. CHủ yếu là các lỗi gán không phù hợp giữa kiểu mới và kiểu cũ thôi. Bạn làm dần dần nhé. Có j thì post lỗi lên để mọi ng cùng tham khảo.
        Phần mã nguồn của nó hầu như k thay đổi ngoài sửa lỗi gán.
        Chúc thành công!

        Vote hộ tớ với. Thanks!

        Comment


        • #5
          Doan ban sua lai:
          union TWI_UstatusReg // Status byte holding flags.
          {
          /* unsigned char all;
          struct
          {
          unsigned char lastTransOK:1;
          unsigned char unusedBits:7;
          };*/
          unsigned char lastTransOK:1;
          unsigned char unusedBits:7;//*/
          };

          extern union TWI_UstatusReg TWI_statusReg;

          Co ve nhu chua on lam. Atmel ho su dung to hop (union) trong co mot cau truc (struct ) lam nhiem vu bit field . Nhung hinh nhu CodeVison khong ho tro kieu to hop ca union va struct. Vi vay neu ban dung union khong, cac khai bao se bi chong len nhau o cac bit LSB. Minh bo cai union ma chi dung struct:
          struct TWI_statusReg_
          {
          unsigned char lastTransOK:1;
          unsigned char RxDataInBuf:1;
          unsigned char genAddressCall:1; // TRUE = General call, FALSE = TWI Address;
          unsigned char unusedBits:5;
          }TWI_statusReg;
          thi hop ly hon. Tuy nhien day la mot thanh ghi tu tao bang phan mem, trong chuong trinh co su dung hay khong la tuy minh. Dieu dang noi la Atmel cung da bo bot nhieu lenh trong than chuong trinh ngat, nen khi dung se gap nhieu rac roi. Minh thu sua lai va chay thu cung tam tam. Cac ban tham khao va gop y dum:
          // 2 Wire bus interrupt service routine
          interrupt [TWI] void twi_isr(void)
          {
          static unsigned char TWI_bufPtr;

          switch (TWSR)
          {
          case TWI_STX_ADR_ACK: // Own SLA+R has been received; ACK has been returned
          // case TWI_STX_ADR_ACK_M_ARB_LOST: // Arbitration lost in SLA+R/W as Master; own SLA+R has been received; ACK has been returned
          TWI_bufPtr = 0; // Set buffer pointer to first data location
          case TWI_STX_DATA_ACK: // Data byte in TWDR has been transmitted; ACK has been received
          TWDR = TWI_buf[TWI_bufPtr++];
          TWCR = 0xC5;
          break;
          case TWI_STX_DATA_NACK: // Data byte in TWDR has been transmitted; NACK has been received.
          // I.e. this could be the end of the transmission.
          if (TWI_bufPtr == TWI_msgSize) // Have we transceived all expected data?
          {
          TWI_statusReg.lastTransOK = TRUE; // Set status bits to completed successfully.
          }else // Master has sent a NACK before all data where sent.
          {
          TWI_state = TWSR; // Store TWI State as errormessage.
          }
          // Put TWI Transceiver in passive mode.
          TWCR = 0x04;
          break;
          case TWI_SRX_GEN_ACK: // General call address has been received; ACK has been returned
          TWI_statusReg.RxDataInBuf = TRUE;
          TWI_bufPtr = 0; // Set buffer pointer to first data location // Reset the TWI Interupt to wait for a new event.
          TWCR = 0xC5;
          // case TWI_SRX_GEN_ACK_M_ARB_LOST: // Arbitration lost in SLA+R/W as Master; General call address has been received; ACK has been returned
          TWI_statusReg.genAddressCall = TRUE;
          case TWI_SRX_ADR_ACK: // Own SLA+W has been received ACK has been returned
          // case TWI_SRX_ADR_ACK_M_ARB_LOST: // Arbitration lost in SLA+R/W as Master; own SLA+W has been received; ACK has been returned
          // Dont need to clear TWI_S_statusRegister.generalAddressCall due to that it is the default state.
          TWI_statusReg.RxDataInBuf = TRUE;
          TWI_bufPtr = 0; // Set buffer pointer to first data location
          // Reset the TWI Interupt to wait for a new event.
          TWCR = 0xC5;
          break;
          case TWI_SRX_ADR_DATA_ACK: // Previously addressed with own SLA+W; data has been received; ACK has been returned
          TWI_buf[TWI_bufPtr++] = TWDR; // Store the TWI Slave Address in the first location of the buffer.
          TWCR = 0xC5;
          if (TWI_buf[0]==0xAA)
          {
          PORTB.1 = 1;
          counter = 0;
          }else
          {
          PORTB.1 = 0;
          }
          break;
          case TWI_SRX_GEN_DATA_ACK: // Previously addressed with general call; data has been received; ACK has been returned
          TWI_buf[TWI_bufPtr++] = TWDR;
          TWCR = 0xC5;
          if (TWI_buf[0]==0xAA)
          {
          PORTB.1 = 1;
          }else
          {
          PORTB.1 = 0;
          }
          TWI_statusReg.lastTransOK = TRUE; // Set flag transmission successfull.
          // Reset the TWI Interupt to wait for a new event.
          break;
          case TWI_SRX_STOP_RESTART: // A STOP condition or repeated START condition has been received while still addressed as Slave
          // Put TWI Transceiver in passive mode.
          TWCR = 0xC5;

          break;
          case TWI_SRX_ADR_DATA_NACK: // Previously addressed with own SLA+W; data has been received; NOT ACK has been returned
          case TWI_SRX_GEN_DATA_NACK: // Previously addressed with general call; data has been received; NOT ACK has been returned
          case TWI_STX_DATA_ACK_LAST_BYTE: // Last data byte in TWDR has been transmitted (TWEA = “0”); ACK has been received
          // case TWI_NO_STATE // No relevant state information available; TWINT = “0”
          case TWI_BUS_ERROR: // Bus error due to an illegal START or STOP condition
          default:
          TWI_state = TWSR; // Store TWI State as errormessage, operation also clears the Success bit.
          TWCR = 0xC5;
          }
          }
          Xin loi minh khong upload file duoc, nen chi goi doan ngat thoi.
          Co cong mai sat co ngay nen kim !

          Comment

          Về tác giả

          Collapse

          va_hien Tìm hiểu thêm về va_hien

          Bài viết mới nhất

          Collapse

          Đang tải...
          X