Thank you for the info Mark,
I have attached my code so you can see what I am referring too as well as for others who may look here for a full answer. The delay I am referring to is on line 218 and as far as i can tell that was the only real change I made from what I had and what I got from someone in Freescale.
Kas
Cant seem to attach a file so I'll just add it here :smileysad:
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#include "derivative.h" /* include peripheral declarations */
#define MWSR 0x00 /* Master write */
#define MRSW 0x01 /* Master read */
#define i2c_Start() I2C0_C1 |= 0x10;\
I2C0_C1 |= I2C_C1_MST_MASK
#define i2c_write_byte(data) I2C0_D = data
#define i2c_Wait() while((I2C0_S & I2C_S_IICIF_MASK)==0) {} \
I2C0_S |= I2C_S_IICIF_MASK;
#define i2c_Stop() I2C0_C1 &= ~I2C_C1_MST_MASK;\
I2C0_C1 &= ~I2C_C1_TX_MASK
unsigned char MasterTransmission;
/*******************************************************************/
/*!
* Pause Routine
*/
void Pause(void){
int n;
for(n=1;n<50;n++) {
asm("nop");
}
}
/***********************************************************************************************
*
* @brief Uart_SendChar - Send a single byte on Uart1
* @param byte to send
* @return none
*
************************************************************************************************/
void Uart_SendChar(uint8_t send)
{
while((UART2_S1&UART_S1_TDRE_MASK)==0);
(void)UART2_S1;
UART2_D=send;
}
unsigned char readRegister(unsigned char slaveID, unsigned char registerAddress)
{
unsigned char result;
unsigned int j;
MasterTransmission = MRSW;
slaveID = slaveID << 1;
slaveID |= MasterTransmission;
i2c_Start();
i2c_write_byte(slaveID);
i2c_Wait();
I2C0_D = registerAddress;
i2c_Wait();
/* Do a repeated start */
I2C0_C1 |= I2C_C1_RSTA_MASK;
/* Send Slave Address */
I2C0_D = (slaveID << 1) | 0x01; //read address
i2c_Wait();
/* Put in Rx Mode */
I2C0_C1 &= (~I2C_C1_TX_MASK);
/* Turn off ACK */
I2C0_C1 |= I2C_C1_TXAK_MASK;
/* Dummy read */
result = I2C0_D ;
for (j=0; j<5000; j++){};
i2c_Wait();
/* Send stop */
i2c_Stop();
result = I2C0_D ;
Pause();
return result;
}
void writeRegister(unsigned char slaveID, unsigned char registerAddress, unsigned char data)
{
MasterTransmission = MWSR;
slaveID = slaveID << 1;
slaveID |= MasterTransmission;
i2c_Start();
i2c_write_byte(slaveID);
i2c_Wait();
I2C0_D = registerAddress;
i2c_Wait();
I2C0_D = data;
i2c_Wait();
i2c_Stop();
}
//void clkInit(){
// //Clock setup
// SIM_BUSDIV = 0; //Divide bus clock by 1
// ICS_C1 |= ICS_C1_IRCLKEN_MASK; //Use internal reference clock
// ICS_C3 = 0x50; //Multiply the internal reference clock by 64, 16 = 39.0625 KHz
// //Wait for clock to lock (running at 40 MHz (1024 * 39.0625Khz)
// while(!(ICS_S & ICS_S_LOCK_MASK ));
// ICS_C2 |= ICS_C2_BDIV(1); //Divide the bus clock by 2
// ICS_S |= ICS_S_LOCK_MASK; //Clear Loss of lock sticky bit
//}
void Clk_Init()
{
ICS_C1|=ICS_C1_IRCLKEN_MASK; /* Enable the internal reference clock*/
ICS_C3= 0x50; /* Reference clock frequency = 39.0625 KHz*/
while(!(ICS_S & ICS_S_LOCK_MASK)); /* Wait for PLL lock, now running at 40 MHz (1024 * 39.0625Khz) */
ICS_C2|=ICS_C2_BDIV(1) ; /*BDIV=2, Bus clock = 20 MHz*/
ICS_S |= ICS_S_LOCK_MASK ; /* Clear Loss of lock sticky bit */
}
//void uartInit(){
// //UART setup
// SIM_SCGC |= SIM_SCGC_UART2_MASK;
// UART2_BDH = 0x00;
// UART2_BDL = 128;
// UART2_C1 = 0x00;
// UART2_C2 |= UART_C2_TE_MASK | UART_C2_RE_MASK;// | UART_C2_RIE_MASK | UART_C2_TIE_MASK;
//}
void UART_Init()
{
SIM_SCGC |= SIM_SCGC_UART2_MASK;
UART2_BDL= 128;
UART2_C1 = 0;
UART2_C2 |= UART_C2_TE_MASK;
UART2_C2 |= UART_C2_RE_MASK;
UART2_C2 |= UART_C2_RIE_MASK;
}
//void i2cInit(){
// //I2C setup
// SIM_SCGC |= SIM_SCGC_I2C_MASK;
// SIM_PINSEL |= SIM_PINSEL_I2C0PS_MASK;
// I2C0_F = 0x00;
// I2C0_C1 = I2C_C1_IICEN_MASK | I2C_C1_MST_MASK | I2C_C1_IICIE_MASK;
//}
int main(void)
{
long int delay;
unsigned char i;
unsigned char result;
char string[] = "My name is Bob C. Marley \r\n\r\n\0";
Clk_Init();
UART_Init();
SIM_SCGC |= SIM_SCGC_I2C_MASK;
SIM_PINSEL |= SIM_PINSEL_I2C0PS_MASK;
I2C0_F = 0x11;
I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK | I2C_C1_IICEN_MASK;
while(1){
/**/ //Start
I2C0_C1 |= I2C_C1_MST_MASK | I2C_C1_TX_MASK;
I2C0_C1 &= ~I2C_C1_TXAK_MASK;
/**/ //Write device to read from
I2C0_D = 0x96;
while ((I2C0_S & I2C_S_IICIF_MASK) == 0){
//Wait for data to transmit
}
I2C0_S |= I2C_S_IICIF_MASK;
/**/ //Write register to read from
I2C0_D = 0x00;
while ((I2C0_S & I2C_S_IICIF_MASK) == 0){
//Wait for data to transmit
}
I2C0_S |= I2C_S_IICIF_MASK;
/**/ //Restart
I2C0_C1 |= I2C_C1_RSTA_MASK;
/********************************************************Read*************/
/**/ //Read from device
I2C0_D = 0x97;
while ((I2C0_S & I2C_S_IICIF_MASK) == 0){
//Wait for data to transmit
}
I2C0_S |= I2C_S_IICIF_MASK;
/**/ //RX mode
I2C0_C1 &= ~I2C_C1_TX_MASK;
/**/ //Turn off its ACK
result = I2C0_D;
while ((I2C0_S & I2C_S_IICIF_MASK) == 0){
//Wait for data to transmit
}
I2C0_C1 |= I2C_C1_TXAK_MASK;
result = I2C0_D;
while ((I2C0_S & I2C_S_IICIF_MASK) == 0){
//Wait for data to transmit
}
for(i = 0; i < 40; i++){
asm("nop");
}
I2C0_S |= I2C_S_IICIF_MASK;
I2C0_C1 &= ~I2C_C1_MST_MASK;
result = I2C0_D;
// while(1) {
// for (i = 0; string[i] != '\0'; i++){
// Uart_SendChar(string[i]);
// }
//
//
for(delay = 0; delay < 99999; delay++);
// }
}
// Uart_SetCallback(Uart_Interrupt); /* Set the callback function that the UART driver will call when receiving a char */
// Enable_Interrupt(INT_UART2); /* Enable UART2 interrupt */
return 0;
}