Simple PIC32 Starter C code (Using MPLAB C32)

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Simple PIC32 Starter C code (Using MPLAB C32)

Postby Brian Griffin » Thu Oct 06, 2011 8:31 pm

Hello there! It's a great news that the SKPIC32 is in the stores.

PIC32 is a 32-bit microcontroller from Microchip, and as usual, it has a lot more peripherals, RAM, program space than the other lower-end PIC counterparts.

In case if you are wondering, the ASM in the PIC32 is totally different, so do not bother to directly 'port' the codes from the PIC16/18F or even 24/33 to the PIC32 and expect it to work 100%. The architecture for the PIC32 is a different one, and it uses the core variant from MIPS technologies. (The things we play, like the Nintendo 64, or the Playstation, Playstation 2, PlayStation Portable, all uses the MIPS' similar architectures.)

Here is a starter code for the PIC32, but it is compiled for the Uno32. However, rest of them are similar.

CODE: SELECT_ALL_CODE
#pragma config POSCMOD=XT, FNOSC=PRIPLL
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF

#include <p32xxxx.h>
#include <system.h>

void UART1_Init();
void UART1_putc(unsigned char);
void UART1_puts(unsigned char*);
void delay500ms();

main()
{
   TRISD = 0x0000;
   TRISF = 0x0000;
        TRISGbits.TRISG6 = 0;
   SYSTEMConfigPerformance(72000000);
   mOSCSetPBDIV(OSC_PB_DIV_2);

   UART1_Init();
   UART1_puts("Hello World!");
   while(1);
}

void UART1_Init()
{
   U1BRG = 77;
   U1MODE = 0x8008;
   U1STAbits.UTXEN = 1;
   U1STAbits.URXEN = 1;
}

void UART1_putc(unsigned char inputc)
{
   while(U1STAbits.UTXBF);
   U1TXREG = inputc;
}

void UART1_puts(unsigned char* string)
{
    while(*string)
      UART1_putc(*string++);
}


The program sends the "Hello World!" sentence through the UART1. Set it to 115200 baud.

Note that the pragmas are different - there are dividers and multiplier to get the speed value for the PIC32. Example in the code, the FPLLIDIV divides the oscillator speed by two, and then FPLLMUL multiplies it by 18, and then FPLLODIV divides again by one, resulting in a 72MHz frequency. Meanwhile, the FPBDIV is the "Peripheral Bus Divide", which divides the total frequency (72MHz) into 2, resulting in a 36MHz. [In this board, crystal is 8Mhz, so it's 8 / 4 * 18 / 1 = 72]

The Peripheral bus is for the UART, SPI, I2C and the rest of the stuff. To calculate the baud rates and stuff, use the Peripheral Bus speed instead.

The SystemConfigPerformance(72000000) function fine-tunes the cache configuration registers and related for performance, since the PIC32 has a cache module inside. And, "mOSCSetPBDIV(OSC_PB_DIV_2)" is change back the Peripheral Bus Speed to 36Mhz since the SystemConfigPerformance() function will bounce back the PB Speed to the default, divide-by-one.

Reference: "Programming 32-bit Microcontrollers in C, Exploring the PIC32" by Lucio Di Jasio, 2008 Elsevier.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby ober » Thu Oct 06, 2011 9:13 pm

Brian, good work! :D
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby Brian Griffin » Thu Oct 06, 2011 9:32 pm

ober WROTE:Brian, good work! :D


Thanks. I haven't tried the SKPIC32, but I guess it will be something more gigantic and very powerful (monstrous amount of RAM) compared to my Uno32.

I would have tried FFT or Wavelet Trans. inside, but I'm in a middle of a research work there. Plus, been fiddling with an FPGA board recently. :lol:
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby shahrul » Thu Oct 06, 2011 11:58 pm

Wow, looks nice. Still long journey I wan't to learn PIC32.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby redips_id » Fri Oct 07, 2011 4:04 pm

wah.. new product again.. very fast.. congrats Cytron team! keep it up! :D
Request for complete solution for FYP? Why don't request for graduation certificate?
redips_id
Discoverer
 
Posts: 82
Joined: Wed Jan 20, 2010 6:23 pm

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby yonghui » Sat Oct 08, 2011 11:37 am

hi all,

if im not wrong. for PIC32 startkit. there is bootloader d. if you are using the bootloader, #pragma code for configuration bits wont work to change the system frequency and peripheral frequency. the system frequency will be same as the setting in bootloader. so there is no need #pragma code for the configuration bits in the code. remember to include the application linker file into the project for bootloader to program the chip correctly.

probably can change with peripheral library, SYSTEMConfigPerformance(SYSfreq); and mOSCSetPBDIV(OSC_PB_DIV_1);



regards,
yh
thanks&regards,
yh
yonghui
Professional
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby aurora » Thu Oct 13, 2011 11:48 pm

wow.. more nice product, and I haven't settle with dsPIC33 come this one already. This baby compute using true 32-bit word right? Anyone bought this and tried the HID bootloader?

What attract me most in this package is the 100-pin. PDIP package limits is 40pin, and I want something that can drive a GLCD with 16 I/O. Cytron should come up with more of such product (64/80/100pin) for PIC16/18 as well :)
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby Brian Griffin » Fri Oct 14, 2011 1:18 am

aurora WROTE:wow.. more nice product, and I haven't settle with dsPIC33 come this one already. This baby compute using true 32-bit word right? Anyone bought this and tried the HID bootloader?

What attract me most in this package is the 100-pin. PDIP package limits is 40pin, and I want something that can drive a GLCD with 16 I/O. Cytron should come up with more of such product (64/80/100pin) for PIC16/18 as well :)


I have it but it's not a SKPIC32, instead it is an Uno-32.

The RAM on those PIC32s are a few times more than any dsPIC30/33f. I did created a WAV player for that, it plays pretty well and clear.

And yes, it is using full 32-bit words. So either way, assembly is going to be much easier.

However, I do not have time to read up all the assembly codes. They have another few more "Traps" which is for handling errors or exceptions.

The SKPIC32 is more than enough for driving GLCD. Instead, you may plant an RTOS inside if you are doing some stuff together. Myke Predko's treatment of RTOS in "PRogramming & Customizing PIC Microcontrollers" is pretty comprehensive. He did rambled about multitasking with a DS1820, clock, calendar and a 44780 together on that and that example can be extended elsewhere in higher-end microcontrollers.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Simple PIC32 Starter C code (Using MPLAB C32)

Postby yonghui » Fri Oct 14, 2011 12:09 pm

hi ,


i wonder is anyone around here had tried up with microchip's audio library on PIC32. can give some comments?


thanks & regards,
yh
thanks&regards,
yh
yonghui
Professional
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 5 guests

cron