Problems with a simple program in MPLABX

Discussion in 'Embedded Systems and Microcontrollers' started by Teknosrp, Dec 31, 2016.

  1. Teknosrp

    Thread Starter New Member

    Dec 31, 2016
    2
    0
    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 (Text):
    1. #define _XTAL_FREQ 10000000
    2.  
    3. // CONFIG
    4. #pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
    5. #pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
    6. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    7. #pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
    8. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    9. #pragma config LVP = OFF         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
    10. #pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
    11. #pragma config WRT = OFF         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)
    12.  
    13. // #pragma config statements should precede project file includes.
    14. // Use project enums instead of #define for ON and OFF.
    15.  
    16. #include <xc.h>
    17. #include <stdio.h>
    18. #include <stdlib.h>
    19.  
    20. int main()
    21. {
    22.   TRISB = 0x00; //PORTB as output
    23.   PORTB = 0x00;
    24.   while(1)
    25.   {
    26.     RB1 = 1;  // LED 1 ON
    27.         __delay_ms(1000); // 1 Second Delay
    28.     RB2 = 1;  // LED 2 ON
    29.     __delay_ms(2000); // 2 Second Delay
    30.     RB1 = 0;  // LED 1 OFF
    31.         __delay_ms(1000); // 1 Second Delay
    32.     RB2 = 0;  // LED 2 OFF
    33.     __delay_ms(1000); // 1 Second Delay
    34.   }
    35.   return 0;
    36. }
    37.  
    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?
     
  2. Art

    AAC Fanatic!

    Sep 10, 2007
    805
    62
    In config, try turning off watchdog timer and brown out reset.
     
  3. Picbuster

    Well-Known Member

    Dec 2, 2013
    893
    117
    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
     
  4. nerdegutta

    Moderator

    Dec 15, 2009
    2,660
    917
    Hi.

    I would think you'll need to include:
    Code (C):
    1.  
    2. #define _XTAL_FREQ xx
    3.  
    where xx is matching the crystal you have. If you don't have a crystal, then you'll need to edit the
    Code (C):
    1.  
    2. #pragma config FOSC = HS     // Oscillator Selection bits (RC oscillator)
    3.  
    to something like:
    Code (C):
    1.  
    2. #pragma config FOSC = INTOSCIO
    3.  
    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 likes this.
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,621

    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.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,621

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

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,621
    TS has that part:

    #define _XTAL_FREQ 10000000

    But not setting the Pic OSC register.
     
  8. Teknosrp

    Thread Starter New Member

    Dec 31, 2016
    2
    0
    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?

    I'm using a 10Mhz crystal and I have written "#define _XTAL_FREQ 10000000". It's not sufficient?
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,621
    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;
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,815
    3,621
    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.
     
  11. Art

    AAC Fanatic!

    Sep 10, 2007
    805
    62
    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.
     
  12. nerdegutta

    Moderator

    Dec 15, 2009
    2,660
    917
    You're right. I didn't notice it. I usually have the include libs on top, then the config, followed by defines, constants and variables. Guess I was trapped in my way of arranging a program...
     
Loading...