Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.
 
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:
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.

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.

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:
__DELAY_MS, __DELAY_US
Synopsis
__delay_ms(x) // request a delay in milliseconds
__delay_us(x) // request a delay in microseconds
Description
As it is often more convenient request a delay in time-based terms rather than in cycle
counts, the macros __delay_ms(x) and __delay_us(x) are provided. These macros
simply wrap around _delay(n) and convert the time based request into instruction
cycles based on the system frequency. In order to achieve this, these macros require
the prior definition of preprocessor symbol _XTAL_FREQ. This symbol should be
defined as the oscillator frequency (in Hertz) used by the system.
An error will result if these macros are used without defining oscillator frequency
symbol or if the delay period requested is too large.
See also
_delay()
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... :p
 
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.. :)

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

void delay(unsigned long x);


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

void delay(unsigned long x)
{
unsigned long i;
for(i=0;i<50000;i++)
for(;x>0;x--);
}
 
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.


#include <htc.h>
__CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);

#define eeney_on RC0
#define meeney_on RC1
#define miney_on RC2
#define moe_on RC3
void time_out(void)
{
unsigned short a;
for (a=0; a<=7000; a++){
}
}
void init_(void)
{
TRISC = 0;
PORTC = 0;
ANSEL = 0;
}

void main()
{
init_();

while (1)
{

miney_on = 1^miney_on;
time_out();
meeney_on = 1^meeney_on;
time_out();
eeney_on = 1^eeney_on;
time_out();
moe_on = 1^moe_on;
time_out();

}
}
 
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top