• AVR Freaks

AnsweredHot!math not adding up correctly

Author
marvinatorrr
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/01/14 03:42:57
  • Location: 0
  • Status: offline
2020/01/17 21:53:53 (permalink)
0

math not adding up correctly

Does anyone know why the calculations for CCPR1L does not seem to add up? The calculation is meant to scale the results of the ADC to steer a servo motor. However, looking into the watch window in Protues I am getting unexpected results.
 
Using PIC18F1220, XC8 v1.34, MPLAB v5.30
 
int main(int argc, char** argv) {
    OSCCON = 0b00110110; //run mode, 500kHz, internal osc block
    
    init_ADC();
            
    TRISBbits.RB3=0; //set as output
    TRISBbits.RB2=0; //set as output
    TRISBbits.RB0=0; //set as output
    
    CCP1CON = 0b10001100; //half bridge output
                          //p1a complements p1b pwm output with dead band control
                          //all active high
    
    
    
    T2CON = 0b00000010; //prescale 16, postscale 1:1
    PR2=155; //calculation PWM Period = [(PR2) + 1] ? 4 ? TOSC ?(TMR2 Prescale Value)
    
    T2CONbits.TMR2ON = 1; //start timer
    
        while(1)
    {
        ADC_Result[0] = ADC_Read(0);
        ADC_Result[1] = ADC_Read(1); //0-1023
        
        if(ADC_Result[0]>=ADC_Result[1]) //left turn
        {
            error = ADC_Result[0]- ADC_Result[1];
            
            CCPR1L = (error-(3069))*(-0.00048875855)*0.05*PR2; //neutral 7.5%, most left 5%, 50hz
        }
        else if(ADC_Result[1]>ADC_Result[0] ) //right turn
        {
            error = ADC_Result[1]- ADC_Result[0];
            CCPR1L = (error+(3069))*(0.00048875855)*0.05*PR2; //neutral 7.5%, most right 10%
        }

        PIR1bits.TMR2IF=0;
        while(PIR1bits.TMR2IF==0);
    }
    return;
}

#1
ric
Super Member
  • Total Posts : 25591
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: math not adding up correctly 2020/01/17 22:04:20 (permalink)
+1 (1)
Not your problem, but this
int main(int argc, char** argv) {

should be
void main(void) {

 
You have not shown your variable definitions for ADC_Result[], or revealed what numbers you are getting, so I can't comment further.
 
whenever you ask a question, "unexpected results" is not helpful.
Stating what you expected, and what you got, is the minimum required to examine the problem.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11577
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: math not adding up correctly 2020/01/17 22:10:20 (permalink)
+1 (1)
You've also not shown what "error" is or how the ADC is configured (8 bit vs. 10 bit).
#3
marvinatorrr
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/01/14 03:42:57
  • Location: 0
  • Status: offline
Re: math not adding up correctly 2020/01/17 22:18:35 (permalink)
0

uint16_t ADC_Read(uint8_t channel)
{
    ADCON0 = (channel << 2);
    ADCON0 |= 0x03;
    
    while (ADCON0bits.GO_nDONE);
    return((ADRESH<<8 | ADRESL));
}
 
void init_ADC(void)
{
    ADCON0 = 0x00;
    ADCON1 = 0x7C;
    ADCON2 = 0x92;
}

 
For the first if case, I was expecting CCPR1L to be 12 when error was 0. However I get it to 20.
#4
ric
Super Member
  • Total Posts : 25591
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: math not adding up correctly 2020/01/17 22:34:16 (permalink)
+2 (2)
Have mercy on anyone trying to read your code
void init_ADC(void)
{
    ADCON0 = 0x00;    // ADC off, Vref+=AVDD, Vref-=AVSS
    ADCON1 = 0x7C;    // AN0 and AN1 analog, all others digital
    ADCON2 = 0x92;    // Right justified, ACQT=4 Tad, ADCS=Fosc/32
}

You still have not revealed how you defined ADC_Result[]
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
marvinatorrr
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/01/14 03:42:57
  • Location: 0
  • Status: offline
Re: math not adding up correctly 2020/01/17 22:48:15 (permalink)
0

uint16_t ADC_Result[2];

 
Sorry bout that, thx for commenting
#6
ric
Super Member
  • Total Posts : 25591
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: math not adding up correctly 2020/01/17 23:37:00 (permalink) ☼ Best Answerby marvinatorrr 2020/01/17 23:54:26
+1 (1)
This calculation
CCPR1L = (error-(3069))*(-0.00048875855)*0.05*PR2; //neutral 7.5%, most left 5%, 50hz

is screwing up here
(error-(3069))

"error" and 3069 are both unsigned 16 bit integers, so that calculation is done using unsigned 16 bit arithmetic
so, 0 - 3069 gives -3069 = 0xF403 but that's treated as 62467 if treated as uint16_t
62467 * -0.00048875855 * 0.05 * 155 = -236.6174 = 0xFF14 as a signed integer
when you stuffed that into an 8 bit register you were left with 0x14 = 20.
 
Try changing your "error" variable from uint16_t to int16_t
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
ric
Super Member
  • Total Posts : 25591
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: math not adding up correctly 2020/01/17 23:40:38 (permalink) ☄ Helpfulby marvinatorrr 2020/01/17 23:54:36
+1 (1)
Another workaround would be to change 3069 to 3069.0 to force that first calculation to use floating point.
 
Now you should understand why we always want to see your variable declarations too.
post edited by ric - 2020/01/17 23:41:53

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
marvinatorrr
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2020/01/14 03:42:57
  • Location: 0
  • Status: offline
Re: math not adding up correctly 2020/01/17 23:58:39 (permalink)
0
Thanks for that. This serves as a reminder for me to not just use uint_16 blindly when declaring.
#9
1and0
Access is Denied
  • Total Posts : 10345
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: math not adding up correctly 2020/01/18 02:06:19 (permalink)
0
Where in this thread shows "error" is an uint16_t type?
 
#10
ric
Super Member
  • Total Posts : 25591
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: math not adding up correctly 2020/01/18 02:11:00 (permalink)
+2 (2)
1and0
Where in this thread shows "error" is an uint16_t type?

It doesn't, but when I tried the code in MPLABX SIM, I had to create that variable, and I got identical results to the OP if I made it uint16_t.
This thread would have come to a much quicker conclusion if all the variable definitions had been visible in post#1. :)
 
Chalk it up as one more "The problem was in the code that WASN'T posted".
 
post edited by ric - 2020/01/18 02:13:01

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
pcbbc
Super Member
  • Total Posts : 1507
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: math not adding up correctly 2020/01/18 02:18:10 (permalink)
+1 (1)
For efficiency your calculation of 0.00048875855*0.05*PR2 could be moved outside the loop (PR2 is constant).
Then you’d only have one real multiply in each expression.
And I’m sure it could be done with all integer arithmetic and probably a single statement (the calculations differ only by sign) as well.
 
Your code or comment on the second calculation is wrong. Comment says 10% but the code is still using 0.05.
//neutral 7.5%, most right 10%, 50hz
#12
1and0
Access is Denied
  • Total Posts : 10345
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: math not adding up correctly 2020/01/18 02:26:37 (permalink)
+1 (1)
ric
This thread would have come to a much quicker conclusion if all the variable definitions had been visible in post#1. :)
 
Chalk it up as one more "The problem was in the code that WASN'T posted".

Agree.
 
#13
Jump to:
© 2020 APG vNext Commercial Version 4.5