hamid159
Full Member level 3
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.
PS: i connected the pull up resistors to SCL and SDA.
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.