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 support@ccsinfo.com

PIC12F683 ADC and PWM issues

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








PIC12F683 ADC and PWM issues
PostPosted: Sun Mar 04, 2007 5:08 am     Reply with quote

Hi, i'm having some issue in making work a really simple demo: read the voltage value from analog input and modify the PWM directly, here is the code:

Code:

void main()
{
long   value=0;

   setup_adc_ports(sAN3|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_16);
   set_adc_channel(3);    //read channel 0
   delay_us(10);
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_4,24,1); //24+1*4 risoluzione pwm
   
   setup_ccp1(CCP_PWM);
   setup_comparator(NC_NC);
   setup_vref(FALSE);
   
   setup_oscillator(OSC_4MHZ);                                     

   // TODO: USER CODE!!
 
while (true)
   {
      value=(read_adc()*100)/1024; // 10 bits
      delay_ms(500);
      set_pwm1_duty(value);  //value must be long
      delay_ms(500);
   }
}


In the .h file i have:

Code:

#include <12F683.h>
#device adc=10
#use delay(clock=4000000)


The analog input is fed by a simple resistive divider (2-1Kohm around). The variable holding the read is "long", the ADC is set to 10 bit, timings and clock division should be correct... but the output isn't working as expected. From 1V to 3V the PWM works fine following the ADC value. After 3V the output fall back to 0V and starts increasing again... i've read previous post in the forum but i didn't find anything usefull. I think it's a stupid thing... but i can't find it ! :(
Ttelmah
Guest







PostPosted: Sun Mar 04, 2007 5:39 am     Reply with quote

What is the biggest value that can be held in a 'long'?. What does 100*1023 equal....

Use:

value=(read_adc()*25)/256; // 10 bits

Best Wishes
Guest








PostPosted: Sun Mar 04, 2007 4:41 pm     Reply with quote

IT works now ! you are right ! thanks !
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