Electronic – PIC Microcontroller – __delay_ms() no effect

delayledmicrocontrollerpic

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
}

}

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!

Best Answer

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

You don't let the LED stay off long enough for it to be noticed. You drive the pin low in the last line of the while loop. As soon as that happens, the execution goes back to the top of the loop. Immediately, you turn the LED on. If you can look at the digital output with an oscilloscope, you should see a very brief dip with a duration on the order of a microsecond. It's too brief for an eye to notice. So add another delay.

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

p.s. This is a popular bug in "Hello Blinky LED!" programs.