/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f4xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */

	//Initialize the touchscreen
	TSC_Init();

#ifdef PRINT_ON_LCD
  /* LCD Display init  */
  Display_Init();
#endif

  /* ADC3 configuration *******************************************************/
  /*  - Enable peripheral clocks                                              */
  /*  - DMA2_Stream0 channel2 configuration                                   */
  /*  - Configure ADC Channel3 pin as analog input                            */
  /*  - Configure ADC3 Channel3                                               */
  ADC3_CH3_DMA_Config();

  /* Start ADC3 Software Conversion */ 
  ADC_SoftwareStartConv(ADC3);

  while (1)
  {
  	int x, y;
	char szTemp[64];

	if (TSC_TouchDet()) {           /* Show touch screen activity         */
		TP_GetAdXY(&x, &y);
	}
	else {
	  x = 0;
	  y = 0;
	}
	sprintf(szTemp, "X:%04d Y:%04d       ", x, y);
	LCD_DisplayStringLine(LINE(5), (uint8_t *)szTemp);

    ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;
#ifdef PRINT_ON_LCD
  /* Display ADC3 converted value on LCD */
    Display();
#endif
  }
}
Ejemplo n.º 2
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    //RCC_Config();
    //GPIO_Config();
    //RCC_MCO1Config(RCC_MCO1Source_PLLCLK,RCC_MCO1Div_1);
	uint8_t tmpreg;
	LIS302DL_InitTypeDef  LIS302DL_InitStruct;
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f4xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f4xx.c file
  */

  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

  //NVIC_SetPriority(SysTick_IRQn, -1);

    /* MEMS configuration and set int1/int2 pins*/
    LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
    LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
    LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
    LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
    LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
    LIS302DL_Init(&LIS302DL_InitStruct);
	//Set INT1/INT2 pins
	tmpreg = 0x40;
	LIS302DL_Write(&tmpreg, LIS302DL_CTRL_REG3_ADDR, 1);

	//Initialize the touchscreen
	TSC_Init();

  	/* Initialize LEDs and push-buttons mounted on STM324xG-EVAL board */
	STM_EVAL_LEDInit(LED1);
	STM_EVAL_LEDInit(LED2);
	//STM_EVAL_LEDInit(LED3);
	//STM_EVAL_LEDInit(LED4);

	/* Initialize the LCD */
  	STM324xG_LCD_Init();
  	//STM_EVAL_LEDOn(LED1);
  	LCD_Clear(Black);
  	LCD_SetTextColor(White);

  	LCD_LOG_SetHeader("STM32 Camera Demo");
  	LCD_LOG_SetFooter ("   Copyright (c) STMicroelectronics" );

  	/* ADC configuration */
  	ADC_Config();

  	/* Initializes the DCMI interface (I2C and GPIO) used to configure the camera */
  	//OV9655_HW_Init();
        OV9712_HW_Init();
  	/* Read the OV9655/OV2640 Manufacturer identifier */
  	//OV9655_ReadID(&OV9655_Camera_ID);
        OV9712_ReadID(&OV9712_Camera_ID);
        //while(1);  
  	if(OV9655_Camera_ID.PID  == 0x96)
  	{
    	Camera = OV9712_CAMERA;
    	sprintf((char*)buffer, "OV9655 Camera ID 0x%x", OV9655_Camera_ID.PID);
    	ValueMax = 2;
  	}
  	else if(OV9712_Camera_ID.PIDH  == 0x97)
  	{
    	Camera = OV9712_CAMERA;
    	sprintf((char*)buffer, "OV9712 Camera ID 0x%x", OV9712_Camera_ID.PIDH);
    	ValueMax = 2;
  	}
  	else
  	{
    	LCD_SetTextColor(LCD_COLOR_RED);
    	sprintf((char*)buffer, "OV Camera ID 0x%02x%02x", OV9655_Camera_ID.Version, OV9712_Camera_ID.PIDH);
    	LCD_DisplayStringLine(LINE(4), buffer);
    	while(1);  
  	}

  	LCD_SetTextColor(LCD_COLOR_YELLOW);
  	LCD_DisplayStringLine(LINE(4), (uint8_t*)buffer);
  	LCD_SetTextColor(LCD_COLOR_WHITE);

  	Delay(200);

  	/* Initialize demo */
  	ImageFormat = (ImageFormat_TypeDef)BMP_QVGA;

  	/* Configure the Camera module mounted on STM324xG-EVAL board */
  	Demo_LCD_Clear();
  	LCD_DisplayStringLine(LINE(4), "Camera Init..               ");
  	Camera_Config();

  	sprintf((char*)buffer, " Image selected: %s", ImageForematArray[ImageFormat]);
  	LCD_DisplayStringLine(LINE(4),(uint8_t*)buffer);

  	LCD_ClearLine(LINE(4));
  	Demo_LCD_Clear();
  
        
        

  	if(ImageFormat == BMP_QQVGA)
  	{
    	/* LCD Display window */
    	LCD_SetDisplayWindow(60, 80, 160, 120);
    	ReverseLCD();
    	LCD_WriteRAM_Prepare(); 
  	}
  	else if(ImageFormat == BMP_QVGA)
  	{
    	/* LCD Display window */
    	LCD_SetDisplayWindow(0, 0, 320, 240);
    	ReverseLCD();
    	LCD_WriteRAM_Prepare(); 
  	}

	{
		int i, j;
		for (j = 0; j < 16; j++)
		{
			LCD_SetDisplayWindow(0, (240 / 16) * j, 320, (240 / 16));
			LCD_WriteRAM_Prepare();
			for (i = 0; i <	320 * 240 / 16; i++)
			{
				LCD_WriteRAM(0x0003 << j);
			}
		}
	}

  	/* Enable DMA2 stream 1 and DCMI interface then start image capture */
  	DMA_Cmd(DMA2_Stream1, ENABLE); 
  	DCMI_Cmd(ENABLE); 

  	/* Insert 100ms delay: wait 100ms */
  	//Delay(200); 

  	DCMI_CaptureCmd(ENABLE); 

  	while(1)
  	{
	      OV9655_BrightnessConfig(0x7F);//

		//static int step = 0;
		int i, block, begin;
		unsigned short *p;

		if (!bShot)
		{
			begin = (datablock - 11 + 48) % 48;
	
			for (block = begin; block < begin + 11; block++)
			{
				p = (unsigned short *)(0x20000000 + (320 * 240 * 2 / 16) * ((block) % 12));
				LCD_SetDisplayWindow(0, (block % 16) * (240 / 16), 320, (240 / 16));
				//LCD_SetCursor(0, (block % 16) * (240 / 16));
				LCD_WriteRAM_Prepare();
				for (i = 0; i <	320 * 240 / 16; i++)
				{
					LCD_WriteRAM(*p++);
				}
			}
		}
		if (TSC_TouchDet())
		{
			int x, y;
			TP_GetAdXY(&x, &y);
			if (x > 300 && x < 2800 && y > 300 && y < 2800)
			{
				if (x < 1550 && y < 1550)
				{
					uint32_t StartSector = 0, EndSector = 0, Address = 0, SectorCounter = 0;
					int m;
					//拍照bShot
					bShot = 1;
					while(datablock % 16);
					DMA_Cmd(DMA2_Stream1, DISABLE); 
					DCMI_Cmd(DISABLE);
					//STM_EVAL_LEDOn(LED2); 
					DMA_ClearFlag(DMA2_Stream1, 0x2F7D0F7D);
					DMA_ClearFlag(DMA2_Stream1, 0x0F7D0F7D);
					DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
					datablock = 0;
					DMA2_Stream1->M0AR = 0x20000000 + 9600 * (datablock % 12);
					DMA_SetCurrDataCounter(DMA2_Stream1, (320 * 240 * 2 / 4) / 16);

					DCMI_Cmd(ENABLE);
					DMA_Cmd(DMA2_Stream1, ENABLE);
					
					{
						unsigned long *p, *q;
						while(datablock < 4);	//等待准备好前4块数据,就将这4块数据导入到0x10000000+0x50000之后。
						q = (unsigned long *)(0x20000000);
						p = (unsigned long *)(0x10000000 + 0x5000);
						while (q < (unsigned long *)(0x20000000 + 4 * (320 * 240 * 2 / 16)))
						{
							*p = *q;
							p++;
							q++;
						}
					}


					while(datablock < 16);		//等待全部采集完成。

					STM_EVAL_LEDOn(LED2);		//LED2亮表示采集完成。
					LCD_SetDisplayWindow(0, 0, 320, 240);
					LCD_WriteRAM_Prepare();
					LCD_Clear(Black);
					//RAM位置
					/*
						序号  地址                大小
						1:    0x10005000+9600*0     9600
						2:    0x10005000+9600*1     9600
						3:    0x10005000+9600*2     9600
						4:    0x10005000+9600*3     9600
						5:    0x20000000+9600*5     9600
						6:    0x20000000+9600*6     9600
						7:    0x20000000+9600*7     9600
						8:    0x20000000+9600*8     9600
						9:    0x20000000+9600*9     9600
						10:   0x20000000+9600*10    9600
						11:   0x20000000+9600*11    9600
						12:   0x20000000+9600*0     9600
						13:   0x20000000+9600*1     9600
						14:   0x20000000+9600*2     9600
						15:   0x20000000+9600*3     9600					
					*/
				 	for (m = 0; m < 16; m++)	//显示保存的图片
					{
						unsigned short *q;
						if (m < 4)
						{
							q = (unsigned short *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16));
						}
						else
						{
							q = (unsigned short *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16));
						}
						LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16));
						LCD_WriteRAM_Prepare();
						for (i = 0; i <	320 * 240 / 16; i++)
						{
							LCD_WriteRAM(*q++);
						}
					}

					/* Erase the user Flash area ***********/
					FLASH_Unlock();
					
					/* Clear pending flags (if any) */  
					FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | 
						FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 
					
					/* Get the number of the start and end sectors */
					StartSector = GetSector(FLASH_USER_START_ADDR);
					EndSector = GetSector(FLASH_USER_END_ADDR);
					
					for (SectorCounter = StartSector; SectorCounter < EndSector; SectorCounter += 8)
					{
						/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
						be done by word */ 
						if (FLASH_EraseSector(SectorCounter, VoltageRange_3) != FLASH_COMPLETE)
						{ 
							/* Error occurred while sector erase. 
							User can add here some code to deal with this error  */
							while (1)
							{
							}
						}
					}
										
				 	for (m = 0; m < 16; m++)	//保存图片到flash
					{
						unsigned long *q;
						Address = FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16);
						if (m < 4)
						{
							q = (unsigned long *)(0x10000000 + 0x5000 + m * (320 * 240 * 2 / 16));
						}
						else
						{
							q = (unsigned long *)(0x20000000 + (m % 12) * (320 * 240 * 2 / 16));
						}
						while (Address < FLASH_USER_START_ADDR + (m + 1) *(320 * 240 * 2 / 16))
						{
							if (FLASH_ProgramWord(Address, *q) == FLASH_COMPLETE)
							{
								Address = Address + 4;
								q++;
							}
							else
							{ 
								/* Error occurred while writing data in Flash memory. 
								User can add here some code to deal with this error */
								while (1)
								{
								}
							}
						}
					}

					STM_EVAL_LEDOff(LED2);
					LCD_SetDisplayWindow(0, 0, 320, 240);
					LCD_WriteRAM_Prepare();
					LCD_Clear(Black);
				 	for (m = 0; m < 16; m++)	//显示flash中的图片
					{
						unsigned short *q;
						q = (unsigned short *)(FLASH_USER_START_ADDR + m * (320 * 240 * 2 / 16));
						LCD_SetDisplayWindow(0, m * (240 / 16), 320, (240 / 16));
						LCD_WriteRAM_Prepare();
						for (i = 0; i <	320 * 240 / 16; i++)
						{
							LCD_WriteRAM(*q++);
						}
					}
					/* Lock the Flash to disable the flash control register access (recommended
					 to protect the FLASH memory against possible unwanted operation) *********/
					FLASH_Lock(); 
				}
				else if (x >= 1550 && y < 1550)
				{
					//righttop
					STM_EVAL_LEDOff(LED1);
					STM_EVAL_LEDOff(LED2);
					bShot = 0;
					datablock = 0;
					DMA_Cmd(DMA2_Stream1, ENABLE);
					DCMI_Cmd(ENABLE); 
				}
				else if (x < 1550 && y >= 1550)
				{
					//righttop
					//DMA_Cmd(DMA2_Stream1, ENABLE);
					//DCMI_Cmd(ENABLE); 
				}
				else if (x >= 1550 && y >= 1550)
				{
					//righttop
					//DMA_Cmd(DMA2_Stream1, ENABLE);
					//DCMI_Cmd(ENABLE); 
				}
			}
		}
		//Delay(10);
  	}	
}
Ejemplo n.º 3
0
/**
  * @brief  Calibrate TouchScreen coordinates by LCD touch in 5 points
  * @param  None
  * @retval None
  */
