View previous topic :: View next topic |
Author |
Message |
lxc032
Joined: 12 Jul 2011 Posts: 5
|
ADC IN PWMMING |
Posted: Mon Jul 18, 2011 3:09 pm |
|
|
Dear All,
I'm doing a project about PWM Dimming using ADC. I'm using a variable resistor to do ADC but the dimming effect is not obvious. My code is shown as follows.
Code: |
#include <16F877A.h>
#device adc=10
#use delay(clock=4000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP, NOBROWNOUT,NOPUT
void main() {
int8 temp_res=0;
int8 temp_duty=0;
setup_timer_2(T2_DIV_BY_4,100,1);
set_tris_a(0xFF);*/
set_tris_c(0x00); // Set PORTC as an Output
setup_ccp1(CCP_PWM);
while(1){
set_adc_channel(0);
delay_us(20);
temp_res = read_adc(); // Get results of AD conversion
delay_us(20); // from channel 2 (RA2 pin)
temp_duty = (temp_res); // Convert the 10 bit value
set_pwm1_duty(50);
delay_us(10);
}
}
|
Should I change the mutiple of (temp_res) to make the effect more obvious? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
lxc032
Joined: 12 Jul 2011 Posts: 5
|
|
Posted: Mon Jul 18, 2011 3:46 pm |
|
|
Thanks for your program.
I'm trying to use a variable resistor to change the duty cycle of the PWM signals. Should I need to change the multiple of the "temp_res" ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 18, 2011 3:51 pm |
|
|
You are not using the adc value to set the PWM duty. You are doing
nothing with it. Your PWM duty is set to a constant value of 50.
The program that I gave you in the link does use the ADC value
to set the duty. I suggest that you study it. |
|
|
|