Using LISY300 27922 gyroscope with AVR and SPI
billmania
Posts: 4
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?
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
AVR <-> gyroscope
5 VDC <-> pin 6
Ground <-> pins 1, 3 and 5
pin 7 MISO <-> pin 2
pin 8 SCK <-> pin 4
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:
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)
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);