Bläddra i källkod

Чуть причесал исходники.

Vladimir N. Shilov 8 år sedan
förälder
incheckning
48207673d7
12 ändrade filer med 2545 tillägg och 3013 borttagningar
  1. 253 252
      src/LabArm.c
  2. 26 27
      src/LabArm.h
  3. 0 465
      src/LabArm.uvproj
  4. 5 5
      src/LabArm.uvprojx
  5. 378 378
      src/calibration.c
  6. 212 213
      src/clock.c
  7. 129 130
      src/config.c
  8. 418 419
      src/event.c
  9. 293 293
      src/graph.c
  10. 247 247
      src/n3310.c
  11. 1 1
      src/n3310.h
  12. 583 583
      src/ui.c

+ 253 - 252
src/LabArm.c

@@ -16,47 +16,47 @@ void DMA1_Channel1_IRQHandler()
 void DMAChannel1_IRQHandler(void) /* Read ADC values */
 #endif
 {
-  if (DMA1->ISR & DMA_ISR_HTIF1 && /*   Half Transfer complete */
-       (DMA1_Channel1->CCR & DMA_CCR1_HTIE) /* Half traisfer interrupt enabled */
-      ) 
-  {
-    int i;
-    /* HTIF is set every time when transfer interrupt > half !!! */
-    DMA1_Channel1->CCR &= ~DMA_CCR1_HTIE; /* disable Half traisfer interrupt !!!! */
-
-    for (i = 0; i < ADC_ARRAY_SIZE/2; i = i+2)
+    if (DMA1->ISR & DMA_ISR_HTIF1 && /*   Half Transfer complete */
+            (DMA1_Channel1->CCR & DMA_CCR1_HTIE) /* Half traisfer interrupt enabled */
+       )
     {
-      ADCVoltageSum += AdcOutArray[i];
-      ADCCurrentSum += AdcOutArray[i+1];
-    }
-  } else if (DMA1->ISR & DMA_ISR_TCIF1 ) /* transfer complete */
-  {
-    int i;
-
-    /* transfer complete */
-    DMA1->IFCR |= DMA_IFCR_CGIF1; /* Clear all interrupt flags */
-    DMA1_Channel1->CCR |= DMA_CCR1_HTIE; /* enable Half traisfer interrupt  */
-
-    for (i=ADC_ARRAY_SIZE/2; i< ADC_ARRAY_SIZE; i=i+2)
+        int i;
+        /* HTIF is set every time when transfer interrupt > half !!! */
+        DMA1_Channel1->CCR &= ~DMA_CCR1_HTIE; /* disable Half traisfer interrupt !!!! */
+
+        for (i = 0; i < ADC_ARRAY_SIZE/2; i = i+2)
+        {
+            ADCVoltageSum += AdcOutArray[i];
+            ADCCurrentSum += AdcOutArray[i+1];
+        }
+    } else if (DMA1->ISR & DMA_ISR_TCIF1 ) /* transfer complete */
     {
-      ADCVoltageSum += AdcOutArray[i];
-      ADCCurrentSum += AdcOutArray[i+1];
+        int i;
+
+        /* transfer complete */
+        DMA1->IFCR |= DMA_IFCR_CGIF1; /* Clear all interrupt flags */
+        DMA1_Channel1->CCR |= DMA_CCR1_HTIE; /* enable Half traisfer interrupt  */
+
+        for (i=ADC_ARRAY_SIZE/2; i< ADC_ARRAY_SIZE; i=i+2)
+        {
+            ADCVoltageSum += AdcOutArray[i];
+            ADCCurrentSum += AdcOutArray[i+1];
+        }
+
+        DMACounter++;
+        if (DMACounter == 16) /* 2Mhz/195/ADC_ARRAY_SIZE*2/16 - refresh frenq = 5 Hz */
+        {
+            DMACounter = 0;
+            ADCVoltage = ADCVoltageSum;
+            ADCCurrent = ADCCurrentSum;
+            NVIC_SetPendingIRQ(EXTI4_IRQn);
+            ADCVoltageSum = 0;
+            ADCCurrentSum = 0;
+            if ( EventQueue == 0 ) /* Lowest priority */
+                EventQueue = EV_KEY_PRESSED|KEY_ADC;
+        }
     }
-
-    DMACounter++;
-    if (DMACounter == 16) /* 2Mhz/195/ADC_ARRAY_SIZE*2/16 - refresh frenq = 5 Hz */
-    {
-      DMACounter = 0;
-      ADCVoltage = ADCVoltageSum;
-      ADCCurrent = ADCCurrentSum;
-      NVIC_SetPendingIRQ(EXTI4_IRQn);
-      ADCVoltageSum = 0;
-      ADCCurrentSum = 0;
-      if ( EventQueue == 0 ) /* Lowest priority */
-        EventQueue = EV_KEY_PRESSED|KEY_ADC; 
-    }
-  }
-  DMA1->IFCR |= DMA_IFCR_CGIF1; /* Clear all interrupt flags */
+    DMA1->IFCR |= DMA_IFCR_CGIF1; /* Clear all interrupt flags */
 }
 
 #if defined(__GNUC__)
@@ -65,34 +65,34 @@ void ADC1_IRQHandler(void)
 void ADC_IRQHandler(void) /*  Watchdog */
 #endif
 {
-  DAC_V = 0; /* OFF output */
-  DAC_I = 0;
-  ADC1->CR2 = 0; /* Adc Off */
-  GPIOB->BRR = GPIO_BRR_BR0|GPIO_BRR_BR1;
-  GPIOB->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_CNF1); /* PUSH pull */
-  DAC->CR = 0; /* Dac OFF */
-
-  /* Save current settings */
-  SaveSettings(0);
-  LcdInit(Contrast*3+42);
-  LcdChr(Y_POSITION*3+X_POSITION*0+14+INVERSE, "Power fault" );
-
-  NVIC->ICER[(ADC1_IRQn >> 0x05)] =	(u32)0x01 << (ADC1_IRQn & (u8)0x1F); /* Off ADC interrupt */
-  NVIC->ICPR[(ADC1_IRQn >> 0x05)] =	(u32)0x01 << (ADC1_IRQn & (u8)0x1F); /* Clear pend ADC interrupt */
-
-  { /* Waiting for power off */
-    __IO uint32_t Delay = 5000000;
-    while (Delay--) ;
-  }
-
-  /* Reboot */
-  NVIC_SystemReset();
+    DAC_V = 0; /* OFF output */
+    DAC_I = 0;
+    ADC1->CR2 = 0; /* Adc Off */
+    GPIOB->BRR = GPIO_BRR_BR0|GPIO_BRR_BR1;
+    GPIOB->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_CNF1); /* PUSH pull */
+    DAC->CR = 0; /* Dac OFF */
+
+    /* Save current settings */
+    SaveSettings(0);
+    LcdInit(Contrast*3+42);
+    LcdChr(Y_POSITION*3+X_POSITION*0+14+INVERSE, "Power fault" );
+
+    NVIC->ICER[(ADC1_IRQn >> 0x05)] =	(u32)0x01 << (ADC1_IRQn & (u8)0x1F); /* Off ADC interrupt */
+    NVIC->ICPR[(ADC1_IRQn >> 0x05)] =	(u32)0x01 << (ADC1_IRQn & (u8)0x1F); /* Clear pend ADC interrupt */
+
+    {   /* Waiting for power off */
+        __IO uint32_t Delay = 5000000;
+        while (Delay--) ;
+    }
+
+    /* Reboot */
+    NVIC_SystemReset();
 }
 
 void LcdBlank(void)
 {
-  LcdInit(42+Contrast*3);
-  LcdClear();
+    LcdInit(42+Contrast*3);
+    LcdClear();
 }
 
 int16_t HumanV;
@@ -101,227 +101,228 @@ int16_t HumanI;
 
 void EXTI4_IRQHandler(void)
 {
-  HumanV = VoltageFromAdc(); 
-  HumanI = CurrentFromAdc();
+    HumanV = VoltageFromAdc();
+    HumanI = CurrentFromAdc();
 #if defined(GRAPH)
-  {
-    static Graph_t I = {32000,-32000};
-    static Graph_t V = {32000,-32000};
-    static uint16_t Time;
-
-    if (  HumanI < I.Min )
-      I.Min = HumanI;
-    if ( HumanI > I.Max )
-      I.Max = HumanI;
-    if (HumanV < V.Min)
-      V.Min = HumanV;
-    if (HumanV > V.Max)
-      V.Max = HumanV;
-    Time++;
-    if (Time >= TimeInterval)
     {
-      Time = 0;
-      GraphCurrentPoint++;
-      if (GraphCurrentPoint == GRAPH_SIZE)
-        GraphCurrentPoint = 0;
-      IGraphArray[GraphCurrentPoint] = I;
-      VGraphArray[GraphCurrentPoint] = V;
-      I.Min = 32000;
-      I.Max = -32000;
-      V.Min = 32000;
-      V.Max = -32000;
+        static Graph_t I = {32000,-32000};
+        static Graph_t V = {32000,-32000};
+        static uint16_t Time;
+
+        if (  HumanI < I.Min )
+            I.Min = HumanI;
+        if ( HumanI > I.Max )
+            I.Max = HumanI;
+        if (HumanV < V.Min)
+            V.Min = HumanV;
+        if (HumanV > V.Max)
+            V.Max = HumanV;
+        Time++;
+        if (Time >= TimeInterval)
+        {
+            Time = 0;
+            GraphCurrentPoint++;
+            if (GraphCurrentPoint == GRAPH_SIZE)
+                GraphCurrentPoint = 0;
+            IGraphArray[GraphCurrentPoint] = I;
+            VGraphArray[GraphCurrentPoint] = V;
+            I.Min = 32000;
+            I.Max = -32000;
+            V.Min = 32000;
+            V.Max = -32000;
+        }
     }
-  }
 #endif
 }
 
 #if 1
-void SystemInit() 
+void SystemInit()
 {};
 
 
 int main()
 {
- /* init hardware */
-  SCB->VTOR = FLASH_BASE;
-  NVIC_EnableIRQ(DMA1_Channel1_IRQn); /* Enable DMA interrupt */
-  NVIC_EnableIRQ(ADC1_IRQn); /* Enable ADC interrupt */
-  NVIC_EnableIRQ(EXTI4_IRQn); /*Additional IRQ to calculate V and I */
-  NVIC_SetPriority(EXTI4_IRQn, 12); /* Low priority */
-
-  /* CLOCK = 8MHz / 8 = 1 MHz */
-  RCC->CFGR |= RCC_CFGR_HPRE_DIV2|RCC_CFGR_PPRE1_DIV4|RCC_CFGR_PPRE2_DIV2; /* APB1 - 1MHz, AHB - 4MHz, APB2 - 2MHz, ADC - 1MHz */
-  /* Enable peripery clock */
-  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |RCC_APB2ENR_ADC1EN|RCC_APB2ENR_TIM1EN;
-  RCC->APB1ENR |= RCC_APB1ENR_TIM3EN|RCC_APB1ENR_TIM2EN|RCC_APB1ENR_DACEN|RCC_APB1ENR_PWREN|RCC_APB1ENR_BKPEN; 
-  RCC->AHBENR  |= RCC_AHBENR_DMA1EN|RCC_AHBENR_CRCEN; 
-
-  /* Unlock the backup domain */
-  PWR->CR |= PWR_CR_DBP;
-
-  /* GPIO configure */
-  /* PB12 - PB14 - LCD, PA0 - PA2 - encoder(TIM2), PA4-PA5 - DAC Out, PB0-PB1 - ADC(8-9channel), PA7 - ADC power measure */
-  /* PB0 - Voltage IN, PB1 - CurrentIN, PA4 - VoltageOut, PA5 - CurrentOut */
-  GPIOB->CRH |= GPIO_CRH_MODE12_1|GPIO_CRH_MODE13_1|GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1; /* Output 2MHz */
-  GPIOB->CRH &= ~(GPIO_CRH_CNF12|GPIO_CRH_CNF13|GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* Output Push-pull */
-  GPIOB->CRL |= GPIO_CRL_MODE0_1 |GPIO_CRL_MODE1_1;
-  GPIOB->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_CNF1); /* PUSH pull */
-  
-  GPIOA->CRL |= GPIO_CRL_MODE4_1 |GPIO_CRL_MODE5_1|GPIO_CRL_MODE6_1|GPIO_CRL_MODE7_1; /* Out 2 MHZ*/
-  GPIOA->CRL &= ~(GPIO_CRL_CNF4|GPIO_CRL_CNF5|GPIO_CRL_CNF6|GPIO_CRL_CNF7); /* PUSH pull */
-  GPIOA->BSRR =  GPIO_BSRR_BS6;
-
-  LcdBlank();
-  /* Enable the programmable voltage detector */
-  PWR->CR |= PWR_CR_PLS_2|PWR_CR_PLS_1; /* 2.8 V */
-  PWR->CR |= PWR_CR_PVDE; /* Enable the PVD */
-
-  { /* Waiting for power stabilize */
-    __IO uint32_t Delay = 1000000;
-    while (Delay--) ;
-  }
-
-  /* Check power supply */
-  while ( (PWR->CSR & PWR_CSR_PVDO ) != 0 )
-    ; /* BLANK */
-  /* Power is OK for ADC */
-
-  /* Waiting for stabilize power voltage. It should be more then 1.2 V on the PB5 pin */
-  {
-    /* Measure PB5 and Vint by injected group */
-    GPIOA->CRL &= ~GPIO_CRL_MODE7; /* Input, Analog*/
-    ADC1->CR1 |= ADC_CR1_SCAN;
-    ADC1->CR2 |= ADC_CR2_TSVREFE|ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL; /* injected start by JSWSTART */
-    ADC1->SMPR1 |= ADC_SMPR1_SMP17; /* Internal Vref, max sampling time */
-    ADC1->SMPR2 |= ADC_SMPR2_SMP7; /* max sampling time */
-    ADC1->CR2 |= ADC_CR2_ADON; /* On the ADC */
-    
+    /* init hardware */
+    SCB->VTOR = FLASH_BASE;
+    NVIC_EnableIRQ(DMA1_Channel1_IRQn); /* Enable DMA interrupt */
+    NVIC_EnableIRQ(ADC1_IRQn); /* Enable ADC interrupt */
+    NVIC_EnableIRQ(EXTI4_IRQn); /*Additional IRQ to calculate V and I */
+    NVIC_SetPriority(EXTI4_IRQn, 12); /* Low priority */
+
+    /* CLOCK = 8MHz / 8 = 1 MHz */
+    RCC->CFGR |= RCC_CFGR_HPRE_DIV2|RCC_CFGR_PPRE1_DIV4|RCC_CFGR_PPRE2_DIV2; /* APB1 - 1MHz, AHB - 4MHz, APB2 - 2MHz, ADC - 1MHz */
+    /* Enable peripery clock */
+    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |RCC_APB2ENR_ADC1EN|RCC_APB2ENR_TIM1EN;
+    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN|RCC_APB1ENR_TIM2EN|RCC_APB1ENR_DACEN|RCC_APB1ENR_PWREN|RCC_APB1ENR_BKPEN;
+    RCC->AHBENR  |= RCC_AHBENR_DMA1EN|RCC_AHBENR_CRCEN;
+
+    /* Unlock the backup domain */
+    PWR->CR |= PWR_CR_DBP;
+
+    /* GPIO configure */
+    /* PB12 - PB14 - LCD, PA0 - PA2 - encoder(TIM2), PA4-PA5 - DAC Out, PB0-PB1 - ADC(8-9channel), PA7 - ADC power measure */
+    /* PB0 - Voltage IN, PB1 - CurrentIN, PA4 - VoltageOut, PA5 - CurrentOut */
+    GPIOB->CRH |= GPIO_CRH_MODE12_1|GPIO_CRH_MODE13_1|GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1; /* Output 2MHz */
+    GPIOB->CRH &= ~(GPIO_CRH_CNF12|GPIO_CRH_CNF13|GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* Output Push-pull */
+    GPIOB->CRL |= GPIO_CRL_MODE0_1 |GPIO_CRL_MODE1_1;
+    GPIOB->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_CNF1); /* PUSH pull */
+
+    GPIOA->CRL |= GPIO_CRL_MODE4_1 |GPIO_CRL_MODE5_1|GPIO_CRL_MODE6_1|GPIO_CRL_MODE7_1; /* Out 2 MHZ*/
+    GPIOA->CRL &= ~(GPIO_CRL_CNF4|GPIO_CRL_CNF5|GPIO_CRL_CNF6|GPIO_CRL_CNF7); /* PUSH pull */
+    GPIOA->BSRR =  GPIO_BSRR_BS6;
+
+    LcdBlank();
+    /* Enable the programmable voltage detector */
+    PWR->CR |= PWR_CR_PLS_2|PWR_CR_PLS_1; /* 2.8 V */
+    PWR->CR |= PWR_CR_PVDE; /* Enable the PVD */
+
+    {   /* Waiting for power stabilize */
+        __IO uint32_t Delay = 1000000;
+        while (Delay--) ;
+    }
+
+    /* Check power supply */
+    while ( (PWR->CSR & PWR_CSR_PVDO ) != 0 )
+        ; /* BLANK */
+    /* Power is OK for ADC */
+
+    /* Waiting for stabilize power voltage. It should be more then 1.2 V on the PB5 pin */
+    {
+        /* Measure PB5 and Vint by injected group */
+        GPIOA->CRL &= ~GPIO_CRL_MODE7; /* Input, Analog*/
+        ADC1->CR1 |= ADC_CR1_SCAN;
+        ADC1->CR2 |= ADC_CR2_TSVREFE|ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL; /* injected start by JSWSTART */
+        ADC1->SMPR1 |= ADC_SMPR1_SMP17; /* Internal Vref, max sampling time */
+        ADC1->SMPR2 |= ADC_SMPR2_SMP7; /* max sampling time */
+        ADC1->CR2 |= ADC_CR2_ADON; /* On the ADC */
+
+        /* Additional delay to Vref on */
+        Delay(10000);
+
+        /* Calibration */
+        ADC1->CR2 |= ADC_CR2_RSTCAL;
+        while ( ADC1->CR2 & ADC_CR2_RSTCAL )
+            ; /* BLANK */
+        ADC1->CR2 |= ADC_CR2_CAL;
+        while ( ADC1->CR2 & ADC_CR2_CAL )
+            ; /* BLANK */
+
+        Delay(10000);
+    }
+
+    /* theck the power voltage */
+    /* 2 conversions. 7-th channels, 17-th channel  */
+    ADC1->JSQR |= ADC_JSQR_JL_0| /* 2 conversion */
+                  ADC_JSQR_JSQ3_0|ADC_JSQR_JSQ3_1|ADC_JSQR_JSQ3_2| /* CH 7 */
+                  ADC_JSQR_JSQ4_4|ADC_JSQR_JSQ4_0; /* CH 17 */
+    do
+    {
+        ADC1->SR &= ~ADC_SR_JEOC; /* Reset convertion flag */
+        ADC1->CR2 |= ADC_CR2_JSWSTART; /* Start the convertion for two channels */
+        while ((ADC1->SR & ADC_SR_JEOC) == 0) /* Waiting end of convertion */
+            ; /* BLANK */
+#if defined(AWD_ENABLED)
+    } while ( ADC1->JDR1/* Vin */ < ADC1->JDR2/* Vref */ );
+#else
+    }
+    while ( 0 );
+#endif
+    {
+        uint16_t SavedVRef = ADC1->JDR2;
+
+        /* Set up analog watch dog */
+        ADC1->CR2 = 0; /* Power off */
+        /* Reset the ADC */
+        RCC->APB2RSTR |= RCC_APB2RSTR_ADC1RST;
+        RCC->APB2RSTR &= ~RCC_APB2RSTR_ADC1RST;
+
+        ADC1->HTR = 0xFFF; /* Max value for HIGHT threshold*/
+        ADC1->LTR = SavedVRef/6*5; /* 1 V */
+    }
+
+    /* Set up ADC, TIM1, DMA for conversion */
+
+    ADC1->CR1 |=
+#if defined(AWD_ENABLED)
+        ADC_CR1_JAWDEN|ADC_CR1_AWDIE| /* AWD on injected channels whith interrupt */
+#endif
+        ADC_CR1_JAUTO|ADC_CR1_SCAN; /* Scan mode + auto injection */
+    ADC1->SMPR2 = ADC_SMPR2_SMP7_0|ADC_SMPR2_SMP8_0|ADC_SMPR2_SMP9_0; /* 7.5 sampling time, 20 - adc time, 5,6,7 ch */
+    ADC1->SQR1 =  ADC_SQR1_L_0; /* 2 conversion */
+    ADC1->SQR3 =  ADC_SQR3_SQ1_3| /* 8 channel - voltage*/
+                  ADC_SQR3_SQ2_3|ADC_SQR3_SQ2_0; /* 9- channel - current */
+    ADC1->JSQR = ADC_JSQR_JSQ4_2|ADC_JSQR_JSQ4_1|ADC_JSQR_JSQ4_0; /* 1 conversion by 7-th channel in inj */
+    ADC1->CR2 |= ADC_CR2_EXTTRIG|ADC_CR2_DMA; /* External trigger by T1 CC1, DMA */
+
+    GPIOA->CRL &= ~(GPIO_CRL_MODE4_1|GPIO_CRL_MODE5_1|GPIO_CRL_MODE7_1); /* Analog IN */
+    GPIOB->CRL &= ~(GPIO_CRL_MODE0_1|GPIO_CRL_MODE1_1); /* Analog IN */
+
+
+    /* TIM1 is configyred to trigger the ADC */
+    TIM1->ARR = 194; /* 2MHz / 200 = 10KHz */
+    TIM1->CCMR1 |= TIM_CCMR1_OC1M; /* PWM 2 mode */
+    TIM1->CCR1 = 99;
+    TIM1->BDTR |= TIM_BDTR_MOE; /* Only for TIM1 - MAIN OUTPUT ENABLE!!! */
+    TIM1->CCER |= TIM_CCER_CC1E; /* Output enable */
+
+    /* DMA configuring */
+    DMA1_Channel1->CCR |= DMA_CCR1_PL|DMA_CCR1_MSIZE_0|DMA_CCR1_PSIZE_0| /*Hight pry, 16 byte mem, 16 byte pereph */
+                          DMA_CCR1_MINC|DMA_CCR1_CIRC|DMA_CCR1_HTIE|DMA_CCR1_TCIE; /*  mem inc, circular, enterrupts by Half and End of conv */
+    DMA1_Channel1->CNDTR = ADC_ARRAY_SIZE;
+    DMA1_Channel1->CPAR = (uint32_t)&ADC1->DR;
+    DMA1_Channel1->CMAR = (uint32_t)&AdcOutArray[0];
+
+    /* On converting */
+    ADC1->CR2 |= ADC_CR2_ADON; /* Adc ON */
     /* Additional delay to Vref on */
     Delay(10000);
 
     /* Calibration */
     ADC1->CR2 |= ADC_CR2_RSTCAL;
     while ( ADC1->CR2 & ADC_CR2_RSTCAL )
-      ; /* BLANK */
+        ; /* BLANK */
     ADC1->CR2 |= ADC_CR2_CAL;
     while ( ADC1->CR2 & ADC_CR2_CAL )
-      ; /* BLANK */
+        ; /* BLANK */
 
     Delay(10000);
-  }
-    
-  /* theck the power voltage */
-  /* 2 conversions. 7-th channels, 17-th channel  */
-  ADC1->JSQR |= ADC_JSQR_JL_0| /* 2 conversion */
-                ADC_JSQR_JSQ3_0|ADC_JSQR_JSQ3_1|ADC_JSQR_JSQ3_2| /* CH 7 */
-                ADC_JSQR_JSQ4_4|ADC_JSQR_JSQ4_0; /* CH 17 */
-  do
-  {
-    ADC1->SR &= ~ADC_SR_JEOC; /* Reset convertion flag */
-    ADC1->CR2 |= ADC_CR2_JSWSTART; /* Start the convertion for two channels */
-    while ((ADC1->SR & ADC_SR_JEOC) == 0) /* Waiting end of convertion */
-      ; /* BLANK */
-#if defined(AWD_ENABLED)
-  } while ( ADC1->JDR1/* Vin */ < ADC1->JDR2/* Vref */ );
-#else
-  } while ( 0 );
-#endif
-  {
-    uint16_t SavedVRef = ADC1->JDR2;
+    DMA1_Channel1->CCR |= DMA_CCR1_EN; /* Enable DMA */
+    TIM1->CR1 |= TIM_CR1_CEN;    /* start TIM1 */
 
-    /* Set up analog watch dog */
-    ADC1->CR2 = 0; /* Power off */
-    /* Reset the ADC */
-    RCC->APB2RSTR |= RCC_APB2RSTR_ADC1RST;
-    RCC->APB2RSTR &= ~RCC_APB2RSTR_ADC1RST;
+    /* DAC Init */
+    DAC_V = 0;
+    DAC_I = 0;
 
-    ADC1->HTR = 0xFFF; /* Max value for HIGHT threshold*/
-    ADC1->LTR = SavedVRef/6*5; /* 1 V */
-  }
+    DAC->CR |= DAC_CR_BOFF2|DAC_CR_BOFF1; /* Disable buffers */
+    DAC->CR |= DAC_CR_EN1|DAC_CR_EN2; /* On DAC */
 
-  /* Set up ADC, TIM1, DMA for conversion */
-
-  ADC1->CR1 |= 
-#if defined(AWD_ENABLED)
-                ADC_CR1_JAWDEN|ADC_CR1_AWDIE| /* AWD on injected channels whith interrupt */
-#endif
-                ADC_CR1_JAUTO|ADC_CR1_SCAN; /* Scan mode + auto injection */
-  ADC1->SMPR2 = ADC_SMPR2_SMP7_0|ADC_SMPR2_SMP8_0|ADC_SMPR2_SMP9_0; /* 7.5 sampling time, 20 - adc time, 5,6,7 ch */
-  ADC1->SQR1 =  ADC_SQR1_L_0; /* 2 conversion */
-  ADC1->SQR3 =  ADC_SQR3_SQ1_3| /* 8 channel - voltage*/
-                ADC_SQR3_SQ2_3|ADC_SQR3_SQ2_0; /* 9- channel - current */
-  ADC1->JSQR = ADC_JSQR_JSQ4_2|ADC_JSQR_JSQ4_1|ADC_JSQR_JSQ4_0; /* 1 conversion by 7-th channel in inj */
-  ADC1->CR2 |= ADC_CR2_EXTTRIG|ADC_CR2_DMA; /* External trigger by T1 CC1, DMA */
-
-  GPIOA->CRL &= ~(GPIO_CRL_MODE4_1|GPIO_CRL_MODE5_1|GPIO_CRL_MODE7_1); /* Analog IN */
-  GPIOB->CRL &= ~(GPIO_CRL_MODE0_1|GPIO_CRL_MODE1_1); /* Analog IN */
-
-
-  /* TIM1 is configyred to trigger the ADC */
-  TIM1->ARR = 194; /* 2MHz / 200 = 10KHz */
-  TIM1->CCMR1 |= TIM_CCMR1_OC1M; /* PWM 2 mode */
-  TIM1->CCR1 = 99;
-  TIM1->BDTR |= TIM_BDTR_MOE; /* Only for TIM1 - MAIN OUTPUT ENABLE!!! */
-  TIM1->CCER |= TIM_CCER_CC1E; /* Output enable */
-  
-  /* DMA configuring */
-  DMA1_Channel1->CCR |= DMA_CCR1_PL|DMA_CCR1_MSIZE_0|DMA_CCR1_PSIZE_0| /*Hight pry, 16 byte mem, 16 byte pereph */
-          DMA_CCR1_MINC|DMA_CCR1_CIRC|DMA_CCR1_HTIE|DMA_CCR1_TCIE; /*  mem inc, circular, enterrupts by Half and End of conv */
-  DMA1_Channel1->CNDTR = ADC_ARRAY_SIZE;
-  DMA1_Channel1->CPAR = (uint32_t)&ADC1->DR;
-  DMA1_Channel1->CMAR = (uint32_t)&AdcOutArray[0];
-
-  /* On converting */
-  ADC1->CR2 |= ADC_CR2_ADON; /* Adc ON */
-  /* Additional delay to Vref on */
-  Delay(10000);
-
-  /* Calibration */
-  ADC1->CR2 |= ADC_CR2_RSTCAL;
-  while ( ADC1->CR2 & ADC_CR2_RSTCAL )
-    ; /* BLANK */
-  ADC1->CR2 |= ADC_CR2_CAL;
-  while ( ADC1->CR2 & ADC_CR2_CAL )
-    ; /* BLANK */
-
-  Delay(10000);
-  DMA1_Channel1->CCR |= DMA_CCR1_EN; /* Enable DMA */
-  TIM1->CR1 |= TIM_CR1_CEN;    /* start TIM1 */
-
-  /* DAC Init */
-  DAC_V = 0;
-  DAC_I = 0;
-  
-  DAC->CR |= DAC_CR_BOFF2|DAC_CR_BOFF1; /* Disable buffers */
-  DAC->CR |= DAC_CR_EN1|DAC_CR_EN2; /* On DAC */
-
-  LcdBlank();
-  EventInit();
+    LcdBlank();
+    EventInit();
 #if defined(GRAPH)
-  ClearGraph();
+    ClearGraph();
 #endif
 
-  if ( RestoreConfig() != 0 || (GPIOA->IDR & KEY_ENTER) == 0 )
-  {
-      AfterContrast = CalibrationMenu;
-      Contrast = 7;
-      CurrentFunc(ContrastMenu);
+    if ( RestoreConfig() != 0 || (GPIOA->IDR & KEY_ENTER) == 0 )
+    {
+        AfterContrast = CalibrationMenu;
+        Contrast = 7;
+        CurrentFunc(ContrastMenu);
 //    CurrentFunc(StartFunction);
-  }
-  else
-  {
-    CurrentFunc(StartFunction);
-  }
+    }
+    else
+    {
+        CurrentFunc(StartFunction);
+    }
 #if defined(CLOCK_ENABLED)
-  if ( IS_ON_CLOCK  ) /* RTC clock is on */
-  {
-    SwitchOnTheClock();
-  }
+    if ( IS_ON_CLOCK  ) /* RTC clock is on */
+    {
+        SwitchOnTheClock();
+    }
 #endif
-  
-  do
-  	EventCheck();
-  while(1);
+
+    do
+        EventCheck();
+    while(1);
 }
 
 #endif

+ 26 - 27
src/LabArm.h

@@ -37,7 +37,7 @@ void SaveMenu(void); /* Save settings to flash menu */
 int  SwitchOnTheClock(void);
 void SetupTheClock(void);
 
