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 CCS Technical Support

dspic30f4013 and timers

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



Joined: 05 Jun 2006
Posts: 41
Location: Belgium

View user's profile Send private message Visit poster's website MSN Messenger

dspic30f4013 and timers
PostPosted: Sun Sep 26, 2010 2:20 am     Reply with quote

hello group
i have a timer issue and i know not what is happening

i have a external 20ms intterrupt form zerocross detection (mains)
i want to syncronize a second timer for thyristor firering...
but the timer count only to 8....

any sugestions?

Carl
Code:

#include <30f4013.h>

#FUSES NOWDT // Watch Dog Timer
#FUSES EC
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#fuses FRC_pll16 // div by 3 and  multiply the ext. clock of 40mhz by 8x
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#define XTAL_FREQUENCY 117920000//,RESTART_WDT
#use delay(clock=XTAL_FREQUENCY)
#use rs232(stream = RS232, baud=115200,parity=N,xmit=PIN_F5,rcv=PIN_F4,bits=8)

/////////////////// Variabelen /////////////////
int32       tel,tempvar;               
#define thry_azi_ccw   PIN_B3   // output
/////////////////////////////////////////////////////////////////////////////////////
//   incoming interrupt every 20 ms from mains  /
/////////////////////////////////////////////////////////////////////////////////////
#int_ext0
void EXT_ISR()
{
   tempvar= tel;
   tel=0;
   set_timer1(2);
}
/////////////////////////////////////////////////////////////////////////////////////

#INT_TIMER1
void timer1_isr(void)
{
   tel++;
   output_high(thry_azi_ccw);
   output_low(thry_azi_ccw);
 }

/////////////////////////////////////////////////////////////////////////////////////
Void main()
{
   EXT_INT_EDGE(L_TO_H);
    set_timer1(0);
   setup_timer1(TMR_internal|TMR_DIV_BY_1);
   enable_interrupts(INT_EXT0);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(intr_GLOBAL);

   While(1)
   {
      printf("%c[2J%c[H",27,27);
      printf("tel               %u\n\r",tempvar);
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 4:09 am     Reply with quote

Comments:
117920000.......
Max supported frequency is 40MHz.

If you are firing a thyristor off a pin, you presumably want it 'X' uSec after the external interrupt triggers. As such, leave the timer interrupt disabled, till the external interrupt triggers. Then in the external interrupt, setup the timer, and enable it's interrupt, then in it's interrupt, disable the interrupt. This way the trigger will only occur when the external event is seen, avoiding accidental triggers. I can't see any reason to keep triggering the pin after this point. Once a thyristor is fired, it remains on, till the next zero crossing (except for things like GTO types)..

Then, your 'print' statement, which is meant to reflect the count of interrupts, won't. You send something like 26 characters each time. At 115200bps, 2.2mSec. Your output will be falling behind the counter all the time.
Your print, also uses '%u'. Look up the correct format for an int32.

Best Wishes
on7nh



Joined: 05 Jun 2006
Posts: 41
Location: Belgium

View user's profile Send private message Visit poster's website MSN Messenger

PostPosted: Sun Sep 26, 2010 4:36 am     Reply with quote

@ Ttelmah

max frequency is 120Mhz !
internal osc *16 = 117920000

If you not set this value into the osc, all communication is wrong
40 mhz by external osc....then internal PLL !

I see also the 8 output pulses on my scope.... so i am very sure about this value...
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 9:50 am     Reply with quote

Yes, may internal frequency, but not maximum crystal. This is 40MHz.
The 120MHz, is not the 'XTAL_FREQUENCY', but the internal clock rate. The maximum allowed external clock, is 40MHz. This is the 'XTAL_FREQUENCY'. Your internal clock rate can then be a multiple of this, but to my mind your nomenclature is totally wrong.
You have both the fuses selected to run an external clock without PLL, and the internal RC. Wrong. You talk about a 40MHz external clock, but don't appear to actually be using this (FRC=use internal RC oscillator). 7.37*16.

Not a crystal at all....

This sort of error, is a sign of 'careless' thinking, and is not the way to get things to actually work right.

The timer will wrap at 449.8 times per second. 2.22mSec. At 50Hz, you get an interrupt from the edge detector, every 20mSec. Since the first pulse will occur at this interval after the interrupt, you would expect to see eight pulses, before the next external interrupt occurs, which sets 'tel' to zero, so the count will never go past 8.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 10:22 am     Reply with quote

I think, the timer behaviour explanation is correct. Regarding conflicting fuses, CCS C is using the last setting in this case and simply ignores the wrong EC fuse. I also didn't understand what's been said about a 40 MHz crystal.
on7nh



Joined: 05 Jun 2006
Posts: 41
Location: Belgium

View user's profile Send private message Visit poster's website MSN Messenger

problem solved ...
PostPosted: Sun Sep 26, 2010 12:02 pm     Reply with quote

thank for the input,
problem was in timer settings,

the correct way was :
Code:
   setup_timer1(TMR_internal|TMR_DIV_BY_1,1900);



btw,
frequency i am running is :
Code:
#define XTAL_FREQUENCY 117920000

internal osc and FRC_PLL16...
serial communication is working...so i believe that the freq set is correct Not,

thanks for replying....
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 1:33 pm     Reply with quote

No, CCS doesn't just use the last fuse. It effectively 'OR's the patterns together. If the first one happens to be a '0' pattern, then it has no effect, but otherwise using two conflicting fuses results in a third pattern that may well not work at all.
On the oscillator, change the name you give your frequency to something like 'CLOCK_RATE'. It is _not_ a XTAL at all, and while you think it doesn't matter, it is a bit like calling a screwdriver a hammer. It doesn't matter while _you_ are the only person involved in the code, but if someone else later has to get involved with the code, they will get mislead. It is vital in code development, to 'call a spade a spade'....

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Sep 26, 2010 11:33 pm     Reply with quote

Quote:
No, CCS doesn't just use the last fuse. It effectively 'OR's the patterns together.

It actually does in the present case. If EC is last, it sets EC, if FRC_PLL16 is last, it set's FRC_PLL16. Did you try yourself?

Please notice, that I just reported an empirical observation without jumping into conclusions.
Quote:
CCS C is using the last setting in this case and simply ignores the wrong EC fuse.
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