Advertisement
Guest User

Untitled

a guest
May 13th, 2012
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #define F_CPU 20000000UL
  2. #include <pololu/orangutan.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "i2cmaster.h"
  6. #define COMPASS 0x3C
  7.  
  8. int main()
  9. {
  10.     play_from_program_space(PSTR(">g32>>c32"));  // Play welcoming notes.
  11.    
  12.     lcd_goto_xy(0,0);
  13.     print("i2c init");
  14.     i2c_init();
  15.     i2c_start_wait(COMPASS + I2C_WRITE);
  16.     i2c_write(0x00);
  17.     i2c_write(0x70);
  18.  
  19.     i2c_start_wait(COMPASS + I2C_WRITE);
  20.     i2c_write(0x01);
  21.     i2c_write(0xA0);   
  22.  
  23.     i2c_start_wait(COMPASS + I2C_WRITE);
  24.     i2c_write(0x02);
  25.     i2c_write(0x00);
  26.     lcd_goto_xy(0, 0);
  27.     print("i2c init done");
  28.     delay_ms(1000);
  29.  
  30.     int buffer[6];
  31.     int x, y, z = 0;
  32.     short i;
  33.    
  34.     char* msg = malloc(sizeof(char)*16);
  35.     while(1)
  36.     {
  37.         i2c_start(COMPASS + I2C_READ);
  38.         i2c_write(0x06);
  39.        
  40.         for (i = 0; i < 6; i++) {
  41.             buffer[i] = i2c_readNak();
  42.         }
  43.  
  44.         x = (buffer[0] << 8) | buffer[1];
  45.         z = (buffer[2] << 8) | buffer[3];
  46.         y = (buffer[4] << 8) | buffer[5];
  47.  
  48.         lcd_goto_xy(0, 1);
  49.         sprintf(msg, "%d %d %d", x, y, z);
  50.         print(msg);
  51.        
  52.         delay_ms(1000);
  53.         lcd_goto_xy(0, 1);
  54.         print("                ");
  55.         delay_ms(200);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement