Hi,
I new to PIC programming and am just working out some blinking LEDs to practice.
Here's my code:
Code:
// PIC16F1847 Configuration Bit Settings
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
//#pragma config FOSC = HS
#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 MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data 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 Switchover (Internal/External Switchover 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 PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = OFF // 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 LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled)
#include <pic16f1847.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
//Internal clock setting
#define _XTAL_FREQ 8000000 // for INTOSC in PRAGMA oscillator configuration bits
//#define _XTAL_FREQ 10000000 // for HS in PRAGMA oscillator configuration bits
//Pre-defninition of functions for use in main function
void indvblinker ();
void allblinker ();
void main ()
{
TRISA = 0x00; //Setting PORTA as output
indvblinker () ;
allblinker () ;
}
void indvblinker () // To blink every LED in order
{
RA0 = 0xff;
__delay_ms(200);
RA0 = 0x00;
__delay_ms(200);
RA1 = 0xff;
__delay_ms(200);
RA1 = 0x00;
__delay_ms(200);
RA2 = 0xff;
__delay_ms(200);
RA2 = 0x00;
__delay_ms(200);
RA3 = 0xff;
__delay_ms(200);
RA3 = 0x00;
__delay_ms(200);
RA4 = 0xff;
__delay_ms(200);
RA4 = 0x00;
__delay_ms(200);
}
void allblinker () // to blink all LEDs
{
int n;
for (n = 0; n != 5; n++)
{
PORTA = 0xff;
__delay_ms (1000);
PORTA = 0x00;
__delay_ms (1000);
}
}
MPLAB X w/ XC8 fresh install
The for loop iterates only once.
Then there is then a pause before repeating the entire main function. It seems like the amount of time it would take to execute the other iterations of the for loop.
Any explanation for any of this?
I'm baffled by this.
Also, in an attempt to kill two birds with one stone, any ideas as to why MPLAB has trouble resolving identifiers for __delay_ms(x) instructions?
Seems like an unanswerable question from everywhere I've looked.
Thanks in advance.
EDIT1: New Developpements
I ran a makeshift test:
Code:
while (1)
{
PORTA = 0xff;
}
in the main function.
It gave the same result as the for loop: what seemed to be one iteration then it started the main function again.
So, I turned off Brown-out reset and set the brown out voltage to HI in the config, and the infinite while loop sustained all five LEDs without problem.
Therefore it's probably not a power problem.
I then went back to the original for loop with this new config and got the same symptoms as the beginning.
Any suggestions?