Inaccurate delay for XC8

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
I'm new to mplab and xc8.. my codes works fine except for the delay, here are my codes..

Code:
#include<pic.h>
#include "PIC16F84A.h"
/*
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection disabled)
#define _XTAL_FREQ 4000000*/

void delay(){
    for(int i = 0;i<20;i++)
        __delay_ms(50);
        ;
}
void main(){
    TRISB&=~0x01;
    PORTB&=~0x01;
    while(1){
    RB0=~RB0;
    delay();
    
    }
  
}
i want to blink an led for 1 sec but it blinks greater than the delay i want..any help would be appreciated,,Thank you

Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

tshuck

Joined Oct 18, 2012
3,534
You need to #define _XTAL_FREQ to the oscillator frequency.

Did you comment out the XC8-specific code and use a different compiler?

Also, please use [ code][ /code] tags when posting code.
 

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
by the way..
under #include "PIC16F84A.h" are the following codes

Code:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
#define _XTAL_FREQ 4000000
 

tshuck

Joined Oct 18, 2012
3,534
by the way..
under #include "PIC16F84A.h" are the following codes

Code:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
#define _XTAL_FREQ 4000000
This says you have a 4MHz crystal attached to your PIC, is this the case?
 

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
Then please post that as well.

What is the value of the oscillator attached to your controller?
well Sir, I'm just simulating it in Proteus.. I tried different tutorials on creating delay, but still they are also inaccurate..
I can't try it yet in actual because I don't have yet my Pickit2..
 

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
well Sir, I'm just simulating it in Proteus.. I tried different tutorials on creating delay, but still they are also inaccurate..
I can't try it yet in actual because I don't have yet my Pickit2..
Then please post that as well.

What is the value of the oscillator attached to your controller?
On proteus I used 4MHz crystal oscillator
 

tshuck

Joined Oct 18, 2012
3,534
Most of the simulators out there do not do a very good job at capturing all the nuances of microcontrollers, so I don't have much faith in your simulation.

You may also need to declare that int i in delay() as a volatile int. This prevents it from being optimized out (which may be the problem)
 

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
Most of the simulators out there do not do a very good job at capturing all the nuances of microcontrollers, so I don't have much faith in your simulation.

You may also need to declare that int i in delay() as a volatile int. This prevents it from being optimized out (which may be the problem)
Still,, I thank you for the time :) I will try it sooner on actual PIC..to find if its because only of Simulation
 

JohnInTX

Joined Jun 26, 2012
4,787
A couple of things.
Your XTAL_FREQ is commented out as are the config bits. For XC8, you don't need (or want) the two includes on top.
I built the resulting code and ran it with the XC8 simulator using the stopwatch. The LED is ON for 1 sec and OFF for 1 sec resulting in a .5Hz frequency (not counting overhead).
You should be sure to initialize ALL of the bits on all of the ports and don't use a bit operation on TRIS, load W with the whole 8 bits then write it to TRIS.

Code:
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
#define _XTAL_FREQ 4000000

void delay(){
for(int i = 0;i<20;i++)
    __delay_ms(50);
}
void main(){
    TRISB&=~0x01;  // not recommended by uCHIP
    PORTB&=~0x01;
    while(1){
    RB0=~RB0;
    delay();
    }
}
Good luck.
 

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
A couple of things.
Your XTAL_FREQ is commented out as are the config bits. For XC8, you don't need (or want) the two includes on top.
I built the resulting code and ran it with the XC8 simulator using the stopwatch. The LED is ON for 1 sec and OFF for 1 sec resulting in a .5Hz frequency (not counting overhead).
You should be sure to initialize ALL of the bits on all of the ports and don't use a bit operation on TRIS, load W with the whole 8 bits then write it to TRIS.

Code:
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
#define _XTAL_FREQ 4000000

void delay(){
for(int i = 0;i<20;i++)
    __delay_ms(50);
}
void main(){
    TRISB&=~0x01;  // not recommended by uCHIP
    PORTB&=~0x01;
    while(1){
    RB0=~RB0;
    delay();
    }
}
Good luck.
can I see your edited code? Thanks sir
 

JohnInTX

Joined Jun 26, 2012
4,787
Just highlight the code in the code window, ctrl-C to copy it then ctrl-V to paste it into MPLABX's source window. I didn't make any changes other than shown.

when I use __delay_ms(250); it blinks on and off for 1 second, seems like that 250ms is one second
It shouldn't. When you say 'on and off for 1 second' does that mean on for 1 second and off for 1 second (.5Hz) or on and off each second (1Hz)?

In MPLABX:
Paste the code to your main .c file exactly as you had it with my edits - (post #11)
In Project->Properties, select Simulator under Hardware Tools. Click OK.
In the source code, click in the line numbers on the left on the line with RB0 = ~RB0 to set a breakpoint - a pink box will replace the line number. (You may have to build the code first..)
In Window->Debugging, open the Stopwatch window. In that window, mouse over the icon on the left, second from the top and set it to 'Reset on Run'.
On the main toolbar, click Debug Main Project. It should build with no errors.
Click RUN (green arrow) if its not running already - it should run and stop at the breakpoint. Click RUN again. It will stop again.
Inspect the Stopwatch window - it will show you the cycle count and time between breakpoints - cleverly chosen in this case to be the LED output.
Mine says 1.000368 sec ON time and the same for OFF time.
If Proteus says differently, well... fix that. I don't know much about Proteus but I will say that if you put this code on a 4MHz F84, the LED will blink at a .5Hz rate i.e. 1 sec on then 1 sec off.

Nice.

EDIT: Click on Variables in the lower window - RB0 should be shown. It will toggle between 1 and 0 each time you click RUN (with the breakpoint set).
On Proteus, is the oscillator specified in oscillator frequency or the instruction freq. which is 1/4 of that?
 
Last edited:

Thread Starter

Danilo H. Mariano Jr

Joined May 23, 2015
10
Just highlight the code in the code window, ctrl-C to copy it then ctrl-V to paste it into MPLABX's source window. I didn't make any changes other than shown.

It shouldn't. When you say 'on and off for 1 second' does that mean on for 1 second and off for 1 second (.5Hz) or on and off each second (1Hz)?
yes sir, thats what i mean..
 
Top