Advertisement
Guest User

CMPS10 I2C sketch fix

a guest
Mar 21st, 2012
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. /****************************************************************
  2. * Arduino CMPS10 example code *
  3. * CMPS10 running I2C mode *
  4. * by James Henderson, 2012 *
  5. *****************************************************************/
  6. #include <Wire.h>
  7. #include <SoftwareSerial.h>
  8.  
  9. #define ADDRESS 0x60 // Defines address of CMPS10
  10.  
  11. #define LCD_RX 0x02 // RX and TX pins used for LCD0303 serial port
  12. #define LCD_TX 0x03
  13. #define LCD03_HIDE_CUR 0x04
  14. #define LCD03_CLEAR 0x0C
  15. #define LCD03_SET_CUR 0x02
  16.  
  17. SoftwareSerial lcd03 = SoftwareSerial(LCD_RX, LCD_TX); // Defines software serial port for LCD03
  18.  
  19. void setup(){
  20. Wire.begin(); // Conects I2C
  21. lcd03.begin(9600);
  22. lcd03.write(LCD03_HIDE_CUR);
  23. lcd03.write(LCD03_CLEAR);
  24. }
  25.  
  26. void loop(){
  27. byte highByte, lowByte, fine; // highByte and lowByte store high and low bytes of the bearing and fine stores decimal place of bearing
  28. char pitch, roll; // Stores pitch and roll values of CMPS10, chars are used because they support signed value
  29. int bearing; // Stores full bearing
  30.  
  31. Wire.beginTransmission(ADDRESS); //starts communication with CMPS10
  32. Wire.write(2); //Sends the register we wish to start reading from
  33. Wire.endTransmission();
  34.  
  35. Wire.requestFrom(ADDRESS, 4); // Request 4 bytes from CMPS10
  36. while(Wire.available() < 4); // Wait for bytes to become available
  37. highByte = Wire.read();
  38. lowByte = Wire.read();
  39. pitch = Wire.read();
  40. roll = Wire.read();
  41.  
  42. bearing = floor(float(((highByte<<8)+lowByte))/10); // Calculate full bearing
  43. fine = ((highByte<<8)+lowByte)%10; // Calculate decimal place of bearing
  44.  
  45. display_data(bearing, fine, pitch, roll); // Display data to the LCD03
  46.  
  47. delay(100);
  48. }
  49.  
  50. void display_data(int b, int f, int p, int r){ // pitch and roll (p, r) are recieved as ints instead oif bytes so that they will display corectly as signed values.
  51.  
  52. lcd03.write(LCD03_SET_CUR); // Set the LCD03 cursor position
  53. lcd03.write(1);
  54. lcd03.print("CMPS10 Example V:");
  55. lcd03.print(soft_ver()); // Display software version of the CMPS10
  56.  
  57. delay(5); // Delay to allow LCD03 to proscess data
  58.  
  59. lcd03.write(LCD03_SET_CUR);
  60. lcd03.write(21);
  61. lcd03.print("Bearing = "); // Display the full bearing and fine bearing seperated by a decimal poin on the LCD03
  62. lcd03.print(b);
  63. lcd03.print(".");
  64. lcd03.print(f);
  65. lcd03.print(" ");
  66.  
  67. delay(5);
  68.  
  69. lcd03.write(LCD03_SET_CUR); // Display the Pitch value to the LCD03
  70. lcd03.write(41);
  71. lcd03.print("Pitch = ");
  72. lcd03.print(p);
  73. lcd03.print(" ");
  74.  
  75. delay(5);
  76.  
  77. lcd03.write(LCD03_SET_CUR); // Display the roll value to the LCD03
  78. lcd03.write(61);
  79. lcd03.print("Roll = ");
  80. lcd03.print(r);
  81. lcd03.print(" ");
  82. }
  83.  
  84. int soft_ver(){
  85. int data; // Software version of CMPS10 is read into data and then returned
  86.  
  87. Wire.beginTransmission(ADDRESS);
  88. // Values of 0 being sent with write need to be masked as a byte so they are not misinterpreted as NULL this is a bug in arduino 1.0
  89. Wire.write((byte)0); // Sends the register we wish to start reading from
  90. Wire.endTransmission();
  91.  
  92. Wire.requestFrom(ADDRESS, 1); // Request byte from CMPS10
  93. while(Wire.available() < 1);
  94. data = Wire.read();
  95.  
  96. return(data);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement