# Simple delay issue with ledblinking for PIC 16F628a

Status
Not open for further replies.

#### DanT29

##### Newbie level 4
Hey guys I'm new PIC microcontrollers and programming them in C. I started many months ago but had to stop since school started and I got busy with other things. But now I'm back to it determined to learn and I really need your help guys. I have a small issue in my program and I was wondering if you guys can help! I have an issue with the delay function...I'm not sure what to use for it. This is a newbie issue and I know you guys can help. I hope I don't need to make my own library for it.

I'm trying to make an led blink this is what I'm using:
MPLAB X
HI-TECH C complier
PIC KIT 2
PIC 16F628a

Here's my code:

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
#include<pic.h>
#include<htc.h>
void main(){
TRISB=0x00;
PORTB=0x00;

while(1){
PORTB=~PORTB;
__delay_ms(1000);
PORTB=~PORTB;
}
}

I tried a lot of functions but none worked. I appreciate the help!

Last edited by a moderator:

#### bigdogguru

1. Only the htc.h header file needs to be included, not the pic.h header file.

2. To utilize the delay macro routines you must specify the system clock frequency by defining _XTAL_FREQ as the clock/oscillator frequency.

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

_delay()
For example if your clock/oscillator frequency is 4MHz then include the following compiler/preprocessor directive:

Code:
#define _XTAL_FREQ 4000000
bigDog

#### DanT29

##### Newbie level 4
Thank you!

Status
Not open for further replies.