Led flash in 'c'

Status
Not open for further replies.

poppy2008

New Member
Hi
i am new to c,and i know little assembly language

i want to flash the LED one by one from right to left and left to write using 'c ' with some delay.
i am using 'Explorer 16 development board'
in assembly we use rrc,rlc instructions ,the codes are very less.
but i am writing in c ,the program is big ,but this program is working.
my question is how to write this program in simple way?
pls help me
Code:
/* LED's ON one by one from left to right and right to left with 0.25 sec delay in PORTA*/

#include <p24FJ128GA010.h>

#define DELAY 15625 //31250,0.5 sec //62500,1 sec delay//


main ()
{
	//Code goes here 
    AD1PCFG = 0xFFFF; 		// all pins are digital
	TRISA = 0xFF00;  	    // bit 0-7 output,bit 8-15 input
	T1CON = 0x8030;  	 	// timer1 on,input clock prescale 256,internal clock(FOSC/2)
     

	LATAbits.LATA0 = 1;
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA0 = 0;
	LATAbits.LATA1 = 1;
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA1 = 0;
	LATAbits.LATA2 = 1;
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA2 = 0;
	LATAbits.LATA3 = 1;
   
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA3 = 0;
	LATAbits.LATA4= 1;
    
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA4 = 0;
	LATAbits.LATA5= 1;

	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA5 = 0;
	LATAbits.LATA6= 1;

	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA6 = 0;
	LATAbits.LATA7= 1;

//	TMR1 = 0;
//	while(TMR1< DELAY)
  //		{
		
//	    }
//	LATAbits.LATA7 = 0;
	        
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA7 = 0;
	LATAbits.LATA6 = 1;
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA6 = 0;
	LATAbits.LATA5 = 1;
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA5 = 0;
	LATAbits.LATA4 = 1;
   
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA4 = 0;
	LATAbits.LATA3= 1;
    
	
	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA3 = 0;
	LATAbits.LATA2= 1;

	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA2 = 0;
	LATAbits.LATA1= 1;

	TMR1 = 0;
	while(TMR1< DELAY)
  		{
		
	    }
	LATAbits.LATA1 = 0;
	LATAbits.LATA0= 1;

}
 
The way I would do this in a loop is,
Code:
main ()
{
unsigned char Mask;
    //Code goes here 
    AD1PCFG = 0xFFFF;         // all pins are digital
    TRISA = 0xFF00;          // bit 0-7 output,bit 8-15 input
    T1CON = 0x8030;           // timer1 on,input clock prescale 256,internal clock(FOSC/2)
 
    while(1){	                //loop forever
        for(Mask=1;Mask!=0;Mask<<=1){
            LATA=Mask;
            TMR1 = 0;
            while(TMR1< DELAY);
        }
    }
Note that Mask will become zero after being shifted left from 128.

The above is not the conventional way to do this. An easier to understand piece of code is,
Code:
    while(1){	                //loop forever
        Mask=1;			//start with bit 0
        while(Mask!=0){		//do until bit shifts out the end
            LATA=Mask;		//output it
            TMR1 = 0;		//reset timer
            while(TMR1< DELAY);	//do delay
            Mask=Mask<<1;	//shift mask left 1 place
        }
    }

HTH.

Mike.
 
L

thank you so much mike,it looks very simple. i have one question

while(Mask!=0){ //do until bit shifts out the end

if i write

Code:
while(Mask ==1){		//do until bit shifts out the end

shifting is not working,why? (!=0 ,==1 both are same or different meaning)
 
Mask is shifted left each time around the loop and so it goes 1,2,4,8,16,32,64,128,0. If Mask is 16 then (Mask!=0) is true whereas (Mask==1) is false.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…