Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

dspic33f code review

Status
Not open for further replies.

alimash

Newbie level 4
Joined
Aug 28, 2015
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,421
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;
}
--- 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;
}
 
]

 

There are a number of things wrong or missing from your code.
First is the assumption that a simulator will mimic the real hardware correctly. In my experience (and nothing more!) that is partly true but simulators also add a layer of bugs as well.
Second you need to look at all of the functions that can be on any pin. The data sheet lists:
PGED1/AN2/RP0(1)/CN4/RB0
for the pin you are using. The items are in order of priority so that if you turn on a function then it will override any function listed to the right.
Always look for an 'analog' function because all ports with an analog capability will have that turned on by default at power-on. Read Section 10.2 of the data sheet.
Next look at the item on the extreme left - PGED1. Also look at the CONFIG settings, especially the one for ICS and you will see that you have said that you will be using the PGEC1/PGED1 pin pair for debugging. This is probably what is stopping your LED from being toggled.
By the way , top marks for using the LAT register.
In your ISR you don't need to test for the IF bit being set. These MCUs have multiple interrupt vectors and your ISR will ONLY be called if the T1IF bit is set. Therefore all you need is to clear the bit and toggle the LAT register.
Finally a suggestion - I have only ever once needed to alter the priority of any interrupt (and that was a very complex FreeRTOS based system; even then only 1 interrupt by one level). I would not start playing with interrupt priorities unless you can isolate whatever is going wrong to this being the actual cause
Susan
 
There are a number of things wrong or missing from your code.
First is the assumption that a simulator will mimic the real hardware correctly. In my experience (and nothing more!) that is partly true but simulators also add a layer of bugs as well.
Second you need to look at all of the functions that can be on any pin. The data sheet lists:
PGED1/AN2/RP0(1)/CN4/RB0
for the pin you are using. The items are in order of priority so that if you turn on a function then it will override any function listed to the right.
Always look for an 'analog' function because all ports with an analog capability will have that turned on by default at power-on. Read Section 10.2 of the data sheet.
Next look at the item on the extreme left - PGED1. Also look at the CONFIG settings, especially the one for ICS and you will see that you have said that you will be using the PGEC1/PGED1 pin pair for debugging. This is probably what is stopping your LED from being toggled.
By the way , top marks for using the LAT register.
In your ISR you don't need to test for the IF bit being set. These MCUs have multiple interrupt vectors and your ISR will ONLY be called if the T1IF bit is set. Therefore all you need is to clear the bit and toggle the LAT register.
Finally a suggestion - I have only ever once needed to alter the priority of any interrupt (and that was a very complex FreeRTOS based system; even then only 1 interrupt by one level). I would not start playing with interrupt priorities unless you can isolate whatever is going wrong to this being the actual cause
Susan
Thanks susan,i really appreciate your help.
I will try what you suggest about the pin function and tell you what happen.
Just a note:
The RB0 pin flashed successfully when using delays instead of timer interrupt, why the pin functions priority was not triggered then🤔.
Thanks again.
 

The RB0 pin flashed successfully when using delays instead of timer interrupt, why the pin functions priority was not triggered then🤔.
Thanks again.
On the real hardware?
In "debug" mode, or just run mode?
As Susan stated, in debug mode, that pin is reserved for the debugger. An LED on that pin could not work.
 
On the real hardware?
In "debug" mode, or just run mode?
As Susan stated, in debug mode, that pin is reserved for the debugger. An LED on that pin could not work.
On the real hardware, when runing the code.
I haven't done any debug.
The scenario i stated is is all in the run mode, no debug were made.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top