Home › Forums › Microcontrollers › PIC Microcontroller › Microsecond variable Delay in MikroC
- This topic has 6 replies, 2 voices, and was last updated 9 years, 4 months ago by Ligo George.
-
AuthorPosts
-
April 17, 2015 at 3:45 pm #11232PrasadParticipant
How to create “us” variable delay in MikroC
April 17, 2015 at 7:12 pm #11234Ligo GeorgeKeymasterMicro second variable delays will be very difficult to create and it will not accurate too as each instructions will take a few microsecond to execute. That is why MikroC doesn’t implement this function.
April 20, 2015 at 7:07 am #11239PrasadParticipantBut I need to control a 180 deg servo with pic16f84a. PIC16f84a doesn’t have PWM.
In servo motor datasheet it says that,
1000us delay = 0 deg
1500us delay = 90 deg
2000us delay = 180 deg
So, how can I obtain a delay between 1000us and 2000us
April 20, 2015 at 12:24 pm #11240Ligo GeorgeKeymasterTry like this :
switch(angle) { case 0 : PORTX.FX = 1; Delay_us(...); PORTX.FX = 0; Delay_us(...); break; case 1: PORTX.FX = 1; Delay_us(...); PORTX.FX = 0; Delay_us(...); break; }
April 21, 2015 at 9:20 pm #11250PrasadParticipantThis method is correct for some specified angles. Because delay_us(), can’t have a variable value. It should be fixed. But I want to rotate Servo in random angles. That angle comes from some calculation.
So the delay is in between 1000us and 2000us.
ex
1300, 1850 or something.
How can I obtain that type of delay.
April 22, 2015 at 10:41 am #11251Ligo GeorgeKeymasterIt is very difficult to obtain accurate variable microsecond delay function. Make the oscillator clock frequency as high as possible and you can use Delay_Cyc() in MikroC which creates delay based in clock cycle. You may also try creating your own delay function like the following (following code is not calibrated for microsecond).
void Delay(unsigned int i) { for(;i>0;i--) { } }
June 5, 2015 at 1:57 pm #11456Ligo GeorgeKeymasterHi, the following variable microsecond delay functions will work with enough accuracy if you use clock 20MHz or more.
For PIC 16Fvoid Vdelay_us(unsigned time_us) { unsigned ncyc; ncyc = Clock_MHz()>>2; ncyc *= time_us>>4; while (ncyc--) { asm nop; asm nop; } }
For PIC 18F
void Vdelay_us(unsigned time_us) { unsigned ncyc; ncyc = Clock_MHz()>>2; ncyc *= time_us>>4; while (ncyc--) { asm nop; asm nop; asm nop; } }
-
AuthorPosts
- You must be logged in to reply to this topic.