Message Boards
Threads [ Previous | Next ]
need help with i2c
need help with i2c
9/13/10 5:22 AM
i am working with PIC32MX795F512L, and need to send data via i2c to dspic30F4013

i've tried a simple code but it's not working.

  1
  2#include "HardwareProfile.h"
  3#include <plib.h>
  4
  5#define SYSCLK    (72000000)
  6#define PBCLK  (SYSCLK/2)
  7#pragma config FPBDIV = DIV_2                //Sets PBCLK to SYSCLK
  8#define Fsck    100000
  9#define BRG_VAL     ((PBCLK/2/Fsck)-2)  //0xB2
 10 #define Nop() asm( "nop" )                 //No-operation; asm stands for assembly, using an assembly command in C.  Cool!
 11
 12//#define INPUT_A9       PORTAbits.RA9
 13//#define INPUT_A10       PORTAbits.RA10
 14
 15int data1 = 0x1;
 16int data2 = 0x0;
 17unsigned int slaveaddress = 0x4;
 18
 19//function declaration for sending data and selecting slave address
 20void SendData(int,unsigned int);
 21void Delayms( unsigned t);
 22
 23/*
 24This function is a delay function, causing the program to wait for approximately 4 * cnt cycles
 251 cycle is 1/SYSCLK seconds.
 26*/
 27void i2c_wait(unsigned int cnt)
 28{
 29    while(--cnt)
 30    {
 31        Nop();
 32        Nop();
 33    }
 34}
 35
 36
 37/* Main function */
 38int main(void)
 39{
 40    // Configure the proper PB frequency and the number of wait states.
 41    SYSTEMConfigPerformance(72000000);
 42
 43    AD1PCFG = 0xFFF0;
 44    TRISA = 0xFE;
 45    DDPCONbits.JTAGEN = 0;
 46
 47    //Enable I2C channel and set the baud rate to BRG_VAL)
 48    OpenI2C1( I2C_EN, BRG_VAL );
 49
 50    int rcv;            //For received data
 51
 52    while(1) {
 53         
 54
 55            SendData(data1,slaveaddress);              //Sends hex data 0xAA to slave address 0x40
 56            //rcv = RcvData(0x40);            //Receives data from address 0x40               
 57            Delayms(1000);
 58            PORTA = 0x1;   
 59       
 60
 61            SendData(data2,slaveaddress);            //Sends hex data 0xAA to slave address 0x40
 62            //rcv = RcvData(0x40);                //Receives data from address 0x40            
 63            Delayms(1000);
 64            PORTA = 0x0;       
 65       
 66    }//while loop ending
 67
 68    return 0;
 69}  //ending main
 70
 71
 72/*****************************************************
 73 * RcvData(unsigned int address)             *
 74 *                               *
 75 * Gets a byte of data from I2C slave device at      *
 76 *  ADDRESS.                         *
 77 *                             *
 78 * Returns: Received data                 *
 79 ****************************************************/
 80int RcvData(unsigned int address) {
 81    StartI2C1();                //Send line start condition
 82    IdleI2C1();                    //Wait to complete
 83    MasterWriteI2C1((address << 1) | 1);    //Write out slave address OR 1 (read command)
 84    IdleI2C1();                //Wait to complete
 85    int rcv = MasterReadI2C1();        //Read in a value
 86    StopI2C1();                //Send line stop condition
 87    IdleI2C1();                //Wait co complete
 88    return rcv;                //Return read value
 89}
 90
 91
 92
 93/***************************************************
 94 * SendData(int data, unsigned int address)        *
 95 *                            *
 96 * Sends a byte of data (DATA) over the I2C line   *
 97 *    to I2C address ADDRESS                *
 98 *                            *
 99 * Returns: nothing                    *
100 ***************************************************/
101void SendData (int data, unsigned int address){
102    StartI2C1();            //Send the Start Bit
103    IdleI2C1();        //Wait to complete
104
105    MasterWriteI2C1((address << 1) | 0);  //Sends the slave address over the I2C line.  This must happen first so the
106                                             //proper slave is selected to receive data.
107    IdleI2C1();            //Wait to complete
108
109    MasterWriteI2C1(data);  //Sends data byte over I2C line
110    IdleI2C1();        //Wait to complete
111
112    StopI2C1();            //Send the Stop condition
113    IdleI2C1();            //Wait to complete
114
115} //end function
116
117
118
119void Delayms( unsigned t)
120// This uses Timer 1, can be changed to another timer. Assumes FPB = SYS_FREQ
121{
122    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
123    while (t--)
124    {  // t x 1ms loop
125        WriteTimer1(0);
126        while (ReadTimer1() < PBCLK/256/1000);
127    }
128    CloseTimer1();
129} // Delayms


I'am using Explorer 16 development board for the PIC32. From the datasheet i see that the i2c pins (i2c1 and i2c2) are 5 V tolerant, so I assume that I dont have to use 3,3V-5V level translator?

any help please? thx a lot guys