Pic12f1572 delay function

Discussion in 'Embedded Systems and Microcontrollers' started by franklin75, Sep 27, 2016.

  1. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Hi everyone!
    I am studying pic which is 12f1572 and i use mplab xc8 compiler so firstly i started blink led but i encountered some problems about delay function.my problem is delay function is not work.i confugurate osccon register and i use internal oscilator .thanks for your help
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,911
    379
    What delay routine did you use - the XC8 ones or something else?
    Post the source code please and can you be more specific in what way it is not working.
     
  3. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Code (C):
    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. // CONFIG1
    7. #pragma config FOSC = INTOSC   //  (INTOSC oscillator; I/O function on CLKIN pin)
    8. #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
    9. #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
    10. #pragma config MCLRE = ON      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
    11. #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
    12. #pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
    13. #pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
    14.  
    15. // CONFIG2
    16. #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
    17. #pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
    18. #pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
    19. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    20. #pragma config LPBOREN = OFF    // Low Power Brown-out Reset enable bit (LPBOR is disabled)
    21. #pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
    22. #define _XTAL_FREQ 16000000L
    23.  
    24. #include <xc.h>
    25. #include "12f1572.h"
    26.  
    27. void main(void) {
    28.     //osccon register
    29.     SPLLEN=0;
    30.     OSCCONbits.IRCF = 0b0111;
    31.     SCS0=0;
    32.     SCS1=1;
    33.      //oscstat register
    34.    PLLR=0;
    35.    OSTS=0;
    36.    HFIOFR=1;
    37.    HFIOFL=0;
    38.    MFIOFR=0;
    39.    LFIOFR=0;
    40.    HFIOFS=0;
    41.  
    42.     TRISA=0x00;
    43.     PORTA=0x00;
    44.  
    45.     while(1){
    46.      
    47.         RA4=1;
    48.         __delay_ms(500);
    49.       RA4=0;
    50.      
    51.     }
    52.                  
    53. }
    Mod edit: code tags
     
    Last edited by a moderator: Sep 27, 2016
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    You need another delay after RA4=0;

    Edit:
    You also have to make RA4 a digital port by writing 0 to ANSELA bit 4.

    Finally, be sure that writing RA4 uses the LATA register, not PORTA - look at the disassembly listing or check the compiler manual or header file to be sure. I don't recall right off if it uses LATx automatically or you have to specify it explicitly. Single bit writes to PORTx can be problematic.
    Edit 2: I took a look at the .h file for you and RA4 does indeed reference PORTA. You want to use LATA4 instead of RA4:
    Code (C):
    1.  
    2. LATA4 = 1;
    3. delay..
    4. LATA4 = 0;
    5. delay..
     
    Last edited: Sep 27, 2016
  5. TQFP44

    New Member

    Sep 3, 2016
    25
    3
    What do you have for the OFF delay ? .... needs another delay
     
  6. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Thanks for your suggestions. it works !!!
     
    ErnieM likes this.
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Congratulations. Step one complete... But only if you understand why you need the second delay.

    We can help explain why if you need the help.
     
  8. jayanthd

    Member

    Jul 4, 2015
    270
    29
    Code (C):
    1.  
    2. RA4 = 1;
    3. __delay_ms(500);
    4. RA4 = 0;
    5.  
    can be replaced with

    Code (C):
    1.  
    2. RA4 = ~RA4;
    3. __delay_ms(500);
    4.  
     
  9. dandrous

    New Member

    Mar 2, 2015
    22
    0
    hi i gets error while using delay function in pic18f2220
    here is the error message,
    "C:\Program Files (x86)\Microchip\xc8\v1.38\bin\xc8.exe" --pass1 --chip=18F2220 -Q -G --double=24 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/newmain.p1 newmain.c
    newmain.c:21: error: (195) expression syntax
    newmain.c:21: error: (187) too few function arguments
    newmain.c:21: error: (194) ")" expected
    newmain.c:21: error: (195) expression syntax
    (908) exit status = 1
    nbproject/Makefile-default.mk:100: recipe for target 'build/default/production/newmain.p1' failed
    make[2]: Leaving directory 'C:/Users/dandrous/MPLABXProjects/test.X'
    nbproject/Makefile-default.mk:84: recipe for target '.build-conf' failed
    make[1]: Leaving directory 'C:/Users/dandrous/MPLABXProjects/test.X'
    nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
    make[2]: *** [build/default/production/newmain.p1] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2

    BUILD FAILED (exit value 2, total time: 1s)



    Code (Text):
    1. //////////////header file
    2. #pragma config OSC = INTIO2     // Oscillator Selection bits (Internal RC oscillator, port function on RA6 and port function on RA7)
    3. #pragma config FSCM = ON        // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
    4. #pragma config IESO = OFF       // Internal/External Switchover bit (Internal/External Switchover mode disabled)
    5.  
    6. // CONFIG2L
    7. #pragma config PWRT = OFF       // Power-up Timer enable bit (PWRT disabled)
    8. #pragma config BOR = OFF        // Brown-out Reset enable bit (Brown-out Reset disabled)
    9. #pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)
    10.  
    11. // CONFIG2H
    12. #pragma config WDT = OFF         // Watchdog Timer Enable bit (WDT enabled)
    13. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    14.  
    15. // CONFIG3H
    16. #pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    17. #pragma config PBAD = ANA       // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
    18. #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
    19.  
    20. // CONFIG4L
    21. #pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    22. #pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
    23.  
    24. // CONFIG5L
    25. #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-0007FFh) not code-protected)
    26. #pragma config CP1 = OFF        // Code Protection bit (Block 1 (000800-000FFFh) not code-protected)
    27.  
    28. // CONFIG5H
    29. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0001FFh) is not code-protected)
    30. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
    31.  
    32. // CONFIG6L
    33. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-0007FFh) not write-protected)
    34. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (000800-000FFFh) not write-protected)
    35.  
    36. // CONFIG6H
    37. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
    38. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0001FFh) is not write-protected)
    39. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
    40.  
    41. // CONFIG7L
    42. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000200-0007FFh) not protected from table reads executed in other blocks)
    43. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (000800-000FFFh) not protected from table reads executed in other blocks)
    44.  
    45. // CONFIG7H
    46. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0001FFh) is not protected from table reads executed in other blocks)
    47.  
    48. // #pragma config statements should precede project file includes.
    49. // Use project enums instead of #define for ON and OFF.
    50.  
    51. #include <xc.h>
    52. #define _XTAL_FREQ=8000000
    53.  
    54. /////main c
    55. #include <stdio.h>
    56. #include <stdlib.h>
    57. #include "newfile.h"
    58.  
    59. void main(void) {
    60.  
    61.     TRISAbits.RA0=0;
    62.     OSCCON=0x76;
    63.     while(1)
    64.     {
    65.      
    66.         LATBbits.LATB0=0;
    67.         __delay_ms(50);
    68.           LATBbits.LATB0=1;
    69.     }
    70.        
    71. }
    72.  
     
  10. TQFP44

    New Member

    Sep 3, 2016
    25
    3
    Check #define
     
  11. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,911
    379
    There should be no equal sign in this line, just a space.
     
  12. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    thanks for your help
     
  13. dannyf

    Well-Known Member

    Sep 13, 2015
    1,780
    360
    Not Sure.

    The first piece has a near 100 percent duty cycle and about 500ms period.

    The second piece has a 50 percent duty cycle and 1000ms period.
     
  14. dandrous

    New Member

    Mar 2, 2015
    22
    0
    Thanks its working now.Any one know the programming of dspic33ev256gv for pwm output
     
  15. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,911
    379
    I've never used dsPIC. Just work through the datasheet.
     
    TQFP44 likes this.
  16. TQFP44

    New Member

    Sep 3, 2016
    25
    3
    I have the dsPIC33EV256GM104 chip on a breakout board, but not done any PWM with it , should be some examples out there... as a starting place, (super mcu )
    dsPIC33EV.jpg
     
    Last edited: Sep 30, 2016
  17. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    TQFP44 likes this.
  18. dannyf

    Well-Known Member

    Sep 13, 2015
    1,780
    360
    really easy 4 steps:

    1) read the datasheet; and
    2) lay out in english what you need to do to enable the pwm;
    3) we will help you translate that into C.
    4) done.
     
Loading...