Not sure why, but the UART has started working again. Im not sure what i have changed to make it work, but for now it does.
These are the i2c functions i have. It all compiles but i get nothing at all on pins RA14 and RA15 when looking at it on Logic Analyser. So i think i still need to map the pins??
Code:
// I2C_init() initialises I2C1 at at 100Khz
void I2C_Init(void)
{
I2C1CON = 0; // Turn off I2C1 module
I2C1CONbits.DISSLW = 1; // Disable slew rate for 100kHz
I2C1BRG = 390; // Set baud rate to 100KHz
I2C1CONbits.ON = 1; // Turn on I2C1 module
}
// I2C_wait_for_idle() waits until the I2C peripheral is no longer doing anything
void I2C_Wait_For_Idle(void)
{
while(I2C1CON & 0x1F); // Acknowledge sequence not in progress
// Receive sequence not in progress
// Stop condition not in progress
// Repeated Start condition not in progress
// Start condition not in progress
while(I2C1STATbits.TRSTAT); // Bit = 0 ? Master transmit is not in progress
}
// I2C_start() sends a start condition
void I2C_Start()
{
I2C_Wait_For_Idle();
I2C1CONbits.SEN = 1;
while (I2C1CONbits.SEN == 1);
}
// I2C_stop() sends a stop condition
void I2C_Stop()
{
I2C_Wait_For_Idle();
I2C1CONbits.PEN = 1;
}
// I2C_restart() sends a repeated start/restart condition
void I2C_Restart()
{
I2C_Wait_For_Idle();
I2C1CONbits.RSEN = 1;
while (I2C1CONbits.RSEN == 1);
}
// I2C_ack() sends an ACK condition
void I2C_Ack(void)
{
I2C_Wait_For_Idle();
I2C1CONbits.ACKDT = 0; // Set hardware to send ACK bit
I2C1CONbits.ACKEN = 1; // Send ACK bit, will be automatically cleared by hardware when sent
while(I2C1CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning ACK bit has been sent
}
// I2C_nack() sends a NACK condition
void I2C_Nack(void) // Acknowledge Data bit
{
I2C_Wait_For_Idle();
I2C1CONbits.ACKDT = 1; // Set hardware to send NACK bit
I2C1CONbits.ACKEN = 1; // Send NACK bit, will be automatically cleared by hardware when sent
while(I2C1CONbits.ACKEN); // Wait until ACKEN bit is cleared, meaning NACK bit has been sent
}
// address is I2C slave address, set wait_ack to 1 to wait for ACK bit or anything else to skip ACK checking
void I2C_Write(unsigned char address, char wait_ack)
{
I2C1TRN = address | 0; // Send slave address with Read/Write bit cleared
while (I2C1STATbits.TBF == 1); // Wait until transmit buffer is empty
I2C_Wait_For_Idle(); // Wait until I2C bus is idle
if (wait_ack) while (I2C1STATbits.ACKSTAT == 1); // Wait until ACK is received
}
// value is the value of the data we want to send, set ack_nack to 0 to send an ACK or anything else to send a NACK
void I2C_Read(unsigned char *value, char ack_nack)
{
I2C1CONbits.RCEN = 1; // Receive enable
while (I2C1CONbits.RCEN); // Wait until RCEN is cleared (automatic)
while (!I2C1STATbits.RBF); // Wait until Receive Buffer is Full (RBF flag)
*value = I2C1RCV; // Retrieve value from I2C1RCV
if (!ack_nack) // Do we need to send an ACK or a NACK?
I2C_Ack(); // Send ACK
else
I2C_Nack(); // Send NACK
When using the above functions for this...
Code:
void I2C_Init(void);
Delay_mS(1000);
I2C_Start(); /* Send start condition */
I2C_Write(0x70 << 1, 1); /* Send address, read/write bit not set (AD + R) */
I2C_Write(1 << 7,1); //Send byte to select bus
I2C_Stop(); /* Send stop condition */
I2C_Start(); /* Send start condition */
I2C_Write(0x57 << 1, 1); /* Send address, read/write bit not set (AD + R) */
I2C_Stop(); /* Send stop condition */
It doesnt hang, it just seems to skip right over it.