Hi-Tech C delay problem (PIC16F877)

Status
Not open for further replies.

juvenile86

New Member
Hi, I am new to PIC programming with C language..
My program is simple, which is to set Port B as output, and make a LED connected on any of the Port B pins blink with an interval of 1 second.

-------------------------------------------------------------------------------------------------------------------------------------

#include <htc.h>
__CONFIG (0x3F32);

void delay(unsigned long x);

void main()
{
TRISB = 0x00;
PORTB = 0;

while(1)
{

PORTB = 0xFF;
delay(1000);
PORTB = 0x00;
delay(1000);
}
}

void delay(unsigned long x)
{
for(;x>0;x--);
}
------------------------------------------------------------------------------------------------------------------------------------

I am able to compile and write the HEX code into the PIC microcontroller, but I got output voltage of about 2.45V in all of the port B pins. Anyone have idea what is going wrong here?

I tried to use MikroC built-in delay library, Delay_ms(1000) and everything works fine. So I can be pretty sure there is no problem on my hardware connections. I am using Crystal Oscillator of 20MHz anyway.
 

Even if you use the largest number for a long - 0xFF - your delay is only going to be about 13 milliseconds: 65535 cycles / (5,000,000 cycles/second) = 0.013107seconds (plus any 'compiler overhead' to get into your delay loop.)

Your delay at 1000 cycles / (5000000 cycles/second) = 0.0002 seconds is much too short to see the LED flash.

If you look at the built in delay you used in Mikro, it wants a parameter for the number of milliseconds to delay, not cycles. You need to use a nested for or something else to get a longer delay.
 
Last edited:

Hi BeeBop, thanks for spending your time answering my question in here. I have used a nested loop as you suggested yet I still get output voltage of 2.4~2.5V at all the port B pins..


#include <htc.h>
__CONFIG (0x3F32);


void delay(unsigned long x);


void main()
{
TRISB = 0x00;
while(1)
{
PORTB = 0xFF;
delay(1000);
PORTB = 0x00;
delay(1000);
}
}

void delay(unsigned long x)
{
unsigned long i;
for(i=0;i<5000;i++)
for(;x>0;x--);

}
 
Last edited:
Well I just went and downloaded the manual so I could find out the size of data in HiTech... (don't use this compiler) and I saw this:
why not give that a try?

On HiTech an unsigned short is 16 bits and an unsigned long is 32 bits so the largest number for a long is 0xFFFFFFFF = decimal 4294967296

I just did a quick reply on what looked to be the problem; haven't even looked at your configuration word, and wondering if that is it...

Your second one should delay about 1 second....
 
Last edited:
Just took a look at your configuration word - seems good - no problems there....

I'm wondering if you have to turn off the analog on port A for this to work...
in assembler:
MOVLW 0x06 ; Configure all pins
MOVWF ADCON1 ; as digital inputs

Try:
ADCON1 = 0x06;
TRISA = 0x00;

Like I say, I don't use this compiler and knew I should have left this alone...
 
Last edited:
I tried to make PORT A as digital output
Still gives me the same problem, I see the LED flash in an interval of less than 1 second
I think I should be other value rather than 5000 in the nested loop condition?
 
I tried to make PORT A as digital output
Still gives me the same problem, I see the LED flash in an interval of less than 1 second
I think I should be other value rather than 5000 in the nested loop condition?

I would think it should be longer than one second... (one second plus the time it takes to enter and exit the loops....)

You LED is flashing, though? How much less than 1 second?
 
I changed the 5000 to 50000 and now the LED is flashing at about 1 second..

 
dI quite frequently use the same time delay, declaring and defining it as a function and calling whenever. It works pretty well for low freq stuff.


 
May I know why is it 50000 as I need to explain this to the supervisor as part of my project.
if your oscillator is running at 20,000,000 cycles per second the PC is advancing 5,000,000 times per second:
50,000 x 1,000 = 50,000,000 / 5,000,000 cycles per second = 10 seconds!

does not make sense to me either! The 5000 should have worked, but ...?
 
I would suspect he's running 4MHz. With that arrangement, the overhead in running the for loop accounts for more time than one would imagine.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…