Interfacing AT91SAM7SE-EK and MPU6050 board using i2c .

Microchip in-system programming solution: SAM-BA

Moderators: nferre, fab

ankitkumarojha
Posts: 2
Joined: Thu Sep 04, 2014 8:56 pm

Interfacing AT91SAM7SE-EK and MPU6050 board using i2c .

Mon Nov 03, 2014 2:20 pm

Sir,
I am trying to interface AT91SAM7SE-EK and MPU6050 board using I2C protocol but I am unable to read any data from the slave ie., MPU6050(board). I think hardware connections are correct as I connected twd and twck pins of micro controller to SDA and SCK pins of MPU6050(board) and GND and power are connected properly.
                 Now the issue left is in the coding part. I did the following steps in  my code:
1.Enabled the clock for twi
2.Disabled the GPIO pins
3.Dedicated the pins to A select registers
4.Open drived the pins
5.Reset the swr
6.Enable the master mode and disabled the slave mode.
7.Set the clock wave form register (ckdiv=2 chdiv=255 cldiv=255) 
8.Set the Master mode register (slave address, mread ,1 byte internal address)
9.Set the internal address as 0x41
10.Enabled the Interrupts
11.Send the start signal
12.Using RHR and rxrdy receive the character.

I did all the above steps but still unable to receive anything from the MPU6050(board).
My code is written below, Please have a look and rectify the my code.
Thank you. 

Code: Select all

//to enable the peripheral clock for the twi
AT91C_BASE_PMC->PMC_PCER=(1 << AT91C_ID_TWI);
//configuring the i/o lines for twi
//to disable the pins for gpio function
AT91C_BASE_PIOA->PIO_PDR =  AT91C_PIO_PA3 |  AT91C_PIO_PA4;
//to select peripheral A by A-select-register
AT91C_BASE_PIOA->PIO_ASR= AT91C_PA3_TWD | AT91C_PA4_TWCK;
//to disable peripheral B
AT91C_BASE_PIOA->PIO_BSR=0;
//Open drive the pins
AT91C_BASE_PIOA->PIO_MDDR=~(((unsigned int) AT91C_PA3_TWD ) | ((unsigned int) AT91C_PA4_TWCK ));
AT91C_BASE_PIOA->PIO_MDER=(((unsigned int) AT91C_PA3_TWD ) | ((unsigned int) AT91C_PA4_TWCK ));
//to disable interrupts
AT91C_BASE_TWI->TWI_IER=0;
//to reset twi
AT91C_BASE_TWI->TWI_CR= AT91C_TWI_SWRST;
//to set master mode(msen=1,svdis=1)
AT91C_BASE_TWI->TWI_CR = AT91C_TWI_SVDIS;
AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN;
//to set ckdiv=2, cldiv=255, chdiv=255 (CLOCK COMPONENTS)
AT91C_BASE_TWI->TWI_CWGR = 0;
AT91C_BASE_TWI->TWI_CWGR=0x2FFFF;
//Address at Master Mode Register slave address(16),mread(12),no of bytes(0) 
AT91C_BASE_TWI->TWI_MMR = (slave_address<<16) | AT91C_TWI_MREAD |(AT91C_TWI_IADRSZ_1_BYTE);
 //Internal memory address 
AT91C_BASE_TWI->TWI_IADR=0x41;
//Enabling required interrupts
AT91C_BASE_TWI->TWI_IER=0x103;
//Sending start signal
AT91C_BASE_TWI->TWI_CR= AT91C_TWI_START;
//to check the nack status and continue
while(AT91C_BASE_TWI->TWI_SR & AT91C_TWI_NACK_MASTER)
{
 AT91C_BASE_TWI->TWI_CR= AT91C_TWI_STOP;//Stop the transmission
 AT91C_BASE_TWI->TWI_CR= AT91C_TWI_START;	//Again send the start signal
}
while(1)// Super loop
{
 AT91C_BASE_TWI->TWI_IDR= AT91C_TWI_RXRDY;
 AT91C_BASE_TWI->TWI_IER= AT91C_TWI_RXRDY;//Enabling the Receiver Ready
	if(!(AT91C_BASE_TWI->TWI_SR & AT91C_TWI_NACK_MASTER))//to check nack status 
	{  
		while(!(AT91C_BASE_TWI->TWI_SR & AT91C_TWI_RXRDY))//loop runs till rxrdy==1
	{
		continue;
	}
	c[0]=AT91C_BASE_TWI->TWI_RHR;//received character is stored in local variable.
	uart_puts(c);//printing the recieved character
	c[0]=((char)((int)c[0]+48));//calculating the ascii value
	uart_puts(c);//printing the ascii value
 }
	else
	{
   AT91C_BASE_TWI->TWI_CR= AT91C_TWI_STOP;
   uart_puts("NACK RECEIVED");		
	}
}
}

Return to “SAM-BA”

Who is online

Users browsing this forum: No registered users and 1 guest