alimash
Newbie level 4
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)
// FOSCSEL
#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)
// FOSC
#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)
// FWDT
#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)
// FPOR
#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)
// FICD
#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.
#include <xc.h>
unsigned char portValue=0;
void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void) {
if(IFS0bits.T1IF==1){
LATBbits.LATB0 = ~LATBbits.LATB0;
IFS0bits.T1IF=0;
}
}
int main(void) {
TRISAbits.TRISA3=0;
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;
T1CONbits.TON=1;
while (1){
}
return 0;
}
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)
// FOSCSEL
#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)
// FOSC
#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)
// FWDT
#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)
// FPOR
#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)
// FICD
#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.
#include <xc.h>
unsigned char portValue=0;
void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void) {
if(IFS0bits.T1IF==1){
LATBbits.LATB0 = ~LATBbits.LATB0;
IFS0bits.T1IF=0;
}
}
int main(void) {
TRISAbits.TRISA3=0;
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;
T1CONbits.TON=1;
while (1){
}
return 0;
}
--- Updated ---
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 [/ // 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) // FOSCSEL #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) // FOSC #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) // FWDT #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) // FPOR #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) // FICD #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. #include <xc.h> unsigned char portValue=0; void __attribute__((interrupt,no_auto_psv)) _T1Interrupt(void) { if(IFS0bits.T1IF==1){ LATBbits.LATB0 = ~LATBbits.LATB0; IFS0bits.T1IF=0; } } int main(void) { TRISAbits.TRISA3=0; 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; T1CONbits.TON=1; while (1){ } return 0; } ]