Using peripheral with Arduino UNO

int ev=0;
byte cx=0,sx; 
byte dg[]={9,2,8};
int cntr;
//----------- pgfedcba pgfedcba pgfedcba pgfedcba
const byte dx[]= { ….... };   //store BCD representations from 0 to 9

void setup() {
  Serial.begin(9600);
  DDRC = 0x3F; //PC[0..5] as output
  DDRB = 0x3F; //PB[0..5] as output
}

void loop() {
  sx = dx[dg[cx]];
  PORTB = PORTB|B011100;
  PORTC = sx & 0x3F;
  PORTB = ((PORTB&0xFC)|(sx>>6));
  PORTB = PORTB&(~(4<<cx));
  cx=++cx%3;
  ev++;
  if (ev==50) {
  ev=0;
  cntr = ++cntr % 1000;
  dg[0] = cntr …....;
  dg[2] = cntr …....;
  dg[1] = cntr …....;
}
delay(20); //50-10
//delayMicroseconds(5000);
}

Hi guys,

The code above controls the display of a 7-segment LED, and the code for calculating values of dg[0] , dg[1], dg[2] is left to complete. I don't understand why even though dg[] is assigned already, we need to override or reassign the value again. Anyone can explain the working principle behind it? Thanks!

It might help if the code was complete. "......" is not valid syntax.

If the value (cntr) changes the display (dg[]) has to be changed accordingly.

I believe that you're supposed to end up with a display that counts up, starting from the initial value 928 (or perhaps the initial value is just to provide quick verification that you're re-processing the value.)

It looks like there are two parts that have been left out (for you to fill in, presumably):

  1. Come up with the dx table that translates between a BCD digit and its 7-segment representation.
  2. Figure out how to extract the individual bcd digits dg from the binary integer cntr

It bothers me that cntr is not initialized (C specs say it will be initialized to zero. But an explicit initialization would be much better "style.") Perhaps you can initialize it to various values to make sure that all your digit extractions and conversions work correctly without having to watch for 1000 iterations.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.