
Originally Posted by
oogabooga
I don't see anything wrong.
How exactly are you printing the value?
A uint16_t is not necessarily the same as an unsigned short, so if you're just using %hu that could be the problem.
Try
Code:
printf("%"PRIu16"\n", finalValue); // include <inttypes.h>
The returned value is used in other code that controls a H-Bridge motor. In this case the motor is driven incorectly. When sent via serial USB the value is wrong. When sent to a LCD display it is wrong also.
The same code used in C++ Builder gives the correct value.
This is my full embedded test code that sends the value to a LCD display.
Code:
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"
void init_Things(void)
{
lcd_init(LCD_DISP_ON);
lcd_clrscr();
_delay_ms(10);
}
int main(void)
{
init_Things();
char buffer[10];
char buf[10];
strcpy(buffer, "Value: ");
uint16_t x1, x2, y1, y2;
uint16_t Q11, Q12, Q21, Q22;
uint16_t xAxis, yAxis;
float InterpVal;
uint16_t finalValue;
x1 = 1000;
x2 = 2000;
y1 = 400;
y2 = 600;
Q11 = 0;
Q12 = 0;
Q21 = 100;
Q22 = 200;
xAxis = 1500;
yAxis = 450;
InterpVal = (float)(((x2 - xAxis) * (y2 - yAxis)) / (float)((x2 - x1) * (y2 - y1)) * Q11) +
(float)(((xAxis - x1) * (y2 - yAxis)) / (float)((x2 - x1) * (y2 - y1)) * Q21) +
(float)(((x2 - xAxis) * (yAxis - y1)) / (float)((x2 - x1) * (y2 - y1)) * Q12) +
(float)(((xAxis - x1) * (yAxis - y1)) / (float)((x2 - x1) * (y2 - y1)) * Q22);
finalValue = (uint16_t)InterpVal; // Gives the value 1753 when 62 is the correct value.
utoa(finalValue, buf,10);
strcat(buffer, buf);
lcd_gotoxy(0,0);
lcd_puts(buffer);
while(1)
{
//TODO:: Please write your application code
}
}