Question about Shifting LEDs in microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by glenn_boy13, Aug 1, 2013.

  1. glenn_boy13

    Thread Starter New Member

    Dec 19, 2012
    29
    0
    Hello! I know the basics of programming in Zilog, Atmega(arduino), and PIC microcontrollers.

    My questions here will be all about Zilog (z8F6421).

    1. Can I use each pins of a certain port in Zilog, just like in arduino?
    like: pin1 = HIGH; //in arduino.
    I only know: PAOUT = 0x01; //in Zilog.

    2. I have 8 Leds, and I have to shift each bit to the right.

    Code ( (Unknown Language)):
    1. int dataShift = 0x80 //Start from the value leftmost bit
    2. for_loop... from 1 to 8
    3. if(dataShift==0x00) dataShit = 0x80; //reset if data becomes 0
    4. PAOUT = dataShift;
    5. dataShift=dataShift>>1; //shift 1 bit at a time
    The codes above is only for shifting one bit at a time.
    But here comes my problem:

    I have to shift one bit at a time, but when it comes to the right most bit, the display will be still. Ughh, I can't describe it well. But I have a picture below.
    [​IMG]

    loop[0] = 1000 0000 up to 0000 0001
    loop[1] = 1000 0001 up to 0000 0011
    loop[2] = 1000 0011 up to 0000 0111

    until 1's occupy the whole octet. Please help
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    I'm sure the first person who actually understands this will give you an answer. Is the image that you're showing what you want to get, or what you actually are getting?

    And what does "when it comes to the right most bit, the display will be still" mean? Still as in stationary, or there's a word missing and it's "still something" or other?
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I'm likewise confused, but, assuming you want each LED on and remain on, just OR the previous value with what you have shifted in.

    Or, you could just shift 0xFF in by the value of a counter...

    This is the problem with taking someone else's code, getting it to work, and claiming you know how to use the device...
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    You need to show more complete code. Your code is close but there are a couple of issues with your for loop and your order of testing.

    Like this;
    Code ( (Unknown Language)):
    1.  
    2. int dataShift = 0x80;  // make sure "int" is actually an 8bit variable!
    3.  
    4.   for(i=0; i<8; i++)
    5.   {
    6.     dataShift = dataShift >> 1;            //shift 1 bit at a time
    7.     if(dataShift==0x00) dataShift = 0x80;  //reset if data becomes 0
    8.     PAOUT = dataShift;
    9.   }
    10.  
    Notice the order of operation is improved. First the data is modified (shifted), then tested and corrected, then displayed.
     
  5. glenn_boy13

    Thread Starter New Member

    Dec 19, 2012
    29
    0
    guys! take it easy. I didn't copy anyone's codes.
    It is mine. I posted the codes to show you my idea. It is mine. I use ">>" or "<<" for shifting bits. Zilog Microcontroller is not like arduino nor pic. In arduino and PIC, I can manipulate the output of each pin. In Zilog, I can control each pin but it will also affect the others.

    [​IMG]

    Example: In arduino, say I have 8bits output (8 LEDs). I can control whatever pins I want anytime without affecting the others.
    While In zilog, it affects the others.

    Here is the code:
    PAOUT = 0x1F; //makes Pins 1,2,3,4,5 high.

    I've made a Zilog Microcontroller Module (for Z8F6421). It has 4 ports, namely: PORT A, B,C,D.

    @john P: the image that I uploaded is the one I want to get.
    @tshuck: see I didn't copy anything.
    @THE_RB: thanks for the help sir. Ill try to give an update later.BRB
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    uhhh... no. How could what you've shown prove that you haven't copied the code? I say this because you claim to know these controllers, yet don't understand basic bit manipulations?

    To set a bit:
    register = register | 0x04; // set bit 3

    To clear a bit:
    register = register & 0xF7; // clear bit 3

    Call me a skeptic, I guess.:rolleyes:
     
  7. glenn_boy13

    Thread Starter New Member

    Dec 19, 2012
    29
    0
    hang on mr. skeptic. XD. I am currently uploading a clip onto youtube. I programmed the output one by one. I mean, no patterns, no for loops, no shifting of bits. As in One Line of codes per shift of bits.
    So what I am asking from the start is... the pattern for this output, the shortcut. :)
     
  8. glenn_boy13

    Thread Starter New Member

    Dec 19, 2012
    29
    0
    HERE: http://youtu.be/VzNwW-SKZ24
    please see this sir.

    and here is my code:


    Code ( (Unknown Language)):
    1. #include <ez8.h>
    2.  
    3.  
    4. void delay(void){
    5.     int x,y;
    6.     for(x=0;x<=500;x++)
    7.         for(y=0;y<=512;y++);
    8.     }
    9.    
    10.  
    11. void main(){
    12.     int x=0;
    13.     int dataShift = 0x80;
    14.    
    15.     PADD = 0x00;
    16.     PAAF = 0x00;
    17.     PAOC = 0x00;
    18.     PAHDE = 0xFF;
    19.    
    20.    
    21.     while(1){
    22.        
    23.        
    24.         /*LOOP1*/
    25.         PAOUT = ~0x80;  delay();
    26.         PAOUT = ~0x40;  delay();
    27.         PAOUT = ~0x20;  delay();
    28.         PAOUT = ~0x10;  delay();
    29.         PAOUT = ~0x08;  delay();
    30.         PAOUT = ~0x04;  delay();
    31.         PAOUT = ~0x02;  delay();
    32.         PAOUT = ~0x01;  delay();
    33.         /*end of loop1*/
    34.        
    35.         /*LOOP2*/  
    36.         PAOUT = ~0x81;  delay();
    37.         PAOUT = ~0x41;  delay();
    38.         PAOUT = ~0x21;  delay();
    39.         PAOUT = ~0x11;  delay();
    40.         PAOUT = ~0x09;  delay();
    41.         PAOUT = ~0x05;  delay();
    42.         PAOUT = ~0x03;  delay();   
    43.         /*end of loop2*/
    44.            
    45.         /*LOOP3*/  
    46.         PAOUT = ~0x83;  delay();
    47.         PAOUT = ~0x43;  delay();
    48.         PAOUT = ~0x23;  delay();
    49.         PAOUT = ~0x13;  delay();
    50.         PAOUT = ~0x0B;  delay();
    51.         PAOUT = ~0x07;  delay();   
    52.         /*end of loop3*/   
    53.        
    54.         /*LOOP4*/
    55.         PAOUT = ~0x87;  delay();
    56.         PAOUT = ~0x47;  delay();
    57.         PAOUT = ~0x27;  delay();
    58.         PAOUT = ~0x17;  delay();
    59.         PAOUT = ~0x0f;  delay();
    60.         /*end of loop4*/
    61.        
    62.         /*LOOP5*/
    63.         PAOUT = ~0x8f;  delay();
    64.         PAOUT = ~0x4f;  delay();
    65.         PAOUT = ~0x2f;  delay();
    66.         PAOUT = ~0x1f;  delay();
    67.         /*end of loop5*/
    68.        
    69.         /*LOOP6*/
    70.         PAOUT = ~0x9f;  delay();
    71.         PAOUT = ~0x5f;  delay();
    72.         PAOUT = ~0x3f;  delay();
    73.         /*end of loop6*/
    74.        
    75.         /*LOOP7*/
    76.         PAOUT = ~0xbf;  delay();
    77.         PAOUT = ~0x7f;  delay();
    78.         /*end of loop7*/
    79.        
    80.         /*LOOP8*/
    81.         PAOUT = ~0xff;  delay();
    82.         /*end of loop8*/
    83.        
    84.         }
    85.    
    86.    
    87.     }
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,815
    835
    Let's see... first, I would think of how to stop "shifting" at one position greater than the last loop...
    Secondly, I would think of how to keep the previous shifted bits set. I can think of two ways. One is to save the previous loops result and setting it after each shift and another way is to not change it in the first place. Maybe by other operations other than shifting...
    Note one operation affects the entire register while the second, although perhaps more costly, preserves your previous work...
    There are at least 2 or 3 ideas in here for you to think about. I hope you find them useful...
     
  10. absf

    Senior Member

    Dec 29, 2010
    1,493
    373

    I wrote this code based on yours. In order to test it, I modified it a bit so it can work on PIC16f876A using HTC compiler.

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. //#include <math.h>
    3. #define _XTAL_FREQ 4000000
    4.  
    5. void delay(void)
    6.     {
    7.     unsigned int x,y;
    8.     for(x=0;x<=25;x++)
    9.         for(y=0;y<=512;y++);
    10.     }
    11.    
    12. void main()
    13. {
    14.     unsigned int i, j, k ;
    15.     unsigned int dataShift = 0x80;
    16.  
    17.     const char m[ 9 ] =
    18.     {   0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00
    19.     };
    20.     // pow( 2 , i ) -1;  
    21.     PORTB = 0X00;                   //PAOUT = 0x00;
    22.     TRISB = 0X00;                   //PAAF = 0x00;
    23.                                     //PAOC = 0x00;
    24.                                     //PAHDE = 0xFF;
    25.    
    26.     while (1)
    27. {
    28.     for (j=8; j>0; j--)
    29.     {
    30.     dataShift=0x80;
    31.     for (i=0; i<j ; i++)
    32.     {
    33.         k = m[ j ];
    34.         PORTB = ~(dataShift | k);   delay();    //dataShift [OR] k
    35.         dataShift = dataShift >> 1;
    36.     }  
    37.     }
    38.        
    39. }
    40. }
    41.    
    42.  
    Allen
     
Loading...