Question about Shifting LEDs in microcontroller

Thread Starter

glenn_boy13

Joined Dec 19, 2012
29
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.

Rich (BB code):
int dataShift = 0x80 //Start from the value leftmost bit
for_loop... from 1 to 8
if(dataShift==0x00) dataShit = 0x80; //reset if data becomes 0
PAOUT = dataShift; 
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.


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
 

John P

Joined Oct 14, 2008
2,025
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?
 

tshuck

Joined Oct 18, 2012
3,534
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...
 

THE_RB

Joined Feb 11, 2008
5,438
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;
Rich (BB code):
int dataShift = 0x80;  // make sure "int" is actually an 8bit variable!

  for(i=0; i<8; i++)
  {
    dataShift = dataShift >> 1;            //shift 1 bit at a time
    if(dataShift==0x00) dataShift = 0x80;  //reset if data becomes 0
    PAOUT = dataShift;
  }
Notice the order of operation is improved. First the data is modified (shifted), then tested and corrected, then displayed.
 

Thread Starter

glenn_boy13

Joined Dec 19, 2012
29
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.



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
 

tshuck

Joined Oct 18, 2012
3,534
@tshuck: see I didn't copy anything.
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:
 

Thread Starter

glenn_boy13

Joined Dec 19, 2012
29
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. :)
 

Thread Starter

glenn_boy13

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

and here is my code:


Rich (BB code):
#include <ez8.h>


void delay(void){
	int x,y;
	for(x=0;x<=500;x++)
		for(y=0;y<=512;y++);
	}
	

void main(){
	int x=0;
	int dataShift = 0x80;
	
	PADD = 0x00;
	PAAF = 0x00;
	PAOC = 0x00;
	PAHDE = 0xFF;
	
	
	while(1){
		
		
		/*LOOP1*/
		PAOUT = ~0x80;	delay();
		PAOUT = ~0x40;	delay();
		PAOUT = ~0x20;	delay();
		PAOUT = ~0x10;	delay();
		PAOUT = ~0x08;	delay();
		PAOUT = ~0x04;	delay();
		PAOUT = ~0x02;	delay();
		PAOUT = ~0x01;	delay();
		/*end of loop1*/
		
		/*LOOP2*/	
		PAOUT = ~0x81;	delay();
		PAOUT = ~0x41;	delay();
		PAOUT = ~0x21;	delay();
		PAOUT = ~0x11;	delay();
		PAOUT = ~0x09;	delay();
		PAOUT = ~0x05;	delay();
		PAOUT = ~0x03;	delay();	
		/*end of loop2*/
			
		/*LOOP3*/	
		PAOUT = ~0x83;	delay();
		PAOUT = ~0x43;	delay();
		PAOUT = ~0x23;	delay();
		PAOUT = ~0x13;	delay();
		PAOUT = ~0x0B;	delay();
		PAOUT = ~0x07;	delay();	
		/*end of loop3*/	
		
		/*LOOP4*/
		PAOUT = ~0x87;	delay();
		PAOUT = ~0x47;	delay();
		PAOUT = ~0x27;	delay();
		PAOUT = ~0x17;	delay();
		PAOUT = ~0x0f;	delay();
		/*end of loop4*/
		
		/*LOOP5*/
		PAOUT = ~0x8f;	delay();
		PAOUT = ~0x4f;	delay();
		PAOUT = ~0x2f;	delay();
		PAOUT = ~0x1f;	delay();
		/*end of loop5*/
		
		/*LOOP6*/
		PAOUT = ~0x9f;	delay();
		PAOUT = ~0x5f;	delay();
		PAOUT = ~0x3f;	delay();
		/*end of loop6*/
		
		/*LOOP7*/
		PAOUT = ~0xbf;	delay();
		PAOUT = ~0x7f;	delay();
		/*end of loop7*/
		
		/*LOOP8*/
		PAOUT = ~0xff;	delay();
		/*end of loop8*/
		
		}
	
	
	}
 

