Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

i2c is not working for Tiva C TM4C123GXL and 24C04 (EEPROM)

Status
Not open for further replies.

hamid159

Full Member level 3
Joined
Aug 25, 2013
Messages
176
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Lahore
Activity points
2,488
Hi guyz,
I am trying to run i2c on Tiva C TM4C123GXL board. After Initiating the communication, Controller is always busy and does not come back in idle state. I don't know why buti am pretty sure, it is not sending the data to 24C04 because in "Master Bus Monitor" register i am seeing the SCL and SDA always low. I wasted my whole day to solve the problem but no avail.
Here is the code.

Code:
/*
 * main.c
 */

#include "registers_addresses.h"

void Init(void);
void GPIO_PORTF_Init(void);
int main(void) {
	int i;

	/*
	 * Initialization for LED
	*/
	GPIO_PORTF_Init();

	/*
	 * Initialization for PORTB and I2C
	*/
	Init();

	while(1){

		/*
		 * Slave Adress 0b10100000
		*/
		Write(((I2C0_BASE)|(I2CMSA)), (0xA0));

		/*
		 * Memory Address 0x5
		*/
		Write(((I2C0_BASE)|(I2CMDR)), (0x00000005)); // this is address

		/*
		 * Start the communication and run (Start bit and run)
		*/
		Write(((I2C0_BASE)|(I2CMCS)), (0x00000003)); // start and run

		/*
		 * Wait until the controller is not busy
		*/
		while(((Read(((I2C0_BASE)|(I2CMCS))))&0x1)); // run until busy flag is 0

		/*
		 * Check for error
		*/
		if(((Read(((I2C0_BASE)|(I2CMCS))))&0x2)){
			Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), (1<<1)); // if there is error, turn ON the LED
		}
		else{

			Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), ~(1<<1)); // turn OFF the LED

			/*
			 * Insert this data to 0x5 location
			*/
			Write(((I2C0_BASE)|(I2CMDR)), (0x000000FF)); // this is data

			/*
			 * run again and put stop bit at the end
			*/
			Write(((I2C0_BASE)|(I2CMCS)), (0x00000005)); // stop and run

			/*
			 * Wait until the controller is not busy
			*/
			while(((Read(((I2C0_BASE)|(I2CMCS))))&0x1)); // run until busy flag is 0

			/*
			 * Check for errors
			*/
			if(((Read(((I2C0_BASE)|(I2CMCS))))&0x2)){
				Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), (1<<1)); // if there is error, turn ON the LED
			}
			else {
				Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), ~(1<<1)); // turn OFF the LED
			}
		}


		/*
		 * Toggle LED
		*/
		Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), (1<<1));
		for(i=0;i<200000; i++);

		Write(((GPIO_PORTF_BASE)|(DATA)|((1<<1)<<2)), ~(1<<1));
		for(i=0;i<200000; i++);

	}
	return 0;
}

void Init(void){
	/*
	 * Enable Clock for I2C 0 module
	 */
	WritewithOR(((SYS_BASE)|(RCGCI2C)), (1<<0));

	/*
	 * Enable Clock for GPIO PORTB
	*/
	WritewithOR(((SYS_BASE)|(RCGCGPIO)), (1<<1));

	/*
	 * Enable Alternate Functionality for PB2(SCL) and PB3(SDA)
	*/
	WritewithOR(((GPIO_PORTB_BASE)|(AFSEL)), (1<<2)|(1<<3));

	/*
	 * Enable Open Drain for PB3 (SDA)
	*/
	WritewithOR(((GPIO_PORTB_BASE)|(ODR)), (1<<3));

	/*
	 * Select Alternate Functionality I2C0 for PB2 and PB3
	*/
	WritewithOR(((GPIO_PORTB_BASE)|(PCTL)), (0x00003300));

	/*
	 * I2C0 is master device
	*/
	Write(((I2C0_BASE)|(I2CMCR)), (0x00000010));

	/*
	 * Time Period register value
	*/
	Write(((I2C0_BASE)|(I2CMTPR)), (0x00000009));
}

void GPIO_PORTF_Init(void){
	WritewithOR(((SYS_BASE)|(RCGCGPIO)), (1<<5));
	WritewithOR(((GPIO_PORTF_BASE)|(DIR)), (1<<1));
	WritewithOR(((GPIO_PORTF_BASE)|(DEN)), (1<<1));
}

PS: i connected the pull up resistors to SCL and SDA.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Tiva is 3.3V device ? If yes, are there pullups on I2C lines ?
 

hamid159

Full Member level 3
Joined
Aug 25, 2013
Messages
176
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Lahore
Activity points
2,488
yes .. as I above mentioned. I connected the pull up resistors to SCL and SDA.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top