Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Testing I2C comms to my ATtiny2313
- */
- #include <Wire.h>
- // local LED pins
- #define LED1 8
- #define LED2 9
- #define LED3 10
- #define LED4 11
- // Array to hold the led pin numbers
- int LEDS[4] = {LED1, LED2, LED3, LED4};
- const int SLAVE_ADDRESS = 0x01;// this must match the address assigned to the ATtiny in its code or they won't talk
- // setup
- void setup() {
- Wire.begin();
- for (byte i=0;i<4;i++) {
- pinMode(LEDS[i], OUTPUT);
- }
- }
- // Main
- void loop () {
- // Demonstrate each of the display modes available
- delay(1000);
- testRaw();
- delay(1000);
- testTime();
- delay(1000);
- testDegrees();
- delay(1000);
- testDecimal();
- delay(1000);
- }
- //-------------------------------------------------------------------
- // Functions
- //-------------------------------------------------------------------
- void testRaw() {
- // test the raw mode
- // A simple set of patterns to display.
- byte number_maps[15] = {
- //abcdefgp - 1 for on, 0 for off.
- B11000111,
- B00111010,
- B11000110,
- B00111010,
- B11000110,
- B00111010,
- B11000110,
- B00111010,
- B11000110,
- B00111010,
- };
- byte option_byte = B00110000; // turn the colon & apostrophe on
- for (int i=0;i<3;i++){
- for (byte i = 0; i<10; i++) {
- displayData(i); // show the current data on the locallly connected LEDs
- Wire.beginTransmission(SLAVE_ADDRESS);
- Wire.write(option_byte);
- for (int x=0;x<4;x++) {
- Wire.write(number_maps[((i+x) % 10)]);
- }
- Wire.endTransmission();
- delay(100);
- }
- //delay(250);
- }
- }
- //-------------------------------------------------------------------
- void testTime() {
- // test the time mode
- // slave is only reading mm:ss for now
- char option_byte = B10000000;
- for (byte m=5;m<15;m++) {
- for (byte s=0;s<60;s++) {
- Wire.beginTransmission(SLAVE_ADDRESS);
- Wire.write(option_byte);
- Wire.write((char)B00000001); // hours ignored for now
- Wire.write((char)m);
- Wire.write((char)s);
- Wire.write((char)B00000100); // 10th seconds ignored for now
- Wire.endTransmission();
- delay(20);
- }
- }
- }
- //-------------------------------------------------------------------
- void testDegrees() {
- // test the degrees mode
- byte option_byte= B11100000;
- // just whole numbers for now. hacking 4 bytes together. If your temp
- // will never exceed 255 you can just send 3 zeros and the raw temp.
- for (int x=0; x<2 ; x++ ) {
- for (int y=200; y<256 ; y++ ) { // cut down range for testing
- Wire.beginTransmission(SLAVE_ADDRESS);
- Wire.write(option_byte);
- Wire.write((byte)0);
- Wire.write((byte)0);
- Wire.write((byte)x);
- Wire.write((byte)y);
- Wire.endTransmission();
- delay(100);
- }
- }
- }
- //-------------------------------------------------------------------
- void testDecimal() {
- // test the decimal mode
- int temp;
- for (int i=500;i<2000;i++) {
- temp = i;
- Wire.beginTransmission(SLAVE_ADDRESS);
- Wire.write((byte)B01110000); //option byte
- // the number is broken into 4 bytes. Most significant byte first.
- // Quick and dirty hack with constants.
- Wire.write((byte)(temp/16777216));
- temp %= 16777216;
- Wire.write((byte)(temp/65536));
- temp %= 65536;
- Wire.write((byte)(temp/256));
- temp %= 256;
- Wire.write((byte)temp);
- Wire.endTransmission();
- delay(20);
- }
- }
- //-------------------------------------------------------------------
- void displayData(byte pattern) {
- // show the current data on this device's leds.
- // first blank out the leds
- for (byte i=0;i<4;i++) {
- digitalWrite(LEDS[i], LOW);
- }
- byte pat = pattern;
- byte i = 0;
- while (pat > 0 && i < 4) {
- if ((pat % 2) == 1) {
- digitalWrite(LEDS[i], HIGH);
- }
- pat = pat / 2;
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement