SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By ozoney
#63228
this works!
Code: Select all


#include "device.h"
#include "lcd.h"

#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "spi.h"

#define DDR_SPI		DDRB
#define PORT_SPI	PORTB
#define PIN_SPI		PINB
#define O_SS		DDB2
#define DD_MOSI		DDB3
#define DD_MISO		DDB4
#define DD_SCK		DDB5
#define DD_RDY		DDB7

#define DDR_MM3		DDRD
#define PORT_MM3	PORTD
#define PIN_MM3		PIND
#define DD_SS		DDD0
#define DD_RST		DDD1

#define MM3_X_AXIS	0x01
#define MM3_Y_AXIS	0x02
#define MM3_Z_AXIS	0x03

#define MM3_PS32	0x00
#define MM3_PS64	0x10
#define MM3_PS128	0x20
#define MM3_PS256	0x30
#define MM3_PS512	0x40
#define MM3_PS1024	0x50
#define MM3_PS2048	0x60
#define MM3_PS4096	0x70



int main()
{
   char s[10];
   int delay;

   uint16_t x = 0;   
   uint16_t y = 0;

   int i = 0;

   int j = 0;
   
   int i0 = 0;
   int i1 = 0;
   
   lcd_init();
   
   spi_init();

   
   // mm3 init
   
   DDR_SPI |= (1 << O_SS); // output
   
   //SS high
   PORT_MM3 |= (1<<DD_SS);
   DDR_MM3 |= (1 << DD_SS); // output
   
   // Set SS, MOSI, SCK, RST output, rest input
   DDR_SPI |=  (1<<DD_MOSI) | (1<<DD_SCK);
   DDR_MM3 |= (1<<DD_RST);
   
   // Enable SPI, Master
   SPCR = (1<<SPE)|(1<<MSTR)| (1<<SPR1) | (1<<SPR0);   
   
   spi_disable();

   
   

   for(;;)
   {
	  SPCR &=~(1<<CPOL);	
	  SPCR &=~(1<<CPHA);
	
	  spi_enable();
	   
      PORT_MM3 &= ~(1<<DD_SS);

      //Pulse RST
	  PORT_MM3 &= ~(1<<DD_RST);
	  _delay_ms(1);
      PORT_MM3 |= (1<<DD_RST);
      _delay_ms(1);
      PORT_MM3 &= ~(1<<DD_RST);
      
	  spi_transfer(MM3_PS32 | MM3_X_AXIS);
  	  
	  while(!(PIN_SPI & (1<<DD_RDY)));

	  i0 = spi_transfer(0x00);
	  i1 = spi_transfer(0x00);
	  
      x = (i0 << 8) | i1;    
	  
	  PORT_MM3 |= (1<<DD_SS);
	   
	  spi_disable();

	   
	  lcd_gotoxy(0,0);
	  sprintf(s, "%d %d", i0, i1);
	  lcd_string(s);
	  
	  lcd_gotoxy(0,1);
	  sprintf(s, "%d %d", i++, x);
	  lcd_string(s);
	  
      //Delay between measurements
      for(delay = 0; delay < 500; delay++)
         _delay_ms(1);
   }
}