-/* 
+/*
 RTCVal - seconds to display
 InvPos - position that has to be inverted. If the first bit is 1 - display seconds also? else - onlu hours and minutes
 Position - start coordinated to display the clock. It can have size 5 or 8 characters depend on InPos 1-st bit */
@@ -50,46 +50,46 @@ extern uint16_t RemainTimerValue;
 
 typedef struct
 {
-  uint16_t VoltageDAC;  // & Contrast
-  uint16_t CurrentDAC;  // & Flafs
-}Settings_t;
+    uint16_t VoltageDAC;  // & Contrast
+    uint16_t CurrentDAC;  // & Flafs
+} Settings_t;
 
 typedef struct
 {
-  float ADCRamp;
-  float ADCOffset;
-  float DACRamp;
-  float DACOffset;
-  int8_t DotPosition;
-  int8_t Res[3];
+    float ADCRamp;
+    float ADCOffset;
+    float DACRamp;
+    float DACOffset;
+    int8_t DotPosition;
+    int8_t Res[3];
 }
 SubConfig_t;
 
 typedef struct
 {
-  uint16_t Value;
-  int8_t   DotPosition;
+    uint16_t Value;
+    int8_t   DotPosition;
 #define MENU_I_FLAG 1
 #define MENU_V_FLAG 2
-  int8_t   IVFlag;
-  char     Text[4];
+    int8_t   IVFlag;
+    char     Text[4];
 }
 UserMenu_t;
 
 typedef struct
 {
-  SubConfig_t V;
-  SubConfig_t I;
-  UserMenu_t Menu[14];
-  int32_t Crc;
-}Config_t;
+    SubConfig_t V;
+    SubConfig_t I;
+    UserMenu_t Menu[14];
+    int32_t Crc;
+} Config_t;
 
 #define SAVED_SETTINGS_COUNT (1024 - sizeof(Config_t))/sizeof(Settings_t)
 //#define SAVED_SETTINGS_COUNT 4
-typedef struct 
+typedef struct
 {
-  Settings_t   Settings[SAVED_SETTINGS_COUNT];
-  Config_t     Config;
+    Settings_t   Settings[SAVED_SETTINGS_COUNT];
+    Config_t     Config;
 //  char res[1024-sizeof(Config_t) - SAVED_SETTINGS_COUNT*sizeof(Settings_t)];
 } SavedDomain_t;
 
@@ -122,8 +122,8 @@ extern int16_t HumanI;
 #define GRAPH_SIZE 80
 typedef struct
 {
-  int16_t Min;
-  int16_t Max;
+    int16_t Min;
+    int16_t Max;
 }
 Graph_t;
 extern Graph_t IGraphArray[GRAPH_SIZE];
@@ -131,8 +131,8 @@ extern Graph_t VGraphArray[GRAPH_SIZE];
 extern uint8_t GraphCurrentPoint;
 typedef struct
 {
-  Graph_t* GraphArray;
-  int8_t   DotPosition;
+    Graph_t* GraphArray;
+    int8_t   DotPosition;
 } GraphData_t;
 extern GraphData_t GraphData;
 extern uint16_t TimeInterval;
@@ -140,4 +140,3 @@ void DisplayGraph(void);
 void ClearGraph(void);
 #endif /*FRAPH*/
 #endif /*__LAB_ARM_H__*/
-

+ 0 - 465
src/LabArm.uvproj

@@ -1,465 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
-<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project_proj.xsd">
-
-  <SchemaVersion>1.1</SchemaVersion>
-
-  <Header>### uVision Project, (C) Keil Software</Header>
-
-  <Targets>
-    <Target>
-      <TargetName>LabArm</TargetName>
-      <ToolsetNumber>0x4</ToolsetNumber>
-      <ToolsetName>ARM-ADS</ToolsetName>
-      <TargetOption>
-        <TargetCommonOption>
-          <Device>STM32F100C4</Device>
-          <Vendor>STMicroelectronics</Vendor>
-          <Cpu>IRAM(0x20000000-0x20000FFF) IROM(0x8000000-0x8003FFF) CLOCK(8000000) CPUTYPE("Cortex-M3")</Cpu>
-          <FlashUtilSpec></FlashUtilSpec>
-          <StartupFile>"STARTUP\ST\STM32F10x\startup_stm32f10x_ld_vl.s" ("STM32 Low density Value Line Startup Code")</StartupFile>
-          <FlashDriverDll>UL2CM3(-O14 -S0 -C0 -N00("ARM Cortex-M3") -D00(1BA00477) -L00(4) -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F10x_16 -FS08000000 -FL04000)</FlashDriverDll>
-          <DeviceId>5079</DeviceId>
-          <RegisterFile>stm32f10x.h</RegisterFile>
-          <MemoryEnv></MemoryEnv>
-          <Cmp></Cmp>
-          <Asm></Asm>
-          <Linker></Linker>
-          <OHString></OHString>
-          <InfinionOptionDll></InfinionOptionDll>
-          <SLE66CMisc></SLE66CMisc>
-          <SLE66AMisc></SLE66AMisc>
-          <SLE66LinkerMisc></SLE66LinkerMisc>
-          <SFDFile>SFD\ST\STM32F10xx\STM32F10xx4.sfr</SFDFile>
-          <bCustSvd>0</bCustSvd>
-          <UseEnv>0</UseEnv>
-          <BinPath></BinPath>
-          <IncludePath></IncludePath>
-          <LibPath></LibPath>
-          <RegisterFilePath>ST\STM32F10x\</RegisterFilePath>
-          <DBRegisterFilePath>ST\STM32F10x\</DBRegisterFilePath>
-          <TargetStatus>
-            <Error>0</Error>
-            <ExitCodeStop>0</ExitCodeStop>
-            <ButtonStop>0</ButtonStop>
-            <NotGenerated>0</NotGenerated>
-            <InvalidFlash>1</InvalidFlash>
-          </TargetStatus>
-          <OutputDirectory>.\</OutputDirectory>
-          <OutputName>LabArm</OutputName>
-          <CreateExecutable>1</CreateExecutable>
-          <CreateLib>0</CreateLib>
-          <CreateHexFile>1</CreateHexFile>
-          <DebugInformation>1</DebugInformation>
-          <BrowseInformation>1</BrowseInformation>
-          <ListingPath>.\</ListingPath>
-          <HexFormatSelection>1</HexFormatSelection>
-          <Merge32K>0</Merge32K>
-          <CreateBatchFile>0</CreateBatchFile>
-          <BeforeCompile>
-            <RunUserProg1>0</RunUserProg1>
-            <RunUserProg2>0</RunUserProg2>
-            <UserProg1Name></UserProg1Name>
-            <UserProg2Name></UserProg2Name>
-            <UserProg1Dos16Mode>0</UserProg1Dos16Mode>
-            <UserProg2Dos16Mode>0</UserProg2Dos16Mode>
-            <nStopU1X>0</nStopU1X>
-            <nStopU2X>0</nStopU2X>
-          </BeforeCompile>
-          <BeforeMake>
-            <RunUserProg1>0</RunUserProg1>
-            <RunUserProg2>0</RunUserProg2>
-            <UserProg1Name></UserProg1Name>
-            <UserProg2Name></UserProg2Name>
-            <UserProg1Dos16Mode>0</UserProg1Dos16Mode>
-            <UserProg2Dos16Mode>0</UserProg2Dos16Mode>
-            <nStopB1X>0</nStopB1X>
-            <nStopB2X>0</nStopB2X>
-          </BeforeMake>
-          <AfterMake>
-            <RunUserProg1>0</RunUserProg1>
-            <RunUserProg2>0</RunUserProg2>
-            <UserProg1Name></UserProg1Name>
-            <UserProg2Name></UserProg2Name>
-            <UserProg1Dos16Mode>0</UserProg1Dos16Mode>
-            <UserProg2Dos16Mode>0</UserProg2Dos16Mode>
-          </AfterMake>
-          <SelectedForBatchBuild>0</SelectedForBatchBuild>
-          <SVCSIdString></SVCSIdString>
-        </TargetCommonOption>
-        <CommonProperty>
-          <UseCPPCompiler>0</UseCPPCompiler>
-          <RVCTCodeConst>0</RVCTCodeConst>
-          <RVCTZI>0</RVCTZI>
-          <RVCTOtherData>0</RVCTOtherData>
-          <ModuleSelection>0</ModuleSelection>
-          <IncludeInBuild>1</IncludeInBuild>
-          <AlwaysBuild>0</AlwaysBuild>
-          <GenerateAssemblyFile>0</GenerateAssemblyFile>
-          <AssembleAssemblyFile>0</AssembleAssemblyFile>
-          <PublicsOnly>0</PublicsOnly>
-          <StopOnExitCode>3</StopOnExitCode>
-          <CustomArgument></CustomArgument>
-          <IncludeLibraryModules></IncludeLibraryModules>
-          <ComprImg>1</ComprImg>
-        </CommonProperty>
-        <DllOption>
-          <SimDllName>SARMCM3.DLL</SimDllName>
-          <SimDllArguments></SimDllArguments>
-          <SimDlgDll>DCM.DLL</SimDlgDll>
-          <SimDlgDllArguments>-pCM3</SimDlgDllArguments>
-          <TargetDllName>SARMCM3.DLL</TargetDllName>
-          <TargetDllArguments></TargetDllArguments>
-          <TargetDlgDll>TCM.DLL</TargetDlgDll>
-          <TargetDlgDllArguments>-pCM3</TargetDlgDllArguments>
-        </DllOption>
-        <DebugOption>
-          <OPTHX>
-            <HexSelection>1</HexSelection>
-            <HexRangeLowAddress>0</HexRangeLowAddress>
-            <HexRangeHighAddress>0</HexRangeHighAddress>
-            <HexOffset>0</HexOffset>
-            <Oh166RecLen>16</Oh166RecLen>
-          </OPTHX>
-          <Simulator>
-            <UseSimulator>0</UseSimulator>
-            <LoadApplicationAtStartup>1</LoadApplicationAtStartup>
-            <RunToMain>1</RunToMain>
-            <RestoreBreakpoints>1</RestoreBreakpoints>
-            <RestoreWatchpoints>1</RestoreWatchpoints>
-            <RestoreMemoryDisplay>1</RestoreMemoryDisplay>
-            <RestoreFunctions>1</RestoreFunctions>
-            <RestoreToolbox>1</RestoreToolbox>
-            <LimitSpeedToRealTime>0</LimitSpeedToRealTime>
-            <RestoreSysVw>1</RestoreSysVw>
-          </Simulator>
-          <Target>
-            <UseTarget>1</UseTarget>
-            <LoadApplicationAtStartup>1</LoadApplicationAtStartup>
-            <RunToMain>1</RunToMain>
-            <RestoreBreakpoints>1</RestoreBreakpoints>
-            <RestoreWatchpoints>1</RestoreWatchpoints>
-            <RestoreMemoryDisplay>1</RestoreMemoryDisplay>
-            <RestoreFunctions>0</RestoreFunctions>
-            <RestoreToolbox>1</RestoreToolbox>
-            <RestoreTracepoints>0</RestoreTracepoints>
-            <RestoreSysVw>1</RestoreSysVw>
-          </Target>
-          <RunDebugAfterBuild>0</RunDebugAfterBuild>
-          <TargetSelection>-1</TargetSelection>
-          <SimDlls>
-            <CpuDll></CpuDll>
-            <CpuDllArguments></CpuDllArguments>
-            <PeripheralDll></PeripheralDll>
-            <PeripheralDllArguments></PeripheralDllArguments>
-            <InitializationFile></InitializationFile>
-          </SimDlls>
-          <TargetDlls>
-            <CpuDll></CpuDll>
-            <CpuDllArguments></CpuDllArguments>
-            <PeripheralDll></PeripheralDll>
-            <PeripheralDllArguments></PeripheralDllArguments>
-            <InitializationFile></InitializationFile>
-            <Driver>STLink\ST-LINKIII-KEIL.dll</Driver>
-          </TargetDlls>
-        </DebugOption>
-        <Utilities>
-          <Flash1>
-            <UseTargetDll>1</UseTargetDll>
-            <UseExternalTool>0</UseExternalTool>
-            <RunIndependent>0</RunIndependent>
-            <UpdateFlashBeforeDebugging>1</UpdateFlashBeforeDebugging>
-            <Capability>1</Capability>
-            <DriverSelection>4100</DriverSelection>
-          </Flash1>
-          <bUseTDR>0</bUseTDR>
-          <Flash2>STLink\ST-LINKIII-KEIL.dll</Flash2>
-          <Flash3>"" ()</Flash3>
-          <Flash4></Flash4>
-          <pFcarmOut></pFcarmOut>
-          <pFcarmGrp></pFcarmGrp>
-          <pFcArmRoot></pFcArmRoot>
-          <FcArmLst>0</FcArmLst>
-        </Utilities>
-        <TargetArmAds>
-          <ArmAdsMisc>
-            <GenerateListings>0</GenerateListings>
-            <asHll>1</asHll>
-            <asAsm>1</asAsm>
-            <asMacX>1</asMacX>
-            <asSyms>1</asSyms>
-            <asFals>1</asFals>
-            <asDbgD>1</asDbgD>
-            <asForm>1</asForm>
-            <ldLst>0</ldLst>
-            <ldmm>1</ldmm>
-            <ldXref>1</ldXref>
-            <BigEnd>0</BigEnd>
-            <AdsALst>0</AdsALst>
-            <AdsACrf>1</AdsACrf>
-            <AdsANop>0</AdsANop>
-            <AdsANot>0</AdsANot>
-            <AdsLLst>1</AdsLLst>
-            <AdsLmap>1</AdsLmap>
-            <AdsLcgr>1</AdsLcgr>
-            <AdsLsym>1</AdsLsym>
-            <AdsLszi>1</AdsLszi>
-            <AdsLtoi>1</AdsLtoi>
-            <AdsLsun>1</AdsLsun>
-            <AdsLven>1</AdsLven>
-            <AdsLsxf>1</AdsLsxf>
-            <RvctClst>0</RvctClst>
-            <GenPPlst>0</GenPPlst>
-            <AdsCpuType>"Cortex-M3"</AdsCpuType>
-            <RvctDeviceName></RvctDeviceName>
-            <mOS>0</mOS>
-            <uocRom>0</uocRom>
-            <uocRam>0</uocRam>
-            <hadIROM>1</hadIROM>
-            <hadIRAM>1</hadIRAM>
-            <hadXRAM>0</hadXRAM>
-            <uocXRam>0</uocXRam>
-            <RvdsVP>0</RvdsVP>
-            <hadIRAM2>0</hadIRAM2>
-            <hadIROM2>0</hadIROM2>
-            <StupSel>8</StupSel>
-            <useUlib>1</useUlib>
-            <EndSel>0</EndSel>
-            <uLtcg>0</uLtcg>
-            <RoSelD>3</RoSelD>
-            <RwSelD>5</RwSelD>
-            <CodeSel>0</CodeSel>
-            <OptFeed>1</OptFeed>
-            <NoZi1>0</NoZi1>
-            <NoZi2>0</NoZi2>
-            <NoZi3>0</NoZi3>
-            <NoZi4>0</NoZi4>
-            <NoZi5>0</NoZi5>
-            <Ro1Chk>0</Ro1Chk>
-            <Ro2Chk>0</Ro2Chk>
-            <Ro3Chk>0</Ro3Chk>
-            <Ir1Chk>1</Ir1Chk>
-            <Ir2Chk>0</Ir2Chk>
-            <Ra1Chk>0</Ra1Chk>
-            <Ra2Chk>0</Ra2Chk>
-            <Ra3Chk>0</Ra3Chk>
-            <Im1Chk>1</Im1Chk>
-            <Im2Chk>0</Im2Chk>
-            <OnChipMemories>
-              <Ocm1>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm1>
-              <Ocm2>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm2>
-              <Ocm3>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm3>
-              <Ocm4>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm4>
-              <Ocm5>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm5>
-              <Ocm6>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </Ocm6>
-              <IRAM>
-                <Type>0</Type>
-                <StartAddress>0x20000000</StartAddress>
-                <Size>0x1000</Size>
-              </IRAM>
-              <IROM>
-                <Type>1</Type>
-                <StartAddress>0x8000000</StartAddress>
-                <Size>0x4000</Size>
-              </IROM>
-              <XRAM>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </XRAM>
-              <OCR_RVCT1>
-                <Type>1</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT1>
-              <OCR_RVCT2>
-                <Type>1</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT2>
-              <OCR_RVCT3>
-                <Type>1</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT3>
-              <OCR_RVCT4>
-                <Type>1</Type>
-                <StartAddress>0x8000000</StartAddress>
-                <Size>0x4000</Size>
-              </OCR_RVCT4>
-              <OCR_RVCT5>
-                <Type>1</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT5>
-              <OCR_RVCT6>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT6>
-              <OCR_RVCT7>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT7>
-              <OCR_RVCT8>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT8>
-              <OCR_RVCT9>
-                <Type>0</Type>
-                <StartAddress>0x20000000</StartAddress>
-                <Size>0x1000</Size>
-              </OCR_RVCT9>
-              <OCR_RVCT10>
-                <Type>0</Type>
-                <StartAddress>0x0</StartAddress>
-                <Size>0x0</Size>
-              </OCR_RVCT10>
-            </OnChipMemories>
-            <RvctStartVector></RvctStartVector>
-          </ArmAdsMisc>
-          <Cads>
-            <interw>1</interw>
-            <Optim>4</Optim>
-            <oTime>0</oTime>
-            <SplitLS>0</SplitLS>
-            <OneElfS>0</OneElfS>
-            <Strict>0</Strict>
-            <EnumInt>0</EnumInt>
-            <PlainCh>0</PlainCh>
-            <Ropi>0</Ropi>
-            <Rwpi>0</Rwpi>
-            <wLevel>0</wLevel>
-            <uThumb>0</uThumb>
-            <uSurpInc>0</uSurpInc>
-            <uC99>0</uC99>
-            <useXO>0</useXO>
-            <VariousControls>
-              <MiscControls></MiscControls>
-              <Define>STM32F10X_LD_VL BIG REAL_FLASH AWD_ENABLED CLOCK_ENABLED GRAPH</Define>
-              <Undefine></Undefine>
-              <IncludePath></IncludePath>
-            </VariousControls>
-          </Cads>
-          <Aads>
-            <interw>1</interw>
-            <Ropi>0</Ropi>
-            <Rwpi>0</Rwpi>
-            <thumb>0</thumb>
-            <SplitLS>0</SplitLS>
-            <SwStkChk>0</SwStkChk>
-            <NoWarn>0</NoWarn>
-            <uSurpInc>0</uSurpInc>
-            <useXO>0</useXO>
-            <VariousControls>
-              <MiscControls></MiscControls>
-              <Define></Define>
-              <Undefine></Undefine>
-              <IncludePath></IncludePath>
-            </VariousControls>
-          </Aads>
-          <LDads>
-            <umfTarg>1</umfTarg>
-            <Ropi>0</Ropi>
-            <Rwpi>0</Rwpi>
-            <noStLib>0</noStLib>
-            <RepFail>1</RepFail>
-            <useFile>0</useFile>
-            <TextAddressRange>0x08000000</TextAddressRange>
-            <DataAddressRange>0x20000000</DataAddressRange>
-            <pXoBase></pXoBase>
-            <ScatterFile></ScatterFile>
-            <IncludeLibs></IncludeLibs>
-            <IncludeLibsPath></IncludeLibsPath>
-            <Misc></Misc>
-            <LinkerInputFile></LinkerInputFile>
-            <DisabledWarnings></DisabledWarnings>
-          </LDads>
-        </TargetArmAds>
-      </TargetOption>
-      <Groups>
-        <Group>
-          <GroupName>asm</GroupName>
-          <Files>
-            <File>
-              <FileName>STM32F10x.s</FileName>
-              <FileType>2</FileType>
-              <FilePath>.\STM32F10x.s</FilePath>
-            </File>
-          </Files>
-        </Group>
-        <Group>
-          <GroupName>Src</GroupName>
-          <Files>
-            <File>
-              <FileName>n3310.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\n3310.c</FilePath>
-            </File>
-            <File>
-              <FileName>event.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\event.c</FilePath>
-            </File>
-            <File>
-              <FileName>LabArm.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\LabArm.c</FilePath>
-            </File>
-            <File>
-              <FileName>config.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\config.c</FilePath>
-            </File>
-            <File>
-              <FileName>ui.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\ui.c</FilePath>
-            </File>
-            <File>
-              <FileName>calibration.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\calibration.c</FilePath>
-            </File>
-            <File>
-              <FileName>clock.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\clock.c</FilePath>
-            </File>
-            <File>
-              <FileName>graph.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>.\graph.c</FilePath>
-            </File>
-          </Files>
-        </Group>
-      </Groups>
-    </Target>
-  </Targets>
-
-</Project>

+ 5 - 5
src/LabArm.uvprojx

@@ -47,14 +47,14 @@
             <NotGenerated>0</NotGenerated>
             <InvalidFlash>1</InvalidFlash>
           </TargetStatus>
-          <OutputDirectory>.\</OutputDirectory>
+          <OutputDirectory>.\Obj\</OutputDirectory>
           <OutputName>LabArm</OutputName>
           <CreateExecutable>1</CreateExecutable>
           <CreateLib>0</CreateLib>
           <CreateHexFile>1</CreateHexFile>
           <DebugInformation>1</DebugInformation>
           <BrowseInformation>1</BrowseInformation>
-          <ListingPath>.\</ListingPath>
+          <ListingPath>.\Lst\</ListingPath>
           <HexFormatSelection>1</HexFormatSelection>
           <Merge32K>0</Merge32K>
           <CreateBatchFile>0</CreateBatchFile>
@@ -310,7 +310,7 @@
           </ArmAdsMisc>
           <Cads>
             <interw>1</interw>
-            <Optim>4</Optim>
+            <Optim>3</Optim>
             <oTime>0</oTime>
             <SplitLS>0</SplitLS>
             <OneElfS>0</OneElfS>
@@ -319,7 +319,7 @@
             <PlainCh>0</PlainCh>
             <Ropi>0</Ropi>
             <Rwpi>0</Rwpi>
-            <wLevel>0</wLevel>
+            <wLevel>2</wLevel>
             <uThumb>0</uThumb>
             <uSurpInc>0</uSurpInc>
             <uC99>0</uC99>
@@ -339,7 +339,7 @@
             <interw>1</interw>
             <Ropi>0</Ropi>
             <Rwpi>0</Rwpi>
-            <thumb>0</thumb>
+            <thumb>1</thumb>
             <SplitLS>0</SplitLS>
             <SwStkChk>0</SwStkChk>
             <NoWarn>0</NoWarn>

+ 378 - 378
src/calibration.c

@@ -4,13 +4,13 @@
 uint8_t MenuMode;
 typedef struct
 {
-  uint32_t Adc;
-  int16_t Dac; /* 1 - 4095 */
-  int16_t Out; /* 1 - 9999 */
-  int8_t DotPosition; /* 0,1,2,3,4 */
-  int8_t Ok; /* Fiilled flag */
-  int16_t Res;
-}Calibration_t;
+    uint32_t Adc;
+    int16_t Dac; /* 1 - 4095 */
+    int16_t Out; /* 1 - 9999 */
+    int8_t DotPosition; /* 0,1,2,3,4 */
+    int8_t Ok; /* Fiilled flag */
+    int16_t Res;
+} Calibration_t;
 
 
 
@@ -26,170 +26,170 @@ MenuFunction_t  AfterContrast;
 
 void ContrastMenu(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-//    Contrast = CurrentSettings.VoltageDAC>>12;
-    LcdBlank(); /* Clear screen */
-    LcdChr(Y_POSITION*1 + X_POSITION * 0 + 14, "Contrast");
-    LcdChr(Y_POSITION*4 + X_POSITION * 0 + 14, "Press");
-    LcdChr(Y_POSITION*5 + X_POSITION * 0 + 14, "to exit");
-    goto redraw;
-  }
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        if ( Contrast < 15 )
-          Contrast++;
-        break;
-      case KEY_DOWN:
-        if ( Contrast > 0 )
-          Contrast--;
-        break;
-      case KEY_ENTER:
-        LcdContrast ( Contrast*3+42 );
-        CurrentFunc(AfterContrast);
-         return;
+//    Contrast = CurrentSettings.VoltageDAC>>12;
+        LcdBlank(); /* Clear screen */
+        LcdChr(Y_POSITION*1 + X_POSITION * 0 + 14, "Contrast");
+        LcdChr(Y_POSITION*4 + X_POSITION * 0 + 14, "Press");
+        LcdChr(Y_POSITION*5 + X_POSITION * 0 + 14, "to exit");
+        goto redraw;
     }
-redraw:
-    LcdContrast ( Contrast*3+42 );
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      char Buf[2];
-      uint8_t Counter = Contrast;
-      
-      Buf[0] = Counter/10 + '0';
-      Counter = Counter%10;
-      Buf[1] = Counter + '0';
-      LcdChr(BIG_UP+Y_POSITION*2 + X_POSITION * 2 + 2, Buf);
-      LcdChr(BIG_DOWN+Y_POSITION*3 + X_POSITION * 2 + 2, Buf);
+        switch (Event & KEY_MASK)
+        {
+        case KEY_UP:
+            if ( Contrast < 15 )
+                Contrast++;
+            break;
+        case KEY_DOWN:
+            if ( Contrast > 0 )
+                Contrast--;
+            break;
+        case KEY_ENTER:
+            LcdContrast ( Contrast*3+42 );
+            CurrentFunc(AfterContrast);
+            return;
+        }
+redraw:
+        LcdContrast ( Contrast*3+42 );
+        {
+            char Buf[2];
+            uint8_t Counter = Contrast;
+
+            Buf[0] = Counter/10 + '0';
+            Counter = Counter%10;
+            Buf[1] = Counter + '0';
+            LcdChr(BIG_UP+Y_POSITION*2 + X_POSITION * 2 + 2, Buf);
+            LcdChr(BIG_DOWN+Y_POSITION*3 + X_POSITION * 2 + 2, Buf);
+        }
     }
-  }
 }
 
 
 static float Offset(float YMax, float YMin, float XMax, float XMin)
 {
-  return (YMin*XMax - YMax*XMin)/(XMax-XMin);
+    return (YMin*XMax - YMax*XMin)/(XMax-XMin);
 }
 static void CalibrationToConfig(Config_t* Config)
 {
-  int i;
-
-  Config->V.ADCRamp = ((float)(VMax.Out - VMin.Out))/(VMax.Adc-VMin.Adc);
-  Config->V.ADCOffset = Offset(VMax.Out, VMin.Out, VMax.Adc, VMin.Adc);
-  Config->I.ADCRamp = ((float)(IMax.Out - IMin.Out))/(IMax.Adc-IMin.Adc);
-  Config->I.ADCOffset = Offset(IMax.Out, IMin.Out, IMax.Adc, IMin.Adc);
-  Config->V.DACRamp = ((float)(VMax.Out - VMin.Out))/(VMax.Dac-VMin.Dac);
-  Config->V.DACOffset = Offset(VMax.Out, VMin.Out, VMax.Dac, VMin.Dac);
-  Config->I.DACRamp = ((float)(IMax.Out - IMin.Out))/(IMax.Dac-IMin.Dac);
-  Config->I.DACOffset = Offset(IMax.Out,IMin.Out,IMax.Dac, IMin.Dac);
-  Config->V.DotPosition = VMax.DotPosition;
-  Config->I.DotPosition = IMax.DotPosition;
-  
-  /* Check menu correctness */
-  for (i=0; i<12; i++)
-  {
-    UserMenu_t* Menu = &CurrentConfig.Menu[i];
-    if ( (Menu->IVFlag == MENU_I_FLAG && IMax.DotPosition != Menu->DotPosition ) ||
-         (Menu->IVFlag == MENU_V_FLAG && VMax.DotPosition != Menu->DotPosition ) ) /* Dot position was changed */
+    int i;
+
+    Config->V.ADCRamp = ((float)(VMax.Out - VMin.Out))/(VMax.Adc-VMin.Adc);
+    Config->V.ADCOffset = Offset(VMax.Out, VMin.Out, VMax.Adc, VMin.Adc);
+    Config->I.ADCRamp = ((float)(IMax.Out - IMin.Out))/(IMax.Adc-IMin.Adc);
+    Config->I.ADCOffset = Offset(IMax.Out, IMin.Out, IMax.Adc, IMin.Adc);
+    Config->V.DACRamp = ((float)(VMax.Out - VMin.Out))/(VMax.Dac-VMin.Dac);
+    Config->V.DACOffset = Offset(VMax.Out, VMin.Out, VMax.Dac, VMin.Dac);
+    Config->I.DACRamp = ((float)(IMax.Out - IMin.Out))/(IMax.Dac-IMin.Dac);
+    Config->I.DACOffset = Offset(IMax.Out,IMin.Out,IMax.Dac, IMin.Dac);
+    Config->V.DotPosition = VMax.DotPosition;
+    Config->I.DotPosition = IMax.DotPosition;
+
+    /* Check menu correctness */
+    for (i=0; i<12; i++)
     {
-      memset(Menu, 0xFF, sizeof(*Menu)); /* Clear the menu */
+        UserMenu_t* Menu = &CurrentConfig.Menu[i];
+        if ( (Menu->IVFlag == MENU_I_FLAG && IMax.DotPosition != Menu->DotPosition ) ||
+                (Menu->IVFlag == MENU_V_FLAG && VMax.DotPosition != Menu->DotPosition ) ) /* Dot position was changed */
+        {
+            memset(Menu, 0xFF, sizeof(*Menu)); /* Clear the menu */
+        }
     }
-  }
 }
 
 
 void OutValue(uint8_t Y, uint8_t X, uint16_t Num, uint8_t DotPosition, uint8_t SelectPos)
 {
-  int i;
-  int Div = 1000;
-  uint8_t DisplayFlag = 0;
-  
-  for(i=0; i<4; i++)
-  {
-    char Chr;
-    uint32_t Light = 0;
-
-    if (i == DotPosition )
-    {
-      LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
-      LcdChr ( Y_POSITION*(Y+1)+X_POSITION*X+1, "." );
-      X=X+1;
-      DisplayFlag++;
-    }
+    int i;
+    int Div = 1000;
+    uint8_t DisplayFlag = 0;
 
-    if ( DisplayFlag == 0 && i == (DotPosition - 1))
+    for(i=0; i<4; i++)
     {
-      DisplayFlag++;
-    }
+        char Chr;
+        uint32_t Light = 0;
 
-    Chr = Num / Div;
-    if ( DisplayFlag == 0 && Chr == 0)
-      Chr = ' ';
-    else
-    {
-      DisplayFlag = 1;
-      Chr = Chr + '0';
+        if (i == DotPosition )
+        {
+            LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
+            LcdChr ( Y_POSITION*(Y+1)+X_POSITION*X+1, "." );
+            X=X+1;
+            DisplayFlag++;
+        }
+
+        if ( DisplayFlag == 0 && i == (DotPosition - 1))
+        {
+            DisplayFlag++;
+        }
+
+        Chr = Num / Div;
+        if ( DisplayFlag == 0 && Chr == 0)
+            Chr = ' ';
+        else
+        {
+            DisplayFlag = 1;
+            Chr = Chr + '0';
+        }
+        if (i == SelectPos)
+        {
+            Light = INVERSE;
+        }
+        LcdChr (Light + Y_POSITION*Y+X_POSITION*X+BIG_UP+1, &Chr );
+        LcdChr (Light + Y_POSITION*(Y+1)+X_POSITION*X+BIG_DOWN+1, &Chr );
+        X = X + 2;
+        Num = Num % Div;
+        Div = Div / 10;
     }
-    if (i == SelectPos)
+    if ( DotPosition == 4)
     {
-      Light = INVERSE;
-    } 
-    LcdChr (Light + Y_POSITION*Y+X_POSITION*X+BIG_UP+1, &Chr );
-    LcdChr (Light + Y_POSITION*(Y+1)+X_POSITION*X+BIG_DOWN+1, &Chr );
-    X = X + 2;
-    Num = Num % Div;
-    Div = Div / 10;
-  }
-  if ( DotPosition == 4)
-  {
-    LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
-    LcdChr ( Y_POSITION*(Y+1)+X_POSITION*X+1, " " );
-  }
+        LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
+        LcdChr ( Y_POSITION*(Y+1)+X_POSITION*X+1, " " );
+    }
 }
 
 void OutValueSmall(uint8_t Y, uint8_t X, uint16_t Num, uint8_t DotPosition, uint8_t InverseFlag)
 {
-  int i;
-  int Div = 1000;
-  uint32_t Light = InverseFlag?INVERSE:0;
-  uint8_t DisplayFlag=0;
-  
-  for(i=0; i<4; i++)
-  {
-    char Chr;
-
-    if (i == DotPosition)
-    {
-      DisplayFlag = 1;
-      LcdChr ( Light + Y_POSITION*(Y)+X_POSITION*X+1, "." );
-      X=X+1;     
-      DisplayFlag++;
-    }
+    int i;
+    int Div = 1000;
+    uint32_t Light = InverseFlag?INVERSE:0;
+    uint8_t DisplayFlag=0;
 
-    if ( DisplayFlag == 0 && i == (DotPosition - 1))
+    for(i=0; i<4; i++)
     {
-      DisplayFlag++;
-    }
+        char Chr;
 
-    Chr = Num / Div;
-    if ( DisplayFlag == 0 && Chr == 0)
-      Chr = ' ';
-    else
+        if (i == DotPosition)
+        {
+            DisplayFlag = 1;
+            LcdChr ( Light + Y_POSITION*(Y)+X_POSITION*X+1, "." );
+            X=X+1;
+            DisplayFlag++;
+        }
+
+        if ( DisplayFlag == 0 && i == (DotPosition - 1))
+        {
+            DisplayFlag++;
+        }
+
+        Chr = Num / Div;
+        if ( DisplayFlag == 0 && Chr == 0)
+            Chr = ' ';
+        else
+        {
+            DisplayFlag = 1;
+            Chr = Chr + '0';
+        }
+        LcdChr (Light + Y_POSITION*Y+X_POSITION*X+1, &Chr );
+        X = X + 1;
+        Num = Num % Div;
+        Div = Div / 10;
+    }
+    if ( DotPosition == 4)
     {
-      DisplayFlag = 1;
-      Chr = Chr + '0';
+        LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
     }
-    LcdChr (Light + Y_POSITION*Y+X_POSITION*X+1, &Chr );
-    X = X + 1;
-    Num = Num % Div;
-    Div = Div / 10;
-  }
-  if ( DotPosition == 4)
-  {
-    LcdChr ( Y_POSITION*(Y)+X_POSITION*X+1, " " );
-  }
 }
 
 
