• AVR Freaks

Hot!PIC16F84A LED flashing is slower than it is programmed

Author
asadulhuq
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2020/07/25 02:54:34
  • Location: 0
  • Status: offline
2020/07/31 01:20:44 (permalink)
0

PIC16F84A LED flashing is slower than it is programmed

I am old but new to PIC programming. My question may be childish but please answer!
My Code:
///////////////////////////////////////////////////////////
#define _XTAL_FREQ 20000000 //20 MHz crystal
// PIC16F84A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
int main()
{
TRISB = 0;
PORTB = 0;
PORTA = 0;

while(1)
{
RB1 = 1;
__delay_ms(200); //Pin 7
RB1 = 0;
__delay_ms(200);
}
return 0;
}
//////////////////////////////////
Please note I used MPLAB X v5.35 to compile. I inserted the HEX file into Proteus 8 Pro diagram. The LED at Pin-7 flashes but very slowly (It should be 200ms as in the program, but proteus shows may be 3 seconds or so).
 
My question is why LED is so slow?
Thanks.
AH.
#1

14 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/07/31 12:07:10 (permalink)
    +2 (2)
    Come back when this happens to a real device, not a simulation?
    #2
    Antipodean
    Super Member
    • Total Posts : 1876
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/07/31 12:10:24 (permalink)
    +4 (4)
    asadulhuq
    My question is why LED is so slow?

     
    because it is running in a simulator.

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #3
    PStechPaul
    Super Member
    • Total Posts : 2814
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/07/31 14:01:43 (permalink)
    -1 (1)
    I'm not familiar with Proteus, but using the MPLABX software simulator you can use the "stopwatch" to determine the time between LED ON and OFF states. Proteus should have something similar (like an oscilloscope).

     
    #4
    ric
    Super Member
    • Total Posts : 28011
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/07/31 15:53:01 (permalink)
    +1 (1)
    The __XTAL_FREQ define tells the compiler how fast a real device would be running, so it knows how many delay loops to implement inside the __delay_ms() macro.
    It does NOT tell Proteus anything, Proteus has no idea how fast your PIC is running.
    As others have hinted, don't expect to get real time performance from a simulator.
     
    Also, not your immediate problem, but in XC8
    int main()

    should be
    void main(void)


    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/01 12:31:30 (permalink)
    0
    @ric Thanks a lot. You explained issues and things clearly about the slow LED in simulation. Also, I had confusion about int main()/ void main(void), now, you cleared the issue. A am an old man and a teacher. I have lot of time now due to Covid 19, since I stay home almost all time. I want to utilise the time by learning PIC programming. I want to make a universal timer with LCD display. I want to choose 16PIC628A. My target is that my timer will start count down once I apply power to the system. I intend to use the timer to start a geyser (water heater). I shall preset a time such as 20 minutes (to 45 minutes in winter) into the timer. Additionally, I shall connect a Sononoff device to measure power consumed by the heater and to turn on the heater remotely when needed.
    Thanks again to you all who answered my question. AH.
    post edited by asadulhuq - 2020/08/02 04:40:20
    #6
    upand_at_them
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/01 13:43:33 (permalink)
    +1 (1)
    FYI, there are PIC tutorials using the PIC16F628 at: http://www.winpicprog.co.uk/pic_tutorial.htm
    #7
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 04:38:56 (permalink)
    0
    @PStechPaul Thanks for your answer.
    #8
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 04:42:46 (permalink)
    0
    upand_at_them
    FYI, there are PIC tutorials using the PIC16F628 at: http://www.winpicprog.co.uk/pic_tutorial.htm


    Thanks.
    #9
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 04:50:11 (permalink)
    0
    asadulhuq
    I am old but new to PIC programming. My question may be childish but please answer!My Code:///////////////////////////////////////////////////////////#define _XTAL_FREQ 20000000 //20 MHz crystal
    // PIC16F84A Configuration Bit Settings
    // 'C' source line config statements
    // CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
    #pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)
    #pragma config CP = OFF // Code Protection bit (Code protection disabled)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.#include <xc.h>
    int main()
    {
    TRISB = 0;
    PORTB = 0;
    PORTA = 0;

    while(1)
    {
    RB1 = 1;
    __delay_ms(200); //Pin 7
    RB1 = 0;
    __delay_ms(200);
    }
    return 0;
    }//////////////////////////////////Please note I used MPLAB X v5.35 to compile. I inserted the HEX file into Proteus 8 Pro diagram. The LED at Pin-7 flashes but very slowly (It should be 200ms as in the program, but proteus shows may be 3 seconds or so). My question is why LED is so slow?Thanks.AH.


    Can one please tell me what is the meaning of this comment "// Use project enums instead of #define for ON and OFF.," in the above MPLAB X code?
    #10
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 04:51:19 (permalink)
    0
    asadulhuq
    I am old but new to PIC programming. My question may be childish but please answer!My Code:///////////////////////////////////////////////////////////#define _XTAL_FREQ 20000000 //20 MHz crystal
    // PIC16F84A Configuration Bit Settings
    // 'C' source line config statements
    // CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
    #pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)
    #pragma config CP = OFF // Code Protection bit (Code protection disabled)
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.#include <xc.h>
    int main()
    {
    TRISB = 0;
    PORTB = 0;
    PORTA = 0;

    while(1)
    {
    RB1 = 1;
    __delay_ms(200); //Pin 7
    RB1 = 0;
    __delay_ms(200);
    }
    return 0;
    }//////////////////////////////////Please note I used MPLAB X v5.35 to compile. I inserted the HEX file into Proteus 8 Pro diagram. The LED at Pin-7 flashes but very slowly (It should be 200ms as in the program, but proteus shows may be 3 seconds or so). My question is why LED is so slow?Thanks.AH.


    >> Can one please tell me what is the meaning of this comment "// Use project enums instead of #define for ON and OFF.," in the above MPLAB X code?
    #11
    ric
    Super Member
    • Total Posts : 28011
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 05:28:07 (permalink)
    +1 (1)
    It is saying that you must NOT put any #defines for "ON" or "OFF" before the lines that do all the config settings.
    That's why any "#includes", including for xc.h, must come after the CONFIG settings.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    asadulhuq
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2020/07/25 02:54:34
    • Location: 0
    • Status: offline
    Re: PIC16F84A LED flashing is slower than it is programmed 2020/08/02 06:21:43 (permalink)
    +1 (1)
    ric
    It is saying that you must NOT put any #defines for "ON" or "OFF" before the lines that do all the config settings.That's why any "#includes", including for xc.h, must come after the CONFIG settings. 

    << Thanks. I am learning.
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5