CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Trouble with PWM and ADC of 12F683

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tien168bn



Joined: 04 Apr 2014
Posts: 7
Location: Viet Nam

View user's profile Send private message

Trouble with PWM and ADC of 12F683
PostPosted: Fri Apr 04, 2014 7:53 am     Reply with quote

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 Smile 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

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 8:55 am     Reply with quote

Set_ADC_channel(0) or set_ADC_channel(1).
tien168bn



Joined: 04 Apr 2014
Posts: 7
Location: Viet Nam

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 9:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 11:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 12:33 pm     Reply with quote

He means you should connect your trimpots like this:
http://dm.risd.edu/pbadger/PhysComp/pmwiki/uploads/Devices/Pot.jpg
Power on one end, ground on the other, and connect the middle pin
(the wiper) to the PIC's analog input.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 1:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 8:48 pm     Reply with quote

Thank all. I will try again. :D
tien168bn



Joined: 04 Apr 2014
Posts: 7
Location: Viet Nam

View user's profile Send private message

PostPosted: Fri Apr 04, 2014 8:56 pm     Reply with quote

PCM programmer wrote:
He means you should connect your trimpots like this:
http://dm.risd.edu/pbadger/PhysComp/pmwiki/uploads/Devices/Pot.jpg
Power on one end, ground on the other, and connect the middle pin
(the wiper) to the PIC's analog input.

Hi, thank so much. The error is my schematic. it run. thank you Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group