dsPic30f4011 I2C communication

Discussion in 'Embedded Systems and Microcontrollers' started by ZSR, Oct 20, 2011.

  1. ZSR

    Thread Starter New Member

    Oct 20, 2011
    1
    0
    I'm using a dsPic30f4011 and I'm trying to communicate with a servo via I2C. The reason for I2C is because I replaced the servo's controller board with "open Servo" one:
    http://www.openservo.com/ - the open servo project
    Only mine is from Spurkfun:
    http://www.sparkfun.com/products/9014

    I've tried many things but I can't make it work. Is my code written right?
    I'm new to I2C, though searched the web a lot till now.

    This should move the servo:

    Code ( (Unknown Language)):
    1. #include <p30f4011.h>
    2. #include <i2c.h> //Header File for I2C module Library routines
    3.  
    4. unsigned int I2CConfig;
    5. unsigned int I2C_BRG;
    6.  
    7. int main(void)
    8. {
    9. //Configure the I2C Bus
    10. CloseI2C();
    11. I2CConfig = I2C_ON &         //Enable the I2C Bus
    12.             I2C_IDLE_CON &    
    13.             I2C_CLK_HLD &
    14.             I2C_IPMI_DIS &
    15.             I2C_7BIT_ADD &    //Use a 7 Bit Slave Address
    16.             I2C_SLW_DIS &
    17.             I2C_SM_DIS &
    18.             I2C_GCALL_DIS &
    19.             I2C_STR_DIS &
    20.             I2C_NACK &
    21.             I2C_ACK_DIS &
    22.             I2C_RCV_DIS &
    23.             I2C_STOP_DIS &
    24.             I2C_RESTART_DIS &
    25.             I2C_START_DIS;
    26. I2C_BRG = 0xB5;    //0xB5 = 181 => Fscl=100kHz=Boud Rate, PGD=250ns, Fcy=20MHz
    27. OpenI2C(I2CConfig , I2C_BRG); //Open the I2C Bus
    28. IdleI2C();
    29.  
    30. while(1)
    31. {
    32. //Setting position of 0x226 = 550 (should be center position)
    33. StartI2C();
    34. MasterWriteI2C(0x40);    //7 bit OpenServo address 0x20 + LSB 0 for "write"
    35. MasterWriteI2C(0x08);    //Position_high register
    36. MasterWriteI2C(0x02);    //High byte
    37. MasterWriteI2C(0x26);    //Low byte
    38. StopI2C();
    39. }
    40.  
    41. }
    I want to make sure that the code is ok, so I can find why i can't make work.

    Any help will be appreciated!
     
Loading...