@@ -197,40 +197,40 @@ void CalibrationMenuInternal(void);
 
 void SaveMenu(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    MenuMode = 0;
-    LcdBlank(); /* Clear screen */
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
+    {
+        MenuMode = 0;
+        LcdBlank(); /* Clear screen */
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      case KEY_UP:
-        if ( MenuMode == 0 )
-          MenuMode = 1;
-        break;
-      case KEY_DOWN:
-        if ( MenuMode != 0 )
-          MenuMode = 0;
-        break;
-      case KEY_ENTER:
-        if (MenuMode != 0)
+        switch (Event & KEY_MASK)
         {
-          SaveConfig();
+        case KEY_UP:
+            if ( MenuMode == 0 )
+                MenuMode = 1;
+            break;
+        case KEY_DOWN:
+            if ( MenuMode != 0 )
+                MenuMode = 0;
+            break;
+        case KEY_ENTER:
+            if (MenuMode != 0)
+            {
+                SaveConfig();
+            }
+            CurrentFunc(StartFunction);
+            return;
         }
-        CurrentFunc(StartFunction);
-        return;
     }
-  }
-  else
-    return;
+    else
+        return;
 
 redraw:
-  LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuMode)*INVERSE, "Save to memory");
-  LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuMode)*INVERSE, "Save to flash");
+    LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuMode)*INVERSE, "Save to memory");
+    LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuMode)*INVERSE, "Save to flash");
 }
 
 
@@ -238,267 +238,267 @@ void SetDac(void);
 
 void SelectDot(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        Cur->DotPosition++;
-        if (Cur->DotPosition>4)
-          Cur->DotPosition = 4;
+        LcdBlank(); /* Clear screen */
         goto redraw;
-      case KEY_DOWN:
-        Cur->DotPosition--;
-        if (Cur->DotPosition<0)
-          Cur->DotPosition = 0;
-        goto redraw;
-      case KEY_ENTER:
-        CurrentFunc(SetDac);
     }
-  }
 
-  return;
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
+        {
+        case KEY_UP:
+            Cur->DotPosition++;
+            if (Cur->DotPosition>4)
+                Cur->DotPosition = 4;
+            goto redraw;
+        case KEY_DOWN:
+            Cur->DotPosition--;
+            if (Cur->DotPosition<0)
+                Cur->DotPosition = 0;
+            goto redraw;
+        case KEY_ENTER:
+            CurrentFunc(SetDac);
+        }
+    }
+
+    return;
 redraw:
-  LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
-  LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Dot");
-  OutValue(3, 1, Cur->Out, Cur->DotPosition, 0xFF);
+    LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
+    LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Dot");
+    OutValue(3, 1, Cur->Out, Cur->DotPosition, 0xFF);
 }
 
 int16_t IncrementMul;
 int8_t  ChangeNumberFlag;
 void SetOut(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuMode = 0;
-    IncrementMul = 1000;
-    ChangeNumberFlag = 0;
-    LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
-    LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Out");
-    LcdChr(X_POSITION*0+Y_POSITION*5+14, "Longkey to end");
-    goto redraw;
-  }
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        if ( ChangeNumberFlag )
-        {
-          Cur->Out += IncrementMul;
-          goto redraw;
-        }
-        else
-        {
-          if ( MenuMode < 3 )
-          {
-            IncrementMul /= 10;
-            MenuMode++;
-            goto redraw;
-          }
-        }
-        return;
-      case KEY_DOWN:
-        if ( ChangeNumberFlag )
-        {
-          Cur->Out -= IncrementMul;
-          goto redraw;
-        }
-        else
+        LcdBlank(); /* Clear screen */
+        MenuMode = 0;
+        IncrementMul = 1000;
+        ChangeNumberFlag = 0;
+        LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
+        LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Out");
+        LcdChr(X_POSITION*0+Y_POSITION*5+14, "Longkey to end");
+        goto redraw;
+    }
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          if ( MenuMode > 0 )
-          {
-            IncrementMul *= 10;
-            MenuMode--;
-            goto redraw;
-          }
+        case KEY_UP:
+            if ( ChangeNumberFlag )
+            {
+                Cur->Out += IncrementMul;
+                goto redraw;
+            }
+            else
+            {
+                if ( MenuMode < 3 )
+                {
+                    IncrementMul /= 10;
+                    MenuMode++;
+                    goto redraw;
+                }
+            }
+            return;
+        case KEY_DOWN:
+            if ( ChangeNumberFlag )
+            {
+                Cur->Out -= IncrementMul;
+                goto redraw;
+            }
+            else
+            {
+                if ( MenuMode > 0 )
+                {
+                    IncrementMul *= 10;
+                    MenuMode--;
+                    goto redraw;
+                }
+            }
+            return;
+        case KEY_ENTER:
+            if ( ChangeNumberFlag == 0 )
+                ChangeNumberFlag = 1;
+            else
+                ChangeNumberFlag = 0;
+            return;
         }
-        return;
-      case KEY_ENTER:
-        if ( ChangeNumberFlag == 0 )
-          ChangeNumberFlag = 1;
-        else
-          ChangeNumberFlag = 0;
-        return;
     }
-  }
-  if ( (Event & EV_MASK) == EV_KEY_LONG )
-  {
-    Cur->Ok = 1;
-    CurrentFunc(CalibrationMenuInternal);
-  }
+    if ( (Event & EV_MASK) == EV_KEY_LONG )
+    {
+        Cur->Ok = 1;
+        CurrentFunc(CalibrationMenuInternal);
+    }
 
-  return;
+    return;
 
 redraw:
-  OutValue(3, 1, Cur->Out, Cur->DotPosition, MenuMode);
+    OutValue(3, 1, Cur->Out, Cur->DotPosition, MenuMode);
 }
 
 void WaitMeasure(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    LcdChr(X_POSITION*0+Y_POSITION*3+14, "Please wait");
-    MenuMode = 0;
-    return;
-  }
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        LcdChr(X_POSITION*0+Y_POSITION*3+14, "Please wait");
+        MenuMode = 0;
+        return;
+    }
 
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED &&
-       (Event & KEY_MASK) == KEY_ADC ) /* End of conversion */
-  {
-    if (MenuMode == 2)
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED &&
+            (Event & KEY_MASK) == KEY_ADC ) /* End of conversion */
     {
-      CurrentFunc(SetOut);
-      if ( Cur == &VMax || Cur == &VMin )
-        Cur->Adc = ADCVoltage;
-      else
-        Cur->Adc = ADCCurrent;
+        if (MenuMode == 2)
+        {
+            CurrentFunc(SetOut);
+            if ( Cur == &VMax || Cur == &VMin )
+                Cur->Adc = ADCVoltage;
+            else
+                Cur->Adc = ADCCurrent;
+        }
+        MenuMode++;
     }
-    MenuMode++;
-  }
 }
 
 void SetDac(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
-    LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Out");
-    EncCounter = Cur->Dac;
-    goto redraw;
-  }
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-      case KEY_DOWN:
-        Cur->Dac = EncCounter;
+        LcdBlank(); /* Clear screen */
+        LcdChr(X_POSITION*0+Y_POSITION*1+14, Text);
+        LcdChr(X_POSITION*0+Y_POSITION*2+14, "Set Out");
+        EncCounter = Cur->Dac;
         goto redraw;
-   case KEY_ENTER:
-        CurrentFunc(WaitMeasure);
     }
-  }
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
+        {
+        case KEY_UP:
+        case KEY_DOWN:
+            Cur->Dac = EncCounter;
+            goto redraw;
+        case KEY_ENTER:
+            CurrentFunc(WaitMeasure);
+        }
+    }
 
-  return;
+    return;
 redraw:
-  OutValue(3, 1, Cur->Dac, 4,  0xFF);
-  *CurDac = Cur->Dac;
-  OutValueSmall(5,0, EncStep, 4, 0);
+    OutValue(3, 1, Cur->Dac, 4,  0xFF);
+    *CurDac = Cur->Dac;
+    OutValueSmall(5,0, EncStep, 4, 0);
 }
 
 
 void CalibrationMenu(void)
 {
-  /* Init The calibration */
-  VMax.Ok = 0;
-  VMin.Ok = 0;
-  IMax.Ok = 0;
-  IMin.Ok = 0;
-  CurrentFunc(CalibrationMenuInternal);
+    /* Init The calibration */
+    VMax.Ok = 0;
+    VMin.Ok = 0;
+    IMax.Ok = 0;
+    IMin.Ok = 0;
+    CurrentFunc(CalibrationMenuInternal);
 }
 
 void CalibrationMenuInternal(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    MenuMode = 0;
-    LcdBlank(); /* Clear screen */
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_DOWN:
-        if ( MenuMode == 0 )
-          MenuMode = 4;
-        else
-          MenuMode = MenuMode - 1;
+        MenuMode = 0;
+        LcdBlank(); /* Clear screen */
         goto redraw;
-      case KEY_UP:
-        if ( MenuMode == 4 )
-          MenuMode = 0;
-        else
-          MenuMode = MenuMode + 1;
-        goto redraw;
-      case KEY_ENTER:
-        switch(MenuMode)
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case 0:
-            Cur = &VMax;
-            Text = "Max Voltage";
-            Cur->Dac = 3700;
-            Cur->Out = 5555;
-            Cur->DotPosition = 2;
-            CurDac = &DAC_V;
-            CurrentFunc(SelectDot);
-            return;
-          case 1:
-            if (VMax.Ok == 0 ) /* The max value has not been set */
-              return;
-            Cur = &VMin;
-            Text = "Min Voltage";
-            Cur->Dac = 350;
-            Cur->Out = 555;
-            Cur->DotPosition = VMax.DotPosition;
-            CurDac = &DAC_V;
-            CurrentFunc(SetDac);
-            return;
-          case 2:
-            Cur = &IMax;
-            Text = "Max Current";
-            Cur->Dac = 3700;
-            Cur->Out = 5555;
-            Cur->DotPosition = 2;
-            CurDac = &DAC_I;
-            DAC_V = 2000; /* Set intermidiate voltage value !!! */
-            CurrentFunc(SelectDot);
-            return;
-          case 3:
-            if (IMax.Ok == 0 ) /* The max value has not been set */
-              return;
-            Cur = &IMin;
-            Text = "Min Current";
-            Cur->Dac = 350;
-            Cur->Out = 555;
-            Cur->DotPosition = IMax.DotPosition;
-            CurDac = &DAC_I;
-            CurrentFunc(SetDac);
-            return;
-          default:
-            if ( VMax.Ok && VMin.Ok && IMax.Ok && IMin.Ok )
+        case KEY_DOWN:
+            if ( MenuMode == 0 )
+                MenuMode = 4;
+            else
+                MenuMode = MenuMode - 1;
+            goto redraw;
+        case KEY_UP:
+            if ( MenuMode == 4 )
+                MenuMode = 0;
+            else
+                MenuMode = MenuMode + 1;
+            goto redraw;
+        case KEY_ENTER:
+            switch(MenuMode)
             {
-              CalibrationToConfig(&CurrentConfig);
-              CurrentFunc(SaveMenu);
+            case 0:
+                Cur = &VMax;
+                Text = "Max Voltage";
+                Cur->Dac = 3700;
+                Cur->Out = 5555;
+                Cur->DotPosition = 2;
+                CurDac = &DAC_V;
+                CurrentFunc(SelectDot);
+                return;
+            case 1:
+                if (VMax.Ok == 0 ) /* The max value has not been set */
+                    return;
+                Cur = &VMin;
+                Text = "Min Voltage";
+                Cur->Dac = 350;
+                Cur->Out = 555;
+                Cur->DotPosition = VMax.DotPosition;
+                CurDac = &DAC_V;
+                CurrentFunc(SetDac);
+                return;
+            case 2:
+                Cur = &IMax;
+                Text = "Max Current";
+                Cur->Dac = 3700;
+                Cur->Out = 5555;
+                Cur->DotPosition = 2;
+                CurDac = &DAC_I;
+                DAC_V = 2000; /* Set intermidiate voltage value !!! */
+                CurrentFunc(SelectDot);
+                return;
+            case 3:
+                if (IMax.Ok == 0 ) /* The max value has not been set */
+                    return;
+                Cur = &IMin;
+                Text = "Min Current";
+                Cur->Dac = 350;
+                Cur->Out = 555;
+                Cur->DotPosition = IMax.DotPosition;
+                CurDac = &DAC_I;
+                CurrentFunc(SetDac);
+                return;
+            default:
+                if ( VMax.Ok && VMin.Ok && IMax.Ok && IMin.Ok )
+                {
+                    CalibrationToConfig(&CurrentConfig);
+                    CurrentFunc(SaveMenu);
+                }
+                return;
             }
-            return;
         }
     }
-  }
-  return;
-  
+    return;
+
 redraw:
-  LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuMode)*INVERSE, "Max Voltage");
-  LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuMode)*INVERSE, "Min Voltage");
-  LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuMode)*INVERSE, "Max Current");
-  LcdChr(X_POSITION*0+Y_POSITION*4+14 + (3==MenuMode)*INVERSE, "Min Current");
-  if ( VMax.Ok && VMin.Ok && IMax.Ok && IMin.Ok )
-  {
-    LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuMode)*INVERSE, "Ok");
-  }
-  else
-  {
-    LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuMode)*INVERSE, "");
-  }
+    LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuMode)*INVERSE, "Max Voltage");
+    LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuMode)*INVERSE, "Min Voltage");
+    LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuMode)*INVERSE, "Max Current");
+    LcdChr(X_POSITION*0+Y_POSITION*4+14 + (3==MenuMode)*INVERSE, "Min Current");
+    if ( VMax.Ok && VMin.Ok && IMax.Ok && IMin.Ok )
+    {
+        LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuMode)*INVERSE, "Ok");
+    }
+    else
+    {
+        LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuMode)*INVERSE, "");
+    }
 }

+ 212 - 213
src/clock.c

@@ -5,106 +5,106 @@
 
 void DisplayClock(uint32_t RTCVal, uint16_t InvPos, uint32_t Position)
 {
-  int Sec = RTCVal % (24*60*60); /* Seconds in the day */
-  int Value;
-  char Ch;
-  
-  if (Sec % 2 )
-    Ch = ' ';
-  else
-    Ch = ':';
-  LcdChr(Position + X_POSITION*2 + 1, &Ch );
+    int Sec = RTCVal % (24*60*60); /* Seconds in the day */
+    int Value;
+    char Ch;
 
-  if ( ((int16_t)InvPos) < 0 )
-  {
-    LcdChr(Position + X_POSITION*5 + 1, &Ch );
-    Value = Sec % 60; /* Seconds */
+    if (Sec % 2 )
+        Ch = ' ';
+    else
+        Ch = ':';
+    LcdChr(Position + X_POSITION*2 + 1, &Ch );
+
+    if ( ((int16_t)InvPos) < 0 )
+    {
+        LcdChr(Position + X_POSITION*5 + 1, &Ch );
+        Value = Sec % 60; /* Seconds */
+        Ch = Value/10 + '0';
+        LcdChr( Position + X_POSITION*6 + 1, &Ch );
+        Ch = Value%10 + '0';
+        LcdChr( Position + X_POSITION*7 + 1, &Ch );
+    }
+    InvPos &= ~0x8000;
+    Value = Sec/(60*60); /* Hour */
     Ch = Value/10 + '0';
-    LcdChr( Position + X_POSITION*6 + 1, &Ch );
+    LcdChr( Position + 1 + ((InvPos==0)?INVERSE:0), &Ch );
     Ch = Value%10 + '0';
-    LcdChr( Position + X_POSITION*7 + 1, &Ch );
-  }
-  InvPos &= ~0x8000;
-  Value = Sec/(60*60); /* Hour */
-  Ch = Value/10 + '0';
-  LcdChr( Position + 1 + ((InvPos==0)?INVERSE:0), &Ch );
-  Ch = Value%10 + '0';
-  LcdChr( Position + X_POSITION*1 + 1 +((InvPos==1)?INVERSE:0), &Ch );
-  Sec = Sec%(60*60); /* Sec in the hour*/
-  Value = Sec/60; /* Minutes */
-  Ch = Value/10 + '0';
-  LcdChr( Position + X_POSITION*3 + 1 + ((InvPos==2)?INVERSE:0), &Ch );
-  Ch = Value%10 + '0';
-  LcdChr( Position + X_POSITION*4 + 1 + ((InvPos==3)?INVERSE:0), &Ch );
-  return;
+    LcdChr( Position + X_POSITION*1 + 1 +((InvPos==1)?INVERSE:0), &Ch );
+    Sec = Sec%(60*60); /* Sec in the hour*/
+    Value = Sec/60; /* Minutes */
+    Ch = Value/10 + '0';
+    LcdChr( Position + X_POSITION*3 + 1 + ((InvPos==2)?INVERSE:0), &Ch );
+    Ch = Value%10 + '0';
+    LcdChr( Position + X_POSITION*4 + 1 + ((InvPos==3)?INVERSE:0), &Ch );
+    return;
 }
 
 
 int SwitchOnTheClock(void)
 {
-  if ( (RCC->BDCR & RCC_BDCR_RTCSEL) != RCC_BDCR_RTCSEL_0  ) /* RTC clock is off */
-  {
-    GPIOC->CRH |= GPIO_CRH_MODE14_1 |GPIO_CRH_MODE15_1; /* Out 2 MHZ*/
-    GPIOC->CRH &= ~(GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* PUSH pull */
-    Delay(30);
-    GPIOA->BSRR =  GPIO_BSRR_BS14; /* Boot the cristal */
-    Delay(30);
-    GPIOC->CRH &= ~(GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1); /* Analog IN */
-    RCC->BDCR |= RCC_BDCR_LSEON;
+    if ( (RCC->BDCR & RCC_BDCR_RTCSEL) != RCC_BDCR_RTCSEL_0  ) /* RTC clock is off */
     {
-      __IO int i=1000000;
-      while ( i != 0 && (RCC->BDCR & RCC_BDCR_LSERDY) == 0)
-        i--;
-    }
-    if ( (RCC->BDCR & RCC_BDCR_LSERDY) == 0 )
-      return -1;
-  
-    RCC->BDCR |= RCC_BDCR_RTCSEL_0; /* RTC source is LSE */
-    RCC->BDCR |= RCC_BDCR_RTCEN; /* Enable RTC */
+        GPIOC->CRH |= GPIO_CRH_MODE14_1 |GPIO_CRH_MODE15_1; /* Out 2 MHZ*/
+        GPIOC->CRH &= ~(GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* PUSH pull */
+        Delay(30);
+        GPIOA->BSRR =  GPIO_BSRR_BS14; /* Boot the cristal */
+        Delay(30);
+        GPIOC->CRH &= ~(GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1); /* Analog IN */
+        RCC->BDCR |= RCC_BDCR_LSEON;
+        {
+            __IO int i=1000000;
+            while ( i != 0 && (RCC->BDCR & RCC_BDCR_LSERDY) == 0)
+                i--;
+        }
+        if ( (RCC->BDCR & RCC_BDCR_LSERDY) == 0 )
+            return -1;
 
-    BKP->RTCCR = 63; 
+        RCC->BDCR |= RCC_BDCR_RTCSEL_0; /* RTC source is LSE */
+        RCC->BDCR |= RCC_BDCR_RTCEN; /* Enable RTC */
 
-    RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
-    RTC->PRLL = CLOCK_DIV; /* Freq is some higher that 1 hz */
-    BKP->DR7  = CLOCK_DIV;
-    RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
-    while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-      ;
-  }
+        BKP->RTCCR = 63;
 
-  RTC->CRL &= ~RTC_CRL_RSF; /* This bit will be set at syncronisation time */
-  while ( (RTC->CRL & RTC_CRL_RSF) == 0)
-    ; /* Wait syncronisation */
-  while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-    ;
+        RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
+        RTC->PRLL = CLOCK_DIV; /* Freq is some higher that 1 hz */
+        BKP->DR7  = CLOCK_DIV;
+        RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
+        while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
+            ;
+    }
+
+    RTC->CRL &= ~RTC_CRL_RSF; /* This bit will be set at syncronisation time */
+    while ( (RTC->CRL & RTC_CRL_RSF) == 0)
+        ; /* Wait syncronisation */
+    while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
+        ;
 
-  RTC->CRH |= RTC_CRH_SECIE; /* Enable second interrupt */
-  while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-    ;
-  NVIC->ISER[(RTC_IRQn >> 0x05)] =	(u32)0x01 << (RTC_IRQn & (u8)0x1F); /* Enable RTC second interrupt */
+    RTC->CRH |= RTC_CRH_SECIE; /* Enable second interrupt */
+    while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
+        ;
+    NVIC->ISER[(RTC_IRQn >> 0x05)] =	(u32)0x01 << (RTC_IRQn & (u8)0x1F); /* Enable RTC second interrupt */
 
-  return 0;
+    return 0;
 }
 
 void RTC_IRQHandler(void)
 {
-  if ((EventQueue & ~KEY_ADC) == 0 ) /* ADC has lower priority */
-    EventQueue = EV_KEY_PRESSED | KEY_CLOCK;
-  RTC->CRL &= ~RTC_CRL_SECF;
-  if (TimerValue != 0)
-  {
-    if (RemainTimerValue > 1)
-    {
-      if ( RemainTimerValue != 0xFFFF )
-        RemainTimerValue--;     
-    }
-    else
+    if ((EventQueue & ~KEY_ADC) == 0 ) /* ADC has lower priority */
+        EventQueue = EV_KEY_PRESSED | KEY_CLOCK;
+    RTC->CRL &= ~RTC_CRL_SECF;
+    if (TimerValue != 0)
     {
-      RemainTimerValue = 0xFFFF;
-      DAC_V = 0; /* Off the output */
-      DAC_I = 0;
+        if (RemainTimerValue > 1)
+        {
+            if ( RemainTimerValue != 0xFFFF )
+                RemainTimerValue--;
+        }
+        else
+        {
+            RemainTimerValue = 0xFFFF;
+            DAC_V = 0; /* Off the output */
+            DAC_I = 0;
+        }
     }
-  }
 }
 
 static int32_t NewTime;
@@ -113,137 +113,137 @@ static uint8_t Change;
 static int Inc;
 void SetupTheClock(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 0;
-    Change = 0;
-    Inc = 1;
-    NewTime = (((RTC->CNTH<<16) + RTC->CNTL)%(24*60*60) + (24*60*60));
-    NewTime = NewTime / 60*60; /* Round to 1 minutes */
-    goto redraw;
-  }
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        MenuPos = 0;
+        Change = 0;
+        Inc = 1;
+        NewTime = (((RTC->CNTH<<16) + RTC->CNTL)%(24*60*60) + (24*60*60));
+        NewTime = NewTime / 60*60; /* Round to 1 minutes */
+        goto redraw;
+    }
 
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-       case KEY_UP:
-        if ( Change == 0 )
-        {
-          if ( MenuPos == 6 )
-            MenuPos = 0;
-          else
-            MenuPos = MenuPos + 1;
-          goto redraw;
-        }
-        else
-          Inc = 1;
-      goto ChangeTime;
-      case KEY_DOWN:
-        if ( Change == 0 )
-        {
-          if ( MenuPos == 0 )
-            MenuPos = 6;
-          else
-            MenuPos = MenuPos - 1;
-          goto redraw;
-        }
-        else
-          Inc = -1;
-        goto ChangeTime;
-      case KEY_ENTER:
-        switch(MenuPos)
+        switch (Event & KEY_MASK)
         {
-          case 0:
-          case 1:
-          case 2:
-          case 3:
-            Change = !Change;
-            return;
-          case 4:
-            BKP->DR5 = (NewTime >> 16); 	/* 05.10.11 Clear calibration time to prevent bad next calibration. */
-    		    BKP->DR6 = NewTime; 
-            
-            /* Restore default clock speed 18.10.11 */
-            BKP->RTCCR = 63; 
-            BKP->DR7  = CLOCK_DIV;
+        case KEY_UP:
+            if ( Change == 0 )
+            {
+                if ( MenuPos == 6 )
+                    MenuPos = 0;
+                else
+                    MenuPos = MenuPos + 1;
+                goto redraw;
+            }
+            else
+                Inc = 1;
+            goto ChangeTime;
+        case KEY_DOWN:
+            if ( Change == 0 )
+            {
+                if ( MenuPos == 0 )
+                    MenuPos = 6;
+                else
+                    MenuPos = MenuPos - 1;
+                goto redraw;
+            }
+            else
+                Inc = -1;
+            goto ChangeTime;
+        case KEY_ENTER:
+            switch(MenuPos)
+            {
+            case 0:
+            case 1:
+            case 2:
+            case 3:
+                Change = !Change;
+                return;
+            case 4:
+                BKP->DR5 = (NewTime >> 16); 	/* 05.10.11 Clear calibration time to prevent bad next calibration. */
+                BKP->DR6 = NewTime;
 
-            RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
-            RTC->PRLL = CLOCK_DIV; /* Freq is some higher that 1 hz */
-            while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-              ;
-            goto SaveTime;
-          case 5:
-            CurrentFunc(StartFunction);
-            return;
-          case 6: /* Calibration */
-            if (BKP->DR5 != 0 || BKP->DR6 != 0 ) /* Trim the clock */
-            {  /* Trim the clock speed */
-              int Delta;
-              int Time;
-              Delta = NewTime%(24*60*60) - (((RTC->CNTH<<16) + RTC->CNTL)%(24*60*60)); /* New time - old time. Positive is slow */
-              Time  = ((RTC->CNTH<<16) + RTC->CNTL) - ((BKP->DR5<<16) +  BKP->DR6); /* Old time - last trim date */
-              if ( Delta > (12*60*60) )
-                Delta = Delta - (24*60*60);
-              if ( Delta < -(12*60*60) )
-                Delta = Delta + (24*60*60);
-              if (Time > (24*60*60 - 60*10) ) /* Time eplased is More then 1 day without 10 minutes */
-              {
+                /* Restore default clock speed 18.10.11 */
+                BKP->RTCCR = 63;
+                BKP->DR7  = CLOCK_DIV;
+
+                RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
+                RTC->PRLL = CLOCK_DIV; /* Freq is some higher that 1 hz */
+                while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
+                    ;
+                goto SaveTime;
+            case 5:
+                CurrentFunc(StartFunction);
+                return;
+            case 6: /* Calibration */
+                if (BKP->DR5 != 0 || BKP->DR6 != 0 ) /* Trim the clock */
+                {   /* Trim the clock speed */
+                    int Delta;
+                    int Time;
+                    Delta = NewTime%(24*60*60) - (((RTC->CNTH<<16) + RTC->CNTL)%(24*60*60)); /* New time - old time. Positive is slow */
+                    Time  = ((RTC->CNTH<<16) + RTC->CNTL) - ((BKP->DR5<<16) +  BKP->DR6); /* Old time - last trim date */
+                    if ( Delta > (12*60*60) )
+                        Delta = Delta - (24*60*60);
+                    if ( Delta < -(12*60*60) )
+                        Delta = Delta + (24*60*60);
+                    if (Time > (24*60*60 - 60*10) ) /* Time eplased is More then 1 day without 10 minutes */
+                    {
 #if !defined(__GNUC__)
-                Delta = (Delta * ((int64_t)1024*1024))/Time; /* Delta in PPM */
+                        Delta = (Delta * ((int64_t)1024*1024))/Time; /* Delta in PPM */
 #else
-                /* Int64 operationd adds about 9k of code!!! */		
-                while ( Delta > 2047 || Delta < -2047 )
-                {
-                  Delta = Delta/2;
-                  Time  = Time/2;
-                }
-                Delta = (Delta * (1024*1024))/Time;
+                        /* Int64 operationd adds about 9k of code!!! */
+                        while ( Delta > 2047 || Delta < -2047 )
+                        {
+                            Delta = Delta/2;
+                            Time  = Time/2;
+                        }
+                        Delta = (Delta * (1024*1024))/Time;
 #endif
-                Delta = (BKP->RTCCR & BKP_RTCCR_CAL) - Delta;
-                if (Delta < 0 || Delta > 127 )
-                {
-                  BKP->DR7  = BKP->DR7 + Delta/32 - 2; /* Save new prescaler value */
-                  RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
-                  RTC->PRLL = BKP->DR7; /* Set new prescaler value. PRLL is read only */
-                    RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
-                    while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-                        ;
-                  Delta = 64 + (Delta - Delta/32*32);
+                        Delta = (BKP->RTCCR & BKP_RTCCR_CAL) - Delta;
+                        if (Delta < 0 || Delta > 127 )
+                        {
+                            BKP->DR7  = BKP->DR7 + Delta/32 - 2; /* Save new prescaler value */
+                            RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
+                            RTC->PRLL = BKP->DR7; /* Set new prescaler value. PRLL is read only */
+                            RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
+                            while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
+                                ;
+                            Delta = 64 + (Delta - Delta/32*32);
+                        }
+                        BKP->RTCCR = Delta; /* Write correction */
+                    }
                 }
-                BKP->RTCCR = Delta; /* Write correction */
-              }
+                BKP->DR5 = (NewTime >> 16);
+                BKP->DR6 = NewTime;
+                goto SaveTime;
             }
-            BKP->DR5 = (NewTime >> 16); 
-            BKP->DR6 = NewTime;
-            goto SaveTime;
         }
     }
-  }
-  return;
+    return;
 
 SaveTime:
     RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
     RTC->CNTH = NewTime>>16;
     RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
     while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-      ;
