View previous topic :: View next topic |
Author |
Message |
tien168bn
Joined: 04 Apr 2014 Posts: 7 Location: Viet Nam
|
Trouble with PWM and ADC of 12F683 |
Posted: Fri Apr 04, 2014 7:53 am |
|
|
Hi guys. I have a trouble with PWM and ADC of PIC 12F683. My project is change ADC level at GP0 to change PWM at GP2, use module CCP1. But when I change ADC level, PWM not change. Somebody help me Thank a lots.
Code: | #include <12F683.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOCPD, NOPROTECT, MCLR, NOPUT, BROWNOUT
#use delay(clock=4000000)
//---------------------//
#byte GPIO = 0x05
//---------------------//
#bit vr1 = gpio.0 //VR
#bit vr2 = gpio.1 //CBN
#bit button = gpio.5
#bit motor = gpio.2
#bit led = gpio.4
int16 value;
//---------------------//
void initCCT(void)
{
setup_oscillator(OSC_4MHZ);
setup_adc_ports(sAN0|sAN1|VSS_VDD); //Set GP0-GP1 as analog input, other pins as digitals I/O
setup_adc(ADC_CLOCK_DIV_8);//(ADC_CLOCK_DIV_8); //Divisor for 4 MHz crystal
set_adc_channel(sAN0|sAN1);
setup_comparator(NC_NC);
setup_ccp1(CCP_PWM);// Set the PWM frequency for 245 Hz (with a 4 MHz crystal)
setup_timer_2(T2_DIV_BY_16, 254, 1);
set_tris_a(0x23);
}
void main()
{
initCCT();
while(true)
{
set_adc_channel(sAN0);
delay_us(10);
value = read_adc();// 10-bit ADC result
set_pwm1_duty(value);
}
}
|
https://www.flickr.com/photos/120032952@N04/13623746254/
https://www.flickr.com/photos/120032952@N04/13623388855/ |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Apr 04, 2014 8:55 am |
|
|
Set_ADC_channel(0) or set_ADC_channel(1). |
|
|
tien168bn
Joined: 04 Apr 2014 Posts: 7 Location: Viet Nam
|
|
Posted: Fri Apr 04, 2014 9:46 am |
|
|
alan wrote: | Set_ADC_channel(0) or set_ADC_channel(1). |
I tried. PWM ok, but ADC not run. I change ADC but PWM not change |
|
|
stinky
Joined: 05 Mar 2012 Posts: 99 Location: Central Illinois
|
|
Posted: Fri Apr 04, 2014 11:21 am |
|
|
the schematic shows vr1 and vr2 connected as variable resistors.
Perhaps you intended to use them as potentiometers so you could vary the voltage being read on those pins. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19494
|
|
Posted: Fri Apr 04, 2014 1:02 pm |
|
|
Yes. As it stands you always have the same voltage on the pins, just a varying resistance....
The point about 'or', was the line:
set_adc_channel(sAN0|sAN1);
The set_adc_channel function accepts _one_ channel only at a time. Doesn't allow values to be OR'ed together, and doesn't select the sANx numbers. Just 0, 1, 2 etc., for the channel to be selected.
So the channel should be selected with:
set_adc_channel(0); |
|
|
tien168bn
Joined: 04 Apr 2014 Posts: 7 Location: Viet Nam
|
|
Posted: Fri Apr 04, 2014 8:48 pm |
|
|
Thank all. I will try again. :D |
|
|
tien168bn
Joined: 04 Apr 2014 Posts: 7 Location: Viet Nam
|
|
Posted: Fri Apr 04, 2014 8:56 pm |
|
|
Hi, thank so much. The error is my schematic. it run. thank you |
|
|
|