Author Topic: clock with shift register and digispark  (Read 5589 times)

Scr34m

  • Newbie
  • *
  • Posts: 3
clock with shift register and digispark
« on: August 17, 2013, 10:18:46 am »
I have created a clock with 74HC595 and four 7seg display and would like to power it digispark and make it available trough USB communication to set time and other tasks.


A first i have made a simple code to drive HC595's thats worked flawless after that included USB library to use and communicate with it, but it's just doesnt work maybe some timing issue or anything else? Using P0, P1, P2 for driving HC595's so P2 and P3 on digispark free for USB data communication.


Tryed switching on / off innterupts in writeRegisters() call but no sucess. So when first line commented out nothing works.

Any idea about this issue are welcome, thanks!

Code: [Select]

//#include <DigiUSB.h>


int SER_Pin = 0;
int RCLK_Pin = 2;
int SRCLK_Pin = 1;


#define number_of_74hc595s 4
#define numOfRegisterPins number_of_74hc595s * 8


boolean registers[numOfRegisterPins];


void setup()
{
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
}


void clearRegisters()
{
  for(int i = numOfRegisterPins - 1; i >=  0; i--) {
     registers[i] = HIGH;
  }
}


void writeRegisters()
{
  digitalWrite(RCLK_Pin, LOW);
  for(int i = numOfRegisterPins - 1; i >=  0; i--) {
    digitalWrite(SRCLK_Pin, LOW);
    int val = registers[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
  digitalWrite(RCLK_Pin, HIGH);
}


void setRegisterPin(int index, int value)
{
  registers[index] = value;
}


void number(int seg, int num)
{
  int segments[11][7] = {
   //c,d,e,b,a,f,g
    {1,1,1,1,1,1,0}, // 0
    {1,0,0,1,0,0,0}, // 1
    {0,1,1,1,1,0,1}, // 2
    {1,1,0,1,1,0,1}, // 3
    {1,0,0,1,0,1,1}, // 4
    {1,1,0,0,1,1,1}, // 5
    {1,1,1,0,1,1,1}, // 6
    {1,0,0,1,1,0,0}, // 7
    {1,1,1,1,1,1,1}, // 8
    {1,0,0,1,1,1,1}, // 9
    {1,1,1,1,1,1,1}  // off
  };


  setRegisterPin(1 + seg, !segments[num][0]);
  setRegisterPin(2 + seg, !segments[num][1]);
  setRegisterPin(3 + seg, !segments[num][2]);
  setRegisterPin(4 + seg, !segments[num][3]);
  setRegisterPin(5 + seg, !segments[num][4]);
  setRegisterPin(6 + seg, !segments[num][5]);
  setRegisterPin(7 + seg, !segments[num][6]);
}


int c = 0;


void loop()
{
  clearRegisters();
  writeRegisters();


  if (c > 9999) {
    c = 0;
  } 
 
  int num = c;
  number(0, num % 10);
  num /= 10;
  number(8, num % 10);
  num /= 10;
  number(16, num % 10);
  num /= 10;
  number(24, num % 10);


  writeRegisters();


  delay(100);
}






gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: clock with shift register and digispark
« Reply #1 on: August 20, 2013, 09:57:36 am »
you are just running out of memory!

This happened to me in the very beginning with the digispark as well, coming from much larger memory models.
The attiny85 has only 512 bytes of SRAM and with including DigiUSB you are going over that limit!

Digging with avr-size into your binary (you see the location of the created elf-file in the output-window of your IDE, avr-size is in the avr-subdirectory.

Instead of
Code: [Select]
int segments[11][7]  go with
Code: [Select]
int segments[11] and use bitmasks, to use the single bits of that byte

with that change you should be in limits again.  But be careful: DigiUSB is a memory-eater:

Your code without DigiUSB:

Code: [Select]
./location/to/avr-size(.exe)  ./location/to/file.elf
   text    data     bss     dec     hex filename
   1480     158      45    1683     693 test.elf

Your code with DigiUSB:

Code: [Select]
./location/to/avr-size(.exe)  ./location/to/file.elf
   text    data     bss     dec     hex filename
   2512     170     370    3052     bec test.elf

As you see, DigiUSB includes 12bytes of initialized variables and 325 bytes of uninitialized variables.

That leaves not much for your application!


regards

 .g

gogol

  • Sr. Member
  • ****
  • Posts: 398
Re: clock with shift register and digispark
« Reply #2 on: August 21, 2013, 07:52:04 am »
I have uploaded in the thread http://digistump.com/board/index.php/topic,1055.0.html a version of avrdude, which will run the avr-size command before upload. So you can see, how much from the sram is already used by static variables.

As an explanation:

.text  is the name of the segment, where the binary code is stored
.data is the segment, where initialized variables are stored
.bss  is the segment, which will be used for uninitialized variables

.text and .data will be stored in FLASH

.data and .bss need to fit in SRAM

and you need to think about, that all dynamical allocated structures as well as stack-data needs space in SRAM as well!

regards

  .g

Scr34m

  • Newbie
  • *
  • Posts: 3
Re: clock with shift register and digispark
« Reply #3 on: August 24, 2013, 08:29:32 am »
Huh that's was impressive detailed help, thanks!

I will take a look for my code and do some changes to fix and will drop a reply when done.

Scr34m

  • Newbie
  • *
  • Posts: 3
Re: clock with shift register and digispark
« Reply #4 on: August 24, 2013, 09:46:55 am »
I've optimized the code like you mentioned so then USB working  ;D


Code: [Select]
   text    data     bss     dec     hex filename
   3422      36     357    3815     ee7 ./shift_digi.cpp.elf