+        ;
     RTC->CRL |= RTC_CRL_CNF; /* Enter config mode */
     RTC->CNTL = NewTime&0xFFFF;
     RTC->CRL &= ~RTC_CRL_CNF; /* Write value  */
     while ( (RTC->CRL & RTC_CRL_RTOFF) == 0 ) /* Wait to ready for writing */
-      ;
+        ;
     CurrentFunc(StartFunction);
     return;
 
 ChangeTime:
-  switch (MenuPos)
-  {
-    case 0: 
+    switch (MenuPos)
+    {
+    case 0:
         Inc = Inc * 10 * 60 * 60;  /* +-10 hours */
         break;
-    case 1: 
+    case 1:
         Inc = Inc * 60 * 60;  /* +-1 hours */
         break;
     case 2:
@@ -252,18 +252,18 @@ ChangeTime:
     case 3:
         Inc = Inc * 60;  /* +-1 min */
         break;
-  }
-  if ( NewTime + Inc < 24*60*60 )
-    return;
-  if ( NewTime + Inc >= ( 24*60*60*2 - 1 ) ) /* 2 day without 1 sec */
-    return;
-  NewTime += Inc;
+    }
+    if ( NewTime + Inc < 24*60*60 )
+        return;
+    if ( NewTime + Inc >= ( 24*60*60*2 - 1 ) ) /* 2 day without 1 sec */
+        return;
+    NewTime += Inc;
 
 redraw:
-  DisplayClock(NewTime, MenuPos, Y_POSITION*3+X_POSITION*9);
-  LcdChr (Y_POSITION*4+X_POSITION*0+14+(4==MenuPos)*INVERSE, "Set time" );
-  LcdChr (Y_POSITION*5+X_POSITION*0+14+(5==MenuPos)*INVERSE, "Return" );
-  LcdChr (Y_POSITION*1+X_POSITION*0+14+(6==MenuPos)*INVERSE, "Calibration" );
+    DisplayClock(NewTime, MenuPos, Y_POSITION*3+X_POSITION*9);
+    LcdChr (Y_POSITION*4+X_POSITION*0+14+(4==MenuPos)*INVERSE, "Set time" );
+    LcdChr (Y_POSITION*5+X_POSITION*0+14+(5==MenuPos)*INVERSE, "Return" );
+    LcdChr (Y_POSITION*1+X_POSITION*0+14+(6==MenuPos)*INVERSE, "Calibration" );
 }
 
 
@@ -271,29 +271,28 @@ uint16_t TimerValue = 0;
 uint16_t RemainTimerValue = 0;
 void TimerSetup(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    EncCounter = TimerValue;
-    EncStepMax = 1800;
-    EncMax = 18 * 3600 + 1;
-  }
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        EncCounter = TimerValue;
+        EncStepMax = 1800;
+        EncMax = 18 * 3600 + 1;
+    }
 
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    if ( (Event & KEY_MASK) == KEY_ENTER )
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      TimerValue = EncCounter;
-      EncStepMax = ENC_STEP_MAX_DEFAULT;
-      EncMax = ENC_MAX_DEFAULT;
-      CurrentFunc(StartFunction);
-      return;
+        if ( (Event & KEY_MASK) == KEY_ENTER )
+        {
+            TimerValue = EncCounter;
+            EncStepMax = ENC_STEP_MAX_DEFAULT;
+            EncMax = ENC_MAX_DEFAULT;
+            CurrentFunc(StartFunction);
+            return;
+        }
     }
-  }
 
-  LcdChr(Y_POSITION*1+X_POSITION*0+14, "Timer value" );
-  DisplayClock(EncCounter, 10+0x8000, Y_POSITION*3+X_POSITION*6);
+    LcdChr(Y_POSITION*1+X_POSITION*0+14, "Timer value" );
+    DisplayClock(EncCounter, 10+0x8000, Y_POSITION*3+X_POSITION*6);
 }
 
-
 #endif

+ 129 - 130
src/config.c

@@ -16,177 +16,177 @@ const Settings_t*  NextSettings;
 
 static uint32_t Crc(const void* Pointer, int Len)
 {
-  int i;
-  const uint8_t* Data = (const uint8_t*)Pointer;
-  uint32_t Ret = 0x55;
+    int i;
+    const uint8_t* Data = (const uint8_t*)Pointer;
+    uint32_t Ret = 0x55;
 
 #if 1
-  for (i = 0; i<Len; i++)
-  {
-    Ret = Ret ^ (uint32_t)(Data[i]);
-  }
+    for (i = 0; i<Len; i++)
+    {
+        Ret = Ret ^ (uint32_t)(Data[i]);
+    }
 #else
-  CRC->CR |= CRC_CR_RESET;
-  CRC->IDR = Ret;
-  for (i = 0; i<Len; i++)
-  {
-    CRC->IDR = Data[i];
-    Ret = CRC->IDR;
-  }
+    CRC->CR |= CRC_CR_RESET;
+    CRC->IDR = Ret;
+    for (i = 0; i<Len; i++)
+    {
+        CRC->IDR = Data[i];
+        Ret = CRC->IDR;
+    }
 #endif
 
-  return Ret;
+    return Ret;
 }
 
 
 void WriteFlash(void* Src, void* Dst, int Len)
 {
-  uint16_t* SrcW = (uint16_t*)Src;
-  volatile uint16_t* DstW = (uint16_t*)Dst;
-
-  FLASH->KEYR = FLASH_KEY1;
-  FLASH->KEYR = FLASH_KEY2;
-
-  FLASH->CR |= FLASH_CR_PG; /* Programm the flash */
-  while (Len)
-  {
-    *DstW = *SrcW;
-    while ((FLASH->SR & FLASH_SR_BSY) != 0 )
-      ;
-    if (*DstW != *SrcW )
+    uint16_t* SrcW = (uint16_t*)Src;
+    volatile uint16_t* DstW = (uint16_t*)Dst;
+
+    FLASH->KEYR = FLASH_KEY1;
+    FLASH->KEYR = FLASH_KEY2;
+
+    FLASH->CR |= FLASH_CR_PG; /* Programm the flash */
+    while (Len)
     {
-      break;
+        *DstW = *SrcW;
+        while ((FLASH->SR & FLASH_SR_BSY) != 0 )
+            ;
+        if (*DstW != *SrcW )
+        {
+            break;
+        }
+        DstW++;
+        SrcW++;
+        Len = Len - sizeof(uint16_t);
     }
-    DstW++;
-    SrcW++;
-    Len = Len - sizeof(uint16_t);
-  }
 
-  FLASH->CR &= ~FLASH_CR_PG; /* Reset the flag back !!!! */
-  FLASH->CR |= FLASH_CR_LOCK; /* Lock the flash back */
+    FLASH->CR &= ~FLASH_CR_PG; /* Reset the flag back !!!! */
+    FLASH->CR |= FLASH_CR_LOCK; /* Lock the flash back */
 }
 void ErasePage(void* Addr)
 {
-  FLASH->KEYR = FLASH_KEY1;
-  FLASH->KEYR = FLASH_KEY2;
-
-  FLASH->CR |= FLASH_CR_PER; /* Page erase */
-  FLASH->AR = (uint32_t)Addr; 
-  FLASH->CR|= FLASH_CR_STRT; /* Start erase */
-  while ((FLASH->SR & FLASH_SR_BSY) != 0 ) /* Wait end of eraze */
-    ;
-  FLASH->CR &= ~FLASH_CR_PER; /* Page erase end */
+    FLASH->KEYR = FLASH_KEY1;
+    FLASH->KEYR = FLASH_KEY2;
+
+    FLASH->CR |= FLASH_CR_PER; /* Page erase */
+    FLASH->AR = (uint32_t)Addr;
+    FLASH->CR|= FLASH_CR_STRT; /* Start erase */
+    while ((FLASH->SR & FLASH_SR_BSY) != 0 ) /* Wait end of eraze */
+        ;
+    FLASH->CR &= ~FLASH_CR_PER; /* Page erase end */
 }
 void SaveConfig(void)
 {
-  const SavedDomain_t* ValidDomain;
+    const SavedDomain_t* ValidDomain;
 
-  CurrentConfig.Crc = Crc(&CurrentConfig, sizeof(Config_t) - sizeof(uint32_t));
+    CurrentConfig.Crc = Crc(&CurrentConfig, sizeof(Config_t) - sizeof(uint32_t));
 
-  ValidDomain = SavedDomain;
-  /* Clear 2 page */
-  ErasePage((void*)SavedDomain); 
-  ErasePage((void*)(SavedDomain+1)); 
+    ValidDomain = SavedDomain;
+    /* Clear 2 page */
+    ErasePage((void*)SavedDomain);
+    ErasePage((void*)(SavedDomain+1));
 
-  /* Save header and first settings */
-  WriteFlash(&CurrentConfig, (void*)&ValidDomain->Config, sizeof(Config_t));
+    /* Save header and first settings */
+    WriteFlash(&CurrentConfig, (void*)&ValidDomain->Config, sizeof(Config_t));
 
-  NextSettings = &ValidDomain->Settings[0];
-  SaveSettings(1);
-  NextSettings = &ValidDomain->Settings[1]; /* Prepare pointer to save at power fault */
+    NextSettings = &ValidDomain->Settings[0];
+    SaveSettings(1);
+    NextSettings = &ValidDomain->Settings[1]; /* Prepare pointer to save at power fault */
 }
 
 void SaveSettings(uint16_t AndFlash)
 {
-  if ( (DAC->DOR1 != 0 &&  DAC->DOR2 != 0) ) /* Is the out ON */
-  {
-    CurrentSettings.VoltageDAC = DAC->DOR1; /* Restore real current value */
-    CurrentSettings.CurrentDAC = DAC->DOR2;
-  }
-  /* Else - save preset values */
-
-  CurrentSettings.CurrentDAC |= Flags; /* Adding flags to save */
-  CurrentSettings.VoltageDAC |= (Contrast<<12);
-  if (Flags & BACKUP_SAVE_FLAG)
-  {
-    BKP->DR10 = CurrentSettings.VoltageDAC;
-    BKP->DR9  = CurrentSettings.CurrentDAC;
-    if (AndFlash == 0 )
-      goto Restore;
-  }
-  if ( NextSettings  == NULL )
-    return;
-  WriteFlash(&CurrentSettings, (void*)NextSettings, sizeof(CurrentSettings));
+    if ( (DAC->DOR1 != 0 &&  DAC->DOR2 != 0) ) /* Is the out ON */
+    {
+        CurrentSettings.VoltageDAC = DAC->DOR1; /* Restore real current value */
+        CurrentSettings.CurrentDAC = DAC->DOR2;
+    }
+    /* Else - save preset values */
+
+    CurrentSettings.CurrentDAC |= Flags; /* Adding flags to save */
+    CurrentSettings.VoltageDAC |= (Contrast<<12);
+    if (Flags & BACKUP_SAVE_FLAG)
+    {
+        BKP->DR10 = CurrentSettings.VoltageDAC;
+        BKP->DR9  = CurrentSettings.CurrentDAC;
+        if (AndFlash == 0 )
+            goto Restore;
+    }
+    if ( NextSettings  == NULL )
+        return;
+    WriteFlash(&CurrentSettings, (void*)NextSettings, sizeof(CurrentSettings));
 
 Restore:
-  CurrentSettings.CurrentDAC &= 0x0FFF; /* Remove flags back */
-  CurrentSettings.VoltageDAC &= 0x0FFF;
+    CurrentSettings.CurrentDAC &= 0x0FFF; /* Remove flags back */
+    CurrentSettings.VoltageDAC &= 0x0FFF;
 }
 
 
 int RestoreConfig(void) /* Return !0 if failed */
 {
-  const Settings_t* CurSettings;
-  const Settings_t* PrevSettings;
-  const SavedDomain_t* ValidDomain;
- 
-  ValidDomain = SavedDomain;
-  if ( Crc( &ValidDomain->Config, sizeof(Config_t) - sizeof(uint32_t) ) != ValidDomain->Config.Crc )
-  {
-    ValidDomain = SavedDomain + 1;
+    const Settings_t* CurSettings;
+    const Settings_t* PrevSettings;
+    const SavedDomain_t* ValidDomain;
+
+    ValidDomain = SavedDomain;
     if ( Crc( &ValidDomain->Config, sizeof(Config_t) - sizeof(uint32_t) ) != ValidDomain->Config.Crc )
     {
-      return -1; /* Both blocks are invalid */
+        ValidDomain = SavedDomain + 1;
+        if ( Crc( &ValidDomain->Config, sizeof(Config_t) - sizeof(uint32_t) ) != ValidDomain->Config.Crc )
+        {
+            return -1; /* Both blocks are invalid */
+        }
     }
-  }
-  memcpy((char*)&CurrentConfig, (char*)&ValidDomain->Config, sizeof(CurrentConfig));
+    memcpy((char*)&CurrentConfig, (char*)&ValidDomain->Config, sizeof(CurrentConfig));
 
-  PrevSettings = ValidDomain->Settings;
+    PrevSettings = ValidDomain->Settings;
 
-  for ( CurSettings = PrevSettings; CurSettings < ValidDomain->Settings + SAVED_SETTINGS_COUNT; CurSettings++)
-  {
-    if ( CurSettings->VoltageDAC == 0xFFFF && CurSettings->CurrentDAC == 0xFFFF )
-	  break;
-    PrevSettings = CurSettings;
-  }
+    for ( CurSettings = PrevSettings; CurSettings < ValidDomain->Settings + SAVED_SETTINGS_COUNT; CurSettings++)
+    {
+        if ( CurSettings->VoltageDAC == 0xFFFF && CurSettings->CurrentDAC == 0xFFFF )
+            break;
+        PrevSettings = CurSettings;
+    }
 
-  memcpy(&CurrentSettings, PrevSettings, sizeof(CurrentSettings));
+    memcpy(&CurrentSettings, PrevSettings, sizeof(CurrentSettings));
 #if 0
-  Flags = CurrentSettings.CurrentDAC & 0xF000;
-  CurrentSettings.CurrentDAC &= 0x0FFF;
-  Contrast = CurrentSettings.VoltageDAC>>12;
-  CurrentSettings.VoltageDAC &= 0x0FFF;
+    Flags = CurrentSettings.CurrentDAC & 0xF000;
+    CurrentSettings.CurrentDAC &= 0x0FFF;
+    Contrast = CurrentSettings.VoltageDAC>>12;
+    CurrentSettings.VoltageDAC &= 0x0FFF;
 #endif
 
-  NextSettings = CurSettings;
-  if ( CurSettings == ValidDomain->Settings + SAVED_SETTINGS_COUNT ) // All space is full
-  {
-    const SavedDomain_t* NextDomain = SavedDomain;
-
-    if ( ValidDomain == SavedDomain )
-      NextDomain = SavedDomain + 1;
-
-    WriteFlash((void*)&ValidDomain->Config, (void*)&NextDomain->Config, sizeof(Config_t));
-    WriteFlash((void*)CurSettings, (void*)&NextDomain->Settings[0], sizeof(Settings_t));
-    NextSettings = &NextDomain->Settings[1];
-    ErasePage( (void*) ValidDomain );
-    ValidDomain = NextDomain;
-  }
-  
-  if ( BKP->DR9 & BACKUP_SAVE_FLAG )
-  {
-    /* restore from backup domain */
-    CurrentSettings.VoltageDAC = BKP->DR10;
-    CurrentSettings.CurrentDAC = BKP->DR9;
-  }
-
-  Flags = CurrentSettings.CurrentDAC&0xF000;
-  CurrentSettings.CurrentDAC &= 0x0FFF;
-  Contrast = CurrentSettings.VoltageDAC>>12;
-  CurrentSettings.VoltageDAC &= 0x0FFF;
-  LcdContrast ( Contrast*3+42 );
-
-  return 0;
+    NextSettings = CurSettings;
+    if ( CurSettings == ValidDomain->Settings + SAVED_SETTINGS_COUNT ) // All space is full
+    {
+        const SavedDomain_t* NextDomain = SavedDomain;
+
+        if ( ValidDomain == SavedDomain )
+            NextDomain = SavedDomain + 1;
+
+        WriteFlash((void*)&ValidDomain->Config, (void*)&NextDomain->Config, sizeof(Config_t));
+        WriteFlash((void*)CurSettings, (void*)&NextDomain->Settings[0], sizeof(Settings_t));
+        NextSettings = &NextDomain->Settings[1];
+        ErasePage( (void*) ValidDomain );
+        ValidDomain = NextDomain;
+    }
+
+    if ( BKP->DR9 & BACKUP_SAVE_FLAG )
+    {
+        /* restore from backup domain */
+        CurrentSettings.VoltageDAC = BKP->DR10;
+        CurrentSettings.CurrentDAC = BKP->DR9;
+    }
+
+    Flags = CurrentSettings.CurrentDAC&0xF000;
+    CurrentSettings.CurrentDAC &= 0x0FFF;
+    Contrast = CurrentSettings.VoltageDAC>>12;
+    CurrentSettings.VoltageDAC &= 0x0FFF;
+    LcdContrast ( Contrast*3+42 );
+
+    return 0;
 }
 
 #else  /* REAL_FLASH */
@@ -196,7 +196,7 @@ void SaveConfig(void)
 
 int RestoreConfig(void)
 {
-  return -1;
+    return -1;
 }
 
 void SaveSettings(void)
@@ -204,4 +204,3 @@ void SaveSettings(void)
 }
 
 #endif /* RAL_FLASH */
-

+ 418 - 419
src/event.c

@@ -14,120 +14,120 @@ static uint8_t  RealizeCounter; // Realize delay counter
 static uint16_t PrevCounter;
 void EventInit(void)
 {
-  NVIC->ISER[(TIM3_IRQn >> 0x05)] =	(u32)0x01 << (TIM3_IRQn & (u8)0x1F); /* Enable Timer interrupt */
-  NVIC->ISER[(TIM2_IRQn >> 0x05)] =	(u32)0x01 << (TIM2_IRQn & (u8)0x1F); /* Enable Timer interrupt */
+    NVIC->ISER[(TIM3_IRQn >> 0x05)] =	(u32)0x01 << (TIM3_IRQn & (u8)0x1F); /* Enable Timer interrupt */
+    NVIC->ISER[(TIM2_IRQn >> 0x05)] =	(u32)0x01 << (TIM2_IRQn & (u8)0x1F); /* Enable Timer interrupt */
+
+    TIM3->DIER |= TIM_DIER_UIE; /* enable update IRQ */
+    TIM3->ARR = 10000; /* 2MHz / 10000 = 200Hz  - 5mSec */
+    TIM3->CR1 |= TIM_CR1_CEN;    /* start TIM7 */
 
-  TIM3->DIER |= TIM_DIER_UIE; /* enable update IRQ */
-  TIM3->ARR = 10000; /* 2MHz / 10000 = 200Hz  - 5mSec */
-  TIM3->CR1 |= TIM_CR1_CEN;    /* start TIM7 */
-  
 //  TIM2->CNT = 0x8000;
-  TIM2->ARR = 0xFFFF;
-  TIM2->SMCR |= (TIM_SMCR_SMS_0|TIM_SMCR_SMS_1); /* encoder mode 2 */
+    TIM2->ARR = 0xFFFF;
+    TIM2->SMCR |= (TIM_SMCR_SMS_0|TIM_SMCR_SMS_1); /* encoder mode 2 */
 //  TIM2->CCMR1 |= TIM_CCMR1_CC1S_0|TIM_CCMR1_IC1F|TIM_CCMR1_CC2S_0|TIM_CCMR1_IC2F;  /* TI1, TI2 - inputs with max filter */
 //  TIM2->CCER |= TIM_CCER_CC1E|TIM_CCER_CC2E; /* Enable inputs */
-  TIM2->SR = 0;
-  TIM2->DIER |= TIM_DIER_CC2IE|TIM_DIER_CC1IE; /* Interrupt by trigger */
-  TIM2->CCR1 = (uint16_t)- 4;
-  TIM2->CCR2 = 0 + 4;
-  PrevCounter = 0;
-  TIM2->CR1 |= TIM_CR1_CEN;
+    TIM2->SR = 0;
+    TIM2->DIER |= TIM_DIER_CC2IE|TIM_DIER_CC1IE; /* Interrupt by trigger */
+    TIM2->CCR1 = (uint16_t)- 4;
+    TIM2->CCR2 = 0 + 4;
+    PrevCounter = 0;
+    TIM2->CR1 |= TIM_CR1_CEN;
 }
 
 void EventKeys(void)
 {
-  uint16_t Key;
-  
-  if ( (EventQueue & KEY_MASK_SYS) != 0 ) /* Previous event hasn't been handled. The key hase highiest  priority */
-    return;
-  if (CurrentFunction != PrevFunc ) /* The function was changed */
-    return;
-  Key = (~KEYPORT) & KEY_MASK_SYS; /* Read the port */
+    uint16_t Key;
 
-  if ( Key == 0 ) // no any key pressed
-  {
-    if ( PrevKey == 0 ) // no any key was pressed before
-    {
-      EvCounter = 0;
-      return;
-    }
-    if ( EvCounter > KEY_PRESSED_VALUE )
+    if ( (EventQueue & KEY_MASK_SYS) != 0 ) /* Previous event hasn't been handled. The key hase highiest  priority */
+        return;
+    if (CurrentFunction != PrevFunc ) /* The function was changed */
+        return;
+    Key = (~KEYPORT) & KEY_MASK_SYS; /* Read the port */
+
+    if ( Key == 0 ) // no any key pressed
     {
-      RealizeCounter++; // increase timer counter
-      if ( RealizeCounter > KEY_REALIZE_VALUE ) // expired realise timeout
-      {
-        if ( EvCounter != 0xFF ) /* There is no switch to new function. New function should not get previos function event */
+        if ( PrevKey == 0 ) // no any key was pressed before
         {
-          EventQueue = EV_KEY_REALIZED | PrevKey; /* Realized event - the last event */
+            EvCounter = 0;
+            return;
+        }
+        if ( EvCounter > KEY_PRESSED_VALUE )
+        {
+            RealizeCounter++; // increase timer counter
+            if ( RealizeCounter > KEY_REALIZE_VALUE ) // expired realise timeout
+            {
+                if ( EvCounter != 0xFF ) /* There is no switch to new function. New function should not get previos function event */
+                {
+                    EventQueue = EV_KEY_REALIZED | PrevKey; /* Realized event - the last event */
+                }
+                EvCounter = 0; // Reset interval timer value
+                PrevKey = 0;   // Reset key pressed value
+                RealizeCounter = 0;  // Reset realise counter
+            }
+        }
+        else
+        {
+            EvCounter = 0; // Reset interval timer value
+            PrevKey = 0;   // Reset key pressed value
+            RealizeCounter = 0;  // Reset realise counter
         }
-        EvCounter = 0; // Reset interval timer value
-        PrevKey = 0;   // Reset key pressed value
-        RealizeCounter = 0;  // Reset realise counter
-      }
-    }
-    else
-    {
-      EvCounter = 0; // Reset interval timer value
-      PrevKey = 0;   // Reset key pressed value
-      RealizeCounter = 0;  // Reset realise counter
-    }
-  }
-  else // Some keys are pressed
-  {
-    RealizeCounter = 0; //reset realise delay
-   
-    if ( EvCounter == 0xFF ) /* Locked - new function has been set */
-      return; 
-    
-    if ( Key & (~PrevKey) ) //there are some new keys
-    {
-      PrevKey |= Key;       // adding the new keys
-      if ( EvCounter == 0 )
-      {
-      /* Generate KEY TOUCH event */
-        EventQueue = EV_KEY_TOUCH + PrevKey;
-      }
-      else if ( EvCounter > KEY_LONG_VALUE ) // Delay after first press is not long
-        EventQueue = EV_KEY_LONG | PrevKey; //generate key press event
     }
-    else // the same keys are pressed
+    else // Some keys are pressed
     {
-      if ( EvCounter == KEY_PRESSED_VALUE ) // Delay after first press is not long
-        EventQueue = EV_KEY_PRESSED | PrevKey; //generate key press event
-      else if ( EvCounter == KEY_LONG_VALUE )  // Long press timeout has expired
-      {
-        EventQueue = EV_KEY_LONG | PrevKey; // Generate Long press event
-      }
-      else if ( EvCounter == KEY_REPEATE_VALUE ) // After long press the key is stil pressed
-      {
-        EventQueue = EV_KEY_REPEATE | PrevKey; // Generate repeate press event
-        EvCounter = KEY_LONG_VALUE; // Reset time counter for next delay
-      }
-      EvCounter++; // Delay counter increasing
+        RealizeCounter = 0; //reset realise delay
+
+        if ( EvCounter == 0xFF ) /* Locked - new function has been set */
+            return;
+
+        if ( Key & (~PrevKey) ) //there are some new keys
+        {
+            PrevKey |= Key;       // adding the new keys
+            if ( EvCounter == 0 )
+            {
+                /* Generate KEY TOUCH event */
+                EventQueue = EV_KEY_TOUCH + PrevKey;
+            }
+            else if ( EvCounter > KEY_LONG_VALUE ) // Delay after first press is not long
+                EventQueue = EV_KEY_LONG | PrevKey; //generate key press event
+        }
+        else // the same keys are pressed
+        {
+            if ( EvCounter == KEY_PRESSED_VALUE ) // Delay after first press is not long
+                EventQueue = EV_KEY_PRESSED | PrevKey; //generate key press event
+            else if ( EvCounter == KEY_LONG_VALUE )  // Long press timeout has expired
+            {
+                EventQueue = EV_KEY_LONG | PrevKey; // Generate Long press event
+            }
+            else if ( EvCounter == KEY_REPEATE_VALUE ) // After long press the key is stil pressed
+            {
+                EventQueue = EV_KEY_REPEATE | PrevKey; // Generate repeate press event
+                EvCounter = KEY_LONG_VALUE; // Reset time counter for next delay
+            }
+            EvCounter++; // Delay counter increasing
+        }
     }
-  }
 }
 
 void EventCheck(void)
 {
-      if ( CurrentFunction != PrevFunc ) // Function was changed
-      {
+    if ( CurrentFunction != PrevFunc ) // Function was changed
+    {
         Event = EV_FUNC_FIRST;       // Generate FUNC_FIRST event
         PrevFunc = CurrentFunction;      // Save the function
-    		__disable_irq();
+        __disable_irq();
         if ( EvCounter )             /// Some keys are stil pressed
-          EvCounter = 0xFF;            // Lock any key events until all keys are not realized
-    		__enable_irq();
-      }
-      else
-      {
-    		__disable_irq();
+            EvCounter = 0xFF;            // Lock any key events until all keys are not realized
+        __enable_irq();
+    }
+    else
+    {
+        __disable_irq();
         Event = EventQueue;          // Read event thar was generated in interrupt handlers
         EventQueue = 0;              // The interrupt handlers can write new value
-    		__enable_irq();
-      }
+        __enable_irq();
+    }
 
-      CurrentFunction();      // Run the current menu function
+    CurrentFunction();      // Run the current menu function
 }
 
 static uint16_t TickCounter;
@@ -136,16 +136,16 @@ uint16_t EncCounter;
 
 void TIM3_IRQHandler(void)
 {
-  TIM3->SR = 0; /* Clear pending flag */
-  EventKeys();
-  TickCounter++;
-  if ( TickCounter - PrevTickCounter > 1024 ) /* 10 cek */
-    PrevTickCounter = TickCounter - 512; /* Once in 2.5 second */
-  if ( TickCounter - PrevTickCounter == 2 )
-  {
-	  TIM2->SR = 0; /* Clear pending flag */
-  	TIM2->DIER |= TIM_DIER_CC2IE; /* Interrupt by trigger */
-  }   
+    TIM3->SR = 0; /* Clear pending flag */
+    EventKeys();
+    TickCounter++;
+    if ( TickCounter - PrevTickCounter > 1024 ) /* 10 cek */
+        PrevTickCounter = TickCounter - 512; /* Once in 2.5 second */
+    if ( TickCounter - PrevTickCounter == 2 )
+    {
+        TIM2->SR = 0; /* Clear pending flag */
+        TIM2->DIER |= TIM_DIER_CC2IE; /* Interrupt by trigger */
+    }
 }
 
 uint16_t EncStep;
