0

I am coding SSD display using 2 cascaded shift registers. I am using a mikroC for PIC compiler. I can display a static sequence of numbers upto 4 digits with my code Static Display of 4 Numbers

    #define SHIFT_CLOCK PORTB.F1    //Clock Connection of 74HC595 SSD Driver
    #define SHIFT_LATCH PORTB.F3    //Latch Connection of 74HC595 SSD Driver
    #define SHIFT_DATA PORTB.F2    //Data Connection of 74HC595 SSD Driver

    char array4[4] = {6, 91, 79, 102};           //Display 1234 on SSD
    char digit[4] = {0xFE, 0xFD, 0xFB, 0xF7};   //Switch on the SSD digits one by one

    char i,j,temp,flag1,flag2;

    void InitTimer0()
    {
     OPTION_REG     = 0x86;
     TMR0           = 6;
     INTCON         = 0xA0;
    }

    void latch595()
    {
     SHIFT_LATCH = 1;
     Delay_us(1);
     SHIFT_LATCH = 0;
    }

    void shiftdata595(unsigned char _shiftdata)
    {
     int i;
     unsigned char temp;
     temp = _shiftdata;
     i=8;
     while (i>0)
     {
      if (temp.F7==0)
       {
        SHIFT_DATA = 0;
       }
       else
       {
        SHIFT_DATA = 1;
       }
       temp = temp<<1;
       SHIFT_CLOCK = 1;
       SHIFT_CLOCK = 0;
       i--;
     }
    }

    void Interrupt()
    {
     if (TMR0IF_bit)
     {
      TMR0IF_bit  = 0;
      TMR0        = 6;
      flag1 = 1;
      flag2 = 1;
     }
    }

    void main()
    {
     TRISB = 0;
     TRISC.F1 = 1;
     InitTimer0();
     while (1)
     {
      if (PORTC.F1==0)
      {
       if (flag2==1)
       {
        shiftdata595(digit[i]);
        i++;
        if(i==4)
        {
         i=0;
        }
        if (flag1==1)
        {
         shiftdata595(array4[j]);
         latch595();
         j++;
         if (j==4)
         {
          j=0;
         }
        }
       }
      }
      else if(PORTC.F1==1)
      {
       shiftdata595(0);
       shiftdata595(0);
       latch595();
      }
     }
    }

If I add more digits to the array4[], say upto 9, I will need to scroll the digits to the left sequentially. I tried shifting the array by

temp = array4[0];
for (n=1; n<8; n++)
{
 array4[j-1] = array[j];
}
array[9] = temp;

I hoped that this code will left shift the array and the display will scroll, but all I am getting is a jumbled up display. If I add a delay, I can see that the numbers are getting displayed but without scrolling.

Is the basic algorithm faulty or can it be used by modifying the code?

Ace
  • 11
  • 4
  • 3
    Why are you using `n` as the loop variable but `j` to index `array4[]`? Also you are indexing a 9-element array out of range with `array[9] = temp;` Finally is `array4` a typo? - you use both `array` and `array4`. – Weather Vane Jul 25 '17 at 07:17
  • Sorry, array[9]=temp is a typo. I meant to type array4[9] = temp. Also I tried to use j as the loop variable but the same result – Ace Jul 25 '17 at 08:02
  • So where do you set `array4[8]`? – Weather Vane Jul 25 '17 at 08:08
  • Lets say the array is array4[10] = {6, 91, 79, 102, 109, 125, 7 , 127, 111, 63 }; Now the array needs to be left shifted to give and the first element in the array4[] goes to a temporary variable. The array is left shifted from array4[1] onwards toll array4[9]. Afterwards the temporary variable is placed in the array4[10] place. – Ace Jul 25 '17 at 08:45
  • You have changed it from 9 to 10 elements, but again, *there is no `array4[10]` place*. It is out of bounds. – Weather Vane Jul 25 '17 at 08:48
  • Do I need to keep it to array4[9] place? But still I doesnt solve the problem – Ace Jul 25 '17 at 08:51
  • Post the actual code you're using to shift characters if you corrected the errors pointed out by @WeatherVane. You are already using the `j` field for something else in this function, you should have just used `n` in the entire loop. And don't use global fields as loop variables, that's a really bad idea. – vgru Jul 26 '17 at 21:02
  • Yes, I did correct the errors pointed out by @WeatherVane but still the problem persists. I am still working on it and shall post the working code soon – Ace Jul 27 '17 at 04:48

1 Answers1

0

I did figure out the problem with my code. The loop was wrong. I am using two 74HC595 in cascaded mode. One controls the digits(common cathode) and other controls the segments. Since there is only one data line that passes on the information to the digits as well as the segments, the timing of the digits advancement and the segments needs to be coordinated. The entire problem was that I was unnecessarily concentrating on the timing part which was coordinated but instead, I should have concentrated on the loop condition which advances the segments and then left shifts the array.

On friend pointed out that I need to write down the data values on a piece of paper for each loop and that solved the problem. Instead of feeding the loop the length of the array manually like array4[8] or array4[10], I used the sizeof() function and then after the first four iterations of the loop, left shifted the array one place. Now the scrolling works perfect.

Here is the updated and working code,

shiftdata595(array4[tmp7]);
 latch595();
  Delay_ms(32);
  tmp7++;
 if (tmp7>=4)
 {
  tmp7=0;
  tmp1 = sizeof(array4)/sizeof(array4[0]);
  tmp2 = array4[0];
  for (n=1;n<tmp1;n++)
  {
   array4[n-1] = array4[n];
  }
  array4[tmp1-1] = tmp2;
  }
Ace
  • 11
  • 4