Advertisement
Guest User

Untitled

a guest
May 15th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. /* compass_robot - an application for the Pololu Orangutan SVP
  2.  *
  3.  * This application uses the Pololu AVR C/C++ Library.  For help, see:
  4.  * -User's guide: http://www.pololu.com/docs/0J20
  5.  * -Command reference: http://www.pololu.com/docs/0J18
  6.  *
  7.  * Created: 5/13/2012 6:00:02 PM
  8.  *  Author: Dave
  9.  */
  10.  
  11. #define F_CPU 20000000UL
  12. #include <pololu/orangutan.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "i2cmaster.h"
  16. #define COMPASS 0x3C
  17.  
  18. int main()
  19. {
  20.     play_from_program_space(PSTR(">g32>>c32"));  // Play welcoming notes.
  21.    
  22.     lcd_goto_xy(0,0);
  23.     print("i2c init");
  24.     i2c_init();
  25.     i2c_start_wait(COMPASS + I2C_WRITE);
  26.     i2c_write(0x00);
  27.     i2c_write(0x70);
  28.     i2c_stop();
  29.  
  30.     i2c_start_wait(COMPASS + I2C_WRITE);
  31.     i2c_write(0x01);
  32.     i2c_write(0xA0);   
  33.     i2c_stop();
  34.  
  35.     i2c_start_wait(COMPASS + I2C_WRITE);
  36.     i2c_write(0x02);
  37.     i2c_write(0x00);
  38.     i2c_stop();
  39.     lcd_goto_xy(0, 0);
  40.     print("i2c init done");
  41.     delay_ms(1000);
  42.  
  43.     long buffer[6];
  44.     long x, y, z = 0;
  45.     short i;
  46.    
  47.     char* msg = malloc(sizeof(char)*16);
  48.     while(1)
  49.     {
  50.         i2c_start(COMPASS + I2C_WRITE);
  51.         i2c_write(0x06);
  52.         i2c_stop();
  53.        
  54.         i2c_start(COMPASS + I2C_READ);
  55.         for (i = 0; i < 5; i++) {
  56.             buffer[i] = i2c_readAck();
  57.         }
  58.         buffer[5] = i2c_readNak();
  59.         i2c_stop();
  60.  
  61.         x = (buffer[0] << 8) | buffer[1];
  62.         z = (buffer[2] << 8) | buffer[3];
  63.         y = (buffer[4] << 8) | buffer[5];
  64.  
  65.         lcd_goto_xy(0, 1);
  66.         sprintf(msg, "%d %d %d", x, y, z);
  67.         print(msg);
  68.        
  69.         delay_ms(1000);
  70.         lcd_goto_xy(0, 1);
  71.         print("                ");
  72.         delay_ms(200);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement