Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F628A not working for LED blinking

Status
Not open for further replies.

bj2010

Newbie level 6
Joined
Jul 18, 2010
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,376
Hi,
I am using PIC (16F628a)microcontroller for the first time. I was trying to make an LED blink. The C programme which I was using is pasted below. The LED is connected to pin 6(RB0) . After programming the chip the LED is glowing, but its not blinking. I tried changing the delay, but no difference.
I am using MPLAB IDE and Hi-tech compiler. when tried using the simulator (MPLAB SIM)available in MPLAB it seems like once it enters the delay function the control is lost . In the for loop,when the number 10000 is replaced with 100 I can see that the for loop is executed only twice and the control (PC)returned to the main function, and in the simulator logic trace I can see a square wave.

Pls help !!!

Regards
Bipin

#include <pic.h>
#include <HTC.h>
__CONFIG(0x3FF0);

void delay(void);

void main(void) {
TRISB = 0x00;
while (1) {
PORTB = 0b00000001;
delay();
PORTB = 0b00000000;
delay();
}
}

void delay(void) {
unsigned int i;
for (i=0; i<10000; i ++)
;
}
 

try this

#include <pic.h>
#include <HTC.h>
__CONFIG(0x3FF0);

void delay(void);

void main(void) {
TRISB = 0x00;
while (1) {
PORTB = 0b00000001;
delay(500);
PORTB = 0b00000000;
delay(500);
}
}

void delay(void) {
unsigned int i;
for (i=0; i<10000; i ++)
;
}
 

Increase the delay value.
--
Amr
 

lockman_akim said:
try this

#include <pic.h>
#include <HTC.h>
__CONFIG(0x3FF0);

void delay(void);

void main(void) {
TRISB = 0x00;
while (1) {
PORTB = 0b00000001;
delay(500);
PORTB = 0b00000000;
delay(500);
}
}

void delay(void) {
unsigned int i;
for (i=0; i<10000; i ++)
;
}

That will not compile. You are passing a value to 'delay' which is defined as delay(void) and the function doesn't use the value anyway.

As suggested, increase the delay, unless you have an oscilloscope to look at the pin.

Keith
 

As mentioned by keith1200rs, that piece of code is not compiling, just because it not taking any arguments.
Increasing the delay, as mentioned in my previous mail, even if i increase the delay eg:(for (i=0; i<100000; i ++)) its crashing(not able to find where the Program counter is ) if i change the code to for (i=0; i<100; i ++) then the for loop is exected only twice and the program counter is returnig to main.
I am not sure whether it is a compiler issue.
Can anyone suggest any other free compiler and the procedure on how to compile it?

Thanks for all your reply.
Regards
Bipin
 

Is the watchdog setting ON? If yes then kick the dog away at the main loop.

-ichan
 

You cannot increase the counter to 100000 because that is beyond the limit of int. Using two counters to execute the delay loop multiple times will work, but you must use another variable name for the second loop - not 'i'. That is why it only ran twice.

I am sure the compiler is fine.

Keith
 

keith1200rs,
Thanks for your continuing support.
I tried increasing the delay. and when runinng the MPLAB SIM I am able to see the bit change after putting necessary breakpoints. But in the simulator logic analiyser its allways high. Even after programming the chip, the LED is allways glowing, not blinking. (New C code is pasted below)
One thing i observed is that after a delay call when i am changing the bit (Port value) ,its not actually getting changed.

Ichan,
The watchdog timer bit is Off, [ __CONFIG(0x3FF0);]
Regards
Bipin

#include <pic.h>
#include <HTC.h>
__CONFIG(0x3FF0);

void delay(void);

void main(void) {
TRISB = 0x00;
while (1)
{
PORTB = 0b00000001;
delay();
delay();

PORTB = 0b00000000;
delay();
delay();
}
}

void delay(void) {
unsigned int i,j,k;
for (i=0; i<100; i++)
{
for (j=0; j<1000; j++)
k=j; //just to waste time
}
}
 

Your code has no problem on mine. At 4MHz clock the led blinking at about 4.4 second period. Tried it on MPLAB SIM and Proteus.

-ichan
 

Hi Ichan,
I really thank you for testing my code.
Could you pls do me a favour, can you please upload the corresponding .hex file into this forum?
I think the site won't allow to upload .hex format so u can zip/rar it and send it.
Once again thank you for your support and sorry for the inconvenience.
Regrads
Bipin
 

Here it is, mplab project + proteus dsn.

No need to sorry... :)

-ichan
 

I was including a wrong proteus file, here is the right one.

-ichan
 

    bj2010

    Points: 2
    Helpful Answer Positive Rating
Hi Ichan,
Sorry for the late update, I was out of station for the last few days.
I tested your .hex file, its working fine and I can see the LED blinking.
But the problem is that its not consistant, when I switched OFF and switched ON again its not working. again I tried for some 20 to 30 times then again it worked.
By seeing this behaviour I flahed my .hex file and observed the same thing, its not consistant.
I was little suspecious about my power supply, so I changed it to a pure DC (from a battery) but the result was the same.
Can I suspect the chip? eventhough I am using a breadboard there is no loose conection in the circuit.
I am using the CONFIG register value as 3FF0 and also I have connected Vdd to MCLR through a 4.7K resistor. Pls let me know if this is wrong.
Regards
Bipin
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top