dspic33f code review

Go To Last Post
9 posts / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to use the timer 1 module in the dsPIC33FJ12MC202 to toggle the status of a led connected to RB0.
IDE: Microchip MpLab.
Configuration: internal fast RC oscillator ; Watchdog timer off...
The code run just fine on proteus simulator, but it wont work when writing the code to the physical chip.
Note that the i successfully run another code on the dspic, but no timer module were involved.
I need you to check the code for me for any mistake.
This is my code:

// DSPIC33FJ12MC202 Configuration Bit Settings

// 'C' source line config statements

// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)

// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)

#pragma config FNOSC = FRC // Oscillator Mode (Internal Fast RC (FRC))
#pragma config IESO = OFF // Internal External Switch Over Mode (Start-up device with user-selected oscillator source)

#pragma config POSCMD = NONE // Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)

#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)

#pragma config FPWRT = PWR1 // POR Timer Value (Disabled)
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity)
#pragma config HPOL = ON // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset)

#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)

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


unsigned char portValue=0;

void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void) {
LATBbits.LATB0 = ~LATBbits.LATB0;

int main(void) {

TRISBbits.TRISB0 = 0;
TRISBbits.TRISB12 = 0;
TRISBbits.TRISB13 = 0;

LATBbits.LATB12 = 1;
LATBbits.LATB13 = 1;

T1CONbits.TON = 0;
IEC0bits.T1IE = 1;
IPC0bits.T1IP = 1;
// SRbits.IPL = 7;
T1CONbits.TCS = 0;
T1CONbits.TCKPS = 2;


while (1){


return 0;

Last Edited: Thu. Feb 11, 2021 - 03:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You should post this on the Microchip site.

John Samperi

Ampertronics Pty. Ltd.


* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome back to AVRFreaks!  (absent since 2017)


Hmmmm, you seem to be confused, although PIC's and AVR's are now made by the same company, this is not the site for PIC questions! 





(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you come by here again please use the < > button to post your code. It looks like a dog's dinner otherwise.


I didn't download the dsPIC33 Datasheet - that would be too much effort for a AVRfreak.  but I did browse my copy of the PIC24 Datasheet where I found TIMER1 is a synchronous/asynchronous timer and requires extra configuration steps over and above the synchronous timers. Also I didn't see any initialisation of the Period Register PR1.


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:
please use the < > button to post your code. It looks like a dog's dinner otherwise.



See Tip #1 in my signature for illustrated instructions.


Note that just about every internet forum requires you to take some such action for source code to post legibly - so be sure to find out how to do it on the PIC forums:




Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...