No data receiving via TWI

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys,

currently i'm working with a xmegaA3BU xplained bord and a MAG3110 device.

I'm trying to get data from the MAG3110, but i don't get anyfrown

 

I have used TWI more then ones with other devices and have looked at the code from those projects. But i can't find anything.

void TWI_init(void){
	/* Zet Baud rate op 100KHZ */
	TWIC_MASTER_BAUD |= TWI_BAUD(F_CPU, Baud_rate);
	/* Zet Enable TWI master mode, Read interrupts mode en Medium interrupt lvl */
	TWIC_MASTER_CTRLA |= (TWI_MASTER_ENABLE_bm | TWI_MASTER_RIEN_bm | TWI_MASTER_WIEN_bm | TWI_MASTER_INTLVL_MED_gc);
	/* Dwingt Bus in idle mode */
	TWIC_MASTER_STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
	 /* Enables de interrupt lvl's high en medium */
	 PMIC_CTRL |= PMIC_HILVLEN_bm| PMIC_MEDLVLEN_bm;
	 SREG = (1<<7);//enable global ISR's
}

This is the function that i use to init the TWI.

void clock_init(void){
	/* De clock van de microcontroller wordt ingesteld op 2MHz */
	OSC.CTRL = OSC_RC2MEN_bm;
	/* Wachten totdat 2MHz klok enabled is */
	while(!(OSC.STATUS & OSC_RC2MEN_bm));
	CCP = CCP_IOREG_gc;
	CLK.CTRL = CLK_SCLKSEL_RC2M_gc;
	/* Enables the clock on TC1, SPI and TWI in the power reduction register */
	PR.PRPC = (TC1_clock_enable|SPI_clock_enable| TWI_clock_enable);
}

The clock will be set at 2MHz.

 

Also got a struct where all the address from the MAG3110 are initialised.

#define MAG3110_Adres 0x0C
#define MAG3110_XREG_MSB 0x01
#define MAG3110_XREG_LSB 0x02
#define MAG3110_YREG_MSB 0x03
#define MAG3110_YREG_LSB 0x04
#define MAG3110_ZREG_MSB 0x05
#define MAG3110_ZREG_LSB 0x06
#define MAG3110_WHO_AM_I 0x07
#define MAG3110_control_reg1 0x10
#define MAG3110_control_reg2 0x11

void MAG3110_init(void){
	MAG3110.Adres		= MAG3110_Adres;
	MAG3110.Adres_write	= (MAG3110_Adres<<1);
	MAG3110.Adres_read	= (MAG3110_Adres<<1)+1;
	MAG3110.X_value		= 0;
	MAG3110.Y_value		= 0;
	MAG3110.Z_value		= 0;
	MAG3110.Device_ID	= 0;
};	

In the main i first activate the MAG3110 by writing a 1 to bit 1 in the controll register of the MAG3110 using the ISR of TWI.

 

After that i was first trying to read the variable registers of the MAG3110, but after severals times i didn't get anything.

So i am trying to read de device ID from the MAG3110 register.

#include <asf.h>
#include <MAG3110.h>

void TWI_init(void);
void clock_init(void);
void usart_init(void);
#define Baud_rate 100000
#define TWI_MASTER_STOP_bm 0x03
#define ARBLOST (TWIC_MASTER_STATUS & 0x08)
#define Acknowlegdment 0x02
Bool Bus_free = true;
Bool activate_mag3110= false;
#define TWI_clock_enable (0x20<<0)
#define SPI_clock_enable (0x08<<0)
#define TC1_clock_enable (0x02<<0)
#define WIF (TWIC_MASTER_STATUS & 0x40)
#define RIF (TWIC_MASTER_STATUS & 0x80)

SR(TWIC_TWIM_vect){
	static uint8_t ISRcounter = 0, ISRcounter2=0, Nummer = 0;
 	if(ARBLOST){TWIC_MASTER_STATUS |= TWI_MASTER_ARBLOST_bm; TWIC_MASTER_STATUS = TWI_MASTER_BUSSTATE_IDLE_gc; ISRcounter=0; return;}
		 if (activate_mag3110 == false) {
			 switch(ISRcounter2){
				 case 0:
				 if(TWIC_MASTER_ADDR == MAG3110.Adres_write){
					TWIC_MASTER_DATA = MAG3110_control_reg1;
					ISRcounter2 = 1;
				 }
				 break;
				 case 1:
					TWIC_MASTER_DATA = 0b00000001;//enable MAG3110 tot active
					while(!(WIF));
					TWIC_MASTER_CTRLC |= TWI_MASTER_STOP_bm;
					activate_mag3110 = true;
					ISRcounter2 = 2;
					Bus_free =true;
					break;
			 }
		 }else{
 	switch(ISRcounter){
		case 0: 
 				if(TWIC_MASTER_ADDR == MAG3110.Adres_write){
 					TWIC_MASTER_DATA = MAG3110_WHO_AM_I;
 					ISRcounter=1;
				}
 			break;
 		case 1: 
 			TWIC_MASTER_ADDR = MAG3110.Adres_read;
 			ISRcounter = 2;
			break;
		case 2:
			MAG3110.Device_ID = TWIC_MASTER_DATA;
			TWIC_MASTER_CTRLC = TWI_MASTER_ACKACT_bm;
			TWIC_MASTER_CTRLC |= TWI_MASTER_STOP_bm;
			ISRcounter = 0; Bus_free = true;
			break;
 	      }
	 }
 };
 
 int main (void)
{
	// Insert system clock initialization code here (sysclk_init()).
	uint8_t i=0;
	char format_buffer[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	board_init();
	sysclk_init();
	clock_init();
	TWI_init();
	MAG3110_init();
	/*usart_init();*/
	gfx_mono_init();
	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN);
	gfx_mono_draw_string("Test tekst",0, 0, &sysfont);
	
	while(1){
		LED_Toggle(LED0);
		delay_ms(100);
		if(Bus_free == true && activate_mag3110 ==false && TWIC_MASTER_STATUS == 0x01){
			TWIC_MASTER_ADDR = MAG3110.Adres_write;
			Bus_free =false;
		}
		if(Bus_free ==true && activate_mag3110 == true ){
			TWIC_MASTER_ADDR = MAG3110.Adres_write;
			Bus_free =false;
		}
		sprintf(format_buffer, "%16u", MAG3110.X_value);
		/*sprintf(format_buffer[1], "%16u", MAG3110.Y_value);
		sprintf(format_buffer[2], "%16u", MAG3110.Z_value);*/
		if(i==0){
			gfx_mono_draw_string(format_buffer, 5, 8, &sysfont);
			delay_ms(1000);
			i=1;
		}else{
			gfx_mono_draw_string("ok    ", 5, 8, &sysfont);
			delay_ms(1000);
			i=0;
		}
	}
	return (0);
}

Hope anyone can help me with it :)

PS: also added the project as attachment.

[EDIT]:

this is the MAG3110 that i'm using:

https://www.sparkfun.com/products/12670

Attachment(s): 

This topic has a solution.
Last Edited: Wed. Apr 1, 2015 - 07:30 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DAMN i was stupid, i was trying to communicated with the wrong ADDRESS >.>

[EDIT]:

Also my write / read order was wrong. I was trying to read first then, but i need to change address to write first then, write in the DATA the address. After that change to read address and read the data from DATA register.

 

 

Greeting,

Rgamer145

Last Edited: Wed. Apr 1, 2015 - 07:55 AM