Author Topic: PIC24F + XC16  (Read 11976 times)

0 Members and 1 Guest are viewing this topic.

Offline netwinderTopic starter

  • Regular Contributor
  • *
  • Posts: 69
  • Country: ca
PIC24F + XC16
« on: July 07, 2016, 06:56:31 pm »
Hello EEVBlog Forum!

I am trying to make a light blink with a PIC24FJ16MC101, datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/30009997e.pdf and I am running into errors.

I have a few problems I can't figure out:
  • In the XC8 compiler if you define your crystal frequency, you can use the __delay_ms() function for delays. However here I cannot use the delay function
  • I can't use for loops, you will see in my code below I make a delay function that just counts however it is not ideal.
  • How to make it use its internal oscillator? I can't seem to do that, im setting the configuration bits to use the FRC oscillator, but still nothing.

Sorry for the noob-ish post but thank you for your time!

Code: [Select]
/*
 * File:   main.c
 * Author: netwinder
 *
 * Created on July 6, 2016, 8:05 PM
 */

// PIC24FJ32MC101 Configuration Bit Settings

#pragma config POSCMOD = NONE           // Primary Oscillator Select (Primary oscillator disabled)
#pragma config ALTI2C = OFF             // Alternate I2C pins (I2C mapped to SDA1/SCL1)
#pragma config LPOL = ON                // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity)
#pragma config IOL1WAY = ON             // IOLOCK Protection (Allow Only One Re-configuration)
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSC2 pin has clock out function)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = FRC              // Oscillator Mode (Internal Fast RC (FRC))
#pragma config WDTWIN = WDTWIN25        // Watchdog Window Select (Watchdog Window is 25% of WDT period)
#pragma config PWMPIN = ON              // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset)
#pragma config PWMLOCK = ON             // PWM Lock Enable (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON                // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)

// CONFIG1
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128           // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config PLLKEN = ON              // PLL Lock Enable (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF             // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = ON              // Watchdog Timer Enable (Watchdog timer always enabled)
#pragma config ICS = PGD1               // Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config HPOL = ON                // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity)
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF                // General Segment Code Protection (General Segment Code protect is disabled)

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

#include <xc.h>

#include "headers.h"


 #define _XTAL_FREQ 80000000

/*
 *
 */
int main(int argc, char** argv) {
   //OSCCON = 0x1100;
   //Set up I/O
   AD1PCFGL = 0xFFFF; //set to all digital I/O
   TRISB = 0xFFFF; //configure all PortB as input
   RPINR18bits.U1RXR = 2; //UART1 receive set to RB2/RP2
   
   //Main Program Loop, Loop forever
   while (1){
        LATBbits.LATB4 = 1; //RB4 = 1
        delay();
        LATBbits.LATB4 = 0; //RB4 = 1
        delay();
   }
   return(0);
}

void delay(){
    int i = 10000;
    int x = 0;
    while(x != i) {
        x += x + 1;
    }
}
 

Offline 22swg

  • Frequent Contributor
  • **
  • Posts: 274
  • Country: gb
Re: PIC24F + XC16
« Reply #1 on: July 07, 2016, 07:30:28 pm »
I hit the same problem but had timer loops ( no accurate though) eventually found this snippet..
Code: [Select]
#include <libpic30.h>

#define FCY 8000000UL       // instruction cycle Hrtz

// libpic30 delays
#define __delay_ms(d) \
  { __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000ULL)); }
#define __delay_us(d) \
  { __delay32( (unsigned long) (((unsigned long long) d)*(FCY)/1000000ULL)); }
[\code]
Check your tongue, your belly and your lust. Better to enjoy someone else’s madness.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: PIC24F + XC16
« Reply #2 on: July 07, 2016, 07:44:23 pm »
There's a Freudian bug in your code:

Code: [Select]
void delay(){
    int i = 10000;
    int x = 0;
    while(x != i) {
        x += x + 1;
    }
}

I guess you meant:
Code: [Select]
        x += 1;

Typically, you may want to make your loop termination using the <, <=, >=, > operators instead of the == or !=:

Code: [Select]
void delay(){
    int i = 10000;
    int x = 0;
    while(x < i) {
        x += 1;
    }
}
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: PIC24F + XC16
« Reply #3 on: July 07, 2016, 08:20:33 pm »
I tend to use a timer for that kind of thing with a state machine, or just by setting flags if it is a quick test!

