Inaccurate delay for XC8

Discussion in 'Embedded Systems and Microcontrollers' started by Danilo H. Mariano Jr, May 28, 2015.

  1. Danilo H. Mariano Jr

    Thread Starter New Member

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

    Code (Text):
    1.  
    2. #include<pic.h>
    3. #include "PIC16F84A.h"
    4. /*
    5. #include <xc.h>
    6.  
    7. // #pragma config statements should precede project file includes.
    8. // Use project enums instead of #define for ON and OFF.
    9.  
    10. // CONFIG
    11. #pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
    12. #pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
    13. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power-up Timer is disabled)
    14. #pragma config CP = OFF         // Code Protection bit (Code protection disabled)
    15. #define _XTAL_FREQ 4000000*/
    16.  
    17. void delay(){
    18.     for(int i = 0;i<20;i++)
    19.         __delay_ms(50);
    20.         ;
    21. }
    22. void main(){
    23.     TRISB&=~0x01;
    24.     PORTB&=~0x01;
    25.     while(1){
    26.     RB0=~RB0;
    27.     delay();
    28.    
    29.     }
    30.  
    31. }
    32.  
    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: May 28, 2015
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
     
  3. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    I already put the #define _XTAL_FREQ on the #include "PIC16F84A.h", I'm using XC8..
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Then please post that as well.

    What is the value of the oscillator attached to your controller?
     
    Danilo H. Mariano Jr likes this.
  5. Danilo H. Mariano Jr

    Thread Starter New Member

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

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

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    This says you have a 4MHz crystal attached to your PIC, is this the case?
     
  7. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    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..
     
  8. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    On proteus I used 4MHz crystal oscillator
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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)
     
  10. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    Still,, I thank you for the time :) I will try it sooner on actual PIC..to find if its because only of Simulation
     
  11. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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 (Text):
    1. #include <xc.h>
    2.  
    3. // #pragma config statements should precede project file includes.
    4. // Use project enums instead of #define for ON and OFF.
    5.  
    6. // CONFIG
    7. #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
    8. #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
    9. #pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer is disabled)
    10. #pragma config CP = OFF // Code Protection bit (Code protection disabled)
    11. #define _XTAL_FREQ 4000000
    12.  
    13. void delay(){
    14. for(int i = 0;i<20;i++)
    15.     __delay_ms(50);
    16. }
    17. void main(){
    18.     TRISB&=~0x01;  // not recommended by uCHIP
    19.     PORTB&=~0x01;
    20.     while(1){
    21.     RB0=~RB0;
    22.     delay();
    23.     }
    24. }
    Good luck.
     
    ErnieM likes this.
  12. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    can I see your edited code? Thanks sir
     
  13. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    but sir when I use __delay_ms(250); it blinks on and off for 1 second, seems like that 250ms is one second
     
  14. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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)?

    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: May 28, 2015
  15. Danilo H. Mariano Jr

    Thread Starter New Member

    May 23, 2015
    10
    0
    yes sir, thats what i mean..
     
  16. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Which one? The code does the former - a 2sec cycle.
     
Loading...