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

void delay(long int length);

int main (void) {
		pADI_GP1->GPOEN = 0x8; // GPIO1.3 is output
  pADI_GP1->GPSET = 0x8; // switch LED off
	
  pADI_WDT->T3CON = 0x00 ; // Disable watchdog timer 
	pADI_CLKCTL->CLKCON0 = 0x0;
	pADI_CLKCTL->CLKSYSDIV = 0x0;          //16 mhz
  delay(10000000);

  pADI_WDT->T3CON = 0x00 ; // Disable watchdog timer	

  pADI_GP1->GPCLR = 0x8; // Turn on LED
  delay(10000000);
  pADI_GP1->GPSET = 0x8; // Turn off LED

	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= 0x915C; // baud rate 115200 divm=2 divn=384
	delay(10000000);
	 
	pADI_UART->COMCON= 0x1;
  pADI_ANA->REFCTRL = 0x0; // enable reference
  pADI_ANA->IEXCDAT = 0xE; // Set output for 0,2mA - 0b00001110
  pADI_ANA->IEXCCON = 0x74; // Setup Iexc for AIN6 -0b01110000 a ain4 01000100
  delay(10000000);

  pADI_ANA->REFCTRL = 0x1;
	pADI_ADC1->CON = 0x8C001; // zapnuti periferie
  pADI_ADC1->MDE = 0x51; // g=32 nekonecny prevod
  pADI_ADC1->FLT = 0x8D7C; // 3.53Hz sampling rate, chop off
	delay(10000000);	
	
	while(1){
  
		  pADI_GP1->GPCLR = 0x8; // Turn on LED
  delay(1000000);
  pADI_GP1->GPSET = 0x8; // Turn off LED
		delay(1000000);
		
  SCB->SCR = 0x04; // sleepdeep mode
  pADI_PWRCTL->PWRKEY = 0x4859; // key1
  pADI_PWRCTL->PWRKEY = 0xF27B; // key2
  pADI_PWRCTL->PWRMOD = PWRMOD_MOD_TOTALHALT; // vyber sleep modu
  __WFI;
	}
}
void delay(long int length){
while (length>0)
length--;
}
