اطلاعیه

Collapse
No announcement yet.

مشکل در ارتباط spi

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

    مشکل در ارتباط spi

    اساتید عزیز سلام. بنده سعی دارم تا بین رزپری و mega32 به ارتباط spi داشته باشم. کد سمت mega32 رو ملاحظه میکنید:

    کد:
    #include <mega32.h>
    #include <delay.h>
    #include <glcd.h>
    #include <font5x7.h>
    
    // Declare your global variables here
    
    void main(void)
    {        
      GLCDINIT_t glcd_init_data;
      glcd_init_data.font=font5x7;
      glcd_init(&glcd_init_data);
    
      glcd_clear();
      PORTD=(1<<PORTD7)|(1<<PORTD6)|(1<<PORTD5)|(1<<PORTD4)|(1<<PORTD3)|(1<<PORTD2)|(1<<PORTD1)|(1<<PORTD0); // Activate pullups
      DDRB=(1<<DDB6); // MISO
      SPCR=(1<<SPIE)|(1<<SPE); // Slave mode , Interrupt enabled , Clock = Fcpu / 4    
      #asm("sei");
      while(1);
    }
    interrupt [SPI_STC] void SPI_Serial_Transfer_Complete(void)
    {        
      glcd_outtextxyf(5,40,"GOTCHA!");       
      delay_ms(800);
      glcd_clear();
    }
    مگا32 در نقش slave هست و رزپری Master. سرعت کلاک spi در rpi برابر 250000Hz هست.
    لازم به ذکر هست که سرعت کلاک میکرو 1MHz ست شده. من توقع دارم با هر بار اجرا شدن کد ارسال اطلاعات روی رزپری، عبارت "GOTHA" روی LCD نمایش داده بشه اما یکی دو بار بیشتر این اتفاق نمیفته و از دفعه دوم به بعد این عبارت نشون داده نمیشه روی LCD (انگار برنامه وارد روتین وقفه نمیشه)
    ممنون میشم دوستان کمکم کنین.

    #2
    پاسخ : مشکل در ارتباط spi

    دوستان یک پدیت بذارم در مورد سوالم. کد قبلی رو بی خیال شین. این کد رو نگاه کنین:

    کد:
    #include <mega32.h>
    #include <delay.h>
    #include <glcd.h>
    #include <font5x7.h>
    
    // Declare your global variables here
    
    void main(void)
    {    
      unsigned char temp;    
      GLCDINIT_t glcd_init_data;
      glcd_init_data.font=font5x7;
      glcd_init(&glcd_init_data);
    
      glcd_clear();
      DDRB=(1<<DDB6); // MISO
      DDRC=(1<<DDC7)|(1<<DDC6)|(1<<DDC5)|(1<<DDC4)|(1<<DDC3)|(1<<DDC2)|(1<<DDC1)|(1<<DDC0);
      PORTD=(1<<PORTD7)|(1<<PORTD6)|(1<<PORTD5)|(1<<PORTD4)|(1<<PORTD3)|(1<<PORTD2)|(1<<PORTD1)|(1<<PORTD0); // Activate pullups
      SPCR=(1<<SPIE)|(1<<SPE); // Slave mode , Interrupt enabled , Clock = Fcpu / 4
      temp=SPSR;
      temp=SPDR;
      SPDR=PIND;
      #asm("sei");
      while(1);
    }
    interrupt [SPI_STC] void SPI_Serial_Transfer_Complete(void)
    {        
      glcd_outtextxyf(5,40,"GOTCHA!");
      delay_ms(50);
      glcd_clear();
      PORTC=SPDR;
      SPDR=PIND;
    }
    من همچنان توقع دارم با اجرای برنامه سمت RPI، عبارت GOTHA روی LCD نمایش داده بشه که گاهی اوقات فقط یک بار نمایش داده میشه و دیگه نمایش داده نمیشه. در کد سمت RPI من کارکتر a رو ارسال میکنم و هر بار که کد رو اجرا میکنم خروجی a دوباره برام برمیگرده! انگار ارتباط spi درست هست اما زمانی که وقفه رخ میده، چیزی روی LCd نمایش داده نمیشه.

    این هم کد سمت RPi:
    کد:
    #include <sys/ioctl.h>
    #include <linux/spi/spidev.h>
    #include <fcntl.h>
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    
    /**********************************************************
    Declare Global Variables
    ***********************************************************/
    int fd;
    unsigned char result;
    
    /**********************************************************
    Declare Functions
    ***********************************************************/
    int spiTxRx(unsigned char txDat);
    
    /**********************************************************
    Main
    ***********************************************************/
    int main (void)
    {
      fd = open("/dev/spidev0.0", O_RDWR);
    
      unsigned int speed = 250000;
      ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    
      result = spiTxRx('a');
      cout << endl << result << endl;
      return 0;
    }
    /**********************************************************
    spiTxRx
     Transmits one byte via the SPI device, and returns one byte
     as the result.
    
     Establishes a data structure, spi_ioc_transfer as defined
     by spidev.h and loads the various members to pass the data
     and configuration parameters to the SPI device via IOCTL
    
     Local variables txDat and rxDat are defined and passed by
     reference.
    ***********************************************************/
    
    int spiTxRx(unsigned char txDat)
    {
    
     unsigned char rxDat;
    
     struct spi_ioc_transfer spi;
    
     memset (&spi, 0, sizeof (spi));
    
     spi.tx_buf    = (unsigned long)&txDat;
     spi.rx_buf    = (unsigned long)&rxDat;
     spi.len      = 1;
    
     ioctl (fd, SPI_IOC_MESSAGE(1), &spi);
    
     return rxDat;
    }

    دیدگاه

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