void TS_Calibration(void)
{
  uint32_t coordinate_X1a = 0, coordinate_X2a = 0, coordinate_X3a = 0, coordinate_X4a = 0, coordinate_X5a = 0;
  uint32_t coordinate_Y1a = 0, coordinate_Y2a = 0, coordinate_Y3a = 0, coordinate_Y4a = 0, coordinate_Y5a = 0;
  uint32_t coordinate_X1b = 0, coordinate_X2b = 0, coordinate_X3b = 0, coordinate_X4b = 0, coordinate_X5b = 0;
  uint32_t coordinate_Y1b = 0, coordinate_Y2b = 0, coordinate_Y3b = 0, coordinate_Y4b = 0, coordinate_Y5b = 0;
  uint32_t coordinate_X1 = 0, coordinate_X2 = 0, coordinate_X3 = 0, coordinate_X4 = 0, coordinate_X5 = 0;
  uint32_t coordinate_Y1 = 0, coordinate_Y2 = 0, coordinate_Y3 = 0, coordinate_Y4 = 0, coordinate_Y5 = 0;
  uint16_t Xd1 = (LCD_Width / 2), Xd2 = 1 * (LCD_Width / 5), Xd3 = 4 * (LCD_Width / 5), Xd4 = 4 * (LCD_Width / 5), Xd5 = 1 * (LCD_Width / 5);
  uint16_t Yd1 = (LCD_Height / 2), Yd2 = 1 * (LCD_Height / 5), Yd3 = 1 * (LCD_Height / 5), Yd4 = 4 * (LCD_Height / 5), Yd5 = 4 * (LCD_Height / 5);
  double A = 0.0, B = 0.0, C = 0.0, D = 0.0, E = 0.0, F = 0.0;
  double d = 0.0, dx1 = 0.0, dx2 = 0.0, dx3 = 0.0, dy1 = 0.0, dy2 = 0.0, dy3 = 0.0;
  uint32_t X2_1 = 0, X2_2 = 0, X2_3 = 0, X2_4 = 0, X2_5 = 0;
  uint32_t Y2_1 = 0, Y2_2 = 0, Y2_3 = 0, Y2_4 = 0, Y2_5 = 0;
  uint32_t XxY_1 = 0, XxY_2 = 0, XxY_3 = 0, XxY_4 = 0, XxY_5 = 0;
  uint32_t XxXd_1 = 0, XxXd_2 = 0, XxXd_3 = 0, XxXd_4 = 0, XxXd_5 = 0;
  uint32_t YxXd_1 = 0, YxXd_2 = 0, YxXd_3 = 0, YxXd_4 = 0, YxXd_5 = 0;
  uint32_t XxYd_1 = 0, XxYd_2 = 0, XxYd_3 = 0, XxYd_4 = 0, XxYd_5 = 0;
  uint32_t YxYd_1 = 0, YxYd_2 = 0, YxYd_3 = 0, YxYd_4 = 0, YxYd_5 = 0;
  uint32_t alfa = 0, beta = 0, chi = 0, Kx = 0, Ky = 0, Lx = 0, Ly = 0;
  uint16_t epsilon = 0, fi = 0, Mx = 0, My = 0;

  GL_SetBackColor(GL_White);
  GL_SetTextColor(GL_Black);
  GL_Clear(White);
  GL_DisplayAdjStringLine(3 * (LCD_Height / 7), LCD_Width - 25, "Run Calibration.", GL_FALSE);
  GL_Delay(40);
  GL_DisplayAdjStringLine(3 * (LCD_Height / 7), LCD_Width - 25, "Run Calibration..", GL_FALSE);
  GL_Delay(40);
  GL_DisplayAdjStringLine(3 * (LCD_Height / 7), LCD_Width - 25, "Run Calibration...", GL_FALSE);
  GL_Delay(30);
  touch_done = 0;

  GL_Clear(White);
  GL_Cross( (LCD_Height / 2), (LCD_Width / 2) ); /* Absolute Central Point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X1a = TSC_Value_X;
  coordinate_Y1a = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(1*(LCD_Height / 5), 1*(LCD_Width / 5)); /* Nord-East Corner point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X2a = TSC_Value_X;
  coordinate_Y2a = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(1*(LCD_Height / 5), 4*(LCD_Width / 5)); /* Nord-West Corner */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X3a = TSC_Value_X;
  coordinate_Y3a = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(4*(LCD_Height / 5), 4*(LCD_Width / 5)); /* Sud-West Corner */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X4a = TSC_Value_X;
  coordinate_Y4a = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(4*(LCD_Height / 5), 1*(LCD_Width / 5)); /* Sud-East Corner point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X5a = TSC_Value_X;
  coordinate_Y5a = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross( (LCD_Height / 2), (LCD_Width / 2) ); /* Absolute Central Point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X1b = TSC_Value_X;
  coordinate_Y1b = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(1*(LCD_Height / 5), 1*(LCD_Width / 5)); /* Nord-East Corner point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X2b = TSC_Value_X;
  coordinate_Y2b = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(1*(LCD_Height / 5), 4*(LCD_Width / 5)); /* Nord-West Corner */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X3b = TSC_Value_X;
  coordinate_Y3b = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(4*(LCD_Height / 5), 4*(LCD_Width / 5)); /* Sud-West Corner */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X4b = TSC_Value_X;
  coordinate_Y4b = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  GL_Clear(White);
  GL_Cross(4*(LCD_Height / 5), 1*(LCD_Width / 5)); /* Sud-East Corner point */
  while ( touch_done == 0)
  {
    TSC_Read();
  }
  coordinate_X5b = TSC_Value_X;
  coordinate_Y5b = TSC_Value_Y;

  GL_Delay(90); /* This is to catch only one touch event */
  TSC_Init();
  touch_done = 0;

  /* Average between X and Y coupled Touchscreen values */
  coordinate_X1 = (coordinate_X1a + coordinate_X1b) / 2;
  coordinate_X2 = (coordinate_X2a + coordinate_X2b) / 2;
  coordinate_X3 = (coordinate_X3a + coordinate_X3b) / 2;
  coordinate_X4 = (coordinate_X4a + coordinate_X4b) / 2;
  coordinate_X5 = (coordinate_X5a + coordinate_X5b) / 2;

  coordinate_Y1 = (coordinate_Y1a + coordinate_Y1b) / 2;
  coordinate_Y2 = (coordinate_Y2a + coordinate_Y2b) / 2;
  coordinate_Y3 = (coordinate_Y3a + coordinate_Y3b) / 2;
  coordinate_Y4 = (coordinate_Y4a + coordinate_Y4b) / 2;
  coordinate_Y5 = (coordinate_Y5a + coordinate_Y5b) / 2;

  X2_1 = (coordinate_X1 * coordinate_X1);
  X2_2 = (coordinate_X2 * coordinate_X2);
  X2_3 = (coordinate_X3 * coordinate_X3);
  X2_4 = (coordinate_X4 * coordinate_X4);
  X2_5 = (coordinate_X5 * coordinate_X5);

  Y2_1 = (coordinate_Y1 * coordinate_Y1);
  Y2_2 = (coordinate_Y2 * coordinate_Y2);
  Y2_3 = (coordinate_Y3 * coordinate_Y3);
  Y2_4 = (coordinate_Y4 * coordinate_Y4);
  Y2_5 = (coordinate_Y5 * coordinate_Y5);

  XxY_1 = (coordinate_X1 * coordinate_Y1);
  XxY_2 = (coordinate_X2 * coordinate_Y2);
  XxY_3 = (coordinate_X3 * coordinate_Y3);
  XxY_4 = (coordinate_X4 * coordinate_Y4);
  XxY_5 = (coordinate_X5 * coordinate_Y5);

  XxXd_1 = ( coordinate_X1 * Xd1 );
  XxXd_2 = ( coordinate_X2 * Xd2 );
  XxXd_3 = ( coordinate_X3 * Xd3 );
  XxXd_4 = ( coordinate_X4 * Xd4 );
  XxXd_5 = ( coordinate_X5 * Xd5 );

  YxXd_1 = ( coordinate_Y1 * Xd1 );
  YxXd_2 = ( coordinate_Y2 * Xd2 );
  YxXd_3 = ( coordinate_Y3 * Xd3 );
  YxXd_4 = ( coordinate_Y4 * Xd4 );
  YxXd_5 = ( coordinate_Y5 * Xd5 );

  XxYd_1 = ( coordinate_X1 * Yd1 );
  XxYd_2 = ( coordinate_X2 * Yd2 );
  XxYd_3 = ( coordinate_X3 * Yd3 );
  XxYd_4 = ( coordinate_X4 * Yd4 );
  XxYd_5 = ( coordinate_X5 * Yd5 );

  YxYd_1 = ( coordinate_Y1 * Yd1 );
  YxYd_2 = ( coordinate_Y2 * Yd2 );
  YxYd_3 = ( coordinate_Y3 * Yd3 );
  YxYd_4 = ( coordinate_Y4 * Yd4 );
  YxYd_5 = ( coordinate_Y5 * Yd5 );

  alfa = X2_1 + X2_2 + X2_3 + X2_4 + X2_5;
  beta = Y2_1 + Y2_2 + Y2_3 + Y2_4 + Y2_5;
  chi = XxY_1 + XxY_2 + XxY_3 + XxY_4 + XxY_5;
  epsilon = coordinate_X1 + coordinate_X2 + coordinate_X3 + coordinate_X4 + coordinate_X5;
  fi = coordinate_Y1 + coordinate_Y2 + coordinate_Y3 + coordinate_Y4 + coordinate_Y5;
  Kx = XxXd_1 + XxXd_2 + XxXd_3 + XxXd_4 + XxXd_5;
  Ky = XxYd_1 + XxYd_2 + XxYd_3 + XxYd_4 + XxYd_5;
  Lx = YxXd_1 + YxXd_2 + YxXd_3 + YxXd_4 + YxXd_5;
  Ly = YxYd_1 + YxYd_2 + YxYd_3 + YxYd_4 + YxYd_5;
  Mx = Xd1 + Xd2 + Xd3 + Xd4 + Xd5;
  My = Yd1 + Yd2 + Yd3 + Yd4 + Yd5;

  d = 5 * ( ((double)alfa * beta) - ((double)chi * chi) ) + 2 * ((double)chi * epsilon * fi) - ((double)alfa * fi * fi ) - ( (double)beta * epsilon * epsilon );
  dx1 = 5 * ( ((double)Kx * beta) - ((double)Lx * chi) ) + ((double)fi * ( ((double)Lx * epsilon) - ((double)Kx * fi) )) + ((double)Mx * ( ((double)chi * fi) - ((double)beta * epsilon) ));
  dx2 = 5 * ( ((double)Lx * alfa) - ((double)Kx * chi) ) + ((double)epsilon * ( ((double)Kx * fi) - ((double)Lx * epsilon) )) + ((double)Mx * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
  dx3 = ((double)Kx * ( ((double)chi * fi) - ((double)beta * epsilon) )) + ((double)Lx * ( ((double)chi * epsilon) - ((double)alfa * fi) )) + ((double)Mx * ( ((double)alfa * beta) - ((double)chi * chi) ));
  dy1 = 5 * ( ((double)Ky * beta) - ((double)Ly * chi) ) + ((double)fi * ( ((double)Ly * epsilon) - ((double)Ky * fi) )) + ((double)My * ( ((double)chi * fi) - ((double)beta * epsilon) ));
  dy2 = 5 * ( ((double)Ly * alfa) - ((double)Ky * chi) ) + ((double)epsilon * ( ((double)Ky * fi) - ((double)Ly * epsilon) )) + ((double)My * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
  dy3 = ((double)Ky * ( ((double)chi * fi) - ((double)beta * epsilon) )) + ((double)Ly * ( ((double)chi * epsilon) - ((double)alfa * fi) )) + ((double)My * ( ((double)alfa * beta) - ((double)chi * chi) ));

  A = dx1 / d;
  B = dx2 / d;
  C = dx3 / d;
  D = dy1 / d;
  E = dy2 / d;
  F = dy3 / d;

  /* To avoid computation with "double" variables A, B, C, D, E, F, we use the s32 variables
     A2, B2, C2, D2, E2, F2, multiplied for a Scale Factor equal to 100000 to retain the precision*/
  A2 = (s32)(A * RESCALE_FACTOR);
  B2 = (s32)(B * RESCALE_FACTOR);
  C2 = (s32)(C * RESCALE_FACTOR);
  D2 = (s32)(D * RESCALE_FACTOR);
  E2 = (s32)(E * RESCALE_FACTOR);
  F2 = (s32)(F * RESCALE_FACTOR);

  GL_Clear(White);
  GL_DisplayAdjStringLine(3 * (LCD_Height / 7), 10 * (LCD_Width / 11), "Calibration done!", GL_FALSE);

  GL_Delay(25); /* Now show HOME Menu */
  GL_Clear(White);
  calibration_done = 1;

  /********************* FLASH PROGRAMMING FOR SAVING "calibration_done" variable **********************/
  TSC_FlashStatus = TSC_FLASH_COMPLETE;
  TSC_MemoryProgramStatus = PASSED;
  FlashFree_Address = CalibrationAddr;

  /* Unlock the Flash Program Erase controller */
  TSC_FLASH_Unlock();

  /* Erase Flash sectors ******************************************************/
#if defined(USE_STM3210C_EVAL) || defined(USE_STM32100E_EVAL) || defined(USE_STM3220F_EVAL)
  /* Clear All pending flags */
  TSC_FLASH_ClearFlag( TSC_FLASH_FLAG_BSY | TSC_FLASH_FLAG_EOP | TSC_FLASH_FLAG_PGERR | TSC_FLASH_FLAG_WRPRTERR);

  /* Erase Last Flash Page */
  TSC_FlashStatus = TSC_FLASH_ErasePage( FlashFree_Address );

#endif

  /* Writing calibration parameters to the Flash Memory */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, calibration_done);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, A2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, B2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, C2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, D2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, E2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;
  /* Writing Calibration Parameter */
  TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, F2);
  /* Increasing Flash Memory Page Address */
  FlashFree_Address = FlashFree_Address + 4;

  /* Reading Calibration Flag from the Flash Memory */
  calibration_done = (*(__IO uint32_t*) CalibrationAddr) & 0x000000FF;
}