Thread: How to use Delay1s() function? C Prog

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    6

    How to use Delay1s() function? C Prog

    Hi! I have some queries on how to use this function, can't seem to find a solution anywhere!
    So I'm using MPLAB and PIC18F4550 if these matters.
    What I don't understand is the delay function, as given by my classmate and teacher.
    So according to my friend's code,
    he uses delay1s() , whereas the lecturer's notes uses delay1TCY.
    How to use Delay1s() function? C Prog-screenshot_1-jpg
    Not in a good position to ask my friend hence, my question is,
    how do i use the delay1s() function? example i want to change the delay to 5s or 10s, do i just change it to delay5s() or delay10s() respectively?
    Thanks so much in advance! I am just a beginner in these! ><

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Find the instruction speed of your processor, which may be some division of the clock speed.
    2. Divide that by 10000 to give you a value n.
    3. Write
    Code:
    void delay1s() {
      for ( i = 0 ; i < n ; i++ ) delay10KTCYx();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    They most likely use a loop in a function.

    What compiler are you using? XC8?

    What document is that screenshot from?

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    6
    yup its a XC8 Compiler. the whole code looks like this:

    Code:
    #include <xc.h>
    #include <pic18f4550.h>
    #define _XTAL_FREQ 2000000 
    void delay1s();
    
    
     // 7 SEGMENT LED TEST       
     
     
    void main(void) {
    TRISB=0;             // TRIS0 = setting the pins to output
    LATB=255;            // does it work with PORT?, LAT255 means to initialize
     while(1){           // while(1) is to prevent program from stopping
         delay1s();
         LATB=63;       //LED Display 0          
         delay1s();                                  
         LATB=6;        //LED Display 1   
         delay1s();                                  
         LATB=91;       //LED Display 2 
         delay1s();                                  
         LATB=79;       //LED Display 3
         delay1s();                                  
         LATB=102;       //LED Display 4
         delay1s();                                  
         LATB=109;       //LED Display 5
         delay1s();                                  
         LATB=125;       //LED Display 6
         delay1s();                                  
         LATB=7;         //LED Display 7
         delay1s();                                  
         LATB=127;       //LED Display 8
         delay1s();                                  
         LATB=111;       // LED Display 9
     }
    }
    void delay1s()
    {
     for (int i=0;i<25;i++){  // for i=0 to i<25,i+1
         __delay_ms(20);
     }
    }

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    6
    the document is from MPLAB C18 Compiler guide~

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    6
    May I also use the chance to ask, is the frequency for XTAL fixed? will changing it affect the speed or something?

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The frequency is what you have your programme counter set to - i.e. Your crystal oscillator or internal timer.

    Where are your config settings?

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, the header for the pic is already included in xc.h

  9. #9
    Registered User
    Join Date
    May 2019
    Posts
    6
    Code:
    // CONFIG1L
    #pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
    #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
    #pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
    
    
    // CONFIG1H
    #pragma config FOSC = INTOSC_HS // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    
    
    // CONFIG2L
    #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOR = OFF        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
    #pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting 2.05V)
    #pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
    
    
    // CONFIG2H
    #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
    #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    
    
    // CONFIG3H
    #pragma config CCP2MX = OFF     // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
    #pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
    #pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
    #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
    
    
    // CONFIG4L
    #pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
    #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
    #pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
    #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    
    
    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
    #pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
    #pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
    #pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
    
    
    // CONFIG5H
    #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
    #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
    
    
    // CONFIG6L
    #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
    #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
    #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
    #pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
    
    
    // CONFIG6H
    #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
    #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
    #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
    
    
    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
    #pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
    
    
    // CONFIG7H
    #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
    these are my config settings.
    gosh im so lost in this

  10. #10
    Registered User
    Join Date
    May 2019
    Posts
    6
    i actually have another set of code, to which i tried to change the __delay_ms(1) to delay1s() but it doesnt allow me to build. I included the void delay1s(); on top tho, what could be the main reason?

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
     void delay1s(void);
    ... You missed the "void"

    Also your clock is set to INTOSC_HS (internal HS) -

    You then define _XTAL_FREQ whatever you have set your registers to be for the processor speed. The compiler then uses that value to calculate the __delay_ms.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    And it is also worth mentioning, although the C18 compiler was absorbed be the xc8 compiler, you are better off following the xc8 compiler guide.

    https://www.google.com/url?q=http://...6Wlqiq_Db4EucH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiling c prog with outb function
    By itchy8me in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 06:43 AM
  2. Help with prog
    By Noxir in forum Windows Programming
    Replies: 7
    Last Post: 08-17-2004, 12:57 PM
  3. Help me name my prog.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-01-2002, 08:22 PM
  4. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 07:43 AM
  5. Re: Function problems in simple C++ prog.
    By cj348879 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2001, 07:00 PM

Tags for this Thread