Maker Pro
Maker Pro

C programming on PIC16F877A

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
Hey, can anyone tell me what is the exact code for this application :

I have 3 LED, I want the 3 LED to be ON on seperate time making it like a kind of flow

My code is this :
#include <pic.h>

__CONFIG ( 0x3F32 );

#define LED_1 RC3
#define LED_2 RC2
#define LED_3 RA0

void init(void);
void delay(unsigned long data);

void main(void)
{
init();


for(int i=0;i<10;i++)
{
LED_1=1;
delay(100000);
LED_2=0;
delay(100000);
LED_3=0;
delay(100000);
LED_1=0;
delay(100000);
LED_2=1;
delay(100000);
LED_3=0;
delay(100000);
LED_1=0;
delay(100000);
LED_2=0;
delay(100000);
LED_3=1;

}
}



void init()
{
TRISA=0b00000000;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;

}

void delay(unsigned long data) //delay function, the delay time
{
for( ;data>0;data-=1); //depend on the given value
}

It does not work as predicted :(

Help @@@:confused:
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I'd suggest that what you want is something like this

for(int i=0;i<10;i++)
{
LED_1=1;
LED_2=0;
LED_3=0;
delay(100000);
LED_1=0;
LED_2=1;
LED_3=0;
delay(100000);
LED_1=0;
LED_2=0;
LED_3=1;
delay(100000);
}

But that's just assuming you're wanting what I think you're wanting.
 

jackorocko

Apr 4, 2010
1,284
Joined
Apr 4, 2010
Messages
1,284
It does not work as predicted

what are you predicting, what is your outcome? I agree with steve if I assume like him you want led_1 on then off then led_2 on then off etc... Do all three of the LED's light, or is that your problem?
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
Code:
#include <pic.h>

__CONFIG ( 0x3F32 );

#define LED_1 RC3
#define LED_2 RC2
#define LED_3 RA0

void init(void);
void delay(unsigned long data);

void main(void)
{
init();


for(int i=0;i<3;i++)
{
LED_1=1;
LED_2=0;
delay(100000);
LED_1=0;
LED_2=1;
delay(100000);
LED_1=0;
LED_2=0;
LED_3=1;
delay(100000);
LED_1=0;
LED_2=1;
LED_3=0;
delay(100000);
}
for(int j=0;j<3;j++)
(
LED_1 =1;
LED_2=1;
LED_3=1;
delay(100000);
}




void init()
{
TRISA=0b00000000;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;

}

void delay(unsigned long data) //delay function, the delay time
{
for( ;data>0;data-=1); //depend on the given value
}
This is my code, and this is how it operate..Weird working based on code right? anyone can tell me why ?

 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
It looks to me like its operating exactly the way you've coded it.

What did you expect to happen?
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
I expect it to light up one by one from top to bottom for 3 times then all light up..wrong ?
Mind explaining while teaching ?

Thanks
 

jackorocko

Apr 4, 2010
1,284
Joined
Apr 4, 2010
Messages
1,284
From the video it is working just like you coded. The reason 1 and 3 come on after all 3 are on is clear. You never set it's state here.

Code:
LED_1=1;
LED_2=0;
delay(100000);
LED_1=0;
LED_2=1;
delay(100000);

Of course this is only gonna happen after your main{} function runs the first time through it. Because the initial start-up state is 0, I would set the initial state of the LED's to zero in the init{} as well.
 
Last edited:

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
forgot to mention , the first LED on the breadboard is LED_2, the second LED is LED_1 and the last LED is LED_3
 

jackorocko

Apr 4, 2010
1,284
Joined
Apr 4, 2010
Messages
1,284
forgot to mention , the first LED on the breadboard is LED_2, the second LED is LED_1 and the last LED is LED_3

Code:
#define LED_1 RC3
#define LED_2 RC2
#define LED_3 RA0

Then switch

Code:
#define LED_1 RC2
#define LED_2 RC3
#define LED_3 RA0
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I expect it to light up one by one from top to bottom for 3 times then all light up..wrong ?
Mind explaining while teaching ?

Thanks

OK, note that you're setting the LEDs back to 0 all the time.

You probably want to do something like this:

led1 = 0
led2 = 0
led3 = 0
for (int i = 0; i < 3; i++)
{
// turn off all the LEDs and wait
led1 = 0;
led2 = 0;
led3 = 0 ;
delay(100000);

// turn on the first LED and wait again
led1 = 1;
delay(100000);

// now the second one
led2 = 1;
delay(10000);

// and finally the last one (not forgetting the delay)
led3 = 1;
delay(10000);
}

// all LEDs left ON
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
OK, note that you're setting the LEDs back to 0 all the time.

You probably want to do something like this:

led1 = 0
led2 = 0
led3 = 0
for (int i = 0; i < 3; i++)
{
// turn off all the LEDs and wait
led1 = 0;
led2 = 0;
led3 = 0 ;
delay(100000);

// turn on the first LED and wait again
led1 = 1;
delay(100000);

// now the second one
led2 = 1;
delay(10000);

// and finally the last one (not forgetting the delay)
led3 = 1;
delay(10000);
}

// all LEDs left ON

delay(100000);
Is this means that it will delay for 100000ms ? But it seems not to :(
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
uS perhaps...?
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
How is it possible for me to connect a LDR to the PIC16F877A so that when it is in sunlight, it will activated the light or activate another circuit ?

How will be the connection?

Thank you
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Voltage divider and ADC input.

resistor connected to V+ then to LDR, then to ground. The junction of these 2 devices will move up and down between gnd and Vcc depending on light level. This point should be connected to the ADC input
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
What is so unique about pin RA4 ? why other pins I can light up the LED but not for RA4 pins?
 

jackorocko

Apr 4, 2010
1,284
Joined
Apr 4, 2010
Messages
1,284
What is so unique about pin RA4 ? why other pins I can light up the LED but not for RA4 pins?

You probably need to disable one of the other functions on that pin to use it as an I/O

From the datasheet
Pin RA4 is multiplexed with the Timer0 module clock
input to become the RA4/T0CKI pin. The RA4/T0CKI
pin is a Schmitt Trigger input and an open-drain output.
All other PORTA pins have TTL input levels and full
CMOS output drivers.
Other PORTA pins are multiplexed with analog inputs
and the analog VREF input for both the A/D converters
and the comparators. The operation of each pin is
selected by clearing/setting the appropriate control bits
in the ADCON1 and/or CMCON registers

So I would try first
Code:
CMCON = 0b0000111

If that don't work, look at OPTION_REG<5>
 
Last edited:

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
Hey Vic,

You seem to be progressing rapidly. Congratulations!

Bob
 

jackorocko

Apr 4, 2010
1,284
Joined
Apr 4, 2010
Messages
1,284
vick, I was doing some digging today and found out that you can simulate C code in proteus isis. It actually works out pretty well.

Have a look at this tutorial here. http://smainj.free.fr/tutorial/
Tutorial1.rar contains the flash files, video you can watch in your browser. 7mb's

I think being able to simulate it on the PC makes it a lot easier to make mistakes without wasting time programming the chip and rewiring the hardware. Thought you might like it, maybe you like your way better.
 
Top