Hi
I’m trying to interface the MicroMag3 from sparkfun with the O.
I created a cable for the MISO/MOSI/SCK pins from the MM3 to the programmer interface on the O.
I created a 3v power regulator for the MM3. The source power comes from the O.
Now, the programmer port has power and ground assigned to pins. But, I did not bring power/ground from the MM3 via the cable I built. I am leaving those pins without connection. Based on looking at the O and trying to decipher the schematic, I am assuming that since I already have power for the MM3, I don’t need those pins connected.
So, I’ve been programming it, shutting it off, switching programmer cable for MM3 cable, then powering back on.
Please advise if I have screwed up the above somehow.
If not, then I’ll move to the next section.
The MM3 requires 3 additional pins for its usage, SS, RESET, READY. I have SS=D0, RESET=D1, and READY=B7.
That should be ok. I’m not assuming I need to have them all on the same port. If so, I am going to jumper cable between a motor PD port and the MM3 READY.
If I’m still doing the right thing, next section is programming.
Here’s my code. lcd will work - even after the spi_init. But, if I try any of the other SPI stuff, there is nothing happening. I’m assuming its frozen at this line but cant figure out a way to verify:
while(!(PIN_SPI & (1<<DD_RDY)));
/* begin */
#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>
#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define PIN_SPI PINB
#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
void init_spi()
{
// initialize SPI
// Set MOSI, SCK, RST output, rest input
DDR_SPI = (0<<DD_MISO) | (1<<DD_MOSI) | (1<<DD_SCK) | (1<<DD_RST);
// Enable SPI, Master, set clock rate fck/64
SPCR = (1<<SPE)|(1<<MSTR)| (1<<SPR1) | (1<<SPR0);
// initialize MM3
//SS output
DDR_MM3 = (1<<DD_SS);
//SS high
PORT_MM3 |= (1<<DD_SS);
//RST low
PORT_MM3 &= ~(1<<DD_RST);
}
int spi_comm(int data)
{
// Start transmission
SPDR = data;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
void spi_disable(void)
{
SPCR &= ~( 1 << SPI_SPE );
SPI_DDR |= ( 1 << SPI_MISO );
}
void spi_enable(void)
{
SPCR |= ( 1 << SPI_SPE );
SPI_DDR &= ~( 1 << SPI_MISO );
}
int main()
{
char s[10];
int delay;
int x = 0;
lcd_init();
init_spi();
spi_disable();
for(;;)
{
spi_enable();
// select mm3
PORT_MM3 &= ~(1<<DD_SS);
//Pulse RST
PORT_MM3 |= (1<<DD_RST);
_delay_ms(1);
PORT_MM3 &= ~(1<<DD_RST);
// get x axis
spi_comm(0x01);
while(!(PIN_SPI & (1<<DD_RDY)));
x = (spi_comm(0) << 8) | spi_comm(0);
// deselect mm3
PORT_MM3 |= (1<<DD_SS);
spi_disable();
lcd_gotoxy(0,0);
sprintf(s, "a:%d", x);
lcd_string(s);
//Delay between measurements
for(delay = 0; delay < 500; delay++)
_delay_ms(1);
}
}
/* end */
Thoughts? Help? Hopefully a really dumb problem that I can’t see because I’ve been staring at it so long.