#include <Wire.h>
byte data = 0;
int num = 0, num1 = 0, num2 = 0, num3 = 0;
void setup()
{
Wire.begin();
}
void loop()
{
Wire.beginTransmission(0x20);
data = segment_decoder(num%10);
Wire.write(data);
Wire.endTransmission();
Wire.beginTransmission(0x21);
data = segment_decoder(num1%10);
Wire.write(data);
Wire.endTransmission();
Wire.beginTransmission(0x22);
data = segment_decoder(num2%10);
Wire.write(data);
Wire.endTransmission();
Wire.beginTransmission(0x23);
data = segment_decoder(num3%10);
Wire.write(data);
Wire.endTransmission();
num++;
if(num==80){num=0;}
if(num%2==0){num1++;}
if(num1==10){num1=0;}
if(num%4==0){num2++;}
if(num2==10){num2=0;}
if(num%8==0){num3++;}
if(num3==10){num3=0;}
delay(200);
}
byte segment_decoder(int n){
byte b;
switch(n){
case 0:
b = 0b00111111;
break;
case 1:
b = 0b00000110;
break;
case 2:
b = 0b01011011;
break;
case 3:
b = 0b01001111;
break;
case 4:
b = 0b01100110;
break;
case 5:
b = 0b01101101;
break;
case 6:
b = 0b01111100;
break;
case 7:
b = 0b00000111;
break;
case 8:
b = 0b01111111;
break;
case 9:
b = 0b01101111;
break;
default:
b = 0b00000000;
break;
}
return ~b;
}
The main difference between I2C and Serial, is that Serial is a point to point connection and I2C is a bus which support many devices, each with own address.
I2C have better speed than Serial, but Serial support longer wires, depends what your needs are.
reference:
No comments:
Post a Comment