Pic32mz Beginner Questions - Page 1
Forum section: electronics / general

author: jmsigler
author_rank: Regular Contributor
timestamp: December 26, 2015, 01:30:13 am
content: Hello Everyone,I have designed a board based on the pic32mz2048ecg064 (unfortunately, I made the design decision before learning about the eratta) and I have a couple questions related to programming it. I have assembled the board, and my picKit3 can find the chip and program it, however, I am having trouble actually getting the program to do anything. My current goal is to just turn on an led attached to port RB8. The program I am trying is below. Also, here is a link to the microcontroller's pagehttp://www.microchip.com/wwwproducts/Devices.aspx?product=PIC32MZ2048ECG064Code:#include <xc.h>void main(){TRISB = 0;LATB = 1;while(1);return(0);}I think one of these is my problem:One of the default pin functions is interfering with the output functionality. I realize that I have to disable certain modules like the ADC, but I'm not sure what the general process for this is. On the schematic, the pin is labeled AN48/RPB8/PMA10/RB8 is this the pin function's priority list? i.e. it will go to AN48 and if that is disabled go to RPB8 and so on?The next thing I think it could be is a problem with my code. A lot of the tutorials/examples I have seen are using the plib library, which appears to be legacy now? I think what I am doing should work, the general idea is to set the port to output, then set it to logic high correct?Finally, I may have a problem with the configuration registers. I have not messed with the registers yet and explored all of the possible settings, but I was hoping that the default settings would be enough to let it run a basic program. Am I wrong here? Is there something I need to set that could be tripping me up?Thanks for the help. Like I said, I'm fairly new to microcontrollers, so I appreciate it.

author: oPossum
author_rank: Super Contributor
timestamp: December 26, 2015, 01:47:50 am
content: Quote from: jmsigler on December 26, 2015, 01:30:13 amTry this...Code:#include <xc.h>void main(){LATB = 0; // All of port B pins offTRISB = 0; // All of port B pins are outputLATBbits.LATB8 = 1; // Turn on B8while(1);return(0);}

author: jmsigler
author_rank: Regular Contributor
timestamp: December 26, 2015, 02:13:05 am
content: That appears to have not worked. I can tell something is going on, because my multimeter thinks there is .271v AC (I don't have an oscope yet, it is in the mail currently) across the line but it is not the steady 3.3v dc output I am expecting.

author: oPossum
author_rank: Super Contributor
timestamp: December 26, 2015, 02:47:17 am
content: Hmm, make sure all these are turned off...- Watchdog timer (in config bits, not a register)- ADC inputs- ComparatorsI usually use the config code generator to put all the config bits in the code rather than depending on defaults for anything.

author: dannyf
author_rank: Super Contributor
timestamp: December 26, 2015, 03:05:35 pm
content: Code:LATB = 1;while(1);I would change it to something like this:Code:while(1)LATB ^= 0xff;You should then observe square waves on portb, if the code is working. Adding some delays / LEDs you can make it more obvious, without a scope or logic analyzer.QuoteWithout setting up the configuration registers and putting the pins into GPIO mode, your code is really incomplete. They are necessary elements of a working piece of code.

author: Howardlong
author_rank: Super Contributor
timestamp: December 26, 2015, 03:29:19 pm
content: A few questions...What are your #pragma configs?What oscillator are you using?What PGC/PGD port are you using with you PICkit3?Do you have a schematic?I can have a go for you here, I don't have the 64 pin but I do have the 100 and 144 pin variants.

author: Howardlong
author_rank: Super Contributor
timestamp: December 26, 2015, 05:03:26 pm
content: Here you go.Points to note.1. I use PGC2/PGD2 because RB0/RB1 (you're twiddling RB0) is shared with PGC1/PGD1.2. I use the internal FRC oscillator to save any questions over whether your oscillator is working.3. I assume all power pins are connected include AVDD and AVSS with appropriate decoupling.4. The oscillator output is available on CLKO: if you have a crystal on that pin, either unsolder it or set OSCIOFNC = OFF. Personally I find having access to CLKO very soothing when getting a device up for the first time.5. I am using the default FRCDIV which is div-by-4, so as FRC=8MHz, FOSC=2MHz, available on CLKO.5. I am using the default FRCDIV which is div-by-1, so as FRC=8MHz, Fsys=8MHz. PBCLK1 is derived from this, and by default is Fsys/2. PBCLK1 is further divided by two and available on CLKO (if enabled by OSCIOFNC), hence 2MHz on that pin. These are default settings to avoid complicating things.6. RB0 toggles at 800kHz and doesn't have a 50% duty cycle. This is due to stalling on the CPU as the GPIO is on a different clock to the CPU. I have not made any attempt to set up cache, wait states or the clock to avoid unnecessary complication.7. As a rule of thumb I use the predefined bit field structs rather than targeting an entire SFR with a gobbledegook of hex. The exception comes with LATBINV=1 because this is the only way to do a single cycle read-modify-write on an SFR. You could do it in a whole manner of different ways, this is generally recognised as the fastest.8. Much beyond this, regrettably for these devices you're going to have to use Harmony.9. Yes, the ADCs on the PIC32MZ EC devices are almost a complete waste of time. The EF devices are infinitely better for this.10. This was written for a PIC32MZ2048ECH100 on an MA320012 PIM on an Explorer 16 board, but it has been written to be portable across any current PIC32MZ device.Edit:11. Do you have a 10k resistor from MCLR to Vdd?Code:#include <xc.h>#pragma config FNOSC = FRCDIV // Fast RC Osc w/Div-by-N (FRCDIV)#pragma config OSCIOFNC = ON // Show clock on CLKO pin#pragma config POSCMOD = OFF //Primary oscillator off#pragma config FWDTEN = OFF // Watchdog off#pragma config FDMTEN = OFF // Deadman Timer is disabled#pragma config JTAGEN = OFF // JTAG Disabled#pragma config ICESEL = ICS_PGx2 // PGC2/PGD2int main(void){TRISBbits.TRISB0=0;LATBbits.LATB0=1;while (1){LATBINV=1; // Invert RB0}return 0;}

author: jmsigler
author_rank: Regular Contributor
timestamp: December 26, 2015, 08:00:17 pm
content: Okay, I'm officially retarded. I was hitting the build button on the top of MPLAB and thought I was compiling my project. I actually looked closely at the output this time and realized that I had been compiling the wrong project this whole time (I had been naming my projects pretty similar things like ProjTest1 or ProjTest2). I saw that the build time was suspiciously short at 100ms and then found my mistake.Here is the code that I used and worked setting R8 high:Code:#include <xc.h>#pragma config FNOSC = FRCDIV // Fast RC Osc w/Div-by-N (FRCDIV)#pragma config OSCIOFNC = OFF // Show clock on CLKO pin#pragma config POSCMOD = OFF //Primary oscillator off#pragma config FWDTEN = OFF // Watchdog off#pragma config FDMTEN = OFF // Deadman Timer is disabled#pragma config JTAGEN = OFF // JTAG Disabledvoid main(){ANSELB = 0;CNPUB = 1;LATB = 0; // All of port B pins offTRISB = 0; // All of port B pins are outputLATBbits.LATB8 = 1; // Turn on B8while(1);return(0);}Thanks for the help everyone! I will probably be back soon with more stupid questions.

author: Howardlong
author_rank: Super Contributor
timestamp: December 26, 2015, 08:50:17 pm
content: Making a habit of setting all ANSELs to the appropriate setting every time is a very good one, by the way. Depending on the PIC they might be called something different, and have a different setting, just to confuse further, but yes, in general they all default to analogue inputs if they have an ADC mux on them.