PIC24F + XC16 - Page 1
Forum section: electronics / general

author: netwinder
author_rank: Regular Contributor
timestamp: July 07, 2016, 06:56:31 pm
content: Hello EEVBlog Forum!I am trying to make a light blink with a PIC24FJ16MC101, datasheethttp://ww1.microchip.com/downloads/en/DeviceDoc/30009997e.pdfand 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 functionI 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:/** 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/OAD1PCFGL=0xFFFF;//set to all digital I/OTRISB=0xFFFF;//configure all PortB as inputRPINR18bits.U1RXR=2;//UART1 receive set to RB2/RP2//Main Program Loop, Loop foreverwhile (1){LATBbits.LATB4 = 1;//RB4 = 1delay();LATBbits.LATB4 = 0;//RB4 = 1delay();}return(0);}void delay(){int i = 10000;int x = 0;while(x != i) {x += x + 1;}}

author: 22swg
author_rank: Frequent Contributor
timestamp: July 07, 2016, 07:30:28 pm
content: I hit the same problem but had timer loops ( no accurate though) eventually found this snippet..Code:#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]

author: Kalvin
author_rank: Super Contributor
timestamp: July 07, 2016, 07:44:23 pm
content: There's a Freudian bug in your code:Code:void delay(){int i = 10000;int x = 0;while(x != i) {x += x + 1;}}I guess you meant:Code:x += 1;Typically, you may want to make your loop termination using the <, <=, >=, > operators instead of the == or !=:Code:void delay(){int i = 10000;int x = 0;while(x < i) {x += 1;}}

author: Wilksey
author_rank: Super Contributor
timestamp: July 07, 2016, 08:20:33 pm
content: 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.

author: SteveyG
author_rank: Unknown
timestamp: July 07, 2016, 08:25:09 pm
content: 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.

author: Howardlong
author_rank: Super Contributor
timestamp: July 07, 2016, 08:52:52 pm
content: You can use __delay_ms() and __delay_us() in XC16 and XC32 too, but instead of#define_XTAL_FREQ // Raw oscillator freqyou 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

author: netwinder
author_rank: Regular Contributor
timestamp: July 07, 2016, 09:20:52 pm
content: Quote from: SteveyG on July 07, 2016, 08:25:09 pmI cant include libpic32 with the x16 compiler.Quote from: Wilksey on July 07, 2016, 08:20:33 pmQuote from: Howardlong on July 07, 2016, 08:52:52 pmI saw the post, I now made my code the same as the post's sanity check codeCode:#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

author: netwinder
author_rank: Regular Contributor
timestamp: July 07, 2016, 09:22:46 pm
content: Wait a second...Quote from: Howardlong on July 07, 2016, 08:52:52 pmI 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.

author: Howardlong
author_rank: Super Contributor
timestamp: July 07, 2016, 09:50:38 pm
content: 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)?

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: July 07, 2016, 09:56:47 pm
content: 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);

author: dannyf
author_rank: Super Contributor
timestamp: July 07, 2016, 09:57:18 pm
content: 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

author: SteveyG
author_rank: Unknown
timestamp: July 08, 2016, 09:27:12 am
content: Quote from: netwinder on July 07, 2016, 09:20:52 pmYou have to separately install the peripheral libraries in the latest XC compilers.

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: January 29, 2020, 11:58:50 am
content: 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.

author: JPortici
author_rank: Super Contributor
timestamp: January 29, 2020, 06:13:38 pm
content: it's libpic30, not libpic32.Compiler documentation has everything: 16-Bit_Language_Tools_Libraries_Manual (DS50001456K)

author: JPortici
author_rank: Super Contributor
timestamp: January 29, 2020, 06:15:44 pm
content: Quote from: nigelwright7557 on January 29, 2020, 11:58:50 amFTFY

author: Nerull
author_rank: Frequent Contributor
timestamp: January 29, 2020, 06:53:52 pm
content: I've seen a case where a compiler recognized a for loop was a summation and replaced it with its closed form solution.

author: mpgmike
author_rank: Newbie
timestamp: February 05, 2020, 10:09:36 am
content: 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

author: Kalvin
author_rank: Super Contributor
timestamp: February 08, 2020, 04:37:32 pm
content: Quote from: Nerull on January 29, 2020, 06:53:52 pmIn that case one should declare the loop variable as volatile.