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] Newbie of MPLAB X. Led blinking with PIC32MX

Status
Not open for further replies.

electroman2000

Member level 2
Joined
Feb 3, 2015
Messages
44
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
496
Hi everyone, I have just moved from mikroC to MPLAB X IDE with XC32 programmer. I'm trying to make the simply LED blinking code. The problem is the blinking is faster than expected.
This is the code:

Code:
#include <p32xxxx.h>
#include <plib.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>

// PIC32MX795F512L Configuration Bit Settings

// 'C' source line config statements


// DEVCFG3
// USERID = No Setting
#pragma config FSRSSEL = PRIORITY_7     // SRS Select (SRS Priority 7)
#pragma config FMIIEN = OFF             // Ethernet RMII/MII Enable (RMII Enabled)
#pragma config FETHIO = OFF             // Ethernet I/O Pin Select (Alternate Ethernet I/O)
#pragma config FCANIO = OFF             // CAN I/O Pin Select (Alternate CAN I/O)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier (20x Multiplier)
#pragma config UPLLIDIV = DIV_12        // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF             // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1         // System PLL Output Clock Divider (PLL Divide by 1)

// DEVCFG1
#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = OFF            // Secondary Oscillator Enable (Disabled)
#pragma config IESO = OFF               // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = XT             // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = ON              // Watchdog Timer Enable (WDT Enabled)

// DEVCFG0
#pragma config DEBUG = OFF              // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)

void DelayMs(WORD delay)
 {
     T1CONbits.ON=1;           // timer1 ON
     while( delay-- )         // do this cycle for a number of 1 ms interrupt egual to delay
     {
      while(IFS0bits.T1IF==0){}; //wait until 1 ms interrupt 
      IFS0bits.T1IF=0;           // clear flag
      while(IFS0bits.T1IF==1){}; // if flag is cleared, go on
     }
     T1CONbits.ON=0;           // timer1 OFF
 }

int main(int argc, char** argv) {
    SYSTEMConfigPerformance(80000000L);
	CheKseg0CacheOn();
    mCheConfigure(CHE_CONF_WS2 | CHE_CONF_PF_C);

AD1PCFG = 0xFFFF;      // Configure AN pins as digital I/O
DDPCONbits.JTAGEN = 0; // disable the JTAG port
  
  // Configurazione interrupt del timer
T1CONbits.ON=0;           // timer1 OFF
T1CONbits.TCKPS=0b00;            // prescaler=1 (T1CON <5-4>) , period 1/80 Mhz = 12,5 ns

T1CONbits.TCS=0;            // internal clock 
TMR1=0;                // initial value of timer=0
PR1=80000;              // PR set for 1 ms interrupt
IPC1bits.T1IP=0b101; //  interrupt priority set to 5 ( IPC1 <4-2> )
IEC0bits.T1IE=1;    // generate interrupt if T1IF=1
IFS0bits.T1IF=0;    // overflow  flag. 


TRISB = 0;             // Initialize PORTB as output
LATB = 0;              // Set PORTB to zero


  while(1) {

    PORTBINV=0xFFFF; // invert PORTB
    DelayMs(5000);  // wait 5 seconds
  }

    return (EXIT_SUCCESS);
}

I'm using the easyPIC fusion v7 board. The same program written in mikroC works well. The PIC32MX795 is connected to 8 Mhz external clock. The #pragma configuration is the same that I set in mikroC.
The problem is that the LED blinking every about 1 second instead of 5 seconds.

What I'm wrong?

Thank you very much
 

I noticed that the Watchdog Timer is enabled in your code. Shouldn't that be disabled?
 

I have disabled it and retry, but nothing is changed.

EDIT: the strange thing is that the LEDs blinking faster than expected, not slower, even I set the maximum speed for sysclk and pbclk. Also I don't think I'm wrong in #pragma setting, because the setting is exactly egual to those I used in mikroC.
 

The reason I mentioned the WDT is because I thought you may have used MPLABX's tool for generating the #pragma statements while in MikroC you might have set the configuration bits using the project options (as opposed to copying code you personally wrote from MikroC into MplabX). If this was the case you might have missed the wdt setting which is why i pointed it out. (I do think it is odd that your code runs fine on MikroC with the watchhdog timer enabled without anything clearing the wdt periodically in your code).

I do think that your config bits for the oscillator are correct. Now, I notice that PR1 = 80000. Isn't PR1 a 16 bit register? Max value should be 65335. Try adjusting your prescaler for tmr1 and pr1. Maybe use 1:8 and PR1 =10000. Looking at the datasheets, I really hope you don't say thay PR1 works with 80000 when using MikroC as it clearly says that its a 16 bit register :grin:
 
Thank you for reply
The reason I mentioned the WDT is because I thought you may have used MPLABX's tool for generating the #pragma statements while in MikroC you might have set the configuration bits using the project options (as opposed to copying code you personally wrote from MikroC into MplabX). If this was the case you might have missed the wdt setting which is why i pointed it out. (I do think it is odd that your code runs fine on MikroC with the watchhdog timer enabled without anything clearing the wdt periodically in your code).
for making the #pragma code, I do the following in MPLAB X: in the top bar menu, Windows -> PIC Memory Views -> Configuration bits. This show a menu identical to the mikroC one. I just set the value and click to "generate source code" to produce the relative #pragma code.
Maybe you mean to copypaste this register output from setting menu of mikroC?
Code:
DEVCFG3 : $1FC02FF0 : 0x00070000
DEVCFG2 : $1FC02FF4 : 0x00008751
DEVCFG1 : $1FC02FF8 : 0x0094C503
DEVCFG0 : $1FC02FFC : 0x110FF00F


I do think that your config bits for the oscillator are correct. Now, I notice that PR1 = 80000. Isn't PR1 a 16 bit register? Max value should be 65335. Try adjusting your prescaler for tmr1 and pr1. Maybe use 1:8 and PR1 =10000. Looking at the datasheets, I really hope you don't say thay PR1 works with 80000 when using MikroC as it clearly says that its a 16 bit register :grin:
you are right. Now it works. Of course in mikroC I don't use a PR value of 80000, because I use the delay function that is present in it (I tryied to use delay.h in mplab X but I didn't find it).

Many many thanks
 

The main question is solved. but I have another question that concern the fact I'm a newbie with MPLAB X. I want to use the fatFS library. This library contain many file, that I put in the main directory where is the main.c. Among this file there are ff.h, that contain the declarations of the functions (example f_open(), f_write() ) and ff.c that contain the functions themselves. I included the ff.h in main.c.
When I try to use the functions f_open and f_write etc inside main.c, an error tell me that this function can't be foud.
Maybe I have to do something else than simply put ff.c inside the main folder?
 

Did you add the source files to your project? You should see the folder structure of your project on the left side of MPLABX (the tab called "Projects"). You should see folders called Header Files, Important Files, Source Files, etc. Right click on those folders and you should see "Add Existing Items". Click on that and you should be able to select the files to add to your project.
 
Ok I do it and now it are included. I include every .h file in "header file" section and every .c file in "source file" section. Hope it is right.

Thank you
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top