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

pic16f877a pwm problem

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



Joined: 27 Dec 2011
Posts: 2

View user's profile Send private message

pic16f877a pwm problem
PostPosted: Tue Dec 27, 2011 9:44 am     Reply with quote

Hi all,

I am using a pic16f877a to measure voltages and vary the PWM. The adc voltages vary just fine, but the PWM isn't generated...here is the code (is it ok? ):
Code:

#include <16F777.h>
#device ADC=16
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=20000000) 

//long duty_cycle,period;

void main()
{
long adc_result0;
long adc_result1;

output_low(PIN_C1);   // Set CCP2 output low
output_low(PIN_C2);   // Set CCP1 output low

setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM);  // Configure CCP2 as a PWM

setup_timer_2(T2_DIV_BY_16, 124, 1);  // 500 Hz

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_16);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 255, 1);

while (1)
{
set_adc_channel(0);
delay_us(10);
adc_result0=read_adc();
set_adc_channel(1);
delay_us(10);
adc_result1=read_adc();

if(adc_result0 <655)
  {
   set_pwm1_duty(255);
  }
else if((adc_result0 > 655) &&(adc_result0 < 700))
  {
   set_pwm1_duty(150);
  }
else if((adc_result0 < 750) && (adc_result0 > 700))  //car far from obstacle
  {
   set_pwm1_duty(120);
  }
else if((adc_result0 < 800) && (adc_result0 > 750))  //car medium from obstacle
  {
   set_pwm1_duty(100);
   }
else if((adc_result0 < 900) && (adc_result0 > 800))          //car near to obstacle
  {
   set_pwm1_duty(0);
   set_pwm2_duty(200);
  }


if(adc_result1 < 460)          //car too far from obstacle
  {
  set_pwm2_duty(250);
  }
else if((adc_result1 < 500) && (adc_result1 > 460))          //car too far from obstacle
  {
   set_pwm2_duty(150);
   }
else if((adc_result1 < 550) && (adc_result1 > 500))          //car  far from obstacle
  {
   set_pwm2_duty(120);
  }
else if((adc_result1 < 600) && (adc_result1 > 550))          //car medium to obstacle
  {
   set_pwm2_duty(100);
  }
else if((adc_result1 < 780) && (adc_result1 > 600))          //car near to obstacle
  {
   set_pwm2_duty(0);
   set_pwm1_duty(200);
  }
                   
}
}
masab_ahmad



Joined: 27 Dec 2011
Posts: 2

View user's profile Send private message

PostPosted: Tue Dec 27, 2011 10:32 am     Reply with quote

its working in the 'Real Pic simulator", but not in proteus.....
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Dec 27, 2011 10:42 am     Reply with quote

So you should conclude to throw away Proteus. Search this site and you'll see hundreds of msgs saying how BAD it is with respect to 'simulating' PICs.

I'm old school(doing this for 30+ years...), cut code, program real PICs, test in the real world. NO simulator can simulate the real world properly.
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