I need help!!! How to read from CMPS03 with HC12 thru i2c

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I need help!!! How to read from CMPS03 with HC12 thru i2c

4,367 Views
przemula
Contributor I
Hello
I'm trying to read from CMPS03 using my HC12A64.
I'ts a big problem for me and I can't solve it.
Here under is a code that i've written:

void initI2C(void){
IBCR_IBEN=1; //I-Bus Enabled
IBCR_MS_SL=1; //Master mode selected
IBCR_TX_RX=1; //Transmit mode selected
IBFD=0x14;
}

interrupt void rti_isr(void){
if (PORTA_BIT0==1) //it's only for LED
PORTA_BIT0=0; else
PORTA_BIT0=1;
IBDR=0xC0; //This is the address of cmps03
while (IBSR_TCF==0);
IBDR=0x01; //this is the register that I want to read
while (IBSR_TCF==0);
IBCR_RSTA=1; //Repeated start
IBDR=0xC1; //This address was written in CMPS03 datasheet
while (IBSR_TCF==0);
IBCR_TX_RX=0;
while (IBSR_TCF==0);
IBSR_RXAK=1;
polozenie=IBDR;
while (IBSR_TCF==0);
IBCR=0;

IT dosen't work. Can anybody help me

}
Labels (1)
0 Kudos
3 Replies

534 Views
JonHart
Contributor I

The Freescale documentation does not recommend using a polled approach for IIC.

Integrate the AN2138 example code into your project.  Be aware of the single byte rx bug (see my latest post).

0 Kudos

534 Views
przemula
Contributor I
I've no idea how to read from this compass. can somebody send me his code that works with no problem????
I need to read two byte registers (nr 2 and 3) address of cmps is 0xC0
0 Kudos

534 Views
przemula
Contributor I
OK I've made it works.



Here is an exaple of my code:

void initI2C(void){
IBFD=0x14;
}

void main(void) {
DDRA=0xFF;
PORTA=0xFF;

i=0;

initI2C();

EnableInterrupts;
for(;:smileywink: {
PORTA_BIT0=0; //Led off
IBCR_IBEN=1; //I-Bus Enabled
IBCR_MS_SL=1; //HC12 is Master
IBCR_TXAK=0; //transmit ack disable
while(IBSR_IBB==1){} //wait until i2c bus is free
PORTA_BIT1=0; //Led off
IBCR_TX_RX=1; //Transmit mode

IBDR=0xC0; //sending cmps03 address
while(IBSR_IBIF==0){} //wait until address is send
if (IBSR_RXAK!=1)
PORTA_BIT2=0; else //if ack received led off
IBCR_MS_SL=0; //else sending stop bit
IBSR_IBIF=1; //clear IBIF flag

IBDR=2; //sending number of register to be read
while(IBSR_IBIF==0){}
if (IBSR_RXAK!=1)
PORTA_BIT3=0; else
IBCR_MS_SL=0;
IBSR_IBIF=1;

IBDR=3; //sending number of register to be read
while(IBSR_IBIF==0){}
if (IBSR_RXAK!=1)
PORTA_BIT3=0; else
IBCR_MS_SL=0;
IBSR_IBIF=1;

IBCR_RSTA=1; //genereting of repeated start

IBDR=0xC1; //sending address of cmps03 with R/W on 1
while(IBSR_IBIF==0){}
if (IBSR_RXAK!=1)
PORTA_BIT4=0; else
IBCR_MS_SL=0;
IBSR_IBIF=1;

IBCR_TX_RX=0; //Recieve mode
cmps[i]=IBDR; //dummy readout
while(IBSR_IBIF==0){} //wait for data
cmps[i]=IBDR*256; //store data
IBSR_IBIF=1;
IBCR_TXAK=1; //transmit ack enables

while(IBSR_IBIF==0){}
cmps[i]=cmps[i]+IBDR; //store data

IBCR_MS_SL=0; //sending stop bit
IBCR_IBEN=0; //I-Bus disable

PORTA=0xFF; //all Led on
}
}
0 Kudos