Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] "For loop" only iterates once?

Status
Not open for further replies.

MezmerizedMonkey

Newbie level 5
Newbie level 5
Joined
Feb 9, 2013
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,342
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.:cry:
Any suggestions?
 
Last edited:

Try changing the for statement to:
for (n=0; n==5; n++)
or preferably
for (n = 0; n < 6; n++)

The test in the loop is whether the statement in the middle section is true or not. As 'n' is not initially 5 it fails straight away.

You also need something to stop it "falling off the end of the program. If you want to make it repeat, put the code in the main() function inside a while(1) {.....} loop.

Brian.
 
Last edited:

Try changing the for statement to:
for (n=0; n==5; n++)
or preferably
for (n = 0; n < 6; n++)

I don't see why the original for statement should not work, but the n==5 variant will be never executed.
Code:
for (n = 0; n != 5; n++)
n < 5 would be normaly used instead of n!=5.
 

Try changing the for statement to:
for (n=0; n==5; n++)
or preferably
for (n = 0; n < 6; n++)

The test in the loop is whether the statement in the middle section is true or not. As 'n' is not initially 5 it fails straight away.

You also need something to stop it "falling off the end of the program. If you want to make it repeat, put the code in the main() function inside a while(1) {.....} loop.

Brian.
Unfortunately, there was no change.
 

Zip and post your MPLABX project files so that it can be tested. No time to create a new project.


Code C - [expand]
1
for (n = 0; n < 5; n++)  iterates 5 times. 0, 1, 2, 3, 4... stops at 5.



See if making n as global variable works or not.
 

first of all RA0 is a single bit and you are trying to write 0xFF.. next problem is for loop..
Do
Code C - [expand]

1
2
3
RA0 = 1; // instead of RA0 = 0xFF;

while(1) // instead of for loop (because you need a termination of program)
 
I got a new chip. All is well now; just the annoying inline delay that is too long :-?
Thank you for your help though. You were completely right with what you wrote, but it didn't seem to help...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top