Math problem

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Zentec
Posts: 34
Joined: 21 Sep 2008 17:53

Math problem

#1 Post by Zentec » 02 Apr 2009 19:08

Hallo all PIC guys.

I have a good quastion, why is my math function not working, the code is don below.

Code: Select all

unsigned int myADC;
unsigned short myPWM;
unsigned int SekvensPointer;
unsigned int RampeUpTime;
unsigned int RampeDownTime;
unsigned int MaxValue;
unsigned int MinValue;

void Bootup() {

   CMCON0     = 0b00000111;    //Deactivate the comparator modules
   ADCON1     = 0x80;          // Configure analog inputs and Vref
   ANSEL      = 0b00001111;    //RA0 RA1 RA2 RA4 are anlog input
   TRISA      = 0b00011111;    //
   TRISC      = 0b00001000;    // Output only RC3 input
   Pwm1_Init(5000);             // Initialize PWM module  at RC5
   Pwm1_Start(); // Start PWM

    }

void main() {
   bootup();
   SekvensPointer=1;
   myPWM=0;

  while (1) {

        RampeUpTime      =  ADC_Read(0);
        RampeDownTime    =  ADC_Read(3);
        MaxValue         =  ADC_Read(1);
        MinValue         =  ADC_Read(2);

        RampeUpTime=RampeUpTime*15;
        RampeDownTime=RampeDownTime*15;
        MaxValue = RampeUpTime/4;
        MinValue = MinValue/4;
              if(PORTC.F3==0){
                             while(myPWM < MaxValue){
                             vdelay_ms(RampeUpTime);
                             myPWM= myPWM++;
                             PWM1_Set_Duty(myPWM);
                             }
              }
              if(PORTC.F3==1) {
                              while(myPWM > MinValue){
                              vdelay_ms(RampeDownTime);
                              myPWM= myPWM--;
                              PWM1_Set_Duty(myPWM);
                              }
              }
           }
}
It is a Rampe genrator, but it is not working withe the analogs. input.
but when i run this code withe normal nubers it is working.

So here is my qustions how does i convert a INT to Short, and How does i convert Float to Short???
and have i done some stupid code??

It is a PIC16f684

jumper
Posts: 466
Joined: 07 Mar 2007 14:36

How about this

#2 Post by jumper » 03 Apr 2009 09:10

Hi,


RampeUpTime = ADC_Read(0);
RampeDownTime = ADC_Read(3);
MaxValue = ADC_Read(1); <---- Here you read
MinValue = ADC_Read(2);

RampeUpTime=RampeUpTime*15;
RampeDownTime=RampeDownTime*15;
MaxValue = RampeUpTime/4; <--- Is this right?
MinValue = MinValue/4;


Why do you read MaxValue from AD 1 when you never use that in your code.

Maybe the MaxValue = RampeUpTime/4; line should be MaxValue = MaxValue/4;


It is just an idea

/me

Post Reply

Return to “mikroC PRO for PIC General”