#include <stdio.h>
#include <string.h>
#include <ADuCM360.h>
#include <math.h>

float fVRTD = 0.0 ;                       // RTD voltage, 
float fRrtd = 0.0;                        // resistance of the RTD 
float fVRTD2 = 0.0 ;                       // RTD voltage vnejsi
float fRrtd2 = 0.0;                        // resistance of the RTD vnejsi
float fIexc0 = 0.0002;                       // Measured Excitation current 
float fTRTD =0.0; //teplota
float fTRTD2 =0.0; //teplota
volatile int ITM_RxBuffer = ITM_RXBUFFER_EMPTY;  		  /*  CMSIS Debug Input        */
const uint32_t led_mask[] = { 1UL << 3 };			        /* GPIO1.3 */
int bSendResultToUART=0;															//ready to send uart
void delay(long int a);								//obycejny delay				
void Send_Uart (char x);          //send 1 char
void Send (char Buff[]);					//send a whole line							
void UARTINIT(void);						  //iniciace uartu
float A = 3.9083e-3;
float B = -5.775e-7;
int main (void) {
	pADI_GP1->GPOEN  =  led_mask[0] ;                /* GPIO1.3 is output    */
	pADI_GP1->GPSET  =  led_mask[0];                 /* switch LED off       */                        
	UARTINIT();                         
	pADI_ANA->REFCTRL = 0x0;																// enable reference
	pADI_ANA->IEXCDAT = 0xE;																// Set output for 0,2mA - 0b00001110
	pADI_ANA->IEXCCON = 0x74;												//44				//Setup Iexc for AIN6 a ain4 -0b01110100 01000100

	while (1){

		pADI_ADC1->CON = 0x8C001;				//zapnuty bufery 
		pADI_ADC1->MDE = 0x51;           //g=32 nekonecne mereni 
		pADI_ADC1->FLT = 0x8D7C;         //30Hz sampling rate, chop off //0x8D7C means 3.53Hz ale hodnoty jsou presnejsi
		delay(100);
		 long int c = 0;
		 int d = 0;
		 c = pADI_ADC1->STA;
		 c = c&0xFE;
		 pADI_ADC1->STA = c;
		
		
		 while (d==0) {
				d = pADI_ADC1->STA&1;
	   }
		 int long signed w = 0;
		 w = pADI_ADC1->DAT;         //cteni hodnoty napeti
		 w = w >> 2;
		 fVRTD = w*4*1.2  / 268435456; //vypocet napeti	 2 na 28 stupen - adcdat register;//
		 fRrtd = fVRTD/fIexc0;                     // RTD resistance
		 fRrtd = fRrtd - (0.013*fRrtd + 0.046);
			fTRTD = (-A+sqrt(A*A-4*B*(1-fRrtd/100)))/2/B;
		 char buffer[8] ; 		 
	/*	sprintf(buffer, "%f", fVRTD);               //posilam odpor rtd
  	 Send(buffer);	 
		 delay(100000);*/
		 sprintf(buffer, "%f", fTRTD);               //posilam odpor rtd
  	 Send(buffer);	 
		 delay(1000000);
	//--------------------------------vnejsi rtd----------------------------
		 			pADI_ADC1->CON = 0x8C085;				 //zapnuty bufery 10001100000010000101 8C085
		pADI_ADC1->MDE = 0x51;           //g=32 nekonecne mereni 51
		pADI_ADC1->FLT = 0x8D7C;         //30Hz sampling rate, chop off //0x8D7C means 3.53Hz ale hodnoty jsou presnejsi

		delay(100);
	 c = 0;
	 d = 0;
		 c = pADI_ADC1->STA;
		 c = c&0xFE;
		 pADI_ADC1->STA = c;
		 while (d==0) {
				d = pADI_ADC1->STA&1;
	   }
 w=0;
		 w = pADI_ADC1->DAT;         //cteni hodnoty napeti
		 w = w >> 2;
		 fVRTD2 = w*4*1.2  / 268435456; //vypocet napeti	 2 na 28 stupen - adcdat register;//
		 fRrtd2 = fVRTD2/fIexc0;                     // RTD resistance
		 fRrtd2 = fRrtd2 - (0.013*fRrtd2 + 0.737);   //+0.701 (0.012*fRrtd2 - 0.624)
		 fTRTD2 = (-A+sqrt(A*A-4*B*(1-fRrtd2/100)))/2/B;
		// sprintf(buffer, "%f", fVRTD2);               //posilam odpor rtd
  	// Send(buffer);	 
		//delay(100000);
		 sprintf(buffer, "%f", fTRTD2);               //posilam odpor rtd
  	 Send(buffer);	 
		 delay(1000000);
			 char buffer1[8] ;
		 buffer1[0] = '\n';
  	 Send(buffer1);	 
		 delay(100000);
	}
}

void UARTINIT(void){
	pADI_UART->COMCON= 0x0;    //enable uart
	pADI_GP0->GPCON |= 0x3C;   // Configure P0.1/P0.2 for UART 
  pADI_UART->COMLCR= 0x3;	   //no parity, 1 stop bit, 8 data bits
	pADI_UART->COMDIV= 0x2;    //comdiv=2
	//pADI_UART->COMFBR= 0x9B84; //baud rate 115200 divm=2 divn=384 
  //pADI_UART->COMLCR = ((0UL << 3) | (0UL << 2) | (3UL << 0)  );          /* no Parity  / 1 Stop bit / 8 Data bits / 115200Baud @ 16MHz */
  //pADI_UART->COMDIV =  (2UL << 0);                                       /* COMDIV = 2     */
  pADI_UART->COMFBR = ((  1UL << 15) | (  2UL << 11) | (384UL <<  0)  ); /* enable FBRG / DIVM = 2 / DIVN = 384  */
}
void Send_Uart (char x){
		
		while (!(pADI_UART->COMLSR == 0))
    pADI_UART->COMTX=x;
}
void Send (char Buff[]) {
	int cnt=0; 
	int cnt1=0; 
	int i=0;

		Send_Uart(0x02);                                  //zacinam posilat
	while (i <= 700) i++;                         			//delay 
	
	cnt1=strlen(Buff);																	//cnt1=delka Buff
			while (cnt<cnt1) {															//posilam cely line
				if (1) {
				Send_Uart(Buff[cnt]);
				i=0;
				while (i <= 500) i++; }
				cnt++;
			}
				i=0;	while (i <= 700) i++;                   //delay
	Send_Uart(0x0d);			                              //konec
}
void delay(long int a){
while (a>0) a--;
}