In my experience you have to set up the OSCCON and OSCTUN registers to use internal.
Check the datasheet to make sure you don't have to "unlock" the clock registers beforehand.
 

Offline SteveyG

  • Supporter
  • ****
  • Posts: 997
  • Country: gb
  • Soldering Equipment Guru
Re: PIC24F + XC16
« Reply #4 on: July 07, 2016, 08:25:09 pm »
If you #include <libpic32.h>, you can use the delay function __delay32(uint32_t Tcy) which will delay by Tcy clock cycles.

You can also use __delay_ms() and __delay_us()


edit: oops, just saw this was already pointed out.
« Last Edit: July 07, 2016, 08:27:35 pm by SteveyG »
YouTube Channel: https://www.youtube.com/user/sdgelectronics/
Use code: “SDG5” to get 5% off JBC Equipment at Kaisertech
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5429
  • Country: gb
Re: PIC24F + XC16
« Reply #5 on: July 07, 2016, 08:52:52 pm »
You can use __delay_ms() and __delay_us() in XC16 and XC32 too,  but instead of

#define_XTAL_FREQ // Raw oscillator freq

you do this:

#define FCY 7370000/2 // instruction cycle freq
#include <libpic30.h>

See this post: https://www.eevblog.com/forum/microcontrollers/pic24f-pickit3/msg978576/#msg978576
 

Offline netwinderTopic starter

  • Regular Contributor
  • *
  • Posts: 69
  • Country: ca
Re: PIC24F + XC16
« Reply #6 on: July 07, 2016, 09:20:52 pm »
If you #include <libpic32.h>, you can use the delay function __delay32(uint32_t Tcy) which will delay by Tcy clock cycles.

You can also use __delay_ms() and __delay_us()


edit: oops, just saw this was already pointed out.

I cant include libpic32 with the x16 compiler.

I tend to use a timer for that kind of thing with a state machine, or just by setting flags if it is a quick test!

In my experience you have to set up the OSCCON and OSCTUN registers to use internal.
Check the datasheet to make sure you don't have to "unlock" the clock registers beforehand.

You can use __delay_ms() and __delay_us() in XC16 and XC32 too,  but instead of

#define_XTAL_FREQ // Raw oscillator freq

you do this:

#define FCY 7370000/2 // instruction cycle freq
#include <libpic30.h>

See this post: https://www.eevblog.com/forum/microcontrollers/pic24f-pickit3/msg978576/#msg978576

I saw the post, I now made my code the same as the post's sanity check code
Code: [Select]
#pragma config OSCIOFNC = OFF           // CLKO Enable Configuration bit (CLKO output signal is active on the OSCO pin)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
// Some DIP/SOIC/SSOP dsPIC33/PIC24s have pins 4&5 as PGD3 rather than PGD1, uncomment next line if so
//#pragma config ICS = PGD3               // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)

#include <xc.h>

// FCY depends on the specific device's internal oscillator and default CLKDIV<10:8> bits
#define FCY 7370000/2 // All dsPIC33s, a few PIC24s
//#define FCY 8000000/4 // Most but not all PIC24s

#include <libpic30.h>

int main(void)
{
    TRISBbits.TRISB15=0;
    while (1)
    {
        LATBbits.LATB15=1;
        __delay_ms(250);
        LATBbits.LATB15=0;
        __delay_ms(250);
    }
    return 0;
}


Dosen't work still, I think the output bits are not being set up properly however I could be very wrong
 

Offline netwinderTopic starter

  • Regular Contributor
  • *
  • Posts: 69
  • Country: ca
Re: PIC24F + XC16
« Reply #7 on: July 07, 2016, 09:22:46 pm »
Wait a second...
You can use __delay_ms() and __delay_us() in XC16 and XC32 too,  but instead of

#define_XTAL_FREQ // Raw oscillator freq

you do this:

#define FCY 7370000/2 // instruction cycle freq
#include <libpic30.h>

See this post: https://www.eevblog.com/forum/microcontrollers/pic24f-pickit3/msg978576/#msg978576

I was the one that made this post, well turns out i'm really wrong. I'll rebuild my circuit to match the example and i'll change my code too.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5429
  • Country: gb
