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.

Simple delay issue with ledblinking for PIC 16F628a

Status
Not open for further replies.

DanT29

Newbie level 4
Newbie level 4
Joined
Jul 5, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,345
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:

Two comments:

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(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()

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

Code:
#define _XTAL_FREQ 4000000

bigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top