Shop OBEX P1 Docs P2 Docs Learn Events
Using LISY300 27922 gyroscope with AVR and SPI — Parallax Forums

Using LISY300 27922 gyroscope with AVR and SPI

billmaniabillmania Posts: 4
edited 2011-01-27 18:22 in Accessories
I am attempting to use a Parallax LISY300 #27922 gyroscope with an Atmel AVR ATmega32. The gyro module requires SPI for communications and the ATmega32 supports SPI.
The first thing I've done is build a wiring harness to connect the gyro module to the ATmega32. The 27922 gyro datasheet isn't the most intuitively obvious document I've ever seen so I first need to confirm that I'm correctly interpreting the datasheet. My wiring diagram is as follows. If anyone can confirm or deny that I have it correct, that will get me past the first hurdle.

I used the standard procedure to identify the pins on the gyro. I started with the white dot on the gyro component in the upper left corner and called that "pin 1". I then moved around the component in a counter-clockwise direction, which puts pin 3 in the lower left corner and pin 4 in the lower right corner. Pin 6 is in the upper right corner.


AVR gyroscope

5 VDC pin 6

Ground pins 1, 3 and 5

pin 7 MISO pin 2

pin 8 SCK pin 4

I should also clarify that the ATmega32 is the only SPI master and the gyro module will always be in SPI slave mode.

Have I got it right so far?

Comments

  • billmaniabillmania Posts: 4
    edited 2011-01-18 09:49
    Since I now see that the forum software knows how to format my text better than I do, let me try to clarify the wiring diagram:

    AVR <-> gyroscope

    5 VDC <-> pin 6

    Ground <-> pins 1, 3 and 5

    pin 7 MISO <-> pin 2

    pin 8 SCK <-> pin 4
  • kwinnkwinn Posts: 8,697
    edited 2011-01-19 08:25
    The wiring looks ok. Did you download the documentation for the gyro from Parallax? It looked pretty comprehensive to me.
  • billmaniabillmania Posts: 4
    edited 2011-01-20 12:49
    I'm working from the datasheet at http://www.parallax.com/Portals/0/Downloads/docs/prod/sens/27922-LISY300GyroscopeModuleV1.0.pdf.

    My uncertainty with the contents of that datasheet are:

    1. Can I assume that all of Pin 2, P0 and DataOut are equivalent to MasterInSlaveOut (MISO)?

    2. Can I permanently connect Pin 5, P2, ChipSelect to signal ground or must it be toggled at least once from +5 VDC to 0 VDC?

    3. In the section labeled "Communication Protocol" I don't understand the following:
    clocking 16 bits via the SPI bus. Communication is terminated by bringing /CS high again. The data is sent MSB first
    and is preceded by 3 leading zero bits and trailed by 2 zero bits.

    Does "clocking 16 bits" mean reading two bytes or does it mean wait for 16 clock pulses to occur or something else entirely?
    Since the device has a ten bit ADC, should I expect the "real" data from the gyro to be ten bits? Where will the "3 leading zero bits" and the trailing "2 zero bits" be in the MSB and LSB and in relation to the "real" data bits? (three plus ten plus two is only 15 instead of 16)
  • billmaniabillmania Posts: 4
    edited 2011-01-27 18:22
    The wiring diagram above wasn't quite right. I learned that the ADC on the LISY300 requires the Slave Select signal too. The conversion isn't started until the SS pin changes from a high value to a low value. So, the correct wiring diagram is:

    AVR <-> gyroscope

    5 VDC <-> pin 6

    Ground <-> pins 1 and 3

    pin 5 SS <-> pin 5

    pin 7 MISO <-> pin 2

    pin 8 SCK <-> pin 4

    Then, the code for the AVR ATmega32 looks like:

    /* to initialize the gyro */
    /*
    * setup MISO
    * and enable SPI
    */
    DDRB = _BV(DDB4) | _BV(DDB5) | _BV(DDB7);
    SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1);

    /* disable slave */
    PORTB |= _BV(PORTB4);

    /*
    * initialize the gyro by sending 16 bits
    */
    PORTB &= ~(_BV(PORTB4));
    SPDR = 0u;
    SPDR = 0u;
    PORTB |= _BV(PORTB4);

    /* and then read 16 bits from the gyro */
    PORTB &= ~(_BV(PORTB4));
    SPDR = (unsigned char) 0;
    while (! (SPSR & _BV(SPIF)))
    ;
    MSB = SPDR & 0x7F;
    SPDR = (unsigned char) 0;
    while (! (SPSR & _BV(SPIF)))
    ;
    LSB = (unsigned char) SPDR;
    PORTB |= _BV(PORTB4);
Sign In or Register to comment.