Re: PIC24F + XC16
« Reply #8 on: July 07, 2016, 09:50:38 pm »
The config bits in my example are deliberately set to be the minimum required to avoid random guesswork on irrelevant settings in early stages of getting started: if I presented a page of config bits, I fear that would cause more questions than answers.

There is one extra optional config bit which I enable to give you Fcy on the CLKO pin, which you can probe with a scope. If this isn't oscillating at 7.37/2 MHz after programming, something else more fundamental is afoot.

What symptoms do you have either on the board (CLKO and RB15) or in MPLAB X (errors, or otherwise)?
 

Offline mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 14192
  • Country: gb
    • Mike's Electric Stuff
Re: PIC24F + XC16
« Reply #9 on: July 07, 2016, 09:56:47 pm »
Bear in mind that sometimes an empty for loop may get optimised out. Making the for variable volatile may stop this.
But timers on the 16 & 32 bit PICs are very easy to use, so a better solution for timing.
(having previously set up prescaler and ON bit)
PR2=<delay>
TMR2=0;_T2IF=0;while(T2IF==0);
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
The following users thanked this post: SteveyG

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: PIC24F + XC16
« Reply #10 on: July 07, 2016, 09:57:18 pm »
I wrote this to get people started on pic24/emblocks:

https://dannyelectronics.wordpress.com/2016/04/24/getting-started-on-pic24dspic-emblocks/

You can take the code and make changes to the configuration bits and initialization code. The rest should compile for your chip
================================
https://dannyelectronics.wordpress.com/
 

Offline SteveyG

  • Supporter
  • ****
  • Posts: 997
  • Country: gb
  • Soldering Equipment Guru
Re: PIC24F + XC16
« Reply #11 on: July 08, 2016, 09:27:12 am »
If you #include <libpic32.h>, you can use the delay function __delay32(uint32_t Tcy) which will delay by Tcy clock cycles.

You can also use __delay_ms() and __delay_us()


edit: oops, just saw this was already pointed out.

I cant include libpic32 with the x16 compiler.


You have to separately install the peripheral libraries in the latest XC compilers.
YouTube Channel: https://www.youtube.com/user/sdgelectronics/
Use code: “SDG5” to get 5% off JBC Equipment at Kaisertech
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 709
  • Country: gb
    • Electronic controls
Re: PIC24F + XC16
« Reply #12 on: January 29, 2020, 11:58:50 am »
Beware of the PIC compiler.
They are quick to remove code that looks like it does nothing.
Its possible your loops are being optimised out.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3630
  • Country: it
Re: PIC24F + XC16
« Reply #13 on: January 29, 2020, 06:13:38 pm »
it's libpic30, not libpic32.
Compiler documentation has everything: 16-Bit_Language_Tools_Libraries_Manual (DS50001456K)
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3630
  • Country: it
Re: PIC24F + XC16
« Reply #14 on: January 29, 2020, 06:15:44 pm »
Beware of the PIC compiler compiler optimizations and not knowing when to use volatile.
They are quick to remove code that looks like it does nothing.
Its possible your loops are being optimised out.


FTFY
 

Offline Nerull

  • Frequent Contributor
  • **
  • Posts: 694
Re: PIC24F + XC16
« Reply #15 on: January 29, 2020, 06:53:52 pm »
I've seen a case where a compiler recognized a for loop was a summation and replaced it with its closed form solution.
 

Offline mpgmike

  • Newbie
  • Posts: 4
  • Country: us
Re: PIC24F + XC16
« Reply #16 on: February 05, 2020, 10:09:36 am »
Of all the recent XC16 versions, v1.20 seemed to be plagued with the most bugs.  I hope you're not using that.  MC just released v1.50 which seems to be working rather well.  Also, MPLABX is up to v5.30 now.  You may want to jump on microchip.com and make sure you have the latest tools.

Also, Microchip provides online training to get you started on a myriad of PIC topics:

https://microchipdeveloper.com/training-self:start
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: PIC24F + XC16
« Reply #17 on: February 08, 2020, 04:37:32 pm »
I've seen a case where a compiler recognized a for loop was a summation and replaced it with its closed form solution.

In that case one should declare the loop variable as volatile.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf