Hello Pedro,
Some further observations and issues with the code so far.
The read of the compass status appears quite "tricky". The upper nybble of the send byte represents the command, and the lower nybble of the simultaneously returned byte represents the status.
A further complication is that, in the event that the reading is incomplete, the enable line must be raised and then lowered again for the next status command. However, if the reading has been completed, the enable line remains low while the return of the reading takes place. The enable line is then raised again after the value is returned.
Note that in the following code, I have changed the naming of the macros to EN_L and EN_H (in lieu of EN_N and EN_Y), which is more meaningful to me, especially since the active state of the enable line is low. For the handling of the individual bytes, I have created a union, that seems to make the eventual processing of the data a little less cumbersome.
Finally, a point that so far seems to have been overlooked is that the two 11-bit quantities are signed values. So when the 11-bit values are converted to 16-bits, they will need to be sign extended.
// Compass commands:
#define RESET 0x00
#define START 0x88
#define STATUS 0xC0
#define NEG_MASK 0xF800
#define SIGN_MASK 0x0400
typedef union {
long val32;
int w[2];
byte b[4];
} DWORD;
int Xdata, Ydata;
void HM55B_proc( void)
{
DWORD rdg;
byte i;
EN_L;
(void)getcspi( RESET);
EN_H;
EN_L;
(void)getcspi( START); // Commence conversion
EN_H;
EN_L;
// Wait until measurement is complete
while ((getcspi( STATUS) & 0x0C) == 0) {
EN_H;
EN_L;
}
rdg.b[0] = 0;
for (i = 1; i < 4; i++)
rdg.b[i] = getcspi( 0);
EN_H;
rdg.val32 >>= 2; // Right justify 22 data bits
Ydata = rdg.w[1] & 0x07FF; // 11-bit signed Y-value
if (Ydata & SIGN_MASK)
Ydata |= NEG_MASK; // Sign extend 16-bit value
rdg.val32 >>= 11; // Right justify X-value
Xdata = rdg.w[1]; // 11-bit signed X-value
if (Xdata & SIGN_MASK)
Xdata |= NEG_MASK; // Sign extend 16-bit value
}
As Kef has already suggested, the settings of CPHA and CPOL bits will need to match the device requirements.
Regards,
Mac