So im trying to use i2c to get some data from a mpu6050, but whit the code that im using for some reason the clock does not work. on the osciloscope the scl line seems to be 1 all the time, and from diferent tests it seems that the pic stops waiting for clock signals.
ty in advance for any help.
Rich (BB code):
#include <libpic30.h>
#include <timer.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <p30F4011.h>
#include <i2c.h>
#include <uart.h>
#define FCY 29491
//Configuration bits
_FOSC(CSW_FSCM_OFF & XT_PLL16); //oscilator
//_FWDT(WDT_ON & WDTPSA_512 & WDTPSB_16); //watchdog timer (interval of ~ 16s)
_FWDT(WDT_OFF);
char RXbuffer[200]; //buffer used to store characters from serial port
int str_pos = 0; //position in the RXbuffer
int main() {
unsigned int config2, config1;
unsigned char *wrptr;
unsigned char tx_data[] = {'M','I','C','R','O','C','H','I','P','\0'};
unsigned int UMODEvalue, U2STAvalue; //auxiliary UART config variables
//Configures every PORTB pin as digital I/Os (wont be using analog inputs for now)
ADPCFG = 0xFFFF;
/* Serial port config */
UMODEvalue = UART_EN & UART_IDLE_CON & UART_NO_PAR_8BIT; //activates the uart in continuos mode (no sleep) and 8bit no parity mode
U2STAvalue = UART_INT_TX & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_RX_TX; //activates interrupt of pin Tx + enables Tx + enable Rx interrupt for every char
OpenUART2 (UMODEvalue, U2STAvalue, 15); //configures and activates UART2 at 115000 bps
U2STAbits.URXISEL = 1;
_U2RXIE = 1; //0-Interruption off, 1-Interruption on
U2MODEbits.LPBACK = 0; //disables hardware loopback on UART2. Enable only for tests
__C30_UART = 2; //define UART2 as predefined for use with stdio library, printf etc
TRISFbits.TRISF2=1;
TRISFbits.TRISF3=1;
wrptr = tx_data;
/* Baud rate is set for 100 kHz */
config2 = 0xB5;
/* Configure I2C for 7 bit address mode */
config1 = (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD &
I2C_IPMI_DIS & I2C_7BIT_ADD &
I2C_SLW_DIS & I2C_SM_DIS &
I2C_GCALL_DIS & I2C_STR_DIS &
I2C_NACK & I2C_ACK_DIS & I2C_RCV_DIS &
I2C_STOP_DIS & I2C_RESTART_DIS &
I2C_START_DIS);
printf("\n\rconfig1=%x config2=%x\n",config1,config2);
OpenI2C(config1,config2);
//ConfigIntI2C(MI2C_INT_OFF & MI2C_INT_PRI_1 & SI2C_INT_ON & SI2C_INT_PRI_1);
while(1){
IdleI2C();
StartI2C();
// Wait till Start sequence is completed
while(I2CCONbits.SEN);
// Clear interrupt flag
IFS0bits.MI2CIF = 0;
// Write Slave address and set master for transmission
MasterWriteI2C(0xE);
// Wait till address is transmitted
while(I2CSTATbits.TBF); // 8 clock cycles
while(!IFS0bits.MI2CIF); // Wait for 9th clock cycle
IFS0bits.MI2CIF = 0; // Clear interrupt flag
while(I2CSTATbits.ACKSTAT);
// Transmit string of data
MasterputsI2C(wrptr);
StopI2C();
// Wait till stop sequence is completed
while(I2CCONbits.PEN);
CloseI2C();
}
return 0;
}