domingo, 2 de febrero de 2014

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);
}

No hay comentarios:

Publicar un comentario