bbaacc01
12-05-2009, 09:47 PM
Anh chị nào dùng dspic cho mình hỏi với, mình dùng dspic33F J256MC710.
Mình viết thử chuơng trình như đoạn code kèm theo,
chương trình dùng thạch anh nội, nhấp nháy 2 led, một led nháy bởi timer, một led bở ngắt ngoài.
Không hiểu tại sao pic cứ chạy chừng vài phút (1-2 phút) là bị khởi động,
sau khi khởi động thanh ghi RCON từ 0x0000 chuyển thành 0x0093.
Một điều chắc chắn là không phải tại mạch, vì lúc đầu mình có dịch đuợc một file hex, lấy file đó chạy lại thì không bị khởi động.
Mình nghĩ lỗi là do một exception (traps),
Nhưng không hiểu nỗi nguyên nhân cụ thể là cái gì.
Mình có hỏi trên microchip forum, nguời ta cũng bảo có thể là traps, và dùng Trick của ông Calum để tìm nguyên nhân, nhưng mình chẳng biết trick đó ở đâu.
Mong anh chị, bạn bè giúp mình giải quyết vấn đề này
#include "p33Fxxxx.h"
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC);
// Enable Clock Switching and Configure Primary Oscillator in XT mode
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
void __attribute__((interrupt, no_auto_psv)) _INT1Interrupt(void)
{
LATFbits.LATF5 = 0;//~LATFbits.LATF2;
IFS1bits.INT1IF = 0; //Clear the INT1 interrupt flag or else
}
/************************************************** **/
void Init_Timer23( void )
{
T2CON = 0; // Timer reset
T3CON = 0;
T2CONbits.T32 = 1;
IFS0bits.T3IF = 0; // Reset Timer1 interrupt flag
IPC2bits.T3IP = 6; // Timer1 Interrupt priority level=4
IEC0bits.T3IE = 1; // Enable Timer1 interrupt
T2CONbits.TCKPS = 0b00;
T2CONbits.TCS = 0;
TMR2= 0x0000;
TMR3= 0x0000;
PR3 = 0X0262; // Timer1 period register = 1 s
PR2 = 0x5A00 ;
T2CONbits.TON = 1; // Enable Timer1 and start the counter
}
/************************************************** *******/
/* ISR ROUTINE FOR THE TIMER23 INTERRUPT */
void __attribute__((interrupt,no_auto_psv)) _T3Interrupt( void )
{
IFS0bits.T3IF = 0;
T2CONbits.TON = 0;
LATFbits.LATF2 = ~LATFbits.LATF2;
TMR2 = 0;
TMR3 = 0;
T2CONbits.TON = 1;
}
/************************************************/
int main (void)
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD=41; // M=43
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Configure the Analog functional pins as digital
AD1PCFGL=0xFFFF;
AD1PCFGH=0xFFFF;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH(0x01); // Initiate Clock Switch to
// FRC with PLL (NOSC=0b001)
__builtin_write_OSCCONL(0x01); // Start clock switching
while(OSCCONbits.COSC != 0b001); // Wait for Clock switch to occur
while(OSCCONbits.LOCK!=1); // Wait for PLL to lock
Init_Timer23();
IFS1bits.INT1IF = 0; /*Reset INT1 interrupt flag */
IEC1bits.INT1IE = 1; /*Enable INT1 Interrupt Service Routine */
TRISF=0x0000;
PORTF=0xffff;
while (1) {
}
return 0;
}
Mình viết thử chuơng trình như đoạn code kèm theo,
chương trình dùng thạch anh nội, nhấp nháy 2 led, một led nháy bởi timer, một led bở ngắt ngoài.
Không hiểu tại sao pic cứ chạy chừng vài phút (1-2 phút) là bị khởi động,
sau khi khởi động thanh ghi RCON từ 0x0000 chuyển thành 0x0093.
Một điều chắc chắn là không phải tại mạch, vì lúc đầu mình có dịch đuợc một file hex, lấy file đó chạy lại thì không bị khởi động.
Mình nghĩ lỗi là do một exception (traps),
Nhưng không hiểu nỗi nguyên nhân cụ thể là cái gì.
Mình có hỏi trên microchip forum, nguời ta cũng bảo có thể là traps, và dùng Trick của ông Calum để tìm nguyên nhân, nhưng mình chẳng biết trick đó ở đâu.
Mong anh chị, bạn bè giúp mình giải quyết vấn đề này
#include "p33Fxxxx.h"
// Select Internal FRC at POR
_FOSCSEL(FNOSC_FRC);
// Enable Clock Switching and Configure Primary Oscillator in XT mode
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
void __attribute__((interrupt, no_auto_psv)) _INT1Interrupt(void)
{
LATFbits.LATF5 = 0;//~LATFbits.LATF2;
IFS1bits.INT1IF = 0; //Clear the INT1 interrupt flag or else
}
/************************************************** **/
void Init_Timer23( void )
{
T2CON = 0; // Timer reset
T3CON = 0;
T2CONbits.T32 = 1;
IFS0bits.T3IF = 0; // Reset Timer1 interrupt flag
IPC2bits.T3IP = 6; // Timer1 Interrupt priority level=4
IEC0bits.T3IE = 1; // Enable Timer1 interrupt
T2CONbits.TCKPS = 0b00;
T2CONbits.TCS = 0;
TMR2= 0x0000;
TMR3= 0x0000;
PR3 = 0X0262; // Timer1 period register = 1 s
PR2 = 0x5A00 ;
T2CONbits.TON = 1; // Enable Timer1 and start the counter
}
/************************************************** *******/
/* ISR ROUTINE FOR THE TIMER23 INTERRUPT */
void __attribute__((interrupt,no_auto_psv)) _T3Interrupt( void )
{
IFS0bits.T3IF = 0;
T2CONbits.TON = 0;
LATFbits.LATF2 = ~LATFbits.LATF2;
TMR2 = 0;
TMR3 = 0;
T2CONbits.TON = 1;
}
/************************************************/
int main (void)
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD=41; // M=43
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
// Disable Watch Dog Timer
RCONbits.SWDTEN=0;
// Configure the Analog functional pins as digital
AD1PCFGL=0xFFFF;
AD1PCFGH=0xFFFF;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH(0x01); // Initiate Clock Switch to
// FRC with PLL (NOSC=0b001)
__builtin_write_OSCCONL(0x01); // Start clock switching
while(OSCCONbits.COSC != 0b001); // Wait for Clock switch to occur
while(OSCCONbits.LOCK!=1); // Wait for PLL to lock
Init_Timer23();
IFS1bits.INT1IF = 0; /*Reset INT1 interrupt flag */
IEC1bits.INT1IE = 1; /*Enable INT1 Interrupt Service Routine */
TRISF=0x0000;
PORTF=0xffff;
while (1) {
}
return 0;
}