Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

variable delay generation in PIC18F4550 using mikroC

Status
Not open for further replies.

fuuton

Advanced Member level 4
Joined
Jul 21, 2010
Messages
104
Helped
9
Reputation
18
Reaction score
2
Trophy points
1,298
Location
Pakistan, Rawalpindi
Activity points
1,881
Hi all,

I need to generate variable delays in mikroC in micro seconds for PIC 18F4550. I am trying to generate a PWM signal for servos that I need to vary continuously depending on the values I obtain from my sensors. I have used many techniques but none has worked so far. Please help me out.

I recently used timers for this purpose but it was no good as well. The following code is supposed to generate a 600us delay inside the timer loop. Kindly help me fix it.

Thanks in advance.

-------------------------------------------------------------------------------


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
unsigned int on_time_high;
unsigned int on_time_low;
void main() 
 
{
        TRISD = 0x00;
        RD1_bit = 1;
        on_time_high = 61760/0x100;
        on_time_low  = 61760-(on_time_high*0x100);
        TMR0H        = on_time_high;
        TMR0L        = on_time_low;     // Values calculated for 1 second delay with 12MHz crystal
 
while(1)
        {
        T0CON.TMR0ON = 1;                 // Timer0 On
    RD0_bit = 1;
        while(TMR0H<0xFF  & TMR0L<0xFF);        // Wait until TMR0IF gets flagged
    T0CON.TMR0ON=0;                 // Timer0 Off
    //INTCON.TMR0IF=0;                // Clear Timer0 interrupt flag*/
        delay_us(1000);
    RD0_bit = 0;
    
    TMR0H        = on_time_high;
        TMR0L        = on_time_low;
    }
}





--------------------------------------------
 
Last edited by a moderator:

I am trying to generate a PWM signal for servos that I need to vary continuously depending on the values I obtain from my sensors.

What type of sensor? What are the values? Whar is the relation between those values and speed of the motor?
 

Hi,

I am using a potentiometer as a sensor. Basically I am trying to replicate the number of degrees turned on the potentiometer on a servo motor. The relation between the resistance of potentiometer and the servo is that if I increase the resistance from 50%, the servo will move a towards +90 degrees corresponding to the increase in the resistance and vice versa for decrease in resistance.

Let me know if it is unclear.

Thanks.
 

Hi,

I am using a potentiometer as a sensor. Basically I am trying to replicate the number of degrees turned on the potentiometer on a servo motor. The relation between the resistance of potentiometer and the servo is that if I increase the resistance from 50%, the servo will move a towards +90 degrees corresponding to the increase in the resistance and vice versa for decrease in resistance.

Let me know if it is unclear.

Thanks.

In your previous post you mentioned that you have tried many techniques already. I dont know if you have tried to interface a potentiometer with the help of Analog-To-Digital Conversion module.

A/D conversion is useful for determining the value of an analog voltage. A potentiometer may be used as a variable voltage divider. In other words, connections on the potentiometer are made as follows:-
pin 1 – GND
pin 2 – PORT A, PIN 0 (2)
pin 3 – 5
When the potentiometer’s “wiper” is adjusted, voltage on pin 2 varies from 0 to 5v.

You can have 1024 different values and use them to control your motor.
 

Thanks for the reply.

Yes I am using an ADC for converting the analog voltage into a digitized sequence. However, to control a servo, I have to generate a PWM signal. This PWM signal is to be according to the sequence that is received from the ADC. Hence I need a variable PWM signal.

Regards.
 

Here is an example code:-
need s modification
////////////////
Code:
int servoPin = 7;		// R/C  Servo connected to digital pin
int myAngle;		     // angle of the servo (roughly in degrees) 0-180
int pulseWidth;		  // function variable

void servoPulse(int servoPin, int myAngle) {
  pulseWidth = (myAngle * 11) + 500;  // converts angle to microseconds
  digitalWrite(servoPin, HIGH);	 // set servo high
  delayMicroseconds(pulseWidth);	// wait a very small amount
  digitalWrite(servoPin, LOW);	  // set servo low
  Delay_ms(20);				  // refresh cycle of typical servos (20 ms)
}

void loop() {
  myAngle= ADC_Read(0);		 // Parameter channel represents the channel from which the
  // analog value is to be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.
  myAngle= (myAngle - 50);
  servoPulse(servoPin, myAngle);
  Delay_ms(10);
}


---------- Post added at 14:50 ---------- Previous post was at 14:49 ----------

you can place the loop() in a while loop
 

Hi,

The compiler I am using (MikroC) does not allow the use of variable inside a delay function

Code:
( delayMicroseconds(pulseWidth);	// wait a very small amount)

Hence I need a code segment that would allow me to give variable delays in micro seconds.

Thanks.
 

I mean to say use something like:-

Code C - [expand]
1
2
3
4
5
6
Delay_Ms(usigned int milliseconds) { 
while(milliseconds > 0){
delay_ms(1)     // or the step value you want __delay_ms(1); 
milliseconds--;
  }
 }



So the code will be:--


Code C - [expand]
1
2
3
4
5
6
7
void servoPulse(int servoPin, int myAngle) {
  pulseWidth = (myAngle * 11) + 500;  // converts angle to microseconds
  digitalWrite(servoPin, HIGH);  // set servo high
  Delay_Ms(pulseWidth); // wait a very small amount
  digitalWrite(servoPin, LOW);    // set servo low
  Delay_ms(20);               // refresh cycle of typical servos (20 ms)
}

 
Last edited:
Hi,

The code introduces delay which is more than the required delay. It is not uniform so I cant decrease the number of repetitions for the while loop to accommodate the increase.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top