Pic12f1572 delay function

Thread Starter

franklin75

Joined Jun 11, 2016
14
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
 

AlbertHall

Joined Jun 4, 2014
11,953
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.
 

Thread Starter

franklin75

Joined Jun 11, 2016
14
C:
#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1
#pragma config FOSC = INTOSC   //  (INTOSC oscillator; I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOREN = OFF    // Low Power Brown-out Reset enable bit (LPBOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#define _XTAL_FREQ 16000000L

#include <xc.h>
#include "12f1572.h"

void main(void) {
    //osccon register
    SPLLEN=0;
    OSCCONbits.IRCF = 0b0111;
    SCS0=0;
    SCS1=1;
     //oscstat register
   PLLR=0;
   OSTS=0;
   HFIOFR=1;
   HFIOFL=0;
   MFIOFR=0;
   LFIOFR=0;
   HFIOFS=0;
 
    TRISA=0x00;
    PORTA=0x00;
  
    while(1){
      
        RA4=1;
        __delay_ms(500);
      RA4=0;
      
    }
                  
}
Mod edit: code tags
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,654
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:
C:
LATA4 = 1;
delay..
LATA4 = 0;
delay..
 
Last edited:

dandrous

Joined Mar 2, 2015
22
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:
//////////////header file
#pragma config OSC = INTIO2     // Oscillator Selection bits (Internal RC oscillator, port function on RA6 and port function on RA7)
#pragma config FSCM = ON        // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = OFF       // Internal/External Switchover bit (Internal/External Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer enable bit (PWRT disabled)
#pragma config BOR = OFF        // Brown-out Reset enable bit (Brown-out Reset disabled)
#pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)

// CONFIG2H
#pragma config WDT = OFF         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBAD = ANA       // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-0007FFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (000800-000FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0001FFh) 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 (000200-0007FFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (000800-000FFFh) 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-0001FFh) 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 (000200-0007FFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (000800-000FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0001FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ=8000000

/////main c
#include <stdio.h>
#include <stdlib.h>
#include "newfile.h"

void main(void) {

    TRISAbits.RA0=0;
    OSCCON=0x76;
    while(1)
    {
      
        LATBbits.LATB0=0;
        __delay_ms(50);
          LATBbits.LATB0=1;
    }
       
}
 

TQFP44

Joined Sep 3, 2016
51
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:
Top