Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 23rd October 2008, 04:07 AM   #1
Default Led flash in 'c'

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;

}
poppy2008 is offline  
Old 23rd October 2008, 04:27 AM   #2
Default

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.
Pommie is online now  
Old 23rd October 2008, 07:20 AM   #3
Default L

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

Quote:
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)
poppy2008 is offline  
Old 23rd October 2008, 08:30 AM   #4
Default

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.
Pommie is online now  
Old 23rd October 2008, 09:58 AM   #5
Default

Quote:
If Mask is 16 then (Mask!=0) is true whereas (Mask==1) is false.
sorry i could'nt understand this line,
poppy2008 is offline  
Old 23rd October 2008, 10:40 AM   #6
Default

ok pommie, after simulation i understand that part.
thanks a lot.

reeta.
poppy2008 is offline  
Reply

Tags
flash, led

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Flash and EEPROM. lord loh. General Electronics Chat 8 27th August 2006 08:03 PM
Flash & EEPROM Electrix General Electronics Chat 8 17th September 2005 03:00 AM
usb flash pen sbartley General Electronics Chat 0 20th February 2005 08:05 PM
flash uC louislu Micro Controllers 5 9th July 2004 06:30 AM
Flash a LED twice letsrelaythat Electronic Projects Design/Ideas/Reviews 11 20th June 2004 05:19 AM



All times are GMT. The time now is 04:40 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker