1 uSec using Timer1 on PIC12F675

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dariods
Posts: 25
Joined: 26 Apr 2023 07:12

1 uSec using Timer1 on PIC12F675

#1 Post by dariods » 09 May 2024 17:21

I need a 1 us timer using Timer1 or Timer0 using PiC12F675 using internal oscillator at 4 MHz. Unfortunately the Timer Calculator doesnt calculate for PIC12F.. I mofified the code from the examples on libstock, 2561_timer_interruption_of_100_ms_with_pic_12f675_pic12f675_v1.0.0.0. The best i can reach is 2.5 uSec. i dont want to use Delay_us() as during the delay the pic cannot be used for other functions. Please help

Code: Select all

const  osci=0x30;        //calibration value.

void InitTimer1(){
  T1CON         = 0x01;  
  TMR1IF_bit    = 0;
  TMR1H         = 0xFF;
  TMR1L         = 0xF9;
  TMR1IE_bit    = 1;
  INTCON        = 0xC0;
}

void Interrupt(){
  if (TMR1IF_bit){
    TMR1IF_bit = 0;
    TMR1H         = 0xFF;
    TMR1L         = 0xF9;
    //Enter your code here
    gpio.B4=~gpio.B4;
  }
}

void main()
{
InitTimer1();
 trisio=0;      //all pins as output 
 ANSEL=0b0000;  // ALL ANALOG PINS configured as digital I/O.
 OSCCAL=osci;  //Load internal oscillator calibration value
   

  while(1)
  {
  }
}
}

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: 1 uSec using Timer1 on PIC12F675

#2 Post by dariods » 10 May 2024 06:38

Surprising that no one knows how to configure the Timer0 or Timer1 to 1us

paulfjujo
Posts: 1558
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: 1 uSec using Timer1 on PIC12F675

#3 Post by paulfjujo » 10 May 2024 17:52

hello,



Impossible to use a timer with 1µS delay
with at Fosc 4MHz ,
1 cycle T=FOSC/4= 1µS

1 NOP execution need 1µS
so if you want a delay of 1µS, just use :
_asm nop;

your interrupt treatment needs more than 4µS to be executed !!

or use 18F27K42 with FOSC=64Mhz
by internal special hardware 24 bits counter STM1
can count every cycle of 16nS ! .... :D below 1µS !

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: 1 uSec using Timer1 on PIC12F675

#4 Post by dariods » 10 May 2024 18:51

paulfjujo wrote:
10 May 2024 17:52
hello,



Impossible to use a timer with 1µS delay
with at Fosc 4MHz ,
1 cycle T=FOSC/4= 1µS

1 NOP execution need 1µS
so if you want a delay of 1µS, just use :
_asm nop;

your interrupt treatment needs more than 4µS to be executed !!

or use 18F27K42 with FOSC=64Mhz
by internal special hardware 24 bits counter STM1
can count every cycle of 16nS ! .... :D below 1µS !
Can I use a crystal of 16 Or 20Mhz and get the 1us Timer1 delay?

paulfjujo
Posts: 1558
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: 1 uSec using Timer1 on PIC12F675

#5 Post by paulfjujo » 10 May 2024 19:46

Even with a quartz of 20MHZ
1 Cycle is allways 4/FOSC (MHz) in µS

1 Cycle time = 1/20 *4 = 4/ 20 => 0.2µS
if interrupt duration takes more than 5 cycles => 1µS ...
include also the call interrupt (time) and return (time) to the main programm

the MCU has no time to do else ...
So, you can do nothing with your MCU ! ...

you have to considere time used by interrupt and time to execute the main loop of your program
interrupt must use as low as possible time to be able to execute the main program.
It is a general rule !

dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Re: 1 uSec using Timer1 on PIC12F675

#6 Post by dariods » 11 May 2024 08:18

Screenshot_2024-05-11-12-43-24-703_com.adobe.reader.jpg
Screenshot_2024-05-11-12-43-24-703_com.adobe.reader.jpg (302.04 KiB) Viewed 183 times
paulfjujo wrote:
10 May 2024 19:46
Even with a quartz of 20MHZ
1 Cycle is allways 4/FOSC (MHz) in µS

1 Cycle time = 1/20 *4 = 4/ 20 => 0.2µS
if interrupt duration takes more than 5 cycles => 1µS ...
include also the call interrupt (time) and return (time) to the main programm

the MCU has no time to do else ...
So, you can do nothing with your MCU ! ...

you have to considere time used by interrupt and time to execute the main loop of your program
interrupt must use as low as possible time to be able to execute the main program.
It is a general rule !
Thanks you have been very helpful. I also realised I would be losing 2 pins of the MCU to the crystal.
Just one more query. Without using timer or any interrupt what should be the OSCCAL calibration bits,high, mid or low for internal oscillator at 4 Mhz

paulfjujo
Posts: 1558
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: 1 uSec using Timer1 on PIC12F675

#7 Post by paulfjujo » 11 May 2024 13:17

hello,

As you can see in the figure Register 2-7 ESCAL OSCILLATOR CALIBRATION REGISTER
The OSCAL is set à 4MHZ by default
CAL5=1 CAL4=0 CAL3=0 CAL3=0 CAL2=0 CAL1=0 CAL0=0
so bit 7-2 )= 100000 => Center frequency


you have nothing to do , to use 4MHz ... set by default on Reset..

Post Reply

Return to “mikroC PRO for PIC General”