SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By folrobot
#29134
Hi All,

I'm having trouble getting the MicroMag3 to work. At this point, I can't get it to set the DRDY line high.

I have the SS line grounded. I pulse the RESET line high, then send the command byte (0x01, for an x-axis reading), but the MicroMag3 doesn't set the DRDY line high to indicate that it's performed a reading.

If anyone can see what I'm doing wrong I'd greatly appreciate a correction. Thanks!

I'm using an Atmega324p. Here's a code snippet:

void SPI_MasterInit(void)
{
DDR_SPI |= (1<<DD_MOSI) | (1<<DD_SCK);
SPCR0 |= (1<<SPE0) | (1<<MSTR0) | (1<<SPR01);
}

uint8_t spi_transfer(uint8_t data)
{
SPDR0 = data; // start the transmission
PD5Value = PIND & _BV(PD5); // read the DRDY pin
while (PD5Value == 0) // wait till data is ready
{
PD5Value = PIND & _BV(PD5); // read the DRDY pin
printf(" . ");
}
return SPDR0; // return the received byte
}

uint8_t MM3;

int counter;

int main(void)
{
init_usart(); // Initialize usart
stdout = &mystdout; // set the output stream
SPI_MasterInit(); // Initialize micromag3
DDRD |= (1<<6); // set RESET output
DDRD &= ~(1<<5); // set DRDY input
PORTD &= ~(1<<5); // DRDY pullup disabled
PORTD &= ~(1<<6); // RESET low
printf(" starting ");
// reset micromag3
PORTD |= (1<<6); // RESET high
counter=0;
while (counter<=20) // count to 20 while reset is high
counter++;
PORTD &= ~(1<<6); // RESET low

// send command byte
MM3 = spi_transfer(0x01);
printf(" sending command byte ");

// output result over rs232
printf(" result = %i ",MM3);

while (1)
{
}
}

[edit]corrected code[/edit]
Last edited by folrobot on Sat Apr 28, 2007 3:11 pm, edited 1 time in total.
By jasonharper
#29138
folrobot wrote: PORTD |= (0<<6); // RESET low
That doesn't do what you think it does. In fact, it does nothing at all - ORing any value with 0 does not change that value.

Try:
Code: Select all
PORTD &= ~(1<<6);
to clear bit 6, for example. (Note: that's a tilde in front of the parentheses, not a minus sign.)
By folrobot
#29140
Thanks Jason

I've now corrected all instances of that syntax error. The RESET pulse is working as intended now (verified by wiring an LED to the RESET line).

However, the MicroMag3 still isn't setting the DRDY line high.
Perhaps there's another mistake in my code. :s
By jasonharper
#29142
Your spi_transfer routine contains an infinite loop - if PD5Value is zero at first, there's no way for it to ever change.

It's possible that you will need to actively control the SS line - for some SPI devices, they only act on a shifted-in command when their chip select line is raised. The MicroMag3 data sheet should hopefully clarify this.
By folrobot
#29145
Unfortunately, fixing that error doesn't solve the problem.

(spam filter keeps putting my posts on hold when I try to explain)
By Lajon
#29189
A common problem with AVR SPI is having the AVR SS pin floating or grounded. From the data sheet:
"If SS is configured as an input, it must be held high to ensure Master SPI operation."
An easy fix is to have it as an output.

Also, if you do get that DRDY to go high you should note that you must clock out the data. This means transfering dummy bytes (in this case two for the 16-bit result) and saving the SPDR result after each transfer is complete.

/Lars
By folrobot
#29210
Oops. I accidentally omitted polling the DRDY pin when I slimmed down the code to post here.

Anyway. Thanks again Jason, but unfortunately it's still not working.

The MicroMag3 datasheet doesn't clarify much, other than to say that the SS line must be low while you're using the MicroMag3.

The datasheet prescribes the directions of use as,

1. set SS line to low
2. pulse RESET line
3. send command byte through the MOSI line
4. micromag does its thing, takes a measurement
5. once the micromag has finished, DRDY goes high and data is shifted out the MISO line
By folrobot
#29341
Lajon wrote:A common problem with AVR SPI is having the AVR SS pin floating or grounded. From the data sheet:
"If SS is configured as an input, it must be held high to ensure Master SPI operation."
An easy fix is to have it as an output.
That was it. Thanks Lajon.
By geckosenator
#29484
so, now that you have it reading data.. have you managed to calibrate it?
By gussy
#29488
Does anyone have any bascom code for the MircoMag3? Would save me re-inventing the wheel ;)
By folrobot
#29503
geckosenator wrote:so, now that you have it reading data.. have you managed to calibrate it?
Currently I don't have the MicroMag3 in a high current environment, so there was no need to calibrate it in order to make sufficiently accurate measurements. I just go straight to calculating a heading.
By black_hawk97
#31653
Hi guys,

Could anyone post a code example for the micromag3? I've been trying for days to get it to work and all i can get out of it are some strange values like from -50 to 50 and always jumping in 2 increments when i rotate the micromag3. I mean, 0,2,4,6,8,10 etc.

I'm reading the sensor like this:


spi_transfer (0x01); // send command to read X axis

wait_drdy();

msb = spi_transfer (0x00); //read MSB
lsb = spi_transfer (0x00); //read LSB

x_axis = ((msb << 8 ) | lsb);

BTW, i have it connected to an ATMEGA88 running at 5V.

Any help is greatly appreciated...

Thanks,
James
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]