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.

increasing/decreasing PWM signal by 1microsecond by potentiometer

Status
Not open for further replies.

ditch08

Newbie level 6
Newbie level 6
Joined
Jul 9, 2013
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
114
Good day to all

I am having trouble coding for a project using pic16f877a. I want to increase/decrease the duty cycle of my pulse that i use for my servo project using a potentiometer. So i try to use a/d converter given already in the PIC and use its 10-bit adc value to vary my pwm signal. i want it to increment/decrement by a microsecond but the problem is, the minimum microsecond i can only produce is 4us. i can use __delay_us(1); putting it inside a for loop (int i = 0;i<adc_value;++i){__delay_us(1);} so that while my adc value change, my pwm signal change with it but the result of the for loop is higher than my expected value. actually, based on my observation, 1 for loop is equals to about 3.7 micro seconds. So my PWM increments/decrements by 4microseconds when i use the potentiometer.

my code for the increment goes like this:


Code C - [expand]
1
2
3
4
5
6
7
while(1){ 
portebits.re0 = 1; 
__delay_us(1500); 
for(int i = 0; i< adc_value; ++i){__delay_us(1);}// this delay is about 4.7us per loop.. not 1us.. :/ (supposedly adc_value= around 1000-1024 since it's from a 10-bit value. i want it to be " 1 adc_value = 1 microsec") 
Portebits.re0 = 0; 
__delay_us(18500); 
}



i'm still new to this and i am not yet familiar with using PIC16f877a's capabilities to its fullest. please suggest any methods or ways on how i can produce an exact 1 microsecond value with the use of potentiometer.

i'm using mplab v8.84 with hi-tech c compiler using 20mhz for my xtal frequency

thanks
 
Last edited by a moderator:

1. Use Interrupt of ADC.
2. Use Interrupt of PWM
3. Use High value crystal May be 20 MHz.
4. Make paper calculation so that required time of ISR routine of ADC PWM. Then implement code.
No need of Delay routine also
 
i see.. thanks for the reply, i'll try to study about those suggestions of yours.
 

4. Make paper calculation so that required time of ISR routine of ADC PWM. Then implement code.
No need of Delay routine also

i have no idea about this calculations yet, can you give me a short discussion about this and on how to deal with this? what is the formula and what does ISR stands for? :)

thanks
 

ISR stands for Interrupt service routine
Refer data sheet in which CCP module give details of PWM
 
Refer data sheet in which CCP module give details of PWM

Unfortunately, ditch08 could memorize the datasheet section on the CCP module and it would be of little value, other than to come to the realization that using the CCP module of the PIC16F877A to generate a 50Hz PWM signal, period of 20ms, is an inappropriate choice unless of course you choose to use a slower than normal system clock frequency which has additional drawbacks.


@ditch08

Normally most PWM signals appropriate to drive RC Servos are generated by the PIC16F series devices by utilizing a timer, timer interrupts and interrupt service routine (ISR).

Using timer, timer interrupts and ISR to generate an appropriate PWM signal is preferable to software delays, as in your post example, as it always the PIC to perform other tasks, such as monitoring the resistance/position of a pot when the PIC is not servicing the interrupt.

The following reference document provides several examples of generating such a PWM signal in code:

How to Interface a Microchip PIC MCU with a hobby R/C Servo

The code examples in the above document are for the mikroC Pro Compiler, however they can be readily ported to other compilers such as the HiTech Compiler.

**broken link removed**

There are several possible techniques to controlling the servo position with a pot.

Post any additional questions you may have and I will gladly assist you.

BigDog
 
Thanks for the reference Mr. Bigdogguru. I will will try to study and learn from the site and i will still consider mr. golden electronic's advice. :)

And since you asked for it, here's another question. :D
Aside from using a potentiometer to increase and decrease my pwm, i also want my project to function like when i turn on a switch(increment) the pwm will increment by a 1microsecond every interval from a reference point which is also variable. For example, i set the reference value of the pwm at 1500us then as the switch to increment is on, the duty cycle of 1500us will increase 1us at a time and vise versa for the decrement switch. Because i am not yet familiar with using interrupts, i try to solve my problem by using built-in delays or make my own one but still no luck. i really need this kind of resolution to test servos accurately.

Maybe after i start using the code in your reference website then i will have a slight knowledge of using interrupts but for now, i am just dependent on my logical approach to solve this. I know i am not using the pic's fullest capabilities yet but i'll get there. I just need more time, and also advice on how to make my own delay function that will meet my needs, atleast that's what i think right now... :D

Thanks
 


Wow, thanks for this. I think i must spend some quality time with this tutorials for the following days. And hopefully, i can manage to grasp everything and actually apply it. Again many thanks Mr.BigDog! :D
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top