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

IF statment

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







IF statment
PostPosted: Thu Nov 04, 2004 3:40 am     Reply with quote

Hi,
I am a new one to use CCS C compiler. I am using 16F73 with CCS PCM 3.211 Compiler. I have some problem with my code. I want to get the atan value from two inputs AN0 and AN1, then output PWM in ccp1. Is there someone can help me?
the code is following:
Code:


#include <16F73.h>
#include <math.h>
#device adc=8
#use delay(clock=2000000)
#fuses NOWDT,XS, NOPUT, NOPROTECT, BROWNOUT

void main() {
   float valueX, valueY, data, angle;
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,255,1);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   while(true){
      set_adc_channel(0);
      delay_us(10);
      valueX = read_adc();

      set_adc_channel(1);
      delay_us(10);
      valueY = read_adc();
      valueX = (valueX - 128) / 128;
      valueY = (valueY - 128) / 128;
      data = valueY / valueX;
     
      if(valueX > 0){
         if(valueY > 0)
            angle = atan(data);
         else
            angle = 6.2831853 + atan(data);
      }
      else{
         angle = 3.14159265 + atan(data);
      }
     
      angle = angle * 1024 / (2.0 * PI);
      set_pwm1_duty((long int) angle);
   }
}

drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Thu Nov 04, 2004 8:36 am     Reply with quote

A couple of suggestions:

Enable PUT in your #fuses statement.

Cast your adc value to a float.
i.e. valueX = (float)read_adc();
_________________
David
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