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

Help needed on timer1, 32ns interrrupt

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



Joined: 08 Feb 2011
Posts: 32

View user's profile Send private message

Help needed on timer1, 32ns interrrupt
PostPosted: Wed Dec 28, 2011 3:33 pm     Reply with quote

Hi guys,

I found it difficult to set Timer1 to generate interrupt every 32ns using a 4MHz clock.

in this case have the following doubts:

1-PIC can generate interrupts every 32ns clocked at 4 MHz?

2-I have to use it on (timer mode, on each cycle) or (counter mode, on each rising edge)? TMR1CS

only to explain, do not try to compile.
Code:


 # use delay (4M = int)

 / * = 1:1 TMR0 Prescaler Period = 224 ns = 32 * /
 # define tmr1_value 224 (interrupt Every 32 cycles, with 4 MHz internal clock)

 # int_TIMER1
 TIMER1_isr void (void) {
   set_timer1 (tmr1_value);

   output_toggle (PIN_B1)
 }

 void main () {
 setup_timer_1 (T1_INTERNAL | T1_DIV_BY_1);
 set_timer1 (tmr1_value);
 enable_interrupts (INT_TIMER1);
 enable_interrupts (GLOBAL);

  while (1) {}
 }


however the minimum time is greater than 98ns

know there's something wrong, or am I missing in the code or not pic can´t interrupt too fast clocked at 4mhz.

Thank you in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19569

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 3:56 pm     Reply with quote

No way.
You are so many orders of magnitude from being able to do this it is insane...

Your master clock is 4Mhz - 250nSec cycle.
A single instruction of the PIC takes 4 clock cycles - 1000nSec.
An interrupt, using standard handlers, takes typically 60+ clock instructions - 60000nSec

The chip is a factor of perhaps 2000* too slow to achieve this.

I think you are slipping your number formats and possibly mean uSec, not nSec. Still a factor of about 2* too slow, but actually just about possible with special coding. However the chip won't be able to do anything else at all.
1) If you want to actually do anything else, use the hardware PWM. It is designed to generate clocks for you without the CPU doing anything.
2) If the functions you want to use only execute in the interrupt code, then don't use an interrupt!. Use 'linear' coding, putting everything in the main. You can put a maximum of about 30 instructions in a routine looping in the main, and get operation at 32uSec/loop.

Best Wishes
freesat



Joined: 08 Feb 2011
Posts: 32

View user's profile Send private message

PostPosted: Wed Dec 28, 2011 4:33 pm     Reply with quote

Hi Ttelmah, thanks for reply.

I want to do a AC Phase Control with 255 steps, and it is not possible with hardware pwm.


I can do it with 85 steps at 4Mhz, but with 85 steps, increments are only by 3.
Code:

setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1( 200 );

So, what clock I need to use to get 255 steps? ( 8.33ms / 255 = 0.032 ) Minimal of 12mhz?

ps... use delay instead timer to trigger triac is not good, pic freeze on delay, don't do anything before delay expire.

Maybe I'm confused with uSec and nSec.
RHA



Joined: 25 Apr 2006
Posts: 31
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 29, 2011 3:37 am     Reply with quote

As Ttelmah already described, the compiler generates some code for the interrupts (saving/reload registers an so on) which takes some time :
Code:
00008:  MOVWF  04
0000A:  MOVFF  FD8,05
0000E:  MOVFF  FE0,06
00012:  MOVLB  0
00014:  MOVFF  FE9,0C
00018:  MOVFF  FEA,07
0001C:  MOVFF  FE1,08
00020:  MOVFF  FE2,09
00024:  MOVFF  FD9,0A
00028:  MOVFF  FDA,0B
0002C:  MOVFF  FF3,12
00030:  MOVFF  FF4,13
00034:  MOVFF  FFA,14
00038:  MOVFF  FF5,15
0003C:  MOVFF  FF6,16
00040:  MOVFF  FF7,17
00044:  MOVFF  00,0E
00048:  MOVFF  01,0F
0004C:  MOVFF  02,10
00050:  MOVFF  03,11
00054:  BTFSS  FF2.5
00056:  GOTO   0060
0005A:  BTFSC  FF2.2
0005C:  GOTO   00D2
00060:  BTFSS  F9D.0
00062:  GOTO   006C
00066:  BTFSC  F9E.0
00068:  GOTO   00E6
0006C:  BTFSS  F9D.1
0006E:  GOTO   0078
00072:  BTFSC  F9E.1
00074:  GOTO   00F4
00078:  BTFSS  FA0.1
0007A:  GOTO   0084
0007E:  BTFSC  FA1.1
00080:  GOTO   00FA
00084:  MOVFF  0E,00
00088:  MOVFF  0F,01
0008C:  MOVFF  10,02
00090:  MOVFF  11,03
00094:  MOVFF  0C,FE9
00098:  MOVFF  07,FEA
0009C:  BSF    07.7
0009E:  MOVFF  08,FE1
000A2:  MOVFF  09,FE2
000A6:  MOVFF  0A,FD9
000AA:  MOVFF  0B,FDA
000AE:  MOVFF  12,FF3
000B2:  MOVFF  13,FF4
000B6:  MOVFF  14,FFA
000BA:  MOVFF  15,FF5
000BE:  MOVFF  16,FF6
000C2:  MOVFF  17,FF7
000C6:  MOVF   04,W
000C8:  MOVFF  06,FE0
000CC:  MOVFF  05,FD8
000D0:  RETFIE 0


