![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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;
}
| |
| |
| | #2 |
|
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);
}
}
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
}
}
Mike. | |
| |
| | #3 | |
|
thank you so much mike,it looks very simple. i have one question Quote:
Code: while(Mask ==1){ //do until bit shifts out the end
| ||
| |
| | #4 |
|
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. | |
| |
| | #5 | |
| Quote:
| ||
| |
| | #6 |
|
ok pommie, after simulation i understand that part. thanks a lot. reeta. | |
| |
|
| 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 |