djsfantasi

Joined Apr 11, 2010
9,156
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...
 

absf

Joined Dec 29, 2010
1,968
HERE: http://youtu.be/VzNwW-SKZ24
please see this sir.

and here is my code:


Rich (BB code):
#include <ez8.h>


void delay(void){
	int x,y;
	for(x=0;x<=500;x++)
		for(y=0;y<=512;y++);
	}
	

void main(){
	int x=0;
	int dataShift = 0x80;
	
	PADD = 0x00;
	PAAF = 0x00;
	PAOC = 0x00;
	PAHDE = 0xFF;
	
	
	while(1){
		
		
		/*LOOP1*/
		PAOUT = ~0x80;	delay();
		PAOUT = ~0x40;	delay();
		PAOUT = ~0x20;	delay();
		PAOUT = ~0x10;	delay();
		PAOUT = ~0x08;	delay();
		PAOUT = ~0x04;	delay();
		PAOUT = ~0x02;	delay();
		PAOUT = ~0x01;	delay();
		/*end of loop1*/
		
		/*LOOP2*/	
		PAOUT = ~0x81;	delay();
		PAOUT = ~0x41;	delay();
		PAOUT = ~0x21;	delay();
		PAOUT = ~0x11;	delay();
		PAOUT = ~0x09;	delay();
		PAOUT = ~0x05;	delay();
		PAOUT = ~0x03;	delay();	
		/*end of loop2*/
			
		/*LOOP3*/	
		PAOUT = ~0x83;	delay();
		PAOUT = ~0x43;	delay();
		PAOUT = ~0x23;	delay();
		PAOUT = ~0x13;	delay();
		PAOUT = ~0x0B;	delay();
		PAOUT = ~0x07;	delay();	
		/*end of loop3*/	
		
		/*LOOP4*/
		PAOUT = ~0x87;	delay();
		PAOUT = ~0x47;	delay();
		PAOUT = ~0x27;	delay();
		PAOUT = ~0x17;	delay();
		PAOUT = ~0x0f;	delay();
		/*end of loop4*/
		
		/*LOOP5*/
		PAOUT = ~0x8f;	delay();
		PAOUT = ~0x4f;	delay();
		PAOUT = ~0x2f;	delay();
		PAOUT = ~0x1f;	delay();
		/*end of loop5*/
		
		/*LOOP6*/
		PAOUT = ~0x9f;	delay();
		PAOUT = ~0x5f;	delay();
		PAOUT = ~0x3f;	delay();
		/*end of loop6*/
		
		/*LOOP7*/
		PAOUT = ~0xbf;	delay();
		PAOUT = ~0x7f;	delay();
		/*end of loop7*/
		
		/*LOOP8*/
		PAOUT = ~0xff;	delay();
		/*end of loop8*/
		
		}
	
	
	}

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.

Rich (BB code):
#include <htc.h>
//#include <math.h>
#define _XTAL_FREQ 4000000

void delay(void)
	{
	unsigned int x,y;
	for(x=0;x<=25;x++)
		for(y=0;y<=512;y++);
	}
	
void main()
{
	unsigned int i, j, k ;
	unsigned int dataShift = 0x80;

	const char m[ 9 ] = 
	{	0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00
	};
	// pow( 2 , i ) -1;   
	PORTB = 0X00;					//PAOUT = 0x00;
	TRISB = 0X00;					//PAAF = 0x00;
									//PAOC = 0x00;
									//PAHDE = 0xFF;
	
	while (1)
{
	for (j=8; j>0; j--)
	{
	dataShift=0x80;
	for (i=0; i<j ; i++)
	{
		k = m[ j ];
		PORTB = ~(dataShift | k);	delay();	//dataShift [OR] k
		dataShift = dataShift >> 1;
	}	
	}
		
}
}
Allen
 
Top