Go Down

Topic: Very newbie questions about C and Audrino (Read 539 times) previous topic - next topic

exilepanda

Hi professionals,

I just started with Arduino and love it for it's easy getting start.. But unfortunate I don't familiar with  C programming, as well the compiler family.. so I wish could have some clues from the following... ( I know Perl and Java, however )

I have a program to control a "7 LED" to keep count from 0-9, through a 595 Chip and have this structure to assign which lights should on and off accordingly.

Code: [Select]

byte nums[10][8] = {
  { 1,1,1,0,1,1,1,0 }, //0
  { 0,0,1,0,0,1,0,0 }, // 1
  { 1,0,1,1,1,0,1,0 }, //..
  { 1,0,1,1,0,1,1,0 },
  { 0,1,1,1,0,1,0,0 },
  { 1,1,0,1,0,1,1,0 },
  { 1,1,0,1,1,1,1,0 },
  { 1,0,1,0,0,1,0,0 },
  { 1,1,1,1,1,1,1,0 },
  { 1,1,1,1,0,1,1,0 }, // 9
};

This is quite stupid, though it works.. however, in programming
maintenance aspect, I would like to make some code like this :
Code: [Select]

byte nums[] = { 0xEE, 0x24 ... 0xF6 } ;

for ( int n = 0; n<=9; n++ ) {
  byte thisNum = printf "%08b", nums[n];
  char[8] light = splitEachChar ( boolToString ( thisNum ) );
  sendBits ( light );  // this function suppose to send 8 bit for the 595 chip.
}


Questions :

1. I don't know if there's build-in functions for boolToString() or splitEachChar() for my purpose, even the printf()

2. it then drive me to think, which standard libs I can #inculde ( by default )? And if I got mine libs, where ( and what ) should I put those libs for my project ?

3. If I try to make my own lib, can I say if I can success compile a lib by avr-gcc, then I've grantee can run it on Arduino ?

4. Is that avr-gcc full support all the cpp style coding syntax ?


Please help me with any clues.. I feel so handicap while I can only use the most basic syntax.

lloyddean

Perhaps starting with the following will help

Code: [Select]

uint8_t nums[] =
{
  { 0b11101110 } // 0
, { 0b00100100 } // 1
, { 0b10111010 } // 2
, { 0b10110110 } // 3
, { 0b01110100 } // 4
, { 0b11010110 } // 5
, { 0b11011110 } // 6
, { 0b10100100 } // 7
, { 0b11111110 } // 8
, { 0b11110110 } // 9
};


exilepanda

Thanks lloyddean, but then.. I don't know how to literate bit by bit..
in my real function.. the sending bits goes in reverse order..
that why I prefer to carry the bits with array, to comfort a descending loop

lloyddean

#3
Mar 01, 2012, 04:46 am Last Edit: Mar 01, 2012, 05:00 am by lloyddean Reason: 1
Untested but something along the lines of -

Code: [Select]

for ( int i = 10; i--; )
{
   uint8_t bits = nums[i]
   uint8_t f    = 0b11111110;
   do
   {
       uint8_t bit = bits & 0b10000000;
       bits <<= 1;
       ... do something if bit is 0 or non zero ...
   } while ( f <<= 1 );
}


Brad Burleson


Thanks lloyddean, but then.. I don't know how to literate bit by bit..


There's a great example on the BitMath page (http://www.arduino.cc/playground/Code/BitMath) that can be applied to 8-segment LEDs.

It's more along the lines of lloyd's example.

FWIW,

Brad.

WizenedEE

The shiftOut function takes a byte value, which each bit will represent one output of a shift register.

So, you can just do this:

Code: [Select]

byte ledsToLight[] = {...};

void loop() {
  static int i = 0;
  shfitOut(9, 3, MSBFIRST, ledsToLight[i]);
  delay(1000);
  i++;
}

exilepanda

#6
Mar 01, 2012, 07:11 am Last Edit: Mar 01, 2012, 07:14 am by exilepanda Reason: 1
Thanks WizenedEE, Brad. and lloyddean, I found that the shiftOut() is a good function... so.. I just leave the chain action within this black box. Could you guys further teach me how to make this function as a lib ? It seems that's a block of code in those examples, but not a standard function.

What I really was feeling stuck.. perhaps my logic sense is really not low-level enough, so that I found this bitwise tricks is too hard for me follow, as well maintenance because of its readability, though, it might be more efficient for the machine to work with.

Brad Burleson

Code: [Select]

  shfitOut(9, 3, MSBFIRST, ledsToLight[i]);


I feel like that some days :-)

Oh wait, I guess I added a few letters in my mind...

And to try and help exilepanda... I too have struggled with bit operations.  I keep reading tutorials and I understand the underlying concepts but still have problems applying them (does that mean I don't really understand?)

What helped me in my last project (see http://arduino.cc/forum/index.php/topic,92026.0.html), which was driving a pair of Numitron tubes (basically a pair of 7-segment LEDs with a TLC5940) was to understand that each segment of the LED was assigned to a different pin... a total of 8 pins (if the decimal point worked)... and that can be expressed as a bitmap for each digit.  And seeing the binary definitions on the BitMath page helped me see the relationship.

I'm not sure this helps,

Brad.

PS If you want to see how bad it can get, see http://www.electronicsblog.net/4-digits-7-segments-led-display-multiplexing-with-arduino/#more-65

lloyddean

#8
Mar 01, 2012, 11:31 am Last Edit: Mar 02, 2012, 05:02 am by lloyddean Reason: 1
Brad,

Quote
PS If you want to see how bad it can get, see http://www.electronicsblog.net/4-digits-7-segments-led-display-multiplexing-with-arduino/#more-65


Such code need not be complicated, nor hard to understand - in fact it can be muched simplified.

While I don't have access to the hardware to test it the following simplified version should apporximate the code you pointed out.  It should also, even if it does not work, give an idea as to how a single digit number can be displayed using a binary encoded lookup table to decode a single digit to a 7-segment display.

Code: [Select]

// SEGMENT PIN NUMBERS
const uint8_t   G               = 22;
const uint8_t   F               = 23;
const uint8_t   A               = 24;
const uint8_t   B               = 25;
const uint8_t   E               = 26;
const uint8_t   D               = 27;
const uint8_t   C               = 28;
const uint8_t   DP              = 29;

// COMMONN CATHODES CONTROL
const uint8_t   GND1            = 52;
const uint8_t   GND2            = 53;
const uint8_t   GND3            = 50;
const uint8_t   GND4            = 51;

const uint8_t   pinSegments[]   = { A, B, C, D, E, F, G };
const uint8_t   pinCathodes[]   = { GND1, GND2, GND3, GND4 };

const unsigned long TIMER_INTERVAL  = 1000UL;

#define ARRAY_SIZEOF(ARRAY)     (sizeof(ARRAY) / sizeof(ARRAY[0]))


// REMINDER - GLOBALS ARE INITIALIZED TO ZERO BY THE COMPILER
unsigned long   timer;
uint16_t        counter;

bool isUpdateTime(unsigned const long tmsRef, const unsigned long& tmsTarget)
{
   return ((long)(tmsRef - tmsTarget) >= 0L);
}

void showdigit(uint8_t digit)
{
   const uint8_t   segments[] =
   {
   //       ABCDEFG
         0b01111110    // 0
       , 0b00110000    // 1
       , 0b01101101    // 2
       , 0b01111001    // 3
       , 0b00110011    // 4
       , 0b01011011    // 5
       , 0b01011111    // 6
       , 0b01110000    // 7
       , 0b01111111    // 8
       , 0b01111011    // 0
   };

   uint8_t     bits = segments[digit];

   for ( int i = ARRAY_SIZEOF(pinSegments); i--; bits >>= 1 )
   {
       digitalWrite(pinSegments[i], ((bits & 1) ? HIGH : LOW));
   }
}

void showdigits(int number)
{
   for ( int i = ARRAY_SIZEOF(pinCathodes); i--; number /= 10 )
   {
       digitalWrite(pinCathodes[i], LOW);

           showdigit(number % 10);

       digitalWrite(pinCathodes[i], HIGH);

       delay(1);
   }
}

void loop()
{
   showdigits(counter);

   if ( isUpdateTime(millis(), timer) )
   {
       // TOGGLE DECIMAL POINT TO INDICATE ACTIVITY
       //
       // BUG: CATHODE NEEDS TO BE ACTIVATED
//     digitalWrite(DP, ((counter % 2) ? HIGH : LOW));
       
       counter = (++counter % 1000);

       timer += TIMER_INTERVAL;
   }
}

void setup()
{
   for ( int i = ARRAY_SIZEOF(pinSegments); i--; )
   {
       pinMode(pinSegments[i], OUTPUT);
   }

   for ( int i = ARRAY_SIZEOF(pinCathodes); i--; )
   {
       pinMode(pinCathodes[i], OUTPUT);

       digitalWrite(pinCathodes[i], LOW);
   }

   timer = millis();
}


Go Up