Problems with a simple program in MPLABX

Thread Starter

Teknosrp

Joined Dec 31, 2016
2
Hello
I'm new in PIC programming and I'm trying to modify a program that blinks a LED. Now the program is suposed to:
-Turn on a LED connected to RB1
-Wait 1s
-Turn on another LED connected to RB2 (while the LED connected to RB1 remains turned on)
-Wait 2s
-Turn off the LED connected to RB1 (while the LED connected to RB2 remains turned on)
-Wait 1s
-Turn off the LED connected to RB2
-Wait 1s and restart the program

And the code of my program is this:
Code:
#define _XTAL_FREQ 10000000

// CONFIG
#pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

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

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  TRISB = 0x00; //PORTB as output
  PORTB = 0x00; 
  while(1)
  {
    RB1 = 1;  // LED 1 ON
        __delay_ms(1000); // 1 Second Delay
    RB2 = 1;  // LED 2 ON
    __delay_ms(2000); // 2 Second Delay
    RB1 = 0;  // LED 1 OFF
        __delay_ms(1000); // 1 Second Delay
    RB2 = 0;  // LED 2 OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}
The program turns on the first led, waits a second, turns on the second led and then doesn't follow correctly the delays. Simply turns on and off the two leds very fast and waits a second. Where is the problem?
 

Picbuster

Joined Dec 2, 2013
1,047
Hello
I'm new in PIC programming and I'm trying to modify a program that blinks a LED. Now the program is suposed to:
-Turn on a LED connected to RB1
-Wait 1s
-Turn on another LED connected to RB2 (while the LED connected to RB1 remains turned on)
-Wait 2s
-Turn off the LED connected to RB1 (while the LED connected to RB2 remains turned on)
-Wait 1s
-Turn off the LED connected to RB2
-Wait 1s and restart the program

And the code of my program is this:
Code:
#define _XTAL_FREQ 10000000

// CONFIG
#pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

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

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  TRISB = 0x00; //PORTB as output
  PORTB = 0x00;
  while(1)
  {
    RB1 = 1;  // LED 1 ON
        __delay_ms(1000); // 1 Second Delay
    RB2 = 1;  // LED 2 ON
    __delay_ms(2000); // 2 Second Delay
    RB1 = 0;  // LED 1 OFF
        __delay_ms(1000); // 1 Second Delay
    RB2 = 0;  // LED 2 OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}
The program turns on the first led, waits a second, turns on the second led and then doesn't follow correctly the delays. Simply turns on and off the two leds very fast and waits a second. Where is the problem?
as stated the mpu could make a reset.
invoke between Trisb and while(1)
for (int n=0; n<100;n++)
{
RB1=on;
__delay_ms(100);
RB1=off;
}
prog will start with quick on off then your part does slow
if you get he quick on after the slow part an unwanted restart took place.
Picbuster
 

nerdegutta

Joined Dec 15, 2009
2,684
Hi.

I would think you'll need to include:
C:
#define _XTAL_FREQ xx
where xx is matching the crystal you have. If you don't have a crystal, then you'll need to edit the
C:
#pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
to something like:
C:
#pragma config FOSC = INTOSCIO
Check your datasheet for the right register.

You might also try to turn off the comparators. Again, check the datasheet for the correct register. Which PIC are you working on?
 

spinnaker

Joined Oct 29, 2009
7,830
Hello
I'm new in PIC programming and I'm trying to modify a program that blinks a LED. Now the program is suposed to:
-Turn on a LED connected to RB1
-Wait 1s
-Turn on another LED connected to RB2 (while the LED connected to RB1 remains turned on)
-Wait 2s
-Turn off the LED connected to RB1 (while the LED connected to RB2 remains turned on)
-Wait 1s
-Turn off the LED connected to RB2
-Wait 1s and restart the program

And the code of my program is this:
Code:
#define _XTAL_FREQ 10000000

// CONFIG
#pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

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

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  TRISB = 0x00; //PORTB as output
  PORTB = 0x00;
  while(1)
  {
    RB1 = 1;  // LED 1 ON
        __delay_ms(1000); // 1 Second Delay
    RB2 = 1;  // LED 2 ON
    __delay_ms(2000); // 2 Second Delay
    RB1 = 0;  // LED 1 OFF
        __delay_ms(1000); // 1 Second Delay
    RB2 = 0;  // LED 2 OFF
    __delay_ms(1000); // 1 Second Delay
  }
  return 0;
}
The program turns on the first led, waits a second, turns on the second led and then doesn't follow correctly the delays. Simply turns on and off the two leds very fast and waits a second. Where is the problem?

You are setting the OSC speed anywhere. You are telling the delay function it is 10MHZ but the default frequency of the Pic could be as little as 31khz.
 

spinnaker

Joined Oct 29, 2009
7,830
Hi.

I would think you'll need to include:
C:
#define _XTAL_FREQ xx
where xx is matching the crystal you have. If you don't have a crystal, then you'll need to edit the
C:
#pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
to something like:
C:
#pragma config FOSC = INTOSCIO
Check your datasheet for the right register.

You might also try to turn off the comparators. Again, check the datasheet for the correct register. Which PIC are you working on?

Thanks. I knew that looked suspicious but wanted to confirm. You just saved me doing the research! ;)
 

Thread Starter

Teknosrp

Joined Dec 31, 2016
2
In config, try turning off watchdog timer and brown out reset.
Many thanks! I just have turned off these two options and now it works correctly. Why were them causing trouble and what were them supposed to do?

You are setting the OSC speed anywhere. You are telling the delay function it is 10MHZ but the default frequency of the Pic could be as little as 31khz.
I'm using a 10Mhz crystal and I have written "#define _XTAL_FREQ 10000000". It's not sufficient?
 

spinnaker

Joined Oct 29, 2009
7,830
Also RB1 looks like is defined as a port. You should read ports and set latches.

Always best to use the longhand so you know what is going on

LATBbits.LATB1 = 1;
 

spinnaker

Joined Oct 29, 2009
7,830
I'm using a 10Mhz crystal and I have written "#define _XTAL_FREQ 10000000". It's not sufficient?
I was going by your comments ans assuming internal RC.

#pragma config FOSC = HS // Oscillator Selection bits (RC oscillator)

As I said above that looked suspicious. Take a look at

Window/Pic Memory View/Configuration bits and confirm there is nothing elesr you need to set for an external crystal.
 

Art

Joined Sep 10, 2007
806
Brown out reset causes the pic to reset if the power supply falls below spec.
Your power supply might be ok, and you might be able to turn it on again.

Watchdog timer resets the pic if you do not continually tell it not to,
so that if a program hangs it will automatically reset.

I assume this is a pic that has no latch registers since you modified some existing code to flash the LEDs.
 
Top