Because of that your controller will be too slow at 4 MHz when using interrupts for the PWM.
You have to use a faster Controller (or a higher Clock) or try to use hardware PWM.
Why do you mean it is not possible to use hardware PWM ?
Does your controller have PLL? If so you can use 16MHz with internal clock.
Which controller are you using?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 29, 2011 4:00 am     Reply with quote

Quote:
Maybe I'm confused with uSec and nSec.

Obviously you are. The required time resolution is in fact 32 us, which isn't a problem with 4 MHz clock. You won't want to trigger an interrupt every 32 us, which still may be too fast regarding interrupt overhead rather than set the timer event to the calculated switching time, or use the timer output compare feature, if available with your PIC familiy.
RHA



Joined: 25 Apr 2006
Posts: 31
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 29, 2011 5:03 am     Reply with quote

FvM wrote:
Quote:
Maybe I'm confused with uSec and nSec.

Obviously you are. The required time resolution is in fact 32 us, which isn't a problem with 4 MHz clock. You won't want to trigger an interrupt every 32 us, which still may be too fast regarding interrupt overhead rather than set the timer event to the calculated switching time, or use the timer output compare feature, if available with your PIC familiy.

Yes, he is.

He want´s to build a phase control, i think for 60Hz. So you have 16,66ms per period, which means 8,33ms per a half period. He want´s to control in 255 steps .... per half period. So you have 32us (0,032ms) per step.

But in fact, 32us are a problem for the controller.

The controller, with 4MHz clock, has a cycle from 1us. if you take a look to the assembler code which is generated from the compiler for handling the interrupts you will see that only the code for that needs more than 32us. so i think every second interrupt will be lost.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 29, 2011 11:16 am     Reply with quote

Quote:
But in fact, 32us are a problem for the controller.

A periodical 32 us interrupt, yes. But there are better way to achieve phase angle control with 32 us resolution.
RHA



Joined: 25 Apr 2006
Posts: 31
Location: Germany

View user's profile Send private message

PostPosted: Thu Dec 29, 2011 11:26 pm     Reply with quote

I would use a controller with hardware-PWM and synchronize it with zero crossing.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Dec 30, 2011 1:01 pm     Reply with quote

freesat wrote:

Maybe I'm confused with uSec and nSec.


Yes, you are.

1mS = 1/1000th of a second. (10E-3)
1uS = 1/1,000,000 of a second (10E-6)
1nS = 1/1,000,000,000 of a second (10E-9)

Another item to consider is that a lot of the IRQ overhead can go away if you use FAST interrupts -- but you need a PIC18F device for that if I remember correctly.

(I use PIC18 pretty much all the time now, so I don't remember if the PIC16 has the fast stack return or not. I don't think they do)

Check the CCS manual for the different between HIGH and FAST interrupts.

You might be able to get 32uS (NOT nS!!) if you use a PIC with the fastest clock you can get (48MHz or more) and a FAST ISR.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
freesat



Joined: 08 Feb 2011
Posts: 32

View user's profile Send private message

PostPosted: Wed Jan 04, 2012 10:33 pm     Reply with quote

I want to say thanks for explanations, I'm using a 20mhz clock and it is working fine... thanks all.

I want to explain the reason I do not use hardware pwm, is because I have to control 12 pwm on same pic and I only do it with success using interrupts. With interrupts, I can turn on every pwm pin at different duty.

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