domingo, 2 de febrero de 2014

Display de 7 segmentos con 8 digitos (MAX7219)_2-Contador pasando de binario a decimal

 Código fuente:

********************************************************
const byte datapin = 12;
const byte latchpin = 11;
const byte clockpin = 10;

void setup() {
  pinMode(datapin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  // set : Normal Mode
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0C );
  shiftOut(datapin, clockpin, MSBFIRST, 0x01 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Normal Operation
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0F );
  shiftOut(datapin, clockpin, MSBFIRST, 0x00 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Intensity
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0A );
  shiftOut(datapin, clockpin, MSBFIRST, 0x0B );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Numbers of digits
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0B );
  shiftOut(datapin, clockpin, MSBFIRST, 0x07 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Decode Mode Register
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x09 );
  shiftOut(datapin, clockpin, MSBFIRST, 0xFF );
  digitalWrite(latchpin, HIGH);
}

void loop() {
  unsigned long numero=99988888;
  int i;
  for (i=1;i<9;i++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, i );
    shiftOut(datapin, clockpin, MSBFIRST, 15 );
    digitalWrite(latchpin, HIGH);
    //delay(5);
  }
  do {
    envDispl(numero);
    numero++;
  } while (numero < 100000000);//unsigned long puede representar hasta: 4.294.967.295 (2^32 - 1) porque tiene 4 bytes (32 bits)
}

void envDispl(unsigned long numero) {
  unsigned long k=10000000;
  int i, j;
  for (i=8; i>0; i--) {
    j=numero/k;
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, i );
    shiftOut(datapin, clockpin, MSBFIRST, j );
    digitalWrite(latchpin, HIGH);
    numero%=k;
    k=k/10;
    //delay (5);
  }
}

Display de 7 segmentos con 8 digitos (MAX7219)_1-Contador utilizando recursividad.

Código fuente:

//*************************************************************************
const byte datapin = 12;
const byte latchpin = 11;
const byte clockpin = 10;

void setup()
{
  pinMode(datapin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  // set : Normal Mode
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0C );
  shiftOut(datapin, clockpin, MSBFIRST, 0x01 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Normal Operation
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0F );
  shiftOut(datapin, clockpin, MSBFIRST, 0x00 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Intensity
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0A );
  shiftOut(datapin, clockpin, MSBFIRST, 0x0B );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Numbers of digits
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x0B );
  shiftOut(datapin, clockpin, MSBFIRST, 0x07 );
  digitalWrite(latchpin, HIGH);
  delay(5);
  // set : Decode Mode Register
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, 0x09 );
  shiftOut(datapin, clockpin, MSBFIRST, 0xFF );
  digitalWrite(latchpin, HIGH);
}

void loop()
{
  byte j;
  for (int i=1;i<9;i++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, i );
    shiftOut(datapin, clockpin, MSBFIRST, 15 );
    digitalWrite(latchpin, HIGH);
    delay(5);
  }
  delay(2000);
  contador(8);//funcion recursiva
  delay(2000);
}

void contador (int n) {
  int i;
  i=0;
  do {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, n );
    shiftOut(datapin, clockpin, MSBFIRST, i );
    digitalWrite(latchpin, HIGH);
    if (n==1) delay(5);//para que la transicin sea "instantanea" en caso de acarreo
    if (n>1) contador (n-1);
    i++;
    if (i==10) {
      i=0;
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, n );
      shiftOut(datapin, clockpin, MSBFIRST, i );
      digitalWrite(latchpin, HIGH);
      break;
    }
  }
  while (1);
}