ditch08
Newbie level 6
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:
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
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: