Moin Leute,

ich versuche auch Daten aus dem Sensor mit einem ATMega644 zu lesen (via SPI). Leider gibt mein Display für die 8 höchsten Bits der z-Beschleunigung eine 0 aus.


Ich hab das Programm gut kommentiert, sollte leicht verständlich sein.
Sieht jemand von Euch einen Fehler darin? Meines Erachtens habe ich alles beachtet.

Code:
#include <avr/io.h>
#include <util/delay.h>
#include "m50530.h"
#include <stdlib.h>


#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define PIN_SPI PINB

#define DD_BMA020_CS PB4
#define DD_MOSI PB5
#define DD_MISO PB6
#define DD_SCK PB7

#define P_BMA020_CS PB4
#define P_MOSI PB5
#define P_MISO PB6
#define P_SCK PB7

#define acc_z 7

#define read 7


void SPI_MasterInit(void)
{
	// Set MOSI and SCK output, all others input
	DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK);
	
	// BMA020 ChipSelect is output (low active)
	DDR_SPI |= (1<<DD_BMA020_CS);
	
	// Deselect BMA020
	PORT_SPI |= (1<<P_BMA020_CS);
	
	// Enable SPI, Master, set clock rate fck/16, SPI Mode 3
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPOL)|(1<<CPHA);
}

void SPI_MasterTransmit(char cData)
{
	/* Start transmission */
	SPDR = cData;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)))
	;
}

char getZ(void)
{	
	// Select BMA020
	PORT_SPI &=~ (1<<P_BMA020_CS);
	
	// Send command: Read z_acceleration
	SPI_MasterTransmit((1<<read)|(acc_z));
	
	// Send dummy byte
	SPI_MasterTransmit(0);
	
	// Deselect BMA020
	PORT_SPI |= (1<<P_BMA020_CS);
	
	// Return z acceleration
	return SPDR;
}


int main(void)
{
	uint16_t z=0;
	char string[20];
			
	
	// Wait 2 ms for the BMA020
	_delay_ms(2);

	// Initialize LCD and SPI
	LCDInit();
	SPI_MasterInit();
	

	while(1)
	{
		// Read z-axis via SPI
		z=getZ();	
		
		// Convert z To String
		itoa(z, string, 10);
		
		// Print z on LCD
		LCDWrite(string);
		
		// Set CoursorPosition to [0,0]
		LCDSetCursorPos(0,0);
	}
	return 0;
}

Vielen Dank schonmal und Grüße aus Hamburch