CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

XT_PLL and interrupt timer2 for dspic30f6015

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
congthinh95



Joined: 31 Aug 2017
Posts: 7

View user's profile Send private message

XT_PLL and interrupt timer2 for dspic30f6015
PostPosted: Thu Aug 31, 2017 6:31 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Thu Aug 31, 2017 7:22 am     Reply with quote

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.....
congthinh95



Joined: 31 Aug 2017
Posts: 7

View user's profile Send private message

PostPosted: Thu Aug 31, 2017 8:19 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Thu Aug 31, 2017 8:50 am     Reply with quote

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..
congthinh95



Joined: 31 Aug 2017
Posts: 7

View user's profile Send private message

PostPosted: Thu Aug 31, 2017 9:04 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19359

View user's profile Send private message

PostPosted: Thu Aug 31, 2017 9:12 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group