CMPS03:

Read routine.

Projects Home

 

  The CMPS03 module has 16 registers, but we only need two of them (register 2 &3.) So, when reading from the CMPS03 (=Slave) and seen by the PIC's point of view (=Master), we have to:

1) Send a START, write the device address (0xc0) (last bit is 0 for write), wait for ACK.

2) Write the word address (sets the first register to read from, which is 0x02 here), wait for ACK.

3) Send a REPEATED START, write the device address again (0xc1) (last bit is 1 for read), wait for ACK.

4) Read the first byte, wait for ACK.

5) Read the last byte, no wait for ACK, send STOP.

 

We have to repeat this whole cycle each time we start reading the CMPS03 registers, otherwise they will be incremented automatically, and not set to zero.

 

 

 

 

Hi-Tech PICC:

void read_compass(unsigned char *buffer)

SEN = 1;                                   // 1) send start bit START    
while(SEN);                             // and wait for it to clear
ACKDT = 0;                            // acknowledge bit

SSPIF = 0;                                //  
SSPBUF = 0xc0;                     // set device address 0xc0 // write SLAVE ADDRESS
while(!SSPIF);                        // wait for interrupt
SSPIF = 0;                               // then clear it.

SSPBUF = 2;                          //
2) address = 2 (register =2), WORD ADDRESS - control 1
while(!SSPIF);                       // wait for interrupt
SSPIF = 0;                              // then clear it.

RSEN = 1;                               //
3) send repeated start bit REPEATED START
while(RSEN);                         // and wait for it to clear

SSPIF = 0;
SSPBUF = 0xc1;                   // set device address 0xc1 // write WORD ADDRESS - CONTROL 2
while(!SSPIF);                      // wait for interrupt
SSPIF = 0;                             // then clear it.

RCEN = 1;                                 //
4) start receiving READ & ACK buffer 0
while(!STAT_BF);                  // wait for data
buffer[0] = SSPBUF;               // and get it              // buffer 0
ACKEN = 1;                             // start acknowledge sequence
while(ACKEN);                       // wait for ack. sequence to end

RCEN = 1;                                         // 5) start receiving READ LAST & NO ACK buffer 1
while(!STAT_BF);                          // wait for data
buffer[1] = SSPBUF;                      // and get it     // buffer 1
ACKDT = 1;                                    // not acknowledge for last byte

PEN = 1;                                           // send stop bit STOP
while(PEN);