Hot!PIC16F1619 Curiosity - __delay_ms() no effect

Page: 12 > Showing page 1 of 2
Author
ConfusedCheese
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2016/07/11 09:03:16
  • Location: 0
  • Status: offline
2016/07/24 08:09:55 (permalink)
0

PIC16F1619 Curiosity - __delay_ms() no effect

Hi there, I am newbie to C programming on a pic microcontroller. I am slowly working my way through the various tutorials and literature available to try and apply my knowledge.
 
I am currently trying to  blink an LED with the pic16f1619 using the curiosity board using MPLAB IDE X, specifically pin A5.

My code is as follows:
 
#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
 
#define _XTAL_FREQ 32000000 //internal oscillator of pic16f1619 is 32MHz
 
// Above is header files - xc is for compiler, stdlib is for general function and stdio is for I/O
// Below is the bit configuration - this is taken from the curiosity mcc generated bit configuration
// CONFIG1
 
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switch Over (Internal External Switch Over mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCD = OFF        // Zero Cross Detect Disable Bit (ZCD disable.  ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PLLEN = ON       // PLL Enable Bit (4x PLL is always enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)
// CONFIG3
#pragma config WDTCPS = WDTCPS1F// WDT Period Select (Software Control (WDTPS))
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config WDTCWS = WDTCWSSW// WDT Window Select (Software WDT window size control (WDTWS bits))
#pragma config WDTCCS = SWC     // WDT Input Clock Selector (Software control, controlled by WDTCS bits)
 
void main(void) {
            TRISAbits.TRISA5= 0; // set pin as output
            LATAbits.LATA5 = 1;  //set pin as high
           
    while(1)
    {
        LATAbits.LATA5 = 1;  //set pin as high
__delay_ms(1000); //delay of 1second
 LATAbits.LATA5 = 0;  //set pin as high
}
           
}
// END
 
At the moment there is no effect on the output of pin A5. I have tried changing the function of the pin with the different registers. Any help would be greatly appreciated!
I am sware of this code - LATAbits.LATA5=~ LATAbits.LATA5; // Toggle Bit of Port A5
 
Any help would be greatly appreciated as I am really stuck!
 
Regards
#1

21 Replies Related Threads

    NorthGuy
    Super Member
    • Total Posts : 5020
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/24 08:41:38 (permalink)
    0
    ConfusedCheese
    At the moment there is no effect on the output of pin A5.



    You mean it's always on? You probably need to give it some off time (delay after setting it low). If it goes off for 1us, you won't be able to see it on the LED.
    #2
    ConfusedCheese
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2016/07/11 09:03:16
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/24 08:45:10 (permalink)
    0
    Does the following code at the end of the excerpt:      
     
      LATAbits.LATA5 = 1;  //set pin as high
    __delay_ms(1000); //delay of 1second
     LATAbits.LATA5 = 0;  //set pin as high
     
    Not change the output of A5 from high to low with a delay of 1000ms, i.e. 1second?
    #3
    aschen0866
    Super Member
    • Total Posts : 4355
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/24 08:45:54 (permalink)
    0

    while(1)
    {
            LATAbits.LATA5 = 1;  //set pin as high
            __delay_ms(1000); //delay of 1second
            LATAbits.LATA5 = 0;  //set pin as high
    }

    The code, as-is,  is not going to blink a LED. You'll need another 1-sec delay after LATA5 = 0.
    #4
    ConfusedCheese
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2016/07/11 09:03:16
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/24 08:48:03 (permalink)
    0
    Thanks for getting back - I added an additional __delay_ms(1000) and it has no effect. The LED on pin A5 is constantly high?
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 16306
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/24 10:55:32 (permalink)
    0
    you have this:
    while(1)
    {
            LATAbits.LATA5 = 1;  //set pin as high
            __delay_ms(1000); //delay of 1second
            LATAbits.LATA5 = 0;  //set pin as high
            __delay_ms(1000); //delay of 1second

    }
    _XTAL_FREQ 32000000
    there is not way you are not getting a warning that delays overflows with a 1 second delay
    #6
    krakatou
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2013/01/14 07:54:48
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 01:05:50 (permalink)
    0
    As mentioned in by NKurzman... You're while loop turns the LED on, waits 1000ms, turns the LED off and immediately back on. If you have an oscilloscope you will see pin A5 go low for one clock cycle period (TCY). Also as mentioned __delay_ms(1000) will be giving you a compiler error. 1000 is too big for __delay_ms(). I use a separate 'helper' function when I need a quick and dirty method of generating a long blocking delay.
     

    void delay_ms(uint16_t t)
    {
        while(t--)
            __delay_ms(1);
    }

     
    Are you also aware of the analogue select function of some ports? ANSEL. For example ANSELA = 0x00; will set port A pins to digital IO mode. I'm not familiar with the device you are using, but ANSEL trips me up ocassionally. I don't think this applies specifically to your device, but it's worth noting.
    post edited by krakatou - 2016/07/25 01:16:44
    #7
    toms
    Starting Member
    • Total Posts : 69
    • Reward points : 0
    • Joined: 2006/03/07 18:06:24
    • Location: London, UK
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 03:33:07 (permalink)
    0
    krakatouI'm not familiar with the device you are using, but ANSEL trips me up ocassionally. I don't think this applies specifically to your device, but it's worth noting.

     
    ANSEL tripped me up on a PIC18 the other day, but that was an easy enough fix. Scratched my head for a while trying to work out why an output still wasnt going high. Then I also learned about the pin function priority table... Digital I/O is always at the very bottom of the priority list, so you may need to disable some peripherals first before that works.
     
    Two little gotchyas to keep in mind. The datasheet is pretty good at explaining it all. Dont just give it a cursory glance, spend a good amount of time reading it.
    #8
    jack@kksound
    code tags!
    • Total Posts : 2738
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 10:04:32 (permalink)
    0
    #pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

     
    Also unless you are specifically using low voltage programming mode you should set this to OFF.
     

    Sorry, while this is normally correct I have forgotten that the Curiosity board runs in LVP mode. 
    post edited by jack@kksound - 2016/07/25 10:08:31
    #9
    NorthGuy
    Super Member
    • Total Posts : 5020
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 10:34:14 (permalink)
    0
    I suspect your PIC doesn't run as fast as you expect. I cannot be more specific as this PIC has new oscillator module and I am not yet familiar with it.
    #10
    jack@kksound
    code tags!
    • Total Posts : 2738
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 10:50:18 (permalink)
    0
    Are you also aware of the analogue select function of some ports? ANSEL. For example ANSELA = 0x00; will set port A pins to digital IO mode. I'm not familiar with the device you are using, but ANSEL trips me up ocassionally. I don't think this applies specifically to your device, but it's worth noting.

    The 16F1619 definately has ANSELx registers which must be properly configured to get digital functions on I/O port pins (they DEFAULT to analog inputs).
    #11
    jack@kksound
    code tags!
    • Total Posts : 2738
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 11:04:54 (permalink)
    0
    NorthGuy
    I suspect your PIC doesn't run as fast as you expect. I cannot be more specific as this PIC has new oscillator module and I am not yet familiar with it.


    With the internal oscillator selected by the config word (FOSC = INTOSC ) the default clock speed will be 500Khz (see data sheet). In order to get the 32Mhz listed as "#define _XTAL_FREQ 32000000 //internal oscillator of pic16f1619 is 32MHz" you MUST set the value of the IRCF bts in the OSCCON register to '1111'. This however will produce a frequency of 16 Mhz NOT 32 Mhz as this is the maximum frequency of the internal oscillator UNLESS the 4x PLL is enabled (see the appropriate sections in the data s
    #12
    PStechPaul
    Super Member
    • Total Posts : 2023
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 13:26:57 (permalink)
    0
    The documentation for XC8 does not give a maximum for the argument of __delay_ms(), and I would think any value corresponding to an unsigned integer should be OK. From what I can determine without finding the actual code, the __delay_ms() uses the basic delay functions, which are as follows:
     
    void _delay(unsigned long cycles);
    void _delaywdt(unsigned long cycles);

     
    And
     
    This is an in-line function that is expanded by the code generator. When called, this routine
    expands to an in-line assembly delay sequence. The sequence will consist of code
    that delays for the number of instruction cycles that is specified as the argument. The
    argument must be a constant expression.
    The _delay in-line function can use loops and the NOP instruction to implement the
    delay. On PIC18 devices only, the _delaywdt in-line function performs the same task,
    but can use the CLRWDT instruction, as well as loops to achieve the specified delay.
    An error will result if the delay period requested is not a constant expression or is too
    large (approximately 179,200 for PIC18 devices, and 50,659,000 instructions for other
    8-bit PIC devices). For very large delays, call this function multiple times.

     
    The additional delay is needed to blink the LED as intended, but a more efficient method would be:
     
    LATAbits.LATA5=~ LATAbits.LATA5; // Toggle Bit of Port A5

     
    As an inconsequential observation, the comment in the following code is wrong:
     
    LATAbits.LATA5 = 0;  //set pin as high

    post edited by PStechPaul - 2016/07/25 13:38:59

     
    #13
    jack@kksound
    code tags!
    • Total Posts : 2738
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 14:24:22 (permalink)
    0
    The documentation for XC8 does not give a maximum for the argument of __delay_ms(), and I would think any value corresponding to an unsigned integer should be OK.

    As stated in the documentation these macros use a combination of inline code. The maximum allowed delay will depend on the pic family selected and the operating clock frequency of the pic. If the value of the parameter (must be a literal constant) exceeds the maximum as determined by the compiler at compile time (based upon the _XTAL_FREQ define value and the pic family type) you will receive an error message.
    #14
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2016/07/25 14:32:46 (permalink)
    0
    The compiler release notes give a maximum for the argument of the _delay() psuedofunction, which is used by both the  _delay_ms() and _delay_us() macros.  This isn't totally accurate but if you exceed the actual limit the Error 1355 'in-line delay argument too large' message will tell you the actual limit.   XC8 on PIC10/12/16 uses three bytes for its delay loops, leading to a maximum limit of approximately 224*3 Tcy, (over six seconds) but on PIC18 it only uses two bytes and the limit is much much lower.

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #15
    Sunil_n_g
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2018/08/13 12:18:49
    • Location: 0
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2018/08/13 14:06:02 (permalink)
    -1 (1)
    Hello,
    I faced the same issue when I was trying to blink LED. _delay_ms() is located in #include<pic.h> header and after adding it to main.c my code worked.
    #16
    malaugh
    Super Member
    • Total Posts : 338
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2018/08/13 14:26:33 (permalink)
    0
    jack@kksound
    The 16F1619 definately has ANSELx registers which must be properly configured to get digital functions on I/O port pins (they DEFAULT to analog inputs).

     
    I hate that.
     
    I wish Microchip would default all pins to digital I/O.  To avoid these configuration mistakes we always initialize all the CPU pins to digital inputs immediately on startup, changing the default analog and pull ups. 
     
    That way we can set up the I/O in each individual driver, and if the pins change during the design we do not need to think about analog vs digital pins when we make the firmware changes. 



    #17
    NKurzman
    A Guy on the Net
    • Total Posts : 16306
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2018/08/13 14:52:12 (permalink)
    +1 (1)
    With XC8 you include ONLY <xc.h>  That will add whatever is needed.
    #18
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2018/08/13 15:00:37 (permalink)
    +3 (3)
    malaugh
    ...
    I wish Microchip would default all pins to digital I/O. 

    That will not happen.
    Defaulting to analog is the safer option.
    Microchip don't know which pins might have analog signals on them, so they all power up in the safest mode (analog) and then allow you to switch any that need to work as digital inputs to digital mode.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #19
    malaugh
    Super Member
    • Total Posts : 338
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: PIC16F1619 Curiosity - __delay_ms() no effect 2018/08/13 17:56:55 (permalink)
    0
    qɥb
    That will not happen.
    Defaulting to analog is the safer option.
    Microchip don't know which pins might have analog signals on them, so they all power up in the safest mode (analog) and then allow you to switch any that need to work as digital inputs to digital mode.



    Here the way it works
     
    1) Marketing decides to add a new feature to a 2 year old product.
    2) Hardware needs to remap the pins to add the feature.
    3) Its a pretty easy firmware change so the junior firmware engineer gets the job.
    4) He looks at the code and sees there is a define for the port and bit used for the remapped pins.
    5) He changes the defines to the new pins, and its done in an hour.
     
    or
    5) He changes to the new pins and it fails
    6) He drags out the scope and everything looks OK.
    7) He talks to one of his colleges, but he is only familiar with some aspects of the CPU, he takes a look but cannot fix it.
    8) He tries to find the original developer, but he has left the company.
    9) He start digging through the data sheet.
    10) After a few hours he finds the paragraph about analog pins on page 100 of the 150 page design guide.
     
    Time wasted - Way too long.
     
    I do not understand why analog is safer. Both analog and digital have the same absolute max ratings in the data sheet.
     
    post edited by malaugh - 2018/08/13 17:58:24
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5