Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

LED Combinational Patterns

Status
Not open for further replies.

ElectroNerd

Newbie level 6
Joined
May 15, 2010
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
CO
Activity points
1,366
This thread will resolve to be the major thread of which I will include all my LED combinational patterns that I am having difficulty with. My next pattern is to have the LEDs shift into each other; something like this:

1000000001
0100000010
0010000100
0001001000
0000110000

I have the following code, but it is two seperate while loops that make it so half the LEDs scroll up, and once that's finished, the other half scroll down. I need that to happen simultaneously. Obviously, I need to have a single while loop but I'm not sure how that would work. The code that's after the "//----------[The following code controls the LEDs]----------" is the code that I need to be concerned of:

Code:
//
// WalkingLEDCombo3.C
//

   #include <p18f1320.h>
   #include <delays.h>
   #include <stdlib.h>
   #pragma config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
   #define PAUSE 50

void main()
{
   unsigned int pat;        	 // Variable for TRISA
   unsigned int patstopBits;     // Variable for TRISB
   unsigned int patstop;		 // Variable for TRISB
   OSCCONbits.IRCF0=1; 
   OSCCONbits.IRCF1=1;
   OSCCONbits.IRCF2=1;
   while(!OSCCONbits.IOFS);
   TRISA = 0b11110000;
   TRISB = 0b11000000;
//----------[The following code controls the LEDs]----------
   while(1)
   {
      pat = 0x01;
	  patstop = 0x20;
      while(pat < 0x1F)
      {            
		patstopBits=pat>>4;		// patstopBits is a function of pat.
		LATA = pat;			
		LATB = patstopBits;	    // patstopBits is for TRISB. 
		Delay10KTCYx(PAUSE);
		pat<<=1;				// Shift pat once to the right.
		patstopBits>>=1;
	  }
      patstopBits = 0x20;
      while(patstopBits > 1E0)
      {            
		LATB = patstopBits;	    // patstopBits is for TRISB. 
		Delay10KTCYx(PAUSE);
		patstopBits>>=1;

      }
   
}
}

I have 4 bits going to TRISA and 6 bits going to TRISB, that's what makes this confusing. How do I make this work? Can I get by with just 2 variables instead of 3? I am using the PIC18F1320 with the C18 compiler from MPLAB.

Thanks!
 

Nobody knows? I really need help with this... :cry:

I would appreciate any help!
 

i have only worked with 8051. so its difficult for me to understand your code exactly. however if i were to produce your pattern with 8051 i would have done this way.(i am giving just loop portion here)

while(1)
{
a=128;
b=1; // a and b are variables
for(i=1;i<8;i++)
{
P1=a or b; //P1 is 8 bit port
a=a/2;
b=b*2;
delay();
}
}

i have not tested this code but i feel it should work the way i understand your pattern.
i hope you will get the algo and implement it for pic.
for any querry feel free to ask.
i you found it helpful please click "helped me"
 

himrwt said:
i have only worked with 8051. so its difficult for me to understand your code exactly. however if i were to produce your pattern with 8051 i would have done this way.(i am giving just loop portion here)

while(1)
{
a=128;
b=1; // a and b are variables
for(i=1;i<8;i++)
{
P1=a or b; //P1 is 8 bit port
a=a/2;
b=b*2;
delay();
}
}

i have not tested this code but i feel it should work the way i understand your pattern.
i hope you will get the algo and implement it for pic.
for any querry feel free to ask.
i you found it helpful please click "helped me"

Hmmm...I don't really understand that. :)

Perhaps you could explain what each line of code does? Or maybe you could do a algorithm approach, where you list the order of things that occur.

Thanks!

Added after 10 minutes:

You might be able to help me if I explain things a bit more.

LATA is what's used to activate the I/O, so if I have LATA = 0b00000001; then that's simply 0x01, or the first LED. The same is for LATB, a seperate series of I/O. My problem is that I don't have an even number of I/O (5 for LATA and 5 for LATB to make 10), I have 4 on LATA (because my PIC will only allow that) and 6 for LATB. So let me now explain my code I now have:

Code:
   while(1) 
   { 
      pat = 0x01; 
     patstop = 0x20; 
      while(pat < 0x1F) 
      {            
      patstopBits=pat>>4;      // patstopBits is a function of pat. 
      LATA = pat;          
      LATB = patstopBits;       // patstopBits is for TRISB. 
      Delay10KTCYx(PAUSE); 
      pat<<=1;            // Shift pat once to the right. 
      patstopBits>>=1; 
     } 
      patstopBits = 0x20; 
      while(patstopBits > 1E0) 
      {            
      LATB = patstopBits;       // patstopBits is for TRISB. 
      Delay10KTCYx(PAUSE); 
      patstopBits>>=1; 

      } 
    
} 
}

First of all I declared two variables, one to control LATA (pat) and the other to control LATB (patstopBits). Remember that LATA = 4 bits and LATB = 6 bits.

Outside the first while loop I say that pat = 0x01; which would be my first LED. Then inside the while loop I have LATA = pat, so that it will turn on the first LED (LATA activates the I/O). Before that however I have "patstopBits=pat>>4" because pat only goes up to 4 bits, and I need 5, so I shift 4 so it will go to the next LED. So patstopBits works according to pat.

Outside my next while loop I have pat = 0x20; so that it will now go from the 10th LED and walk down to halfway. My while test makes it so that I only accept 5 bits of LATB, otherwise it would do the full six.

My problem is how do I combine these two while loops together to make it work simultaneously? In other words, I'll have this type of result:

Code:
1000000001
0100000010
0010000100
0001001000
0000110000

What makes this confusing is that LATA is 4 bits, and that I have to use patstopBits for the 5th LED. If I had 5 bits on LATA and 5 bits on LATB, things would be easier, but I can't due to my PIC constraints.

Hope I clarified things! :)
 

assuming your pins to be configured like this: A0 A1 A2 A3 B5 B4 B3 B2 B1 B0



x=0; //bit type variable
z=1; //bit type variable
pat=1; //char type variable. here value of pat first increases then decreases.
while(1)
{
LATA=pat - x*pat;
LATB=pat + 2*x*pat; //later part in both expressions work for x=1 that comes at pat=32 only.

if(z) //value z varies only at pat=32 and pat=1.
{
pat=pat*2; //shift right
}
else
{
pat=pat/2; //shift left
}

if(pat==32)
{
x=1;
z=0; //indicates to shift pat left
}
else if(pat==1)
{
z=1; //indicates pat to shift right
}
else
{
x=0;
}

}

i am still not familiar with pic neither i have ever dealt with your compiler. i use kiel with 8051. but taking cue from how you wrote your code i have tried a bit. it may not be perfect algo for your problem, it may have some syntax errors, but i hope it may help you developing code for your problem.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top