@@ -154,53 +154,53 @@ uint16_t EncMax = ENC_MAX_DEFAULT;
 
 void TIM2_IRQHandler(void)
 {
-  static uint16_t Delta;
-
-  PrevCounter = TIM2->CNT;
-  TIM2->CCR1 = PrevCounter - 4;
-  TIM2->CCR2 = PrevCounter + 4;
-  TIM2->SR = 0; /* Clear pending flag - only trigger interrupt*/
-
-  Delta = TickCounter - PrevTickCounter;
-  PrevTickCounter = TickCounter;
-  
-  Delta = Delta / 2;
-  if (EncStep == 0)
-    EncStep = 1;
-  if ( Delta < 8 && EncStep < EncStepMax) /* The step has to be inreased */
-  {
-    if ( EncStep < 4 )
-      EncStep = EncStep*2;
-    else if (EncStep < 64)
-      EncStep = EncStep + EncStep/4;
-    else
-      EncStep = EncStep + EncStep/8;
-  }
-  if ( Delta > 20 )
-  {
-    if (Delta > 120) /* 0.6 sec */
-      EncStep = 1;
-    EncStep = EncStep - EncStep/2;
-  }
-
-  if (TIM2->CR1 & TIM_CR1_DIR)
-  {
-    if ((int32_t)EncCounter - EncStep < 0 )
-      EncCounter = 0;
-    else
-      EncCounter -= EncStep;
-    if ( (EventQueue & KEY_MASK_SYS) == 0) /* Medium priority */
-      EventQueue = EV_KEY_PRESSED|KEY2; 
-  }
-  else
-  {
-    if ((uint32_t)EncCounter + EncStep > EncMax )
-      EncCounter = EncMax;
+    static uint16_t Delta;
+
+    PrevCounter = TIM2->CNT;
+    TIM2->CCR1 = PrevCounter - 4;
+    TIM2->CCR2 = PrevCounter + 4;
+    TIM2->SR = 0; /* Clear pending flag - only trigger interrupt*/
+
+    Delta = TickCounter - PrevTickCounter;
+    PrevTickCounter = TickCounter;
+
+    Delta = Delta / 2;
+    if (EncStep == 0)
+        EncStep = 1;
+    if ( Delta < 8 && EncStep < EncStepMax) /* The step has to be inreased */
+    {
+        if ( EncStep < 4 )
+            EncStep = EncStep*2;
+        else if (EncStep < 64)
+            EncStep = EncStep + EncStep/4;
+        else
+            EncStep = EncStep + EncStep/8;
+    }
+    if ( Delta > 20 )
+    {
+        if (Delta > 120) /* 0.6 sec */
+            EncStep = 1;
+        EncStep = EncStep - EncStep/2;
+    }
+
+    if (TIM2->CR1 & TIM_CR1_DIR)
+    {
+        if ((int32_t)EncCounter - EncStep < 0 )
+            EncCounter = 0;
+        else
+            EncCounter -= EncStep;
+        if ( (EventQueue & KEY_MASK_SYS) == 0) /* Medium priority */
+            EventQueue = EV_KEY_PRESSED|KEY2;
+    }
     else
-      EncCounter += EncStep;
-    if ( (EventQueue & KEY_MASK_SYS) == 0) /* Medium priority */
-      EventQueue = EV_KEY_PRESSED|KEY3; 
-  }
+    {
+        if ((uint32_t)EncCounter + EncStep > EncMax )
+            EncCounter = EncMax;
+        else
+            EncCounter += EncStep;
+        if ( (EventQueue & KEY_MASK_SYS) == 0) /* Medium priority */
+            EventQueue = EV_KEY_PRESSED|KEY3;
+    }
 }
 
 
@@ -221,98 +221,98 @@ void Contrast(void)
 
 void StartFunction(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    Value = 0;
-    goto redraw;
-  }
-  if ( (Event & KEY2) || (Event & KEY3) )
-  {
-    Value = EncCounter;
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Encoder" );
-    goto redraw;
-  }
-
-  if ( (Event & KEY_MASK) == 0)
-    return;
+    if (Event == EV_FUNC_FIRST)
+    {
+        Value = 0;
+        goto redraw;
+    }
+    if ( (Event & KEY2) || (Event & KEY3) )
+    {
+        Value = EncCounter;
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Encoder" );
+        goto redraw;
+    }
+
+    if ( (Event & KEY_MASK) == 0)
+        return;
+
+    if ( ((Event & EV_MASK) == EV_KEY_LONG) )
+    {
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Long key" );
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_TOUCH )
+    {
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Touch key" );
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_REALIZED)
+    {
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Realize key" );
+        goto redraw;
+    }
 
-  if ( ((Event & EV_MASK) == EV_KEY_LONG) )
-  { 
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Long key" );
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_TOUCH )
-  { 
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Touch key" );
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_REALIZED)
-  { 
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Realize key" );
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_DOUBLE)
-  { 
-    LcdChr ( Y_POSITION*5+X_POSITION*1+9, "Double key" );
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  { 
-    LcdChr ( Y_POSITION*5+X_POSITION*1+9, "" );
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Key" );
-	Value = 1;
-    goto redraw;
-  }
-  
-  if ( (Event & EV_MASK) == EV_KEY_REPEATE )
-  {
-    LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Repeate key" );
-    if ( Value < 9000 )
-   	  Value++;
-	goto redraw;
-  }
-
-  return;
+    if ( (Event & EV_MASK) == EV_KEY_DOUBLE)
+    {
+        LcdChr ( Y_POSITION*5+X_POSITION*1+9, "Double key" );
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        LcdChr ( Y_POSITION*5+X_POSITION*1+9, "" );
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Key" );
+        Value = 1;
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_REPEATE )
+    {
+        LcdChr ( Y_POSITION*3+X_POSITION*1+9, "Repeate key" );
+        if ( Value < 9000 )
+            Value++;
+        goto redraw;
+    }
+
+    return;
 
 redraw:
-  {
+    {
 #define DIGIT_COUNT 4
 #define FIRST_DIV 1000
-    char Out[DIGIT_COUNT];
-  	int Div = FIRST_DIV;
-  	char* Pointer = Out;
-  	char i = DIGIT_COUNT;
-  	char OnFlag = 0;
-	int Data = Value;
-
-  	while (i)
-  	{
-     char Dig = Data/Div;
-
-     if ( Dig == 0 && OnFlag == 0)
-     {
-       *Pointer = ' '; //First zero digits are not displaed
-     }
-     else
-     {
-       *Pointer = Dig + '0';
-       OnFlag = 1;
-     }
-     Pointer++;
-     i--;
-	 if ( i == 1 )
-	   OnFlag = 1; /* Zero also should be displayed */
-     Data = Data%Div;
-     Div = Div/10;
-  	} //while
-    LcdChr ( Y_POSITION*1+X_POSITION*1+DIGIT_COUNT+BIG_UP, Out );
-    LcdChr ( Y_POSITION*2+X_POSITION*1+DIGIT_COUNT+BIG_DOWN, Out );
-    LcdChr ( Y_POSITION*4+X_POSITION*1+9, "Menu Func" );
-  }
+        char Out[DIGIT_COUNT];
+        int Div = FIRST_DIV;
+        char* Pointer = Out;
+        char i = DIGIT_COUNT;
+        char OnFlag = 0;
+        int Data = Value;
+
+        while (i)
+        {
+            char Dig = Data/Div;
+
+            if ( Dig == 0 && OnFlag == 0)
+            {
+                *Pointer = ' '; //First zero digits are not displaed
+            }
+            else
+            {
+                *Pointer = Dig + '0';
+                OnFlag = 1;
+            }
+            Pointer++;
+            i--;
+            if ( i == 1 )
+                OnFlag = 1; /* Zero also should be displayed */
+            Data = Data%Div;
+            Div = Div/10;
+        } //while
+        LcdChr ( Y_POSITION*1+X_POSITION*1+DIGIT_COUNT+BIG_UP, Out );
+        LcdChr ( Y_POSITION*2+X_POSITION*1+DIGIT_COUNT+BIG_DOWN, Out );
+        LcdChr ( Y_POSITION*4+X_POSITION*1+9, "Menu Func" );
+    }
 }
 #endif /* ENC_DEMO */
 
@@ -328,207 +328,207 @@ void FloatChange(void);
 
 void MainMenu()
 {
-  if ( Event == 0 )
-    return;
+    if ( Event == 0 )
+        return;
 
-  if ( Event == EV_FUNC_FIRST )
-  {
-    MenuCounter = 0;
-    goto RedrawMenu;
-  }
+    if ( Event == EV_FUNC_FIRST )
+    {
+        MenuCounter = 0;
+        goto RedrawMenu;
+    }
 
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      case KEY_UP:
-        if ( MenuCounter == 0 )
-          MenuCounter = 4;
-        else
-          MenuCounter = MenuCounter - 1;
-        break;
-      case KEY_DOWN:
-        if ( MenuCounter == 4 )
-          MenuCounter = 0;
-        else
-          MenuCounter = MenuCounter + 1;
-        break;
-      case KEY_ENTER:
-        switch(MenuCounter)
+        switch (Event & KEY_MASK)
         {
-          case 4:
-            CurrentFunc = Contrast;
+        case KEY_UP:
+            if ( MenuCounter == 0 )
+                MenuCounter = 4;
+            else
+                MenuCounter = MenuCounter - 1;
             break;
-#if defined(FLOAT_DEMO)
-          case 2:
-            CurrentFunc = FloatChange;
+        case KEY_DOWN:
+            if ( MenuCounter == 4 )
+                MenuCounter = 0;
+            else
+                MenuCounter = MenuCounter + 1;
             break;
+        case KEY_ENTER:
+            switch(MenuCounter)
+            {
+            case 4:
+                CurrentFunc = Contrast;
+                break;
+#if defined(FLOAT_DEMO)
+            case 2:
+                CurrentFunc = FloatChange;
+                break;
 #endif
-          default:
-            CurrentFunc = MenuSelected;
-            return;
+            default:
+                CurrentFunc = MenuSelected;
+                return;
+            }
         }
     }
-  }
-  else
-    return;
+    else
+        return;
 RedrawMenu:
-  LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuCounter)*INVERSE, "Menu1");
-  LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuCounter)*INVERSE, "Menu2");
+    LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuCounter)*INVERSE, "Menu1");
+    LcdChr(X_POSITION*0+Y_POSITION*2+14 + (1==MenuCounter)*INVERSE, "Menu2");
 #if defined(FLOAT_DEMO)
-  LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuCounter)*INVERSE, "Float demo");
+    LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuCounter)*INVERSE, "Float demo");
 #else
-  LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuCounter)*INVERSE, "Menu3");
-#endif  
-  LcdChr(X_POSITION*0+Y_POSITION*4+14 + (3==MenuCounter)*INVERSE, "Menu4");
-  LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuCounter)*INVERSE, "Contrast");
+    LcdChr(X_POSITION*0+Y_POSITION*3+14 + (2==MenuCounter)*INVERSE, "Menu3");
+#endif
+    LcdChr(X_POSITION*0+Y_POSITION*4+14 + (3==MenuCounter)*INVERSE, "Menu4");
+    LcdChr(X_POSITION*0+Y_POSITION*5+14 + (4==MenuCounter)*INVERSE, "Contrast");
 }
 
 void MenuSelected(void)
 {
-  if ( (Event&EV_MASK) == EV_FUNC_FIRST)
-  {
-    LcdChr(X_POSITION*0+Y_POSITION*1+14,  "Press key     ");
-    LcdChr(X_POSITION*0+Y_POSITION*2+14,  "ENTER for a   ");
-    LcdChr(X_POSITION*0+Y_POSITION*3+14,  "long time to  ");
-    LcdChr(X_POSITION*0+Y_POSITION*4+14,  "return main   ");
-    LcdChr(X_POSITION*0+Y_POSITION*5+14,  "menu");
-    return;
-  }
-  
-  if ( Event == (EV_KEY_LONG + KEY_ENTER) )
-  { /* Return back */
-    CurrentFunc = MainMenu;
-  } 
+    if ( (Event&EV_MASK) == EV_FUNC_FIRST)
+    {
+        LcdChr(X_POSITION*0+Y_POSITION*1+14,  "Press key     ");
+        LcdChr(X_POSITION*0+Y_POSITION*2+14,  "ENTER for a   ");
+        LcdChr(X_POSITION*0+Y_POSITION*3+14,  "long time to  ");
+        LcdChr(X_POSITION*0+Y_POSITION*4+14,  "return main   ");
+        LcdChr(X_POSITION*0+Y_POSITION*5+14,  "menu");
+        return;
+    }
+
+    if ( Event == (EV_KEY_LONG + KEY_ENTER) )
+    {   /* Return back */
+        CurrentFunc = MainMenu;
+    }
 }
 
 #if defined(FLOAT_DEMO)
 void FloatChange(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    EncCounter = 2000;
-    LcdClear(); /* Clear screen */
-    goto redraw;
-  }
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
+    {
+        EncCounter = 2000;
+        LcdClear(); /* Clear screen */
+        goto redraw;
+    }
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      case KEY_ENTER:
-         CurrentFunc = MainMenu;
-         return;
+        switch (Event & KEY_MASK)
+        {
+        case KEY_ENTER:
+            CurrentFunc = MainMenu;
+            return;
+        }
     }
-  }
 redraw:
-  {
-    char Buf[10];
-    float Out = EncCounter *0.00756 + 0.015;
-    
-    FToS(Out, Buf);
-      
-    LcdChr(Y_POSITION*2 + X_POSITION * 2 + 10, Buf);
-  }
+    {
+        char Buf[10];
+        float Out = EncCounter *0.00756 + 0.015;
+
+        FToS(Out, Buf);
+
+        LcdChr(Y_POSITION*2 + X_POSITION * 2 + 10, Buf);
+    }
 }
 #endif /* FLAT_DEMO */
 
 void Contrast(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    MenuCounter = 70;
-    LcdClear(); /* Clear screen */
-    goto redraw;
-  }
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        if ( MenuCounter < 90 )
-          MenuCounter++;
-        break;
-      case KEY_DOWN:
-        if ( MenuCounter > 0 )
-          MenuCounter--;
-        break;
-      case KEY_ENTER:
-         CurrentFunc = MainMenu;
-         return;
+        MenuCounter = 70;
+        LcdClear(); /* Clear screen */
+        goto redraw;
     }
-redraw:
-    LcdContrast ( MenuCounter );
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      char Buf[2];
-      uint8_t Counter = MenuCounter;
-      
-      Buf[0] = Counter/10 + '0';
-      Counter = Counter%10;
-      Buf[1] = Counter + '0';
-      LcdChr(BIG_UP+Y_POSITION*2 + X_POSITION * 2 + 2, Buf);
-      LcdChr(BIG_DOWN+Y_POSITION*3 + X_POSITION * 2 + 2, Buf);
+        switch (Event & KEY_MASK)
+        {
+        case KEY_UP:
+            if ( MenuCounter < 90 )
+                MenuCounter++;
+            break;
+        case KEY_DOWN:
+            if ( MenuCounter > 0 )
+                MenuCounter--;
+            break;
+        case KEY_ENTER:
+            CurrentFunc = MainMenu;
+            return;
+        }
+redraw:
+        LcdContrast ( MenuCounter );
+        {
+            char Buf[2];
+            uint8_t Counter = MenuCounter;
+
+            Buf[0] = Counter/10 + '0';
+            Counter = Counter%10;
+            Buf[1] = Counter + '0';
+            LcdChr(BIG_UP+Y_POSITION*2 + X_POSITION * 2 + 2, Buf);
+            LcdChr(BIG_DOWN+Y_POSITION*3 + X_POSITION * 2 + 2, Buf);
+        }
     }
-  }
 }
 
 int LongCounter;
 
 void StartFunction(void)
 {
-  char* OutString = "";
-  char  KeyArray[3];
-  
-  if (Event == 0)
-    return;
-  
-  switch ( Event & EV_MASK )
-  {
+    char* OutString = "";
+    char  KeyArray[3];
+
+    if (Event == 0)
+        return;
+
+    switch ( Event & EV_MASK )
+    {
     case EV_KEY_TOUCH:
-      OutString = "Touch";
+        OutString = "Touch";
 //      return;
-      break;
+        break;
     case EV_KEY_PRESSED:
-      OutString = "Press";
-      break;
+        OutString = "Press";
+        break;
     case EV_KEY_LONG:
-      OutString = "Long";
-      if ( Event & KEY_ENTER )
-	  {
-	    if (LongCounter > 3 )
-         CurrentFunc = MainMenu;
-        LongCounter++;
-	  }
-      break;
+        OutString = "Long";
+        if ( Event & KEY_ENTER )
+        {
+            if (LongCounter > 3 )
+                CurrentFunc = MainMenu;
+            LongCounter++;
+        }
+        break;
     case EV_KEY_REPEATE:
-      OutString = "Repeate";
-      break;
+        OutString = "Repeate";
+        break;
     case EV_KEY_REALIZED:
-      OutString = "Realize";
-      break;
+        OutString = "Realize";
+        break;
     case EV_KEY_DOUBLE:
-      OutString = "Double";
-      break;
+        OutString = "Double";
+        break;
     case EV_FUNC_FIRST:
-      OutString = "First";
-  }
-    
-  LcdChr(Y_POSITION*(MenuCounter%5+1) + 14, OutString);
-  Event &= KEY_MASK;
-  KeyArray[0] = Event & KEY1 ? '*':'-';
-  KeyArray[1] = Event & KEY2 ? '*':'-';
-  KeyArray[2] = Event & KEY3 ? '*':'-';
-  LcdChr(Y_POSITION*(MenuCounter%5+1) + X_POSITION * 7 + 3, KeyArray);
-
-  {
-    uint8_t Counter = MenuCounter;
-    KeyArray[0] = Counter/100 + '0';
-    Counter = Counter%100;
-    KeyArray[1] = Counter/10 + '0';
-    Counter = Counter%10;
-    KeyArray[2] = Counter + '0';
-    LcdChr(Y_POSITION*(MenuCounter%5+1) + X_POSITION * 10 + 3, KeyArray);
-  }
-  MenuCounter++;
+        OutString = "First";
+    }
+
+    LcdChr(Y_POSITION*(MenuCounter%5+1) + 14, OutString);
+    Event &= KEY_MASK;
+    KeyArray[0] = Event & KEY1 ? '*':'-';
+    KeyArray[1] = Event & KEY2 ? '*':'-';
+    KeyArray[2] = Event & KEY3 ? '*':'-';
+    LcdChr(Y_POSITION*(MenuCounter%5+1) + X_POSITION * 7 + 3, KeyArray);
+
+    {
+        uint8_t Counter = MenuCounter;
+        KeyArray[0] = Counter/100 + '0';
+        Counter = Counter%100;
+        KeyArray[1] = Counter/10 + '0';
+        Counter = Counter%10;
+        KeyArray[2] = Counter + '0';
+        LcdChr(Y_POSITION*(MenuCounter%5+1) + X_POSITION * 10 + 3, KeyArray);
+    }
+    MenuCounter++;
 }
 
 #endif /*MENU_DEMO*/
@@ -537,41 +537,40 @@ void StartFunction(void)
 
 int main()
 {
-  CurrentFunc(StartFunction);
-  /* init hardware */
-  SCB->VTOR = FLASH_BASE;
+    CurrentFunc(StartFunction);
+    /* init hardware */
+    SCB->VTOR = FLASH_BASE;
 //  NVIC->ISER[(TIM3_IRQn >> 0x05)] |=	(u32)0x01 << (TIM3_IRQn & (u8)0x1F); /* Enable Timer interrupt */
 //  NVIC->ISER[(TIM2_IRQn >> 0x05)] |=	(u32)0x01 << (TIM2_IRQn & (u8)0x1F); /* Enable Timer interrupt */
-  LcdInit();
-  LcdClear();
-
-  /* CLOCK = 8MHz / 8 = 1 MHz */
-  RCC->CFGR |= RCC_CFGR_HPRE_DIV2|RCC_CFGR_PPRE1_DIV4;
-  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |RCC_APB2ENR_AFIOEN;
-  RCC->APB1ENR |= RCC_APB1ENR_TIM3EN|RCC_APB1ENR_TIM2EN; 
-
-  GPIOB->CRH |= GPIO_CRH_MODE12_1|GPIO_CRH_MODE13_1|GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1; /* Output 2MHz */
-  GPIOB->CRH &= ~(GPIO_CRH_CNF12|GPIO_CRH_CNF13|GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* Output Push-pull */
-
-#if 0   
-  TIM3->DIER |= TIM_DIER_UIE; /* enable update IRQ */
-  TIM3->ARR = 10000; /* 1MHz / 10000 = 100Hz  - 10mSec */
-  TIM3->CR1 |= TIM_CR1_CEN;    /* start TIM7 */
-  
-  TIM2->CNT = 0x8000;
-  TIM2->ARR = 0xFFFF;
-  TIM2->SMCR |= TIM_SMCR_SMS_0; /* encoder mode 1 */
-  TIM2->CCMR1 |= TIM_CCMR1_CC1S_0|TIM_CCMR1_IC1F|TIM_CCMR1_CC2S_0|TIM_CCMR1_IC2F;  /* TI1, TI2 - inputs with max filter */
-  TIM2->CCER |= TIM_CCER_CC1E|TIM_CCER_CC2E; /* Enable inputs */
-  TIM2->SR = 0;
-  TIM2->DIER |= TIM_DIER_CC2IE/*|TIM_DIER_CC1IE*/; /* Interrupt by trigger */
-  TIM2->CR1 |= TIM_CR1_CEN;
+    LcdInit();
+    LcdClear();
+
+    /* CLOCK = 8MHz / 8 = 1 MHz */
+    RCC->CFGR |= RCC_CFGR_HPRE_DIV2|RCC_CFGR_PPRE1_DIV4;
+    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |RCC_APB2ENR_AFIOEN;
+    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN|RCC_APB1ENR_TIM2EN;
+
+    GPIOB->CRH |= GPIO_CRH_MODE12_1|GPIO_CRH_MODE13_1|GPIO_CRH_MODE14_1|GPIO_CRH_MODE15_1; /* Output 2MHz */
+    GPIOB->CRH &= ~(GPIO_CRH_CNF12|GPIO_CRH_CNF13|GPIO_CRH_CNF14|GPIO_CRH_CNF15); /* Output Push-pull */
+
+#if 0
+    TIM3->DIER |= TIM_DIER_UIE; /* enable update IRQ */
+    TIM3->ARR = 10000; /* 1MHz / 10000 = 100Hz  - 10mSec */
+    TIM3->CR1 |= TIM_CR1_CEN;    /* start TIM7 */
+
+    TIM2->CNT = 0x8000;
+    TIM2->ARR = 0xFFFF;
+    TIM2->SMCR |= TIM_SMCR_SMS_0; /* encoder mode 1 */
+    TIM2->CCMR1 |= TIM_CCMR1_CC1S_0|TIM_CCMR1_IC1F|TIM_CCMR1_CC2S_0|TIM_CCMR1_IC2F;  /* TI1, TI2 - inputs with max filter */
+    TIM2->CCER |= TIM_CCER_CC1E|TIM_CCER_CC2E; /* Enable inputs */
+    TIM2->SR = 0;
+    TIM2->DIER |= TIM_DIER_CC2IE/*|TIM_DIER_CC1IE*/; /* Interrupt by trigger */
+    TIM2->CR1 |= TIM_CR1_CEN;
 #endif
-  EventInit();
-   
-  do
-  	EventCheck();
-  while(1);
+    EventInit();
+
+    do
+        EventCheck();
+    while(1);
 }
 #endif  /* EVENT_DEMO*/
-

+ 293 - 293
src/graph.c

@@ -6,23 +6,23 @@
 
 typedef struct
 {
-  int16_t Delta;
-  int16_t Intercept;
-  int16_t Round0;
-  int16_t Round1;
+    int16_t Delta;
+    int16_t Intercept;
+    int16_t Round0;
+    int16_t Round1;
 } GraphTable_t;
 
 const GraphTable_t GraphTable [] = {
-{30,1,10,5},
-{60,2,20,10},
-{150,5,50,20},
-{300,10,100,50},
-{600,20,200,100},
-{1500,50,500,200},
-{3000,100,1000,500},
-{6000,200,2000,1000},
-{12000,400,4000,2000},
-{24000,800,8000,4000},
+    {30,1,10,5},
+    {60,2,20,10},
+    {150,5,50,20},
+    {300,10,100,50},
+    {600,20,200,100},
+    {1500,50,500,200},
+    {3000,100,1000,500},
+    {6000,200,2000,1000},
+    {12000,400,4000,2000},
+    {24000,800,8000,4000},
 };
 #define GRAPH_TABLE_SIZE (sizeof(GraphTable)/sizeof(GraphTable_t))
 
@@ -38,269 +38,269 @@ uint8_t GlobalDiapason;
 
 void CalculateExtr(int16_t Min, int16_t Max, int16_t* pRoundMin, uint8_t* pDiapason)
 {
-  int16_t Delta;
-  int16_t Round;
-  uint8_t Diapason;
-  int16_t RoundMin;
-  int16_t RoundMax;
-  int16_t RoundDelta;
-
-  Delta = Max - Min;
-  
-  for (Diapason = 0; Diapason<GRAPH_TABLE_SIZE;Diapason++)
-  {
-    if ( Delta > GraphTable[Diapason].Delta )
-      continue;
-    break;
-  }
-
-ReselectDiapason:  
-  RoundDelta = GraphTable[Diapason].Delta;
-  Round = GraphTable[Diapason].Round0;
-
-  do
-  {
-    RoundMin = Min/Round*Round;
-    RoundMax = (Max+Round+1)/Round*Round;
-    if ( (RoundMax - RoundMin) <= RoundDelta )
-      break;
-    if (Round != GraphTable[Diapason].Round1)
+    int16_t Delta;
+    int16_t Round;
+    uint8_t Diapason;
+    int16_t RoundMin;
+    int16_t RoundMax;
+    int16_t RoundDelta;
+
+    Delta = Max - Min;
+
+    for (Diapason = 0; Diapason<GRAPH_TABLE_SIZE; Diapason++)
     {
-      Round = GraphTable[Diapason].Round1;
-      continue;
+        if ( Delta > GraphTable[Diapason].Delta )
+            continue;
+        break;
     }
-    Diapason++;
-    goto ReselectDiapason;
-  }
-  while(1);
-  
-  {
-    int16_t Add = (RoundDelta - (RoundMax - RoundMin))/2;
-    RoundMin = RoundMin - Add;
-    RoundMax = RoundMax + Add;
-    if (RoundMin < 0 )
+
+ReselectDiapason:
+    RoundDelta = GraphTable[Diapason].Delta;
+    Round = GraphTable[Diapason].Round0;
+
+    do
     {
-      RoundMax = RoundMax - RoundMin;
-      RoundMin = 0;
+        RoundMin = Min/Round*Round;
+        RoundMax = (Max+Round+1)/Round*Round;
+        if ( (RoundMax - RoundMin) <= RoundDelta )
+            break;
+        if (Round != GraphTable[Diapason].Round1)
+        {
+            Round = GraphTable[Diapason].Round1;
+            continue;
+        }
+        Diapason++;
+        goto ReselectDiapason;
     }
-  }
-  
-  Round = GraphTable[Diapason].Round0;
-  do
-  {
-    RoundMin = RoundMin/Round*Round;
-    RoundMax = RoundMin + GraphTable[Diapason].Delta;
-    if ( RoundMax >= Max && RoundMin <= Min )
-      break;
-    if (Round != GraphTable[Diapason].Round1)
+    while(1);
+
     {
-      Round = GraphTable[Diapason].Round1;
-      continue;
+        int16_t Add = (RoundDelta - (RoundMax - RoundMin))/2;
+        RoundMin = RoundMin - Add;
+        RoundMax = RoundMax + Add;
+        if (RoundMin < 0 )
+        {
+            RoundMax = RoundMax - RoundMin;
+            RoundMin = 0;
+        }
     }
-    Diapason++;
-    goto ReselectDiapason;
-  } while(1);
 
-  *pRoundMin = RoundMin;
-  *pDiapason = Diapason;
+    Round = GraphTable[Diapason].Round0;
+    do
+    {
+        RoundMin = RoundMin/Round*Round;
+        RoundMax = RoundMin + GraphTable[Diapason].Delta;
+        if ( RoundMax >= Max && RoundMin <= Min )
+            break;
+        if (Round != GraphTable[Diapason].Round1)
+        {
+            Round = GraphTable[Diapason].Round1;
+            continue;
+        }
+        Diapason++;
+        goto ReselectDiapason;
+    } while(1);
+
+    *pRoundMin = RoundMin;
+    *pDiapason = Diapason;
 }
 
 static int8_t Offsetmin(Graph_t* Array, int8_t ColumnNum)
 {
-  return (Array[ColumnNum].Min - GlobalMin)/GraphTable[GlobalDiapason].Intercept+1;
+    return (Array[ColumnNum].Min - GlobalMin)/GraphTable[GlobalDiapason].Intercept+1;
 }
 static int8_t Offsetmax(Graph_t* Array, int8_t ColumnNum)
 {
-  return (Array[ColumnNum].Max - GlobalMin)/GraphTable[GlobalDiapason].Intercept+1;
+    return (Array[ColumnNum].Max - GlobalMin)/GraphTable[GlobalDiapason].Intercept+1;
 }
 
 void OutOneColumn(Graph_t* Array, int8_t ColumnNum)
 {
-  int8_t OffsetMin,OffsetMax;
-  uint32_t Column = 0;
-  int8_t PrevMin;
-  int8_t PrevMax;
-  int8_t  Prev;
-  int8_t  Next;
-
-  Next = GraphCurrentPoint + 1;
-  if ( Next >= GRAPH_SIZE)
-    Next = 0;
-
-  if( ColumnNum == Next)
-  {
-    Column = 0xFFFFFFFF;
-    goto draw;
-  }
-
-  if ( ColumnNum%10 == 0)
-  {
-    Column = (1<<1)+(1<<3)+(1<<5)+(1<<7)+(1<<9)+(1<<11)+
-            (1<<13)+(1<<15)+(1<<17)+(1<<19)+(1<<21)+(1<<23)+(1<<25)+(1<<27)+(1<<29)+(1u<<31);
-  }
-  if ( ColumnNum%2 == 0)
-    Column |= (1<<1)+(1<<11)+(1<<21)+(1u<<31);
-  
-  Prev = ColumnNum - 1;
-  if (Prev < 0)
-    Prev = GRAPH_SIZE - 1;
-
-  
-  if ( Array[ColumnNum].Min != 32000)
-  {
-    int8_t Delta;
-
-    OffsetMin = Offsetmin(Array, ColumnNum);
-    OffsetMax = Offsetmax(Array, ColumnNum);
-    if ( Array[Prev].Min != 32000)
-    {
-      PrevMin = Offsetmin(Array, Prev);
-      PrevMax = Offsetmax(Array, Prev);
-    }
-    else
+    int8_t OffsetMin,OffsetMax;
+    uint32_t Column = 0;
+    int8_t PrevMin;
+    int8_t PrevMax;
+    int8_t  Prev;
+    int8_t  Next;
+
+    Next = GraphCurrentPoint + 1;
+    if ( Next >= GRAPH_SIZE)
+        Next = 0;
+
+    if( ColumnNum == Next)
     {
-      PrevMin = OffsetMin;
-      PrevMax = OffsetMax;
-    }
-    
-    Delta = OffsetMax - PrevMax - 1;
-    while (Delta > 0)
-    {
-      Column |= (1<<(OffsetMax-Delta));
-      Delta--;
+        Column = 0xFFFFFFFF;
+        goto draw;
     }
 
-    Delta = PrevMin - OffsetMin - 1;
-    while (Delta > 0)
+    if ( ColumnNum%10 == 0)
     {
-      Column |= (1<<(PrevMin-Delta));
-      Delta--;
+        Column = (1<<1)+(1<<3)+(1<<5)+(1<<7)+(1<<9)+(1<<11)+
+                 (1<<13)+(1<<15)+(1<<17)+(1<<19)+(1<<21)+(1<<23)+(1<<25)+(1<<27)+(1<<29)+(1u<<31);
     }
+    if ( ColumnNum%2 == 0)
+        Column |= (1<<1)+(1<<11)+(1<<21)+(1u<<31);
+
+    Prev = ColumnNum - 1;
+    if (Prev < 0)
+        Prev = GRAPH_SIZE - 1;
 
-    Delta = PrevMax - OffsetMax - 1;
-    while (Delta > 0)
-    {
-      Column |= (1<<(PrevMax-Delta));
-      Delta--;
-    }
 
-    Delta = OffsetMin - PrevMin - 1;
-    while (Delta > 0)
+    if ( Array[ColumnNum].Min != 32000)
     {
-      Column |= (1<<(OffsetMin-Delta));
-      Delta--;
-    }
+        int8_t Delta;
 
-    Column |= (1<<OffsetMin);
-    Column |= (1<<OffsetMax);
-  }
+        OffsetMin = Offsetmin(Array, ColumnNum);
+        OffsetMax = Offsetmax(Array, ColumnNum);
+        if ( Array[Prev].Min != 32000)
+        {
+            PrevMin = Offsetmin(Array, Prev);
+            PrevMax = Offsetmax(Array, Prev);
+        }
+        else
+        {
+            PrevMin = OffsetMin;
+            PrevMax = OffsetMax;
+        }
+
+        Delta = OffsetMax - PrevMax - 1;
+        while (Delta > 0)
+        {
+            Column |= (1<<(OffsetMax-Delta));
+            Delta--;
+        }
+
+        Delta = PrevMin - OffsetMin - 1;
+        while (Delta > 0)
+        {
+            Column |= (1<<(PrevMin-Delta));
+            Delta--;
+        }
+
+        Delta = PrevMax - OffsetMax - 1;
+        while (Delta > 0)
+        {
+            Column |= (1<<(PrevMax-Delta));
+            Delta--;
+        }
+
+        Delta = OffsetMin - PrevMin - 1;
+        while (Delta > 0)
+        {
+            Column |= (1<<(OffsetMin-Delta));
+            Delta--;
+        }
+
+        Column |= (1<<OffsetMin);
+        Column |= (1<<OffsetMax);
+    }
 
 draw:
-  Column = __RBIT(Column);
-  LcdGotoXY(ColumnNum, 5);
-  LcdSend(Column>>24, LCD_DATA);
-  LcdGotoXY(ColumnNum, 4);
-  LcdSend(Column>>16, LCD_DATA);
-  LcdGotoXY(ColumnNum, 3);
-  LcdSend(Column>>8, LCD_DATA);
-  LcdGotoXY(ColumnNum, 2);
-  LcdSend(Column, LCD_DATA);
+    Column = __RBIT(Column);
+    LcdGotoXY(ColumnNum, 5);
+    LcdSend(Column>>24, LCD_DATA);
+    LcdGotoXY(ColumnNum, 4);
+    LcdSend(Column>>16, LCD_DATA);
+    LcdGotoXY(ColumnNum, 3);
+    LcdSend(Column>>8, LCD_DATA);
+    LcdGotoXY(ColumnNum, 2);
+    LcdSend(Column, LCD_DATA);
 }
 
 void PrintGraph()
 {
-  Graph_t* Array = GraphData.GraphArray;
-  int i;
-  uint8_t Diapason;
-  int16_t Min=32000;
-  int16_t Max=-32000;
-
-  /* Finding extremoum */
-  for(i=0; i<GRAPH_SIZE;i++)
-  {
-    if (Array[i].Max > Max )
+    Graph_t* Array = GraphData.GraphArray;
+    int i;
+    uint8_t Diapason;
+    int16_t Min=32000;
+    int16_t Max=-32000;
+
+    /* Finding extremoum */
+    for(i=0; i<GRAPH_SIZE; i++)
     {
-      Max = Array[i].Max;
-      GlobalMaxPoint = i;
+        if (Array[i].Max > Max )
+        {
+            Max = Array[i].Max;
+            GlobalMaxPoint = i;
+        }
+        if(Array[i].Min < Min)
+        {
+            Min = Array[i].Min;
+            GlobalMinPoint = i;
+        }
     }
-    if(Array[i].Min < Min)
+    CalculateExtr(Min, Max, &Min, &Diapason);
+    Max = Min + GraphTable[Diapason].Delta;
+    if ( GlobalDiapason == Diapason && Min == GlobalMin && Max == GlobalMax)
     {
-      Min = Array[i].Min;
-      GlobalMinPoint = i;
+        int8_t Next = GraphCurrentPoint + 1;
+        if ( Next >= GRAPH_SIZE )
+            Next = 0;
+
+        OutOneColumn(GraphData.GraphArray, Next);
+        OutOneColumn(GraphData.GraphArray, GraphCurrentPoint);
+        return;
     }
-  }
-  CalculateExtr(Min, Max, &Min, &Diapason);
-  Max = Min + GraphTable[Diapason].Delta;
-  if ( GlobalDiapason == Diapason && Min == GlobalMin && Max == GlobalMax)
-  {
-    int8_t Next = GraphCurrentPoint + 1;
-    if ( Next >= GRAPH_SIZE )
-      Next = 0;
-      
-    OutOneColumn(GraphData.GraphArray, Next);
-    OutOneColumn(GraphData.GraphArray, GraphCurrentPoint);
-    return;
-  }
-  GlobalMin = Min;
-  GlobalMax = Max;
-  GlobalDiapason = Diapason;
-  for ( i = 0; i<GRAPH_SIZE; i++)
-  {
-    OutOneColumn(Array, i);
-  }
-  OutValueSmall(1, 0, GlobalMin, GraphData.DotPosition, 0);
-  OutValueSmall(1, 7, GraphTable[GlobalDiapason].Delta/3, GraphData.DotPosition, 0);
+    GlobalMin = Min;
+    GlobalMax = Max;
+    GlobalDiapason = Diapason;
+    for ( i = 0; i<GRAPH_SIZE; i++)
+    {
+        OutOneColumn(Array, i);
+    }
+    OutValueSmall(1, 0, GlobalMin, GraphData.DotPosition, 0);
+    OutValueSmall(1, 7, GraphTable[GlobalDiapason].Delta/3, GraphData.DotPosition, 0);
 }
 
 static void SetupGraph(void);
 void DisplayGraph(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    GlobalDiapason = 0xFF;
-    PrintGraph();
-    return;
-  }
-  if ( (Event& EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_ENTER:
-        CurrentFunc(StartFunction);
+        LcdBlank(); /* Clear screen */
+        GlobalDiapason = 0xFF;
+        PrintGraph();
         return;
-      case KEY_DOWN:
-      case KEY_UP:
-        CurrentFunc(SetupGraph);
-        return;
-      case KEY_ADC:
-      {
-        int16_t ValueMin;
-        int16_t ValueMax;
-        uint8_t Next;
+    }
+    if ( (Event& EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
+        {
+        case KEY_ENTER:
+            CurrentFunc(StartFunction);
+            return;
+        case KEY_DOWN:
+        case KEY_UP:
+            CurrentFunc(SetupGraph);
+            return;
+        case KEY_ADC:
+        {
+            int16_t ValueMin;
+            int16_t ValueMax;
+            uint8_t Next;
 
 
-        ValueMin = GraphData.GraphArray[GraphCurrentPoint].Min;
-        ValueMax = GraphData.GraphArray[GraphCurrentPoint].Max;
-        if (ValueMin < GlobalMin || ValueMax > GlobalMax || /* Larger diapason */
-            GraphCurrentPoint == GlobalMaxPoint || GraphCurrentPoint == GlobalMinPoint) /* Check Diapason */
-        {
-          PrintGraph();
-          return;
-        }
+            ValueMin = GraphData.GraphArray[GraphCurrentPoint].Min;
+            ValueMax = GraphData.GraphArray[GraphCurrentPoint].Max;
+            if (ValueMin < GlobalMin || ValueMax > GlobalMax || /* Larger diapason */
+                    GraphCurrentPoint == GlobalMaxPoint || GraphCurrentPoint == GlobalMinPoint) /* Check Diapason */
+            {
+                PrintGraph();
+                return;
+            }
 
-        Next = GraphCurrentPoint + 1;
-        if ( Next >= GRAPH_SIZE)
-          Next = 0;
+            Next = GraphCurrentPoint + 1;
+            if ( Next >= GRAPH_SIZE)
+                Next = 0;
 
-        OutOneColumn(GraphData.GraphArray, GraphCurrentPoint);
-        OutOneColumn(GraphData.GraphArray, Next);
+            OutOneColumn(GraphData.GraphArray, GraphCurrentPoint);
+            OutOneColumn(GraphData.GraphArray, Next);
 
-        return;
-      }
+            return;
+        }
+        }
     }
-  }
-  return;
+    return;
 }
 
 uint16_t TimeInterval = 5; /* 1 second */
@@ -308,83 +308,83 @@ uint16_t TimeInterval = 5; /* 1 second */
 static uint8_t IntervalCorrection = 0;
 static void SetupGraph(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 0;
-    IntervalCorrection = 0;
-    EncCounter = TimeInterval - 1;
-    goto redraw;
-  }
-  if ( (Event& EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        if (IntervalCorrection == 0)
-        {
-          MenuPos++;
-          if ( MenuPos > 2 )
-            MenuPos = 0;
-          goto redraw;
-        }
-      case KEY_DOWN:
-        if (IntervalCorrection == 0)
-        {
-          if ( MenuPos == 0 )
-            MenuPos = 2;
-          else
-            MenuPos--;
-          goto redraw;
-        }
-        TimeInterval = EncCounter + 1;
+        LcdBlank(); /* Clear screen */
+        MenuPos = 0;
+        IntervalCorrection = 0;
+        EncCounter = TimeInterval - 1;
         goto redraw;
-
-      case KEY_ENTER:
-        switch (MenuPos)
+    }
+    if ( (Event& EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case 1: /* Clear */
-            ClearGraph();
-          case 0: /* Return */
-            CurrentFunc(DisplayGraph);
-            return;
-          default: /* Interval */
-            if ( IntervalCorrection == 0)
-              IntervalCorrection = 1;
-            else
+        case KEY_UP:
+            if (IntervalCorrection == 0)
+            {
+                MenuPos++;
+                if ( MenuPos > 2 )
+                    MenuPos = 0;
+                goto redraw;
+            }
+        case KEY_DOWN:
+            if (IntervalCorrection == 0)
+            {
+                if ( MenuPos == 0 )
+                    MenuPos = 2;
+                else
+                    MenuPos--;
+                goto redraw;
+            }
+            TimeInterval = EncCounter + 1;
+            goto redraw;
+
+        case KEY_ENTER:
+            switch (MenuPos)
             {
-              IntervalCorrection = 0;
+            case 1: /* Clear */
+                ClearGraph();
+            case 0: /* Return */
+                CurrentFunc(DisplayGraph);
+                return;
+            default: /* Interval */
+                if ( IntervalCorrection == 0)
+                    IntervalCorrection = 1;
+                else
+                {
+                    IntervalCorrection = 0;
+                }
             }
         }
     }
-  }      
-  return;
-
-redraw:  
-  LcdChr(14+1*Y_POSITION+0*X_POSITION + (MenuPos==0?INVERSE:0), "Return");
-  LcdChr(14+2*Y_POSITION+0*X_POSITION + (MenuPos==1?INVERSE:0), "Clear");
-  LcdChr(14+3*Y_POSITION+0*X_POSITION + (MenuPos==2?INVERSE:0), "Interval");
-  OutValueSmall(3, 9, TimeInterval*2, 3, MenuPos==2?1:0);
+    return;
+
+redraw:
+    LcdChr(14+1*Y_POSITION+0*X_POSITION + (MenuPos==0?INVERSE:0), "Return");
+    LcdChr(14+2*Y_POSITION+0*X_POSITION + (MenuPos==1?INVERSE:0), "Clear");
+    LcdChr(14+3*Y_POSITION+0*X_POSITION + (MenuPos==2?INVERSE:0), "Interval");
+    OutValueSmall(3, 9, TimeInterval*2, 3, MenuPos==2?1:0);
 }
 
 void ClearGraph(void)
 {
-  int i;
-
-  for (i = 0; i < GRAPH_SIZE; i++)
-  {
-    IGraphArray[i].Min = VGraphArray[i].Min = 32000;
-    IGraphArray[i].Max = VGraphArray[i].Max = -32000;
-  }
-
-  GraphCurrentPoint = 0;
-  IGraphArray[0].Max = 
-      IGraphArray[0].Min = HumanI;
-  VGraphArray[0].Max = 
-      VGraphArray[0].Min = HumanV;
-    
-  GlobalMin = 32000;
-  GlobalMax = -32000;
+    int i;
+
+    for (i = 0; i < GRAPH_SIZE; i++)
+    {
+        IGraphArray[i].Min = VGraphArray[i].Min = 32000;
+        IGraphArray[i].Max = VGraphArray[i].Max = -32000;
+    }
+
+    GraphCurrentPoint = 0;
+    IGraphArray[0].Max =
+        IGraphArray[0].Min = HumanI;
+    VGraphArray[0].Max =
+        VGraphArray[0].Min = HumanV;
+
+    GlobalMin = 32000;
+    GlobalMax = -32000;
 }
 
 #endif

+ 247 - 247
src/n3310.c

@@ -7,97 +7,97 @@
 
 const uint8_t FontLookup [] =
 {
-     0x00, 0x00, 0x00, 0x00, 0x00 ,  // sp
-     0x00, 0x00, 0x2f, 0x00, 0x00 ,   // !
-     0x00, 0x07, 0x00, 0x07, 0x00 ,   // "
-     0x14, 0x7f, 0x14, 0x7f, 0x14 ,   // #
-     0x24, 0x2a, 0x7f, 0x2a, 0x12 ,   // $
-     0xc4, 0xc8, 0x10, 0x26, 0x46 ,   // %
-     0x36, 0x49, 0x55, 0x22, 0x50 ,   // &
-     0x00, 0x05, 0x03, 0x00, 0x00 ,   // '
-     0x00, 0x1c, 0x22, 0x41, 0x00 ,   // (
-     0x00, 0x41, 0x22, 0x1c, 0x00 ,   // )
-     0x14, 0x08, 0x3E, 0x08, 0x14 ,   // *
-     0x08, 0x08, 0x3E, 0x08, 0x08 ,   // +
-     0x00, 0x00, 0x50, 0x30, 0x00 ,   // ,
-     0x10, 0x10, 0x10, 0x10, 0x10 ,   // -
-     0x00, 0x60, 0x60, 0x00, 0x00 ,   // .
-     0x20, 0x10, 0x08, 0x04, 0x02 ,   // /
-     0x3E, 0x51, 0x49, 0x45, 0x3E ,   // 0
-     0x00, 0x42, 0x7F, 0x40, 0x00 ,   // 1
-     0x42, 0x61, 0x51, 0x49, 0x46 ,   // 2
-     0x21, 0x41, 0x45, 0x4B, 0x31 ,   // 3
-     0x18, 0x14, 0x12, 0x7F, 0x10 ,   // 4
-     0x27, 0x45, 0x45, 0x45, 0x39 ,   // 5
-     0x3C, 0x4A, 0x49, 0x49, 0x30 ,   // 6
-     0x01, 0x71, 0x09, 0x05, 0x03 ,   // 7
-     0x36, 0x49, 0x49, 0x49, 0x36 ,   // 8
-     0x06, 0x49, 0x49, 0x29, 0x1E ,   // 9
-     0x00, 0x36, 0x36, 0x00, 0x00 ,   // :
-     0x00, 0x56, 0x36, 0x00, 0x00 ,   // ;
-     0x08, 0x14, 0x22, 0x41, 0x00 ,   // <
-     0x14, 0x14, 0x14, 0x14, 0x14 ,   // =
-     0x00, 0x41, 0x22, 0x14, 0x08 ,   // >
-     0x02, 0x01, 0x51, 0x09, 0x06 ,   // ?
-     0x32, 0x49, 0x59, 0x51, 0x3E ,   // @
-     0x7E, 0x11, 0x11, 0x11, 0x7E ,   // A
-     0x7F, 0x49, 0x49, 0x49, 0x36 ,   // B
-     0x3E, 0x41, 0x41, 0x41, 0x22 ,   // C
-     0x7F, 0x41, 0x41, 0x22, 0x1C ,   // D
-     0x7F, 0x49, 0x49, 0x49, 0x41 ,   // E
-     0x7F, 0x09, 0x09, 0x09, 0x01 ,   // F
-     0x3E, 0x41, 0x49, 0x49, 0x7A ,   // G
-     0x7F, 0x08, 0x08, 0x08, 0x7F ,   // H
-     0x00, 0x41, 0x7F, 0x41, 0x00 ,   // I
-     0x20, 0x40, 0x41, 0x3F, 0x01 ,   // J
-     0x7F, 0x08, 0x14, 0x22, 0x41 ,   // K
-     0x7F, 0x40, 0x40, 0x40, 0x40 ,   // L
-     0x7F, 0x02, 0x0C, 0x02, 0x7F ,   // M
-     0x7F, 0x04, 0x08, 0x10, 0x7F ,   // N
-     0x3E, 0x41, 0x41, 0x41, 0x3E ,   // O
-     0x7F, 0x09, 0x09, 0x09, 0x06 ,   // P
-     0x3E, 0x41, 0x51, 0x21, 0x5E ,   // Q
-     0x7F, 0x09, 0x19, 0x29, 0x46 ,   // R
-     0x46, 0x49, 0x49, 0x49, 0x31 ,   // S
-     0x01, 0x01, 0x7F, 0x01, 0x01 ,   // T
-     0x3F, 0x40, 0x40, 0x40, 0x3F ,   // U
-     0x1F, 0x20, 0x40, 0x20, 0x1F ,   // V
-     0x3F, 0x40, 0x38, 0x40, 0x3F ,   // W
-     0x63, 0x14, 0x08, 0x14, 0x63 ,   // X
-     0x07, 0x08, 0x70, 0x08, 0x07 ,   // Y
-     0x61, 0x51, 0x49, 0x45, 0x43 ,   // Z
-     0x00, 0x7F, 0x41, 0x41, 0x00 ,   // [
-     0x55, 0x2A, 0x55, 0x2A, 0x55 ,   // 55
-     0x00, 0x41, 0x41, 0x7F, 0x00 ,   // ]
-     0x04, 0x02, 0x01, 0x02, 0x04 ,   // ^
-     0x40, 0x40, 0x40, 0x40, 0x40 ,   // _
-     0x00, 0x01, 0x02, 0x04, 0x00 ,   // '
-     0x20, 0x54, 0x54, 0x54, 0x78 ,   // a
-     0x7F, 0x48, 0x44, 0x44, 0x38 ,   // b
-     0x38, 0x44, 0x44, 0x44, 0x20 ,   // c
-     0x38, 0x44, 0x44, 0x48, 0x7F ,   // d
-     0x38, 0x54, 0x54, 0x54, 0x18 ,   // e
-     0x08, 0x7E, 0x09, 0x01, 0x02 ,   // f
-     0x0C, 0x52, 0x52, 0x52, 0x3E ,   // g
-     0x7F, 0x08, 0x04, 0x04, 0x78 ,   // h
-     0x00, 0x44, 0x7D, 0x40, 0x00 ,   // i
-     0x20, 0x40, 0x44, 0x3D, 0x00 ,   // j
-     0x7F, 0x10, 0x28, 0x44, 0x00 ,   // k
-     0x00, 0x41, 0x7F, 0x40, 0x00 ,   // l
-     0x7C, 0x04, 0x18, 0x04, 0x78 ,   // m
-     0x7C, 0x08, 0x04, 0x04, 0x78 ,   // n
-     0x38, 0x44, 0x44, 0x44, 0x38 ,   // o
-     0x7C, 0x14, 0x14, 0x14, 0x08 ,   // p
-     0x08, 0x14, 0x14, 0x18, 0x7C ,   // q
-     0x7C, 0x08, 0x04, 0x04, 0x08 ,   // r
-     0x48, 0x54, 0x54, 0x54, 0x20 ,   // s
-     0x04, 0x3F, 0x44, 0x40, 0x20 ,   // t
-     0x3C, 0x40, 0x40, 0x20, 0x7C ,   // u
-     0x1C, 0x20, 0x40, 0x20, 0x1C ,   // v
-     0x3C, 0x40, 0x30, 0x40, 0x3C ,   // w
-     0x44, 0x28, 0x10, 0x28, 0x44 ,   // x
-     0x0C, 0x50, 0x50, 0x50, 0x3C ,   // y
-     0x44, 0x64, 0x54, 0x4C, 0x44     // z
+    0x00, 0x00, 0x00, 0x00, 0x00 ,  // sp
+    0x00, 0x00, 0x2f, 0x00, 0x00 ,   // !
+    0x00, 0x07, 0x00, 0x07, 0x00 ,   // "
+    0x14, 0x7f, 0x14, 0x7f, 0x14 ,   // #
+    0x24, 0x2a, 0x7f, 0x2a, 0x12 ,   // $
+    0xc4, 0xc8, 0x10, 0x26, 0x46 ,   // %
+    0x36, 0x49, 0x55, 0x22, 0x50 ,   // &
+    0x00, 0x05, 0x03, 0x00, 0x00 ,   // '
+    0x00, 0x1c, 0x22, 0x41, 0x00 ,   // (
+    0x00, 0x41, 0x22, 0x1c, 0x00 ,   // )
+    0x14, 0x08, 0x3E, 0x08, 0x14 ,   // *
+    0x08, 0x08, 0x3E, 0x08, 0x08 ,   // +
+    0x00, 0x00, 0x50, 0x30, 0x00 ,   // ,
+    0x10, 0x10, 0x10, 0x10, 0x10 ,   // -
+    0x00, 0x60, 0x60, 0x00, 0x00 ,   // .
+    0x20, 0x10, 0x08, 0x04, 0x02 ,   // /
+    0x3E, 0x51, 0x49, 0x45, 0x3E ,   // 0
+    0x00, 0x42, 0x7F, 0x40, 0x00 ,   // 1
+    0x42, 0x61, 0x51, 0x49, 0x46 ,   // 2
+    0x21, 0x41, 0x45, 0x4B, 0x31 ,   // 3
+    0x18, 0x14, 0x12, 0x7F, 0x10 ,   // 4
+    0x27, 0x45, 0x45, 0x45, 0x39 ,   // 5
+    0x3C, 0x4A, 0x49, 0x49, 0x30 ,   // 6
+    0x01, 0x71, 0x09, 0x05, 0x03 ,   // 7
+    0x36, 0x49, 0x49, 0x49, 0x36 ,   // 8
+    0x06, 0x49, 0x49, 0x29, 0x1E ,   // 9
+    0x00, 0x36, 0x36, 0x00, 0x00 ,   // :
+    0x00, 0x56, 0x36, 0x00, 0x00 ,   // ;
+    0x08, 0x14, 0x22, 0x41, 0x00 ,   // <
+    0x14, 0x14, 0x14, 0x14, 0x14 ,   // =
+    0x00, 0x41, 0x22, 0x14, 0x08 ,   // >
+    0x02, 0x01, 0x51, 0x09, 0x06 ,   // ?
+    0x32, 0x49, 0x59, 0x51, 0x3E ,   // @
+    0x7E, 0x11, 0x11, 0x11, 0x7E ,   // A
+    0x7F, 0x49, 0x49, 0x49, 0x36 ,   // B
+    0x3E, 0x41, 0x41, 0x41, 0x22 ,   // C
+    0x7F, 0x41, 0x41, 0x22, 0x1C ,   // D
+    0x7F, 0x49, 0x49, 0x49, 0x41 ,   // E
+    0x7F, 0x09, 0x09, 0x09, 0x01 ,   // F
+    0x3E, 0x41, 0x49, 0x49, 0x7A ,   // G
+    0x7F, 0x08, 0x08, 0x08, 0x7F ,   // H
+    0x00, 0x41, 0x7F, 0x41, 0x00 ,   // I
+    0x20, 0x40, 0x41, 0x3F, 0x01 ,   // J
+    0x7F, 0x08, 0x14, 0x22, 0x41 ,   // K
+    0x7F, 0x40, 0x40, 0x40, 0x40 ,   // L
+    0x7F, 0x02, 0x0C, 0x02, 0x7F ,   // M
+    0x7F, 0x04, 0x08, 0x10, 0x7F ,   // N
+    0x3E, 0x41, 0x41, 0x41, 0x3E ,   // O
+    0x7F, 0x09, 0x09, 0x09, 0x06 ,   // P
+    0x3E, 0x41, 0x51, 0x21, 0x5E ,   // Q
+    0x7F, 0x09, 0x19, 0x29, 0x46 ,   // R
+    0x46, 0x49, 0x49, 0x49, 0x31 ,   // S
+    0x01, 0x01, 0x7F, 0x01, 0x01 ,   // T
+    0x3F, 0x40, 0x40, 0x40, 0x3F ,   // U
+    0x1F, 0x20, 0x40, 0x20, 0x1F ,   // V
+    0x3F, 0x40, 0x38, 0x40, 0x3F ,   // W
+    0x63, 0x14, 0x08, 0x14, 0x63 ,   // X
+    0x07, 0x08, 0x70, 0x08, 0x07 ,   // Y
+    0x61, 0x51, 0x49, 0x45, 0x43 ,   // Z
+    0x00, 0x7F, 0x41, 0x41, 0x00 ,   // [
+    0x55, 0x2A, 0x55, 0x2A, 0x55 ,   // 55
+    0x00, 0x41, 0x41, 0x7F, 0x00 ,   // ]
+    0x04, 0x02, 0x01, 0x02, 0x04 ,   // ^
+    0x40, 0x40, 0x40, 0x40, 0x40 ,   // _
+    0x00, 0x01, 0x02, 0x04, 0x00 ,   // '
+    0x20, 0x54, 0x54, 0x54, 0x78 ,   // a
+    0x7F, 0x48, 0x44, 0x44, 0x38 ,   // b
+    0x38, 0x44, 0x44, 0x44, 0x20 ,   // c
+    0x38, 0x44, 0x44, 0x48, 0x7F ,   // d
+    0x38, 0x54, 0x54, 0x54, 0x18 ,   // e
+    0x08, 0x7E, 0x09, 0x01, 0x02 ,   // f
+    0x0C, 0x52, 0x52, 0x52, 0x3E ,   // g
+    0x7F, 0x08, 0x04, 0x04, 0x78 ,   // h
+    0x00, 0x44, 0x7D, 0x40, 0x00 ,   // i
+    0x20, 0x40, 0x44, 0x3D, 0x00 ,   // j
+    0x7F, 0x10, 0x28, 0x44, 0x00 ,   // k
+    0x00, 0x41, 0x7F, 0x40, 0x00 ,   // l
+    0x7C, 0x04, 0x18, 0x04, 0x78 ,   // m
+    0x7C, 0x08, 0x04, 0x04, 0x78 ,   // n
+    0x38, 0x44, 0x44, 0x44, 0x38 ,   // o
+    0x7C, 0x14, 0x14, 0x14, 0x08 ,   // p
+    0x08, 0x14, 0x14, 0x18, 0x7C ,   // q
+    0x7C, 0x08, 0x04, 0x04, 0x08 ,   // r
+    0x48, 0x54, 0x54, 0x54, 0x20 ,   // s
+    0x04, 0x3F, 0x44, 0x40, 0x20 ,   // t
+    0x3C, 0x40, 0x40, 0x20, 0x7C ,   // u
+    0x1C, 0x20, 0x40, 0x20, 0x1C ,   // v
+    0x3C, 0x40, 0x30, 0x40, 0x3C ,   // w
+    0x44, 0x28, 0x10, 0x28, 0x44 ,   // x
+    0x0C, 0x50, 0x50, 0x50, 0x3C ,   // y
+    0x44, 0x64, 0x54, 0x4C, 0x44     // z
 };
 
 
@@ -115,40 +115,40 @@ const uint8_t FontLookup [] =
 --------------------------------------------------------------------------------------------------*/
 void LcdInit ( uint8_t Contrast )
 {
-  RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; /* Port B Clock */
-
-  GPIOB->CRH &= ~(((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_DC_PIN-8)*4) |
-                  ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_IN_PIN-8)*4) |
-                  ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_CLK_PIN-8)*4)|
-                  ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_RST_PIN-8)*4) ); // Reset all pins
-
-
-  GPIOB->CRH |=  (((GPIO_CRL_MODE0_1)<<(LCD_DC_PIN-8)*4) |
-                  ((GPIO_CRL_MODE0_1)<<(LCD_IN_PIN-8)*4) |
-                  ((GPIO_CRL_MODE0_1)<<(LCD_CLK_PIN-8)*4)|
-                  ((GPIO_CRL_MODE0_1)<<(LCD_RST_PIN-8)*4) ); // Pins in push-pull 10MHz
-    
-  GPIOB->BRR = 1<<LCD_RST_PIN; /* RESET to 0 */
-  if (Contrast == 0)
-    Contrast = 60;
-  if (Contrast > 90)
-    Contrast = 90;
-  /* Some delay */
-  {
-    __IO uint32_t Counter;
-    for ( Counter = 0; Counter <10; Counter++)
-      ; /* Blank */
-  }
-  GPIOB->BSRR = 1<<LCD_RST_PIN; /* RESET to 1 */
+    RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; /* Port B Clock */
+
+    GPIOB->CRH &= ~(((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_DC_PIN-8)*4) |
+                    ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_IN_PIN-8)*4) |
+                    ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_CLK_PIN-8)*4)|
+                    ((GPIO_CRL_MODE0|GPIO_CRL_CNF0)<<(LCD_RST_PIN-8)*4) ); // Reset all pins
+
+
+    GPIOB->CRH |=  (((GPIO_CRL_MODE0_1)<<(LCD_DC_PIN-8)*4) |
+                    ((GPIO_CRL_MODE0_1)<<(LCD_IN_PIN-8)*4) |
+                    ((GPIO_CRL_MODE0_1)<<(LCD_CLK_PIN-8)*4)|
+                    ((GPIO_CRL_MODE0_1)<<(LCD_RST_PIN-8)*4) ); // Pins in push-pull 10MHz
+
+    GPIOB->BRR = 1<<LCD_RST_PIN; /* RESET to 0 */
+    if (Contrast == 0)
+        Contrast = 60;
+    if (Contrast > 90)
+        Contrast = 90;
+    /* Some delay */
+    {
+        __IO uint32_t Counter;
+        for ( Counter = 0; Counter <10; Counter++)
+            ; /* Blank */
+    }
+    GPIOB->BSRR = 1<<LCD_RST_PIN; /* RESET to 1 */
 #define FUNCTION_SET 0x20
 #define FUNCTION_PD  0x04
 #define FUNCTION_V   0x02
 #define FUNCTION_EXT 0x01
-  LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD );  // LCD Extended Commands.
+    LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD );  // LCD Extended Commands.
 #define SET_VOP      0x80
-  LcdSend( SET_VOP|Contrast, LCD_CMD );  // Set LCD Vop (Contrast). 3.06+0.06*72=4.38V
+    LcdSend( SET_VOP|Contrast, LCD_CMD );  // Set LCD Vop (Contrast). 3.06+0.06*72=4.38V
 #define TEMPERATURE_COEFFICIENT 0x4
-  LcdSend( TEMPERATURE_COEFFICIENT | 2, LCD_CMD );  // Set Temp coefficent. 2 (0-3)
+    LcdSend( TEMPERATURE_COEFFICIENT | 2, LCD_CMD );  // Set Temp coefficent. 2 (0-3)
 #define SET_BIAS 0x10
 #define BIAS_1_10 0x07
 #define BIAS_1_18 0x06
@@ -158,15 +158,15 @@ void LcdInit ( uint8_t Contrast )
 #define BIAS_1_65 0x02
 #define BIAS_1_80 0x01
 #define BIAS_1_100 0x00
-  LcdSend( SET_BIAS|BIAS_1_48, LCD_CMD );  // LCD bias mode 1:48.
+    LcdSend( SET_BIAS|BIAS_1_48, LCD_CMD );  // LCD bias mode 1:48.
 
-  LcdSend( FUNCTION_SET, LCD_CMD );  // LCD Standard Commands, Horizontal addressing mode.
+    LcdSend( FUNCTION_SET, LCD_CMD );  // LCD Standard Commands, Horizontal addressing mode.
 #define DISPLAY_CONTROL 0x08
 #define DISPLAY_BLANK   0x00
 #define DISPLAY_NORMAL  0x04
 #define DISPLAY_ALLON   0x01
 #define DISPLAY_INVERSE 0x05
-  LcdSend( DISPLAY_CONTROL|DISPLAY_NORMAL, LCD_CMD );  // LCD in normal mode.
+    LcdSend( DISPLAY_CONTROL|DISPLAY_NORMAL, LCD_CMD );  // LCD in normal mode.
 
 #define SET_Y 0x40 /* from 0 to 5 */
 #define SET_X 0x80 /* from 0 to 83 */
@@ -176,19 +176,19 @@ void LcdInit ( uint8_t Contrast )
 //Clear display
 void LcdClear()
 {
-	uint16_t j = 7;
-  
-	while ( j-- )
-	{
-      uint16_t i = 84;
-
-      LcdSend(SET_Y|j, LCD_CMD ); 
-      LcdSend(SET_X|0, LCD_CMD);
-      while ( i-- )
-      {
-        LcdSend(0/*xff*/, LCD_DATA);
-      }  
-	}
+    uint16_t j = 7;
+
+    while ( j-- )
+    {
+        uint16_t i = 84;
+
+        LcdSend(SET_Y|j, LCD_CMD );
+        LcdSend(SET_X|0, LCD_CMD);
+        while ( i-- )
+        {
+            LcdSend(0/*xff*/, LCD_DATA);
+        }
+    }
 }
 
 /*--------------------------------------------------------------------------------------------------
@@ -207,21 +207,21 @@ void LcdClear()
 void LcdContrast ( uint8_t Contrast )
 {
     //  LCD Extended Commands.
-  LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD );  // LCD Extended Commands.
+    LcdSend( FUNCTION_SET|FUNCTION_EXT, LCD_CMD );  // LCD Extended Commands.
 
     // Set LCD Vop (Contrast).
     if ( Contrast > 90 ) /* Limit by 8.5 V */
-      Contrast = 90;
-  LcdSend( SET_VOP | Contrast, LCD_CMD );
+        Contrast = 90;
+    LcdSend( SET_VOP | Contrast, LCD_CMD );
 
     //  LCD Standard Commands, horizontal addressing mode.
-  LcdSend( FUNCTION_SET, LCD_CMD );
+    LcdSend( FUNCTION_SET, LCD_CMD );
 }
 
 void LcdGotoXY(uint8_t X, uint8_t Y)
 {
     LcdSend( SET_X|X, LCD_CMD );
-    LcdSend( SET_Y|Y, LCD_CMD ); 
+    LcdSend( SET_Y|Y, LCD_CMD );
 }
 
 
@@ -241,95 +241,95 @@ void LcdGotoXY(uint8_t X, uint8_t Y)
 
 void LcdChr ( uint32_t Ctrl, const char* Str )
 {
-  unsigned char ch;
-  uint8_t Inverse;
+    unsigned char ch;
+    uint8_t Inverse;
 #if defined(BIG)
 #define INTERNAL_BIG_DOWN
-  uint32_t Big = Ctrl;
-#endif  
-  Inverse = ((Ctrl & INVERSE) != 0);
-  LcdGotoXY((Ctrl/X_POSITION & 0x0F)*6 + ((Ctrl/X_OFFSET)& 0x3F), 
-            (Ctrl/Y_POSITION) & 0x07);
-
-  Ctrl = Ctrl & 0x7F; /* Only Len */
-  while ( Ctrl != 0 )
-  {
-    const uint8_t* Start;
-
-    if ( (ch = *Str) != 0)
-      Str++;
-    Ctrl--;
-    if ( ch == 0 ) 
-      ch = ' ';
-    if ( (ch < 0x20) || (ch > 0x7b) )
+    uint32_t Big = Ctrl;
+#endif
+    Inverse = ((Ctrl & INVERSE) != 0);
+    LcdGotoXY((Ctrl/X_POSITION & 0x0F)*6 + ((Ctrl/X_OFFSET)& 0x3F),
+              (Ctrl/Y_POSITION) & 0x07);
+
+    Ctrl = Ctrl & 0x7F; /* Only Len */
+    while ( Ctrl != 0 )
     {
-        //  Convert to a printable character.
-        ch = 92;
-    }
-    ch = ch - 32;
+        const uint8_t* Start;
+
+        if ( (ch = *Str) != 0)
+            Str++;
+        Ctrl--;
+        if ( ch == 0 )
+            ch = ' ';
+        if ( (ch < 0x20) || (ch > 0x7b) )
+        {
+            //  Convert to a printable character.
+            ch = 92;
+        }
+        ch = ch - 32;
 
-    Start = &FontLookup[ch*4+ch];
+        Start = &FontLookup[ch*4+ch];
 
 #if defined(BIG)
-    if ( (Big & (BIG_UP|BIG_DOWN)) == 0 )
+        if ( (Big & (BIG_UP|BIG_DOWN)) == 0 )
 #endif
-    { 
-      int i;
-      for (i=0; i<=4; i++)
-      {
-        uint8_t E = Start[i];
-        if ( Inverse )
         {
-          E = ~E;
+            int i;
+            for (i=0; i<=4; i++)
+            {
+                uint8_t E = Start[i];
+                if ( Inverse )
+                {
+                    E = ~E;
+                }
+                LcdSend( E, LCD_DATA );
+            }
+            if ( !Inverse )
+                LcdSend( 0, LCD_DATA );
+            else
+                LcdSend( 0xFF, LCD_DATA );
         }
-        LcdSend( E, LCD_DATA );
-      }
-      if ( !Inverse )
-        LcdSend( 0, LCD_DATA );
-      else
-        LcdSend( 0xFF, LCD_DATA );
-    }
 #if defined(BIG)
-    else
-    {
-      int i;
-      for (i=0; i<=4; i++)
-      {
-        uint8_t Element = 0;
-        uint8_t E = Start[i];
-
-        if ( Big & BIG_UP )
-        {
-          if(E & 0x01) Element |= 0x03;
-          if(E & 0x02) Element |= 0x0C;
-          if(E & 0x04) Element |= 0x30;
-          if(E & 0x08) Element |= 0xC0;
-        }
         else
         {
-          if(E & 0x10) Element |= 0x03;
-          if(E & 0x20) Element |= 0x0C;
-          if(E & 0x40) Element |= 0x30;
-          if(E & 0x80) Element |= 0xC0;
+            int i;
+            for (i=0; i<=4; i++)
+            {
+                uint8_t Element = 0;
+                uint8_t E = Start[i];
+
+                if ( Big & BIG_UP )
+                {
+                    if(E & 0x01) Element |= 0x03;
+                    if(E & 0x02) Element |= 0x0C;
+                    if(E & 0x04) Element |= 0x30;
+                    if(E & 0x08) Element |= 0xC0;
+                }
+                else
+                {
+                    if(E & 0x10) Element |= 0x03;
+                    if(E & 0x20) Element |= 0x0C;
+                    if(E & 0x40) Element |= 0x30;
+                    if(E & 0x80) Element |= 0xC0;
+                }
+                if (Inverse)
+                    Element = ~Element;
+                LcdSend( Element, LCD_DATA );
+                LcdSend( Element, LCD_DATA );
+            } /* For */
+            if ( !Inverse )
+            {
+                LcdSend( 0, LCD_DATA );
+                LcdSend( 0, LCD_DATA );
+            }
+            else
+            {
+                LcdSend( 0xFF, LCD_DATA );
+                LcdSend( 0xFF, LCD_DATA );
+            }
         }
-        if (Inverse)
-          Element = ~Element;
-        LcdSend( Element, LCD_DATA );
-        LcdSend( Element, LCD_DATA );
-      } /* For */
-      if ( !Inverse )
-      {
-        LcdSend( 0, LCD_DATA );
-        LcdSend( 0, LCD_DATA );
-      }
-      else
-      {
-        LcdSend( 0xFF, LCD_DATA );
-        LcdSend( 0xFF, LCD_DATA );
-      }
-    }
 #endif /* BIG */
-  }
+    }
 }
 
 
@@ -347,31 +347,31 @@ void LcdChr ( uint32_t Ctrl, const char* Str )
 --------------------------------------------------------------------------------------------------*/
 void LcdSend ( uint8_t Data, LcdCmdData cd )
 {
-  int i;
-
-  if ( cd != LCD_CMD ) /* If data */
-  {
-    GPIOB->BSRR = 1<<LCD_DC_PIN; /* DC pin to hight */
-  }
-  else
-  {
-    GPIOB->BRR = 1<<LCD_DC_PIN; /* DC pin to low */
-  }
-  LCD_DELAY;  
-  i=8;
-  do
-  {
-    if ( Data & 0x80 )
-      GPIOB->BSRR = 1<<LCD_IN_PIN; /* IN pin to hight */
+    int i;
+
+    if ( cd != LCD_CMD ) /* If data */
+    {
+        GPIOB->BSRR = 1<<LCD_DC_PIN; /* DC pin to hight */
+    }
     else
-      GPIOB->BRR  = 1<<LCD_IN_PIN; /* IN pin to low */
-	LCD_DELAY;
-    Data = Data<<1; /* Some additional delay */
-    GPIOB->BSRR  = 1<<LCD_CLK_PIN; /* CLK pin to hight */
-	LCD_DELAY;
-	i--;
-    GPIOB->BRR  = 1<<LCD_CLK_PIN; /* CLK pin to low */
-  }while(i);
+    {
+        GPIOB->BRR = 1<<LCD_DC_PIN; /* DC pin to low */
+    }
+    LCD_DELAY;
+    i=8;
+    do
+    {
+        if ( Data & 0x80 )
+            GPIOB->BSRR = 1<<LCD_IN_PIN; /* IN pin to hight */
+        else
+            GPIOB->BRR  = 1<<LCD_IN_PIN; /* IN pin to low */
+        LCD_DELAY;
+        Data = Data<<1; /* Some additional delay */
+        GPIOB->BSRR  = 1<<LCD_CLK_PIN; /* CLK pin to hight */
+        LCD_DELAY;
+        i--;
+        GPIOB->BRR  = 1<<LCD_CLK_PIN; /* CLK pin to low */
+    } while(i);
 }
 
 /*--------------------------------------------------------------------------------------------------
@@ -394,13 +394,13 @@ void SystemInit()
 
 int main(void)
 {
-  RCC->CFGR |= (RCC_CFGR_HPRE_1|RCC_CFGR_HPRE_3); /* div 8 */
-  LcdInit();
-  LcdClear();
-  LcdChr ( Y_POSITION*1+X_POSITION*1+13, "Hello world" );
-  LcdChr ( Y_POSITION*2+X_POSITION*1+13+INVERSE+X_OFFSET*3, "Hello world" );
-  LcdChr ( Y_POSITION*4+X_POSITION*0+2+BIG_UP, "15" );  
-  LcdChr ( Y_POSITION*5+X_POSITION*0+2+BIG_DOWN, "15" );  
-  return 0;
+    RCC->CFGR |= (RCC_CFGR_HPRE_1|RCC_CFGR_HPRE_3); /* div 8 */
+    LcdInit();
+    LcdClear();
+    LcdChr ( Y_POSITION*1+X_POSITION*1+13, "Hello world" );
+    LcdChr ( Y_POSITION*2+X_POSITION*1+13+INVERSE+X_OFFSET*3, "Hello world" );
+    LcdChr ( Y_POSITION*4+X_POSITION*0+2+BIG_UP, "15" );
+    LcdChr ( Y_POSITION*5+X_POSITION*0+2+BIG_DOWN, "15" );
+    return 0;
 }
 #endif

+ 1 - 1
src/n3310.h

@@ -15,7 +15,7 @@ typedef enum
     LCD_CMD  = 0,
     LCD_DATA = 1
 } LcdCmdData;
-void LcdSend    ( uint8_t data, LcdCmdData cd );
+void LcdSend ( uint8_t data, LcdCmdData cd );
 
 /*--------------------------------------------------------------------------------------------------
                                  Public function prototypes

+ 583 - 583
src/ui.c

@@ -18,39 +18,39 @@ static UserMenu_t* Menu;
 
 static CancelButton(void)
 {
-  if (!(Flags & (AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) && /* New values are set but not applied */
-       (DAC->DOR1 != 0 && DAC->DOR2 !=0 )) /* Only if the output is enabled 05.10.11 */
-  {
-    CurrentSettings.VoltageDAC = DAC->DOR1;
-    CurrentSettings.CurrentDAC = DAC->DOR2;
-  }
-  CurrentFunc(StartFunction);
-  return;
+    if (!(Flags & (AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) && /* New values are set but not applied */
+            (DAC->DOR1 != 0 && DAC->DOR2 !=0 )) /* Only if the output is enabled 05.10.11 */
+    {
+        CurrentSettings.VoltageDAC = DAC->DOR1;
+        CurrentSettings.CurrentDAC = DAC->DOR2;
+    }
+    CurrentFunc(StartFunction);
+    return;
 }
 
 void HandleUserMenu(void)
 {
-  int NewValue;
-  SubConfig_t* SubConfig;
-
-  if ( Menu->IVFlag == MENU_V_FLAG)
-    SubConfig = &CurrentConfig.V;
-  else
-    SubConfig = &CurrentConfig.I;
-
-  NewValue = (Menu->Value - SubConfig->DACOffset)/ SubConfig->DACRamp;
-  if ( NewValue < 0 )
-    NewValue = 0;
-  if ( NewValue > 4095 )
-    NewValue = 4095;
-  if ( Menu->IVFlag == MENU_V_FLAG)
-    CurrentSettings.VoltageDAC = NewValue;
-  else
-    CurrentSettings.CurrentDAC = NewValue;
-  if ( Flags & (AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG) )
-    ApplyOut();
-  else
-    CurrentFunc(StartFunction);
+    int NewValue;
+    SubConfig_t* SubConfig;
+
+    if ( Menu->IVFlag == MENU_V_FLAG)
+        SubConfig = &CurrentConfig.V;
+    else
+        SubConfig = &CurrentConfig.I;
+
+    NewValue = (Menu->Value - SubConfig->DACOffset)/ SubConfig->DACRamp;
+    if ( NewValue < 0 )
+        NewValue = 0;
+    if ( NewValue > 4095 )
+        NewValue = 4095;
+    if ( Menu->IVFlag == MENU_V_FLAG)
+        CurrentSettings.VoltageDAC = NewValue;
+    else
+        CurrentSettings.CurrentDAC = NewValue;
+    if ( Flags & (AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG) )
+        ApplyOut();
+    else
+        CurrentFunc(StartFunction);
 }
 
 #define MENU_0       0
@@ -71,158 +71,158 @@ void HandleUserMenu(void)
 
 static void DisplayMenu(int IsSetMenu)
 {
-  int i;
-  char* Text;
-
-  for (i=MENU_0; i<=MENU_13;i++)
-  {
-    int x = i/5 * 5;
-    int y = i%5 + 1;
-    int TextPos = (i< MENU_7)?i:i-1;
-    Text = CurrentConfig.Menu[TextPos].Text;
-    LcdChr(X_POSITION*x+Y_POSITION*y+4 + (i==MenuPos)*INVERSE, Text);
-  }
-
-  if (!IsSetMenu)
-    Text = "Cncl";
-  else
-    Text = "Retu";
-  LcdChr(X_POSITION*5+Y_POSITION*3+4 + (MENU_CNCL_RET==MenuPos)*INVERSE, Text);
+    int i;
+    char* Text;
+
+    for (i=MENU_0; i<=MENU_13; i++)
+    {
+        int x = i/5 * 5;
+        int y = i%5 + 1;
+        int TextPos = (i< MENU_7)?i:i-1;
+        Text = CurrentConfig.Menu[TextPos].Text;
+        LcdChr(X_POSITION*x+Y_POSITION*y+4 + (i==MenuPos)*INVERSE, Text);
+    }
+
+    if (!IsSetMenu)
+        Text = "Cncl";
+    else
+        Text = "Retu";
+    LcdChr(X_POSITION*5+Y_POSITION*3+4 + (MENU_CNCL_RET==MenuPos)*INVERSE, Text);
 }
 
 void ConfigureTheMenu(void);
 
 void UConfigureTheMenu()
 {
-  char* Text;
-  SubConfig_t* Config;
+    char* Text;
+    SubConfig_t* Config;
 
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 0;
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        MenuPos = 0;
 
-    if (Menu->IVFlag != MENU_I_FLAG) /* It can be polluted */
-      Menu->IVFlag = MENU_V_FLAG;
-    else
-      Menu->IVFlag = MENU_I_FLAG;
-
-    if (Menu->Text[0] == 0)
-      Menu->Text[0] = '0';
-    if (Menu->Text[1] == 0)
-      Menu->Text[1] = '1';
-    if (Menu->Text[2] == 0)
-      Menu->Text[2] = '2';
-    if (Menu->Text[3] == 0)
-      Menu->Text[3] = '3';
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+        if (Menu->IVFlag != MENU_I_FLAG) /* It can be polluted */
+            Menu->IVFlag = MENU_V_FLAG;
+        else
+            Menu->IVFlag = MENU_I_FLAG;
+
+        if (Menu->Text[0] == 0)
+            Menu->Text[0] = '0';
+        if (Menu->Text[1] == 0)
+            Menu->Text[1] = '1';
+        if (Menu->Text[2] == 0)
+            Menu->Text[2] = '2';
+        if (Menu->Text[3] == 0)
+            Menu->Text[3] = '3';
+        goto redraw;
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
     {
-      case KEY_UP:
-      case KEY_DOWN:
-        switch(MenuPos)
+        switch (Event & KEY_MASK)
         {
-          case 0:
-            if (Menu->IVFlag & MENU_I_FLAG)
-              Menu->IVFlag = MENU_V_FLAG;
-            else
-              Menu->IVFlag = MENU_I_FLAG;
-            goto redraw;
-          case 1:
-            if (EncCounter > 999 )
-              EncCounter = 999;
-            Menu->Value = EncCounter*10 + 1; /* it's better to see changes from 9001 to 9000 that 9000 to 8999 */
-            goto redraw;
-          default:
-            if (EncCounter > 'z')
-              EncCounter = 'z';
-            Menu->Text[MenuPos-2] = EncCounter;
+        case KEY_UP:
+        case KEY_DOWN:
+            switch(MenuPos)
+            {
+            case 0:
+                if (Menu->IVFlag & MENU_I_FLAG)
+                    Menu->IVFlag = MENU_V_FLAG;
+                else
+                    Menu->IVFlag = MENU_I_FLAG;
+                goto redraw;
+            case 1:
+                if (EncCounter > 999 )
+                    EncCounter = 999;
+                Menu->Value = EncCounter*10 + 1; /* it's better to see changes from 9001 to 9000 that 9000 to 8999 */
+                goto redraw;
+            default:
+                if (EncCounter > 'z')
+                    EncCounter = 'z';
+                Menu->Text[MenuPos-2] = EncCounter;
+                goto redraw;
+            }
+        case KEY_ENTER:
+            switch(MenuPos)
+            {
+            case 0:
+                if (Menu->IVFlag & MENU_V_FLAG)
+                    Menu->IVFlag = MENU_V_FLAG;
+                else
+                    Menu->IVFlag = MENU_I_FLAG;
+                EncCounter = Menu->Value;
+                break;
+            default:
+                EncCounter = Menu->Text[MenuPos - 1];
+            }
+            MenuPos++;
+            if (MenuPos == 6)
+                MenuPos = 0;
             goto redraw;
         }
-      case KEY_ENTER:
-        switch(MenuPos)
-        {
-          case 0:
-            if (Menu->IVFlag & MENU_V_FLAG)
-              Menu->IVFlag = MENU_V_FLAG;
-            else
-              Menu->IVFlag = MENU_I_FLAG;
-            EncCounter = Menu->Value;
-            break;
-          default:
-            EncCounter = Menu->Text[MenuPos - 1];
-        }
-        MenuPos++;
-        if (MenuPos == 6)
-          MenuPos = 0;
-        goto redraw;
     }
-  }
-  if ( (Event & EV_MASK) == EV_KEY_LONG &&
-       (Event & KEY_MASK) == KEY_ENTER )
-  {
-    CurrentFunc(ConfigureTheMenu);
-  }
-  return;
+    if ( (Event & EV_MASK) == EV_KEY_LONG &&
+            (Event & KEY_MASK) == KEY_ENTER )
+    {
+        CurrentFunc(ConfigureTheMenu);
+    }
+    return;
 
 redraw:
-  if (Menu->IVFlag != MENU_I_FLAG) /* Voltage */
-  {
-    Text = " Voltage";
-    Config = &CurrentConfig.V; 
-  }
-  else
-  {
-    Text = " Current";
-    Config = &CurrentConfig.I; 
-  }
-  LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuPos)*INVERSE, Text);
-  OutValueSmall(3, 3, Menu->Value, Config->DotPosition, MenuPos == 1);
-  LcdChr(X_POSITION*2+Y_POSITION*5+1 + (2==MenuPos)*INVERSE, &Menu->Text[0]);
-  LcdChr(X_POSITION*3+Y_POSITION*5+1 + (3==MenuPos)*INVERSE, &Menu->Text[1]);
-  LcdChr(X_POSITION*4+Y_POSITION*5+1 + (4==MenuPos)*INVERSE, &Menu->Text[2]);
-  LcdChr(X_POSITION*5+Y_POSITION*5+1 + (5==MenuPos)*INVERSE, &Menu->Text[3]);
+    if (Menu->IVFlag != MENU_I_FLAG) /* Voltage */
+    {
+        Text = " Voltage";
+        Config = &CurrentConfig.V;
+    }
+    else
+    {
+        Text = " Current";
+        Config = &CurrentConfig.I;
+    }
+    LcdChr(X_POSITION*0+Y_POSITION*1+14 + (0==MenuPos)*INVERSE, Text);
+    OutValueSmall(3, 3, Menu->Value, Config->DotPosition, MenuPos == 1);
+    LcdChr(X_POSITION*2+Y_POSITION*5+1 + (2==MenuPos)*INVERSE, &Menu->Text[0]);
+    LcdChr(X_POSITION*3+Y_POSITION*5+1 + (3==MenuPos)*INVERSE, &Menu->Text[1]);
+    LcdChr(X_POSITION*4+Y_POSITION*5+1 + (4==MenuPos)*INVERSE, &Menu->Text[2]);
+    LcdChr(X_POSITION*5+Y_POSITION*5+1 + (5==MenuPos)*INVERSE, &Menu->Text[3]);
 }
 
 
 void YesNoCalib(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 0;
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-      case KEY_DOWN:
-        if(MenuPos == 0)
-          MenuPos++;
-        else
-          MenuPos = 0;
+        LcdBlank(); /* Clear screen */
+        MenuPos = 0;
         goto redraw;
-      case KEY_ENTER:
-        if(MenuPos)
-          CurrentFunc(CalibrationMenu);
-        else
-          CurrentFunc(Configure);
     }
-  }
-  return;
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
+        {
+        case KEY_UP:
+        case KEY_DOWN:
+            if(MenuPos == 0)
+                MenuPos++;
+            else
+                MenuPos = 0;
+            goto redraw;
+        case KEY_ENTER:
+            if(MenuPos)
+                CurrentFunc(CalibrationMenu);
+            else
+                CurrentFunc(Configure);
+        }
+    }
+    return;
 redraw:
-  LcdChr(X_POSITION*0+Y_POSITION*1+14, "Do you want");
-  LcdChr(X_POSITION*0+Y_POSITION*2+14, "to start the");
-  LcdChr(X_POSITION*0+Y_POSITION*3+14, "calibration");
-  LcdChr(X_POSITION*0+Y_POSITION*5+7 + (0==MenuPos)*INVERSE,  "No");
-  LcdChr(X_POSITION*7+Y_POSITION*5+7 + (0!=MenuPos)*INVERSE,  "Yes");
+    LcdChr(X_POSITION*0+Y_POSITION*1+14, "Do you want");
+    LcdChr(X_POSITION*0+Y_POSITION*2+14, "to start the");
+    LcdChr(X_POSITION*0+Y_POSITION*3+14, "calibration");
+    LcdChr(X_POSITION*0+Y_POSITION*5+7 + (0==MenuPos)*INVERSE,  "No");
+    LcdChr(X_POSITION*7+Y_POSITION*5+7 + (0!=MenuPos)*INVERSE,  "Yes");
 }
 
 #define  CONF_MENU_EDIT 0
@@ -233,569 +233,569 @@ redraw:
 
 void Configure(void)
 {
- if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = CONF_CONTRAST;
-    goto redraw;
-  } 
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-       case KEY_DOWN:
-        if ( MenuPos == 0 )
-          MenuPos = 4;
-        else
-          MenuPos = MenuPos - 1;
+        LcdBlank(); /* Clear screen */
+        MenuPos = CONF_CONTRAST;
         goto redraw;
-      case KEY_UP:
-        if ( MenuPos == 4 )
-          MenuPos = 0;
-        else
-          MenuPos = MenuPos + 1;
-        goto redraw;
-      case KEY_ENTER:
-        switch(MenuPos)
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case CONF_MENU_EDIT: /* Menu set up */
-            CurrentFunc(ConfigureTheMenu);
-            return;
-          case CONF_CALIBR: /* Calibration */
-            CurrentFunc(YesNoCalib);
-            return;
-          case CONF_BACKUP: /* Save to backup */
-            if ( Flags&BACKUP_SAVE_FLAG )
-              Flags &= ~BACKUP_SAVE_FLAG;
+        case KEY_DOWN:
+            if ( MenuPos == 0 )
+                MenuPos = 4;
             else
-              Flags |= BACKUP_SAVE_FLAG;
+                MenuPos = MenuPos - 1;
             goto redraw;
-          case CONF_CLOCK: /* Time */
-#if defined(CLOCK_ENABLED)
-            if (IS_ON_CLOCK)
-            {
-              CurrentFunc(SetupTheClock);
-              return;
-            }
+        case KEY_UP:
+            if ( MenuPos == 4 )
+                MenuPos = 0;
             else
+                MenuPos = MenuPos + 1;
+            goto redraw;
+        case KEY_ENTER:
+            switch(MenuPos)
             {
-              SwitchOnTheClock();
-            }
+            case CONF_MENU_EDIT: /* Menu set up */
+                CurrentFunc(ConfigureTheMenu);
+                return;
+            case CONF_CALIBR: /* Calibration */
+                CurrentFunc(YesNoCalib);
+                return;
+            case CONF_BACKUP: /* Save to backup */
+                if ( Flags&BACKUP_SAVE_FLAG )
+                    Flags &= ~BACKUP_SAVE_FLAG;
+                else
+                    Flags |= BACKUP_SAVE_FLAG;
+                goto redraw;
+            case CONF_CLOCK: /* Time */
+#if defined(CLOCK_ENABLED)
+                if (IS_ON_CLOCK)
+                {
+                    CurrentFunc(SetupTheClock);
+                    return;
+                }
+                else
+                {
+                    SwitchOnTheClock();
+                }
 #endif
-            goto redraw;
-          case CONF_CONTRAST:
-            AfterContrast = StartFunction;
-            CurrentFunc(ContrastMenu);
+                goto redraw;
+            case CONF_CONTRAST:
+                AfterContrast = StartFunction;
+                CurrentFunc(ContrastMenu);
+            }
         }
     }
-  }
-  return;   
+    return;
 
 redraw:
-  LcdChr (Y_POSITION*1+X_POSITION*0+14+(CONF_MENU_EDIT==MenuPos)*INVERSE, "Menu Edit" );
-  LcdChr (Y_POSITION*2+X_POSITION*0+14+(CONF_CALIBR==MenuPos)*INVERSE, "Calibration" );
-  LcdChr (Y_POSITION*3+X_POSITION*1+14+(CONF_BACKUP==MenuPos)*INVERSE, "Save Backup");
-  LcdChr (Y_POSITION*3+X_POSITION*0+1+ (CONF_BACKUP==MenuPos)*INVERSE, Flags&BACKUP_SAVE_FLAG?"+":"-" );
-  LcdChr (Y_POSITION*4+X_POSITION*0+14+(CONF_CLOCK==MenuPos)*INVERSE, 
+    LcdChr (Y_POSITION*1+X_POSITION*0+14+(CONF_MENU_EDIT==MenuPos)*INVERSE, "Menu Edit" );
+    LcdChr (Y_POSITION*2+X_POSITION*0+14+(CONF_CALIBR==MenuPos)*INVERSE, "Calibration" );
+    LcdChr (Y_POSITION*3+X_POSITION*1+14+(CONF_BACKUP==MenuPos)*INVERSE, "Save Backup");
+    LcdChr (Y_POSITION*3+X_POSITION*0+1+ (CONF_BACKUP==MenuPos)*INVERSE, Flags&BACKUP_SAVE_FLAG?"+":"-" );
+    LcdChr (Y_POSITION*4+X_POSITION*0+14+(CONF_CLOCK==MenuPos)*INVERSE,
 #if defined(CLOCK_ENABLED)
-                              IS_ON_CLOCK?"Setup time":"Time on" 
+            IS_ON_CLOCK?"Setup time":"Time on"
 #else
-                              "Reserved"
-#endif  
-                                                       );
-  LcdChr (Y_POSITION*5+X_POSITION*0+14+(CONF_CONTRAST==MenuPos)*INVERSE, "Contrast" );
+            "Reserved"
+#endif
+           );
+    LcdChr (Y_POSITION*5+X_POSITION*0+14+(CONF_CONTRAST==MenuPos)*INVERSE, "Contrast" );
 
 }
 
 void ConfigureTheMenu(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = MENU_CNCL_RET;
-    goto redraw;
-  } 
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_DOWN:
-        if ( MenuPos == 0 )
-          MenuPos = 14;
-        else
-          MenuPos = MenuPos - 1;
+        LcdBlank(); /* Clear screen */
+        MenuPos = MENU_CNCL_RET;
         goto redraw;
-      case KEY_UP:
-        if ( MenuPos == 14 )
-          MenuPos = 0;
-        else
-          MenuPos = MenuPos + 1;
-        goto redraw;
-      case KEY_ENTER:
-        switch(MenuPos)
+    }
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case MENU_0:
-          case MENU_1:
-          case MENU_2:
-          case MENU_3:
-          case MENU_4:
-          case MENU_5:
-          case MENU_6:
-            Menu=&CurrentConfig.Menu[MenuPos];
-            goto usermenu;
-          case MENU_7:
-          case MENU_8:
-          case MENU_9:
-          case MENU_10:
-          case MENU_11:
-          case MENU_12:
-          case MENU_13:
-            Menu=&CurrentConfig.Menu[MenuPos - 1];
-            goto usermenu;
-          default:
-            CurrentFunc(SaveMenu);
-            return;
-        } /* switch MenuPos*/
-    } /* switch key */
-  }
-  return;
+        case KEY_DOWN:
+            if ( MenuPos == 0 )
+                MenuPos = 14;
+            else
+                MenuPos = MenuPos - 1;
+            goto redraw;
+        case KEY_UP:
+            if ( MenuPos == 14 )
+                MenuPos = 0;
+            else
+                MenuPos = MenuPos + 1;
+            goto redraw;
+        case KEY_ENTER:
+            switch(MenuPos)
+            {
+            case MENU_0:
+            case MENU_1:
+            case MENU_2:
+            case MENU_3:
+            case MENU_4:
+            case MENU_5:
+            case MENU_6:
+                Menu=&CurrentConfig.Menu[MenuPos];
+                goto usermenu;
+            case MENU_7:
+            case MENU_8:
+            case MENU_9:
+            case MENU_10:
+            case MENU_11:
+            case MENU_12:
+            case MENU_13:
+                Menu=&CurrentConfig.Menu[MenuPos - 1];
+                goto usermenu;
+            default:
+                CurrentFunc(SaveMenu);
+                return;
+            } /* switch MenuPos*/
+        } /* switch key */
+    }
+    return;
 usermenu:
-  CurrentFunc(UConfigureTheMenu);
+    CurrentFunc(UConfigureTheMenu);
 
-  return;
+    return;
 redraw:
-  DisplayMenu(1);
+    DisplayMenu(1);
 }
 
 void PredefinedMenu(void)
 {
- /* 1    6    10 */
- /* 2    7    11  */
- /* 3    cncl 12 */
- /* 4    8    13  */
- /* 5    9    14  */
-
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = MENU_CNCL_RET;
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    /* 1    6    10 */
+    /* 2    7    11  */
+    /* 3    cncl 12 */
+    /* 4    8    13  */
+    /* 5    9    14  */
+
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_DOWN:
-        if ( MenuPos == 0 )
-          MenuPos = 14;
-        else
-          MenuPos = MenuPos - 1;
-        goto redraw;
-      case KEY_UP:
-        if ( MenuPos == 14 )
-          MenuPos = 0;
-        else
-          MenuPos = MenuPos + 1;
+        LcdBlank(); /* Clear screen */
+        MenuPos = MENU_CNCL_RET;
         goto redraw;
-      case KEY_ENTER:
-        switch(MenuPos)
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case MENU_CNCL_RET: /* Default cancel input*/
-            CancelButton();
-            return;
-          case MENU_0:
-          case MENU_1:
-          case MENU_2:
-          case MENU_3:
-          case MENU_4:
-          case MENU_5:
-          case MENU_6:
-            Menu=&CurrentConfig.Menu[MenuPos];
-            goto usermenu;
-          case MENU_7:
-          case MENU_8:
-          case MENU_9:
-          case MENU_10:
-          case MENU_11:
-          case MENU_12:
-          case MENU_13:
-            Menu=&CurrentConfig.Menu[MenuPos-1];
-            goto usermenu;
-        } /* switch MenuPos*/
-    } /* switch key */
-  }
-  return;
+        case KEY_DOWN:
+            if ( MenuPos == 0 )
+                MenuPos = 14;
+            else
+                MenuPos = MenuPos - 1;
+            goto redraw;
+        case KEY_UP:
+            if ( MenuPos == 14 )
+                MenuPos = 0;
+            else
+                MenuPos = MenuPos + 1;
+            goto redraw;
+        case KEY_ENTER:
+            switch(MenuPos)
+            {
+            case MENU_CNCL_RET: /* Default cancel input*/
+                CancelButton();
+                return;
+            case MENU_0:
+            case MENU_1:
+            case MENU_2:
+            case MENU_3:
+            case MENU_4:
+            case MENU_5:
+            case MENU_6:
+                Menu=&CurrentConfig.Menu[MenuPos];
+                goto usermenu;
+            case MENU_7:
+            case MENU_8:
+            case MENU_9:
+            case MENU_10:
+            case MENU_11:
+            case MENU_12:
+            case MENU_13:
+                Menu=&CurrentConfig.Menu[MenuPos-1];
+                goto usermenu;
+            } /* switch MenuPos*/
+        } /* switch key */
+    }
+    return;
 usermenu:
-  if ( (Menu->IVFlag != MENU_I_FLAG) &&
-     (Menu->IVFlag != MENU_V_FLAG))  /* Do nothing - invalid menu */
+    if ( (Menu->IVFlag != MENU_I_FLAG) &&
+            (Menu->IVFlag != MENU_V_FLAG))  /* Do nothing - invalid menu */
+        return;
+    CurrentFunc(HandleUserMenu);
     return;
-  CurrentFunc(HandleUserMenu);
-  return;
 redraw:
-  DisplayMenu(0);
+    DisplayMenu(0);
 }
 
 
 void ApplyOut(void)
 {
-  int Delta;
-
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    goto redraw;
-  }
-  if ( CurrentSettings.VoltageDAC == DAC->DOR1 &&
-          CurrentSettings.CurrentDAC == DAC->DOR2 )
-  {
-     CurrentFunc(StartFunction);
-     return;
-  }
-
-  Delta = (int)CurrentSettings.VoltageDAC - (int)(DAC->DOR1);
-  if ( Delta > 50 )
-    Delta = 50;
-  if ( Delta < -50)
-    Delta = -50;
-  DAC_V = (int)(DAC->DOR1) + Delta;
-
-  Delta = (int)CurrentSettings.CurrentDAC - (int)(DAC->DOR2);
-  if ( Delta > 50 )
-    Delta = 50;
-  if ( Delta < -50)
-    Delta = -50;
-  DAC_I = (int)(DAC->DOR2) + Delta;
-
-  OutValueSmall(2, 9, HumanV, CurrentConfig.V.DotPosition, 0);
-  OutValueSmall(5, 9, HumanI, CurrentConfig.I.DotPosition, 0);
-  return;
+    int Delta;
+
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        goto redraw;
+    }
+    if ( CurrentSettings.VoltageDAC == DAC->DOR1 &&
+            CurrentSettings.CurrentDAC == DAC->DOR2 )
+    {
+        CurrentFunc(StartFunction);
+        return;
+    }
+
+    Delta = (int)CurrentSettings.VoltageDAC - (int)(DAC->DOR1);
+    if ( Delta > 50 )
+        Delta = 50;
+    if ( Delta < -50)
+        Delta = -50;
+    DAC_V = (int)(DAC->DOR1) + Delta;
+
+    Delta = (int)CurrentSettings.CurrentDAC - (int)(DAC->DOR2);
+    if ( Delta > 50 )
+        Delta = 50;
+    if ( Delta < -50)
+        Delta = -50;
+    DAC_I = (int)(DAC->DOR2) + Delta;
+
+    OutValueSmall(2, 9, HumanV, CurrentConfig.V.DotPosition, 0);
+    OutValueSmall(5, 9, HumanI, CurrentConfig.I.DotPosition, 0);
+    return;
 
 redraw:
-  LcdChr(Y_POSITION*1+X_POSITION*0+14, " Applying...");
-  OutValue(2, 0, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
-  OutValue(4, 0, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
+    LcdChr(Y_POSITION*1+X_POSITION*0+14, " Applying...");
+    OutValue(2, 0, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
+    OutValue(4, 0, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
 }
 
 uint32_t OriginalValue;
 void SetNewCurrent(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    LcdChr ( Y_POSITION*1+X_POSITION*0+14, "New current" );
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        LcdChr ( Y_POSITION*1+X_POSITION*0+14, "New current" );
 
-    EncCounter = CurrentSettings.CurrentDAC;
-    OriginalValue = CurrentSettings.CurrentDAC;
-    goto redraw;
-  } 
+        EncCounter = CurrentSettings.CurrentDAC;
+        OriginalValue = CurrentSettings.CurrentDAC;
+        goto redraw;
+    }
 
-  if ( (Event& EV_MASK) != EV_KEY_PRESSED )
-    return;
+    if ( (Event& EV_MASK) != EV_KEY_PRESSED )
+        return;
 
-  switch (Event & KEY_MASK)
-  {
+    switch (Event & KEY_MASK)
+    {
     case KEY_UP:
     case KEY_DOWN:
-      CurrentSettings.CurrentDAC = EncCounter;
-	  if (Flags&IMEDDIATE_APPLY_FLAG)
-	  DAC_I	= EncCounter;
-      goto redraw;
+        CurrentSettings.CurrentDAC = EncCounter;
+        if (Flags&IMEDDIATE_APPLY_FLAG)
+            DAC_I	= EncCounter;
+        goto redraw;
     case KEY_ENTER:
-      if ( CurrentSettings.CurrentDAC != OriginalValue ) /* Current is changed */
-      {
-          if ( (Flags&(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) )
-            CurrentFunc(ApplyOut);
-          else
+        if ( CurrentSettings.CurrentDAC != OriginalValue ) /* Current is changed */
+        {
+            if ( (Flags&(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) )
+                CurrentFunc(ApplyOut);
+            else
+                CurrentFunc(StartFunction);
+        }
+        else
             CurrentFunc(StartFunction);
-      }
-      else
-        CurrentFunc(StartFunction);
-  }
-  return;
+    }
+    return;
 redraw:
-  OutValue(3,2, CurrentFromDac(), CurrentConfig.I.DotPosition, 0xFF);
-  OutValueSmall(5, 0, HumanI, CurrentConfig.I.DotPosition, 0);
+    OutValue(3,2, CurrentFromDac(), CurrentConfig.I.DotPosition, 0xFF);
+    OutValueSmall(5, 0, HumanI, CurrentConfig.I.DotPosition, 0);
 }
 
 void SetNewVoltage(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    LcdChr ( Y_POSITION*1+X_POSITION*0+14, "New voltage" );
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+        LcdChr ( Y_POSITION*1+X_POSITION*0+14, "New voltage" );
 
-    EncCounter = CurrentSettings.VoltageDAC;
-    OriginalValue = CurrentSettings.VoltageDAC;
-    goto redraw;
-  } 
+        EncCounter = CurrentSettings.VoltageDAC;
+        OriginalValue = CurrentSettings.VoltageDAC;
+        goto redraw;
+    }
 
-  if ( (Event& EV_MASK) != EV_KEY_PRESSED )
-    return;
+    if ( (Event& EV_MASK) != EV_KEY_PRESSED )
+        return;
 
-  switch (Event & KEY_MASK)
-  {
+    switch (Event & KEY_MASK)
+    {
     case KEY_UP:
     case KEY_DOWN:
-      CurrentSettings.VoltageDAC = EncCounter;
-	  if (Flags&IMEDDIATE_APPLY_FLAG)
-	  DAC_V	= EncCounter;
-      goto redraw;
+        CurrentSettings.VoltageDAC = EncCounter;
+        if (Flags&IMEDDIATE_APPLY_FLAG)
+            DAC_V	= EncCounter;
+        goto redraw;
     case KEY_ENTER:
-      if ( CurrentSettings.VoltageDAC != OriginalValue ) /* Voltage is changed */
-      {
-          if ( (Flags&(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) )
-            CurrentFunc(ApplyOut);
-          else
-            CurrentFunc(StartFunction);
-      }
-      else
-        CurrentFunc(SetNewCurrent);
-  }
-  return;
+        if ( CurrentSettings.VoltageDAC != OriginalValue ) /* Voltage is changed */
+        {
+            if ( (Flags&(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG)) )
+                CurrentFunc(ApplyOut);
+            else
+                CurrentFunc(StartFunction);
+        }
+        else
+            CurrentFunc(SetNewCurrent);
+    }
+    return;
 redraw:
-  OutValue(3,2, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
-  OutValueSmall(5, 0, HumanV, CurrentConfig.V.DotPosition, 0);
+    OutValue(3,2, VoltageFromDac(), CurrentConfig.V.DotPosition, 0xFF);
+    OutValueSmall(5, 0, HumanV, CurrentConfig.V.DotPosition, 0);
 }
 
 void SmallSettings(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 2; /* Cancel */
-    goto redraw;
-  }
-
-  if ( (Event & EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_UP:
-        MenuPos++;
-        if ( MenuPos > 5 )
-          MenuPos = 0;
-        goto redraw;
-      case KEY_DOWN:
-        if ( MenuPos == 0 )
-          MenuPos = 5;
-        else
-          MenuPos--;
+        LcdBlank(); /* Clear screen */
+        MenuPos = 2; /* Cancel */
         goto redraw;
-      case KEY_ENTER:
-        switch(MenuPos)
+    }
+
+    if ( (Event & EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
         {
-          case 0: /* Configuration */
-            CurrentFunc(Configure);
-            return;
-          case 1:
-            if (Flags&AUTO_APPLY_FLAG)
-            {
-              Flags &= ~AUTO_APPLY_FLAG;
-              Flags |= IMEDDIATE_APPLY_FLAG;
-            }
-            else if (Flags & IMEDDIATE_APPLY_FLAG)
-            {
-              Flags &= ~(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG);
-            }
+        case KEY_UP:
+            MenuPos++;
+            if ( MenuPos > 5 )
+                MenuPos = 0;
+            goto redraw;
+        case KEY_DOWN:
+            if ( MenuPos == 0 )
+                MenuPos = 5;
             else
-            {
-              Flags |= AUTO_APPLY_FLAG;
-            }
+                MenuPos--;
             goto redraw;
-          case 2: /* Cancel */
-            CancelButton();
-            return;
-          case 3: /* Current graph */
+        case KEY_ENTER:
+            switch(MenuPos)
+            {
+            case 0: /* Configuration */
+                CurrentFunc(Configure);
+                return;
+            case 1:
+                if (Flags&AUTO_APPLY_FLAG)
+                {
+                    Flags &= ~AUTO_APPLY_FLAG;
+                    Flags |= IMEDDIATE_APPLY_FLAG;
+                }
+                else if (Flags & IMEDDIATE_APPLY_FLAG)
+                {
+                    Flags &= ~(AUTO_APPLY_FLAG|IMEDDIATE_APPLY_FLAG);
+                }
+                else
+                {
+                    Flags |= AUTO_APPLY_FLAG;
+                }
+                goto redraw;
+            case 2: /* Cancel */
+                CancelButton();
+                return;
+            case 3: /* Current graph */
 #if defined(GRAPH)
-            GraphData.GraphArray = IGraphArray;
-            GraphData.DotPosition = CurrentConfig.I.DotPosition;
-            CurrentFunc(DisplayGraph);
+                GraphData.GraphArray = IGraphArray;
+                GraphData.DotPosition = CurrentConfig.I.DotPosition;
+                CurrentFunc(DisplayGraph);
 #endif
-            return;
-          case 4: /* Voltage graph */
+                return;
+            case 4: /* Voltage graph */
 #if defined(GRAPH)
-            GraphData.GraphArray = VGraphArray;
-            GraphData.DotPosition = CurrentConfig.V.DotPosition;
-            CurrentFunc(DisplayGraph);
-            return;
+                GraphData.GraphArray = VGraphArray;
+                GraphData.DotPosition = CurrentConfig.V.DotPosition;
+                CurrentFunc(DisplayGraph);
+                return;
 #endif
-          case 5: /* Timer set up */
+            case 5: /* Timer set up */
 #if defined(CLOCK_ENABLED)
-            if (IS_ON_CLOCK)
-              CurrentFunc(TimerSetup);
+                if (IS_ON_CLOCK)
+                    CurrentFunc(TimerSetup);
 #endif
-            return;
+                return;
 
-        } /* switch MenuPos*/
-    } /* switch key */
-  }
-  return;
+            } /* switch MenuPos*/
+        } /* switch key */
+    }
+    return;
 redraw:
-  LcdChr(14+1*Y_POSITION+0*X_POSITION + (MenuPos==0?INVERSE:0), "Settings");
-  LcdChr(
+    LcdChr(14+1*Y_POSITION+0*X_POSITION + (MenuPos==0?INVERSE:0), "Settings");
+    LcdChr(
 #if !defined(CLOCK_ENABLED)
-  14
+        14
 #else
-  5
-#endif  
-     + 2*Y_POSITION+0*X_POSITION + (MenuPos==1?INVERSE:0), 
-    ( Flags & AUTO_APPLY_FLAG )?
-	      "Auto":
-	  ( (Flags & IMEDDIATE_APPLY_FLAG)? "Immd":"Manu"));
-  LcdChr(14+3*Y_POSITION+0*X_POSITION + (MenuPos==2?INVERSE:0), "Cancel");
+        5
+#endif
+        + 2*Y_POSITION+0*X_POSITION + (MenuPos==1?INVERSE:0),
+        ( Flags & AUTO_APPLY_FLAG )?
+        "Auto":
+        ( (Flags & IMEDDIATE_APPLY_FLAG)? "Immd":"Manu"));
+    LcdChr(14+3*Y_POSITION+0*X_POSITION + (MenuPos==2?INVERSE:0), "Cancel");
 #if defined(GRAPH)
-  LcdChr(14+4*Y_POSITION+0*X_POSITION + (MenuPos==3?INVERSE:0), "I Graph");
-  LcdChr(14+5*Y_POSITION+0*X_POSITION + (MenuPos==4?INVERSE:0), "V Graph");
+    LcdChr(14+4*Y_POSITION+0*X_POSITION + (MenuPos==3?INVERSE:0), "I Graph");
+    LcdChr(14+5*Y_POSITION+0*X_POSITION + (MenuPos==4?INVERSE:0), "V Graph");
 #else
-  LcdChr(14+4*Y_POSITION+0*X_POSITION + (MenuPos==3?INVERSE:0), "Reserved");
-  LcdChr(14+5*Y_POSITION+0*X_POSITION + (MenuPos==4?INVERSE:0), "Reserved");
+    LcdChr(14+4*Y_POSITION+0*X_POSITION + (MenuPos==3?INVERSE:0), "Reserved");
+    LcdChr(14+5*Y_POSITION+0*X_POSITION + (MenuPos==4?INVERSE:0), "Reserved");
 #endif
 #if defined(CLOCK_ENABLED)
-  LcdChr(8+2*Y_POSITION+6*X_POSITION + (MenuPos==5?INVERSE:0), (IS_ON_CLOCK)? "Timer":"");
+    LcdChr(8+2*Y_POSITION+6*X_POSITION + (MenuPos==5?INVERSE:0), (IS_ON_CLOCK)? "Timer":"");
 #endif
 }
 
 #if defined(CLOCK_ENABLED)
 static void ApplyWithDelay(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-  }
-
-  if ( (Event& EV_MASK) == EV_KEY_PRESSED  && 
-       (Event & KEY_MASK) == KEY_CLOCK )
-  {
-    RemainTimerValue = TimerValue;
-    CurrentFunc(ApplyOut);
-  }
+    if (Event == EV_FUNC_FIRST)
+    {
+        LcdBlank(); /* Clear screen */
+    }
+
+    if ( (Event& EV_MASK) == EV_KEY_PRESSED  &&
+            (Event & KEY_MASK) == KEY_CLOCK )
+    {
+        RemainTimerValue = TimerValue;
+        CurrentFunc(ApplyOut);
+    }
 }
 #endif
 
 void StartFunction(void)
 {
-  if (Event == EV_FUNC_FIRST)
-  {
-    LcdBlank(); /* Clear screen */
-    MenuPos = 0; /* There are no key ENTER pressed */
-    goto redraw;
-  }
-
-  if ( (Event& EV_MASK) == EV_KEY_PRESSED )
-  {
-    switch (Event & KEY_MASK)
+    if (Event == EV_FUNC_FIRST)
     {
-      case KEY_DOWN:
-        CurrentFunc(SmallSettings);
-        return;
-      case KEY_UP:
-        CurrentFunc(PredefinedMenu);
-        return;
-      case KEY_ADC:
+        LcdBlank(); /* Clear screen */
+        MenuPos = 0; /* There are no key ENTER pressed */
         goto redraw;
+    }
+
+    if ( (Event& EV_MASK) == EV_KEY_PRESSED )
+    {
+        switch (Event & KEY_MASK)
+        {
+        case KEY_DOWN:
+            CurrentFunc(SmallSettings);
+            return;
+        case KEY_UP:
+            CurrentFunc(PredefinedMenu);
+            return;
+        case KEY_ADC:
+            goto redraw;
 #if defined(CLOCK_ENABLED)
-      case KEY_CLOCK:
-      {
-        if ( TimerValue != 0 && RemainTimerValue != 0xFFFF )
+        case KEY_CLOCK:
         {
-          DisplayClock(RemainTimerValue, 10+0x8000, Y_POSITION*3+X_POSITION*6);
+            if ( TimerValue != 0 && RemainTimerValue != 0xFFFF )
+            {
+                DisplayClock(RemainTimerValue, 10+0x8000, Y_POSITION*3+X_POSITION*6);
+            }
+            else
+            {
+                LcdChr(Y_POSITION*3+X_POSITION*6+3, "" );
+                DisplayClock((RTC->CNTH<<16) + RTC->CNTL, 10, Y_POSITION*3+X_POSITION*9);
+            }
+            return;
         }
-        else
+#endif
+        case KEY_ENTER:/* Reserve current */
         {
-          LcdChr(Y_POSITION*3+X_POSITION*6+3, "" );
-          DisplayClock((RTC->CNTH<<16) + RTC->CNTL, 10, Y_POSITION*3+X_POSITION*9);
+            MenuPos = 1; /* Key ENTER was pressed */
+            goto redraw;
+        }
         }
-        return;
-      }
-#endif        
-      case KEY_ENTER:/* Reserve current */
-      {
-        MenuPos = 1; /* Key ENTER was pressed */
-        goto redraw;
-      }
     }
-  }
 
-  if ( (Event & EV_MASK) == EV_KEY_LONG ) 
-  {
-    if ( CurrentSettings.VoltageDAC == DAC->DOR1 &&
-         CurrentSettings.CurrentDAC == DAC->DOR2 )
+    if ( (Event & EV_MASK) == EV_KEY_LONG )
     {
-      /* Long key set OFF output */
-      DAC_V = 0;
-      DAC_I = 0;
-      MenuPos = 0; /* It is long press. The event was handled. No need do anything on key realized */
+        if ( CurrentSettings.VoltageDAC == DAC->DOR1 &&
+                CurrentSettings.CurrentDAC == DAC->DOR2 )
+        {
+            /* Long key set OFF output */
+            DAC_V = 0;
+            DAC_I = 0;
+            MenuPos = 0; /* It is long press. The event was handled. No need do anything on key realized */
 #if defined(CLOCK_ENABLED)
-      RemainTimerValue = 0;
+            RemainTimerValue = 0;
 #endif
-      return;
-    }
+            return;
+        }
 
-    /* Apply seved or set voltage and current */
-    MenuPos = 0;
+        /* Apply seved or set voltage and current */
+        MenuPos = 0;
 #if defined(CLOCK_ENABLED)
-    if (TimerValue != 0)
-    {
-      CurrentFunc(ApplyWithDelay);
-    }
-    else
+        if (TimerValue != 0)
+        {
+            CurrentFunc(ApplyWithDelay);
+        }
+        else
 #endif
+        {
+            CurrentFunc(ApplyOut);
+        }
+        return;
+    }
+
+
+    if ( (Event& EV_MASK) == EV_KEY_REALIZED ) /* realised event current */
     {
-      CurrentFunc(ApplyOut);
+        if (MenuPos) /* No long press */
+            CurrentFunc(SetNewVoltage);
+        return;
     }
-    return;   
-  }
-  
-
-  if ( (Event& EV_MASK) == EV_KEY_REALIZED ) /* realised event current */
-  {
-    if (MenuPos) /* No long press */
-      CurrentFunc(SetNewVoltage);
+
     return;
-  }
 
-  return;
-  
 redraw:
 #if defined(RAW)
-  if ( DisplayRaw == 0)
+    if ( DisplayRaw == 0)
 #endif
-  {
-    OutValue(1, 0, HumanV, CurrentConfig.V.DotPosition, 0xFF);
-    OutValue(4, 0, HumanI, CurrentConfig.I.DotPosition, 0xFF);
-    OutValueSmall(1, 9, VoltageFromDac(), 
-                  CurrentConfig.V.DotPosition, CurrentSettings.VoltageDAC != DAC->DOR1);
-    OutValueSmall(5, 9, CurrentFromDac(), 
-                  CurrentConfig.I.DotPosition, CurrentSettings.CurrentDAC != DAC->DOR2);
-  }
+    {
+        OutValue(1, 0, HumanV, CurrentConfig.V.DotPosition, 0xFF);
+        OutValue(4, 0, HumanI, CurrentConfig.I.DotPosition, 0xFF);
+        OutValueSmall(1, 9, VoltageFromDac(),
+                      CurrentConfig.V.DotPosition, CurrentSettings.VoltageDAC != DAC->DOR1);
+        OutValueSmall(5, 9, CurrentFromDac(),
+                      CurrentConfig.I.DotPosition, CurrentSettings.CurrentDAC != DAC->DOR2);
+    }
 #if defined(RAW)
-  else
-  {
-    OutValue(1, 0, ADCVoltage/(128*16), 4, 0xFF);
-    OutValue(4, 0, ADCCurrent/(128*16), 4, 0xFF);
-    OutValueSmall(1, 9, CurrentSettings.VoltageDAC, 4, CurrentSettings.VoltageDAC != DAC->DOR1);
-    OutValueSmall(5, 9, CurrentSettings.CurrentDAC, 4, CurrentSettings.CurrentDAC != DAC->DOR2);
-  }
+    else
+    {
+        OutValue(1, 0, ADCVoltage/(128*16), 4, 0xFF);
+        OutValue(4, 0, ADCCurrent/(128*16), 4, 0xFF);
+        OutValueSmall(1, 9, CurrentSettings.VoltageDAC, 4, CurrentSettings.VoltageDAC != DAC->DOR1);
+        OutValueSmall(5, 9, CurrentSettings.CurrentDAC, 4, CurrentSettings.CurrentDAC != DAC->DOR2);
+    }
 #endif
 }
 
 int16_t VoltageFromAdc(void)
 {
-  return ADCVoltage*CurrentConfig.V.ADCRamp + CurrentConfig.V.ADCOffset;
+    return ADCVoltage*CurrentConfig.V.ADCRamp + CurrentConfig.V.ADCOffset;
 }
 
 int16_t CurrentFromAdc(void)
 {
-  return ADCCurrent*CurrentConfig.I.ADCRamp + CurrentConfig.I.ADCOffset;
+    return ADCCurrent*CurrentConfig.I.ADCRamp + CurrentConfig.I.ADCOffset;
 }
 
 int16_t CurrentFromDac()
 {
-  return CurrentSettings.CurrentDAC*CurrentConfig.I.DACRamp + CurrentConfig.I.DACOffset;
+    return CurrentSettings.CurrentDAC*CurrentConfig.I.DACRamp + CurrentConfig.I.DACOffset;
 }
 int16_t VoltageFromDac()
 {
-  return CurrentSettings.VoltageDAC*CurrentConfig.V.DACRamp + CurrentConfig.V.DACOffset;
+    return CurrentSettings.VoltageDAC*CurrentConfig.V.DACRamp + CurrentConfig.V.DACOffset;
 }