View previous topic :: View next topic |
Author |
Message |
congthinh95
Joined: 31 Aug 2017 Posts: 7
|
XT_PLL and interrupt timer2 for dspic30f6015 |
Posted: Thu Aug 31, 2017 6:31 am |
|
|
Hello all, i have problem with crystal and interrupt on dspic30f6015.when i test blink led andinterrupt timer2 can't work.Can you help me plz. Tks so much. THis is my code. I use crystal 10Mhz, and XT_PLL8
Code: | #include <30F6015.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES XT_PLL8
#device ICSP=1
#use delay(clock=80000000,crystal=10000000)
#define data Pin_d2
#define CK Pin_d3
#define latch Pin_d4
#include<74hc595_THINH.c>
unsigned int32 u=0,v=2222;
unsigned int32 i=0;
#INT_TIMER2
void timer2_isr(void)
{
i++;
if(i==1000){i=0;u=u+1;v=v+1;}
set_timer2(9980);
}
void main()
{
enable_interrupts(global);
enable_interrupts(INT_TIMER2);
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 9998);
set_tris_d(0x0000);
while(TRUE)
{
shownumber(u);shownumber(v);
delay_ms(10);
//TODO: User Code
}
} |
_________________ Improve dspic more |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Aug 31, 2017 7:22 am |
|
|
Get rid of this line:
set_timer2(9980);
You never need to use this on the DsPIC's. With conventional PIC's you set 'to' a value, because the timers always count to 65535 (or 255), then reset to zero on the next count. So on these if you want a count less than this you have to set the tijmer to a value.
On the DsPIC's you can instead specify where you want them to reset in the setup (this is the 9998 you have in your setup).
Now the counters count _up_ on the PIC24/30. So you are setting the timer to 9990, and it is counting on every instruction (DIV_1). It interrupts when the count matches 9998. Since it takes typically about 30 instructions to get out of the interrupt handler, the interrupt will have triggered again before the code exits, and the handler will immediately be called again..... |
|
![](templates/subSilver/images/spacer.gif) |
congthinh95
Joined: 31 Aug 2017 Posts: 7
|
|
Posted: Thu Aug 31, 2017 8:19 am |
|
|
HI Ttelmah, I deleted but it doesn't work too. when i dont use interrupt timer, it can work. i don't know what is trouble here. Can you show me an example about interrupt timer2 on dspic. tks you so much! _________________ Improve dspic more |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Aug 31, 2017 8:50 am |
|
|
Seriously, have you actually checked that your chip is working?.
You need to start simpler, and just operate a 1/sec LED flash and setup things correctly first.
Make sure the chip is running at the speed you expect first.
Then, using a chip at 16Mips, I use:
Code: |
setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64, 1249); //Should give 5mSec
enable_interrupts(INT_TIMER1);
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_256, 62499); //should give 1 second
enable_interrupts(INT_TIMER2);
|
So counting cycles, 16000000/ (256*62500) = 1
1600000/(64*1250) = 200
In my case I want the 'tick' timer to have priority below the hardware handlers, so:
Code: |
#INT_TIMER1 level=5 //Put this below the other hardware events
void multiplex(void) //called every 5mSec
{
static unsigned int8 ctr=0;
static unsigned int8 tenth=19; //tick for 1/10th second events
//routine to automatically call various functions at 20mSec ticks
if (ctr==0)
handle_adcs(); //trigger ADC sampling (also starts SPI transfer).
if (ctr==1)
{
if (spi_running)
handle_SPI(); //Now handle the SPI results from DMA
}
ctr++;
if (ctr>=4) //Currently 4 different routes possible
ctr=0;
if (tenth>0)
--tenth;
else
{
tenth=19;
}
}
|
I see no suggestion in your code of how you would know it is doing anything?. No debug enabled, no LED output etc.. |
|
![](templates/subSilver/images/spacer.gif) |
congthinh95
Joined: 31 Aug 2017 Posts: 7
|
|
Posted: Thu Aug 31, 2017 9:04 am |
|
|
Tks you so much, my code was working when i use timer2 with 16bit. I read in datasheet about timer2 that i see it has 32bit timer. _________________ Improve dspic more |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Aug 31, 2017 9:12 am |
|
|
You need to read the data sheet again....
You can internally cascade timer 'pairs', for the even numbered timers. So timer2 can be paired with timer3 etc..
To do this you use the option 'TMR_32_BIT' in the timer setup. |
|
![](templates/subSilver/images/spacer.gif) |
|