Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calibration routine for ADS7843
void ADS7843_Calibrate(void)
{
static Coordinate *Pos;
uint16_t MatriceA[3][3] = {{1,1,1},{1,1,1},{1,1,1}}; // MatriceA for values of ADC ADS7843
uint8_t i,j; // Inserts itself into a matrix
uint32_t detA; // determinant matriceA
int32_t transA[3][3]; // transpose matriceA
float invA[3][3]; // Invert MatriceA
float pom; // to save 1/detA
// Displays 3 points and their coordinates are obtained
for(i=0;i<3;i++)
{
// Caibration Point P1 shown on the LCD Matrices X,Y contain the coordinates of P1
// correct for 16 bit resolution
LCD_DrawCalibPoint(MatriceX[i] - 8, MatriceY[i] - 8);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)); // hold on to confirm position
Pos = ADS7843_Read();
while(!EXTI_GetITStatus(EXTI_Line6)); // wait till stopped pressing touchscreenu
MatriceA[i][0] = Pos->x;
MatriceA[i][1] = Pos->y;
Delay(0x9FFFFF); // time between showing cailbartion points
}
// determinant marice A
detA = (MatriceA[0][0]*((MatriceA[1][1]*MatriceA[2][2])-(MatriceA[1][2]*MatriceA[2][1]))) +
(MatriceA[0][1]*((MatriceA[1][2]*MatriceA[2][0])-(MatriceA[2][2]*MatriceA[1][0]))) +
(MatriceA[0][2]*((MatriceA[1][0]*MatriceA[2][1])-(MatriceA[1][1]*MatriceA[2][0])));
// z matice A urobí transponovu maticu a vloži ju do transA
// | a b c |^-1 | A B C |^ T
// MatriceA^-1 = 1/det(MatriceA)*| d e f | = 1/det(MatriceA)*| D E F | =
// | g h k | | G H K |
// | A D G |
// = 1/det(MatriceA)* | B E H | = 1/det(MatriceA)* transA
// | C F K |
//
// a(0,0) b(0,1) c(0,2) d(1,0) e(1,1) f(1,2) g(2,0) h(2,1) k(2,2)
//
// A = (e*k - f*h) D = (h*c - b*k) G = (b*f - e*c)
// B = (f*g - d*k) E = (a*k - g*c) H = (c*d - a*f)
// C = (d*h - e*g) F = (g*b - a*h) K = (a*e - b*d)
//
// Transposed MatriceA
transA[0][0] = (MatriceA[1][1] * MatriceA[2][2]) - (MatriceA[1][2] * MatriceA[2][1]); // A
transA[1][0] = (MatriceA[1][2] * MatriceA[2][0]) - (MatriceA[1][0] * MatriceA[2][2]); // B
transA[2][0] = (MatriceA[1][0] * MatriceA[2][1]) - (MatriceA[1][1] * MatriceA[2][0]); // C
transA[0][1] = (MatriceA[2][1] * MatriceA[0][2]) - (MatriceA[0][1] * MatriceA[2][2]); // D
transA[1][1] = (MatriceA[0][0] * MatriceA[2][2]) - (MatriceA[2][0] * MatriceA[0][2]); // E
transA[2][1] = (MatriceA[2][0] * MatriceA[0][1]) - (MatriceA[0][0] * MatriceA[2][1]); // F
transA[0][2] = (MatriceA[0][1] * MatriceA[1][2]) - (MatriceA[1][1] * MatriceA[0][2]); // G
transA[1][2] = (MatriceA[0][2] * MatriceA[1][0]) - (MatriceA[0][0] * MatriceA[1][2]); // H
transA[2][2] = (MatriceA[0][0] * MatriceA[1][1]) - (MatriceA[0][1] * MatriceA[1][0]); // K
// Inverse matrix of MatriceA
//MatriceA^-1 = (1/detA) * transA
pom = 1/((float)detA);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
invA[i][j] = pom * ((float)transA[i][j]);
}
}
// |aX| |X1| X1, X2, X3 Are X Coordinates of calibration points on the LCD
// |bX| = A^-1 * |X2|
// |dX| |X3|
aX = invA[0][0]*(float)MatriceX[0] + invA[0][1]*(float)MatriceX[1] + invA[0][2]*(float)MatriceX[2];
bX = invA[1][0]*(float)MatriceX[0] + invA[1][1]*(float)MatriceX[1] + invA[1][2]*(float)MatriceX[2];
dX = invA[2][0]*(float)MatriceX[0] + invA[2][1]*(float)MatriceX[1] + invA[2][2]*(float)MatriceX[2];
// |aY| |Y1| Y1, Y2, Y3 Are Y Coordinates of calibration points on the LCD
// |bY| = A^-1 * |Y2|
// |dY| |Y3|
aY = invA[0][0]*(float)MatriceY[0] + invA[0][1]*(float)MatriceY[1] + invA[0][2]*(float)MatriceY[2];
bY = invA[1][0]*(float)MatriceY[0] + invA[1][1]*(float)MatriceY[1] + invA[1][2]*(float)MatriceY[2];
dY = invA[2][0]*(float)MatriceY[0] + invA[2][1]*(float)MatriceY[1] + invA[2][2]*(float)MatriceY[2];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.