PDA

View Full Version : Help with MicroMag3 SPI interface to PIC



kaan
- 31st May 2006, 15:49
Hello,
I am trying to interface PNI MicroMag3 Magnetometer sensor board to PIC16F877 @4Mhz. I am using SPI with SHIFTIN/SHIFTOUT commands. But I get garbage from input.
I ve tried to slow down communication with DEFINE SHIFT_PAUSEUS, but it did not fix.

Is there anyone, have used MicroMag3 with a PIC.

Please help
Kaan

Ron Marcus
- 31st May 2006, 18:40
One thing I've been hit with, is not setting the mode numbers correctly. It's easier on shiftout with fewer choices. What is the SPI mode, and polarity? Many SPI devices shift their data out while info is being shifted in. With the PicBasic routine, you will lose this data. Hardware SPI is more difficult to set up initially, but is bi-directional, and easy to work with once set up. Any data sheets for the device?

Ron

Zenon
- 31st May 2006, 19:46
There is a sample code file for the BS2 on PNI website

http://www.pnicorp.com/resources?nodeId=c40c

You can use the software shiftin/out provided by PBP, no need for hardware SPI.

Zenon

kaan
- 31st May 2006, 23:20
Thank you zenon, I have already used PNI code for BS2, but slighty modifed for PBP. Here is my code


INCLUDE "modedefs.bas" ' Include serial modes

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE SHIFT_PAUSEUS 100


datain VAR PORTB.0 'SPI Data in
dataout VAR PORTB.1 'SPI Data out
clock VAR PORTB.2 'SPI Clock
devsel VAR PORTB.3 'Device Select
ready VAR PORTB.4 'Data Ready
reset VAR PORTB.5 'Reset

cmd VAR BYTE
result VAR WORD

TRISB.0 = 1
TRISB.1 = 0
TRISB.2 = 0
TRISB.3 = 0
TRISB.4 = 1
TRISB.5 = 0
reset = 0

HSerout ["Magnetometer Test",10,13]
TRISB.7 = 0
PORTB.7 = 1
Pause 200
PORTB.7 = 0
Pause 200
PORTB.7 = 1
devsel = 1
devsel = 0 ' select device
basla:
Low reset
PulsOut reset,5 ' device reset
ShiftOut dataout,clock,MSBFIRST,[%01000001] ' command for X axsis
beklex: IF ready = 0 Then beklex
ShiftIn datain,clock,MSBPRE,[result\16]
Pause 1
HSerout ["x= ",SDEC result," "] '

Low reset
PulsOut reset,5 ' device reset
ShiftOut dataout,clock,MSBFIRST,[%01000010] ' command for Y axsis
bekley: IF ready = 0 Then bekley
ShiftIn datain,clock,MSBPRE,[result\16]
Pause 1
HSerout ["y= ",SDEC result," "]

Low reset
PulsOut reset,5 ' device reset
ShiftOut dataout,clock,MSBFIRST,[%01000011] ' command for Z axsis
beklez: IF ready = 0 Then beklez
ShiftIn datain,clock,MSBPRE,[result\16]
Pause 1
HSerout ["z= ",SDEC result,10,13]


GoTo basla


Here is the data sheet for device
https://www.pnicorp.com/downloadResource/cMM3s/datasheets/110/MicroMag3+Data+Sheet.PDF