Advertisement
grist

Arduino I2C Master Final

Mar 5th, 2012
1,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Testing I2C comms to my ATtiny2313
  3. */
  4.  
  5. #include <Wire.h>
  6.  
  7. // local LED pins
  8. #define LED1 8
  9. #define LED2 9
  10. #define LED3 10
  11. #define LED4 11
  12.  
  13. // Array to hold the led pin numbers
  14. int LEDS[4] = {LED1, LED2, LED3, LED4};
  15. const int SLAVE_ADDRESS = 0x01;// this must match the address assigned to the ATtiny in its code or they won't talk
  16.  
  17. // setup
  18. void setup() {
  19.  Wire.begin();
  20.  for (byte i=0;i<4;i++) {
  21.    pinMode(LEDS[i], OUTPUT);
  22.  }
  23. }
  24.  
  25. // Main
  26. void loop () {
  27.  
  28.   // Demonstrate each of the display modes available
  29.   delay(1000);
  30.   testRaw();
  31.   delay(1000);
  32.   testTime();
  33.   delay(1000);
  34.   testDegrees();
  35.   delay(1000);
  36.   testDecimal();
  37.   delay(1000);
  38.  
  39.  
  40. }
  41. //-------------------------------------------------------------------
  42. // Functions
  43. //-------------------------------------------------------------------
  44. void testRaw() {
  45.   // test the raw mode
  46.  
  47.   // A simple set of patterns to display.
  48.   byte number_maps[15] = {
  49.       //abcdefgp - 1 for on, 0 for off.
  50.       B11000111,
  51.       B00111010,
  52.       B11000110,
  53.       B00111010,
  54.       B11000110,
  55.       B00111010,
  56.       B11000110,
  57.       B00111010,
  58.       B11000110,
  59.       B00111010,
  60.   };
  61.  
  62.   byte option_byte = B00110000; // turn the colon & apostrophe on
  63.   for (int i=0;i<3;i++){
  64.     for (byte i = 0; i<10; i++) {
  65.       displayData(i); // show the current data on the locallly connected LEDs
  66.  
  67.       Wire.beginTransmission(SLAVE_ADDRESS);
  68.       Wire.write(option_byte);
  69.       for (int x=0;x<4;x++) {
  70.         Wire.write(number_maps[((i+x) % 10)]);
  71.       }
  72.       Wire.endTransmission();
  73.       delay(100);
  74.     }
  75.     //delay(250);
  76.   }
  77. }
  78. //-------------------------------------------------------------------
  79. void testTime() {
  80.   // test the time mode
  81.   // slave is only reading mm:ss for now
  82.   char option_byte = B10000000;
  83.   for (byte m=5;m<15;m++) {
  84.     for (byte s=0;s<60;s++) {
  85.       Wire.beginTransmission(SLAVE_ADDRESS);
  86.       Wire.write(option_byte);
  87.       Wire.write((char)B00000001); // hours ignored for now
  88.       Wire.write((char)m);
  89.       Wire.write((char)s);
  90.       Wire.write((char)B00000100); // 10th seconds ignored for now
  91.       Wire.endTransmission();
  92.       delay(20);
  93.     }
  94.   }  
  95. }
  96. //-------------------------------------------------------------------
  97. void testDegrees() {
  98.   // test the degrees mode
  99.  
  100.   byte option_byte= B11100000;
  101.   // just whole numbers for now. hacking 4 bytes together. If your temp
  102.   // will never exceed 255 you can just send 3 zeros and the raw temp.
  103.   for (int x=0; x<2 ; x++ ) {
  104.     for (int y=200; y<256 ; y++ ) { // cut down range for testing
  105.       Wire.beginTransmission(SLAVE_ADDRESS);
  106.       Wire.write(option_byte);
  107.       Wire.write((byte)0);
  108.       Wire.write((byte)0);
  109.       Wire.write((byte)x);
  110.       Wire.write((byte)y);
  111.       Wire.endTransmission();
  112.       delay(100);
  113.     }
  114.   }
  115.  
  116.  
  117. }
  118. //-------------------------------------------------------------------
  119. void testDecimal() {
  120.   // test the decimal mode
  121.   int temp;
  122.   for (int i=500;i<2000;i++) {
  123.     temp = i;
  124.     Wire.beginTransmission(SLAVE_ADDRESS);
  125.     Wire.write((byte)B01110000); //option byte
  126.     // the number is broken into 4 bytes. Most significant byte first.
  127.     // Quick and dirty hack with constants.
  128.     Wire.write((byte)(temp/16777216));
  129.     temp %= 16777216;
  130.     Wire.write((byte)(temp/65536));
  131.     temp %= 65536;
  132.     Wire.write((byte)(temp/256));
  133.     temp %= 256;
  134.     Wire.write((byte)temp);
  135.     Wire.endTransmission();
  136.     delay(20);
  137.   }
  138. }
  139. //-------------------------------------------------------------------
  140. void displayData(byte pattern) {
  141.  // show the current data on this device's leds.
  142.  // first blank out the leds
  143.    for (byte i=0;i<4;i++) {
  144.      digitalWrite(LEDS[i], LOW);
  145.    }
  146.    byte pat = pattern;
  147.    byte i = 0;
  148.    while (pat > 0 && i < 4) {
  149.      if ((pat % 2) == 1) {
  150.        digitalWrite(LEDS[i], HIGH);
  151.      }
  152.        pat = pat / 2;  
  153.        i++;
  154.    }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement