, ! .


»  » RA »  1


1

31 51 51

31

HHIMERA ():

... ...

(, .. ..), . ...
while( button2_in == 0 ){tick++;} ? {tick++;} . ? ( "" , . ?) . . , , - .

RA (2018-02-02 18:50:12)

32

+ . - 0 ..

33

dosikus ():

+

.

34

RA ():

.


.

:
time_out =MaxWait;
while( !button2_in || time_out-- ){tick++;}
 if(!time_out)
       {
         tick=0;
         error_flag=xxx;
        }

35

dosikus ():

.

.

RA (2018-02-04 00:04:43)

36


:
unsigned int pad_chg_time( unsigned char number )
{
  unsigned int tick = 0;
  unsigned char time_out = wait;
  if( number == 1 )
  {
    PB_ODR_bit.ODR4 = 1; //     +
    PB_DDR_bit.DDR4 = 1; //  
    PB_ODR_bit.ODR4 = 0; //     
    PB_DDR_bit.DDR4 = 0; //   
    while( ( button1_in == 0 ) || ( time_out-- ) ){tick++;}//   
    if(!time_out)
    {
      tick=0;
      flag.error_b1 = 1;
    }
  }
  if( number == 2 )
  {
    PB_ODR_bit.ODR5 = 1; //     +
    PB_DDR_bit.DDR5 = 1; //  
    PB_ODR_bit.ODR5 = 0; //     
    PB_DDR_bit.DDR5 = 0; //   
    while( !button2_in || time_out-- ){tick++;}//   
    if(!time_out)
    {
      tick=0;
      flag.error_b2 = 1;
    }
  } 
  return tick;
}

o.O , . . 0FF, .
http://s9.uploads.ru/t/W65hB.png

RA (2018-02-04 15:16:18)

37

RA while(!button1_in && (--time_out)) tick++;

MasterElectric (2018-02-04 19:29:36)

38

- .
- - .
...

39

... ...
... ...

40

dosikus ():

- .

. . .

HHIMERA ():

...

2 3- . :) , . , 1 . . - ? - ? , .
, . :) . HHIMERA , . ?

RA (2018-02-05 19:02:26)

41

RA , .

42

.http://sf.uploads.ru/t/dbief.png
? ?

RA (2018-02-10 00:37:23)

43

. . ... .
http://sh.uploads.ru/t/6Ha0h.png
. . - . . EN st1s10, usb .

RA (2018-02-18 15:09:32)

44

. 4- , . - .

45

:
#define led_blue   PA_ODR_bit.ODR3 //
#define led_green  PA_ODR_bit.ODR2 // 
#define led_red  PA_ODR_bit.ODR1 //
#define Vop 120 //  (1.2V*100) 
#define channal_op 6 //   1,2.   6 
#define channal_res 5 //  4,7.   3
#define out_min 300 // 3,0  *100
#define out_max 420 // 4,2  *100
#define width_ad 255 //   8 255 
#define max_l 400 //    .
:
#include "iostm8s103f3.h"
#include <stdbool.h>
#include "define.h"
#define button PD_IDR_bit.IDR3
// AFR0

unsigned int Vdd;
unsigned int l;
unsigned char tick;
bool state_button = 0;

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////   /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool press_button(){
  bool new_button;
  if(tick>130)new_button = 1;
  else new_button = 0;
  if(( new_button ) & ( state_button == 0 ))state_button = 1;
  else if(( new_button ) & ( state_button == 1 ))state_button = 0;
  return state_button;
}

int main( void )
{
////////////////////////////////////////////////////////////////////////////////
///////////////////////////   ///////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  CLK_SWCR_bit.SWEN = 0;//              
  CLK_CKDIVR_HSIDIV = 0;//     = 1.
  CLK_CKDIVR_CPUDIV = 0;//    = 1.      
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.1   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR1 = 1; //  
  PA_CR1_bit.C11 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C21 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.2   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR2 = 1; //  
  PA_CR1_bit.C12 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C22 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
//////////////////////////  A.3   //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR3 = 1; //  
  PA_CR1_bit.C13 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C23 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////   /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  ADC_CSR_bit.CH = channal_op;//
  ADC_CR3_bit.DBUF = 1;//   
  ADC_CR1_bit.CONT = 1;//    
  ADC_CR1_bit.SPSEL = 0;//   4
  ADC_CR2_bit.ALIGN =0;//     
  ADC_CR1_bit.ADON = 1;//      
  ADC_CSR_bit.EOCIE = 1;//     
  ADC_CR1_bit.ADON = 1;//   
   
////////////////////////////////////////////////////////////////////////////////
////////////   2.   - 1.   - 2 ////////////// 
////////////////////////////////////////////////////////////////////////////////  
  TIM2_ARRH = 255;
  TIM2_ARRL = 255; 
  TIM2_PSCR = 0;  
// 1  /   
  TIM2_CCMR1_OC1M = 6; //  
  TIM2_CCR1H = 0;//
  TIM2_CCR1L = 100;//
  TIM2_CCER1_CC1E = 1; 
//  
  TIM2_CCMR2_CC2S = 1;// 
  TIM2_CCER1_CC2P = 1;// 
  TIM2_CCER1_CC2E = 1;//  .
  TIM2_IER_CC2IE = 1;// 
//
  asm("nop");
  asm("nop");
  TIM2_SR1 = 0; 
  TIM2_CR1_bit.CEN = 1; // */
////////////////////////////////////////////////////////////////////////////////
  asm("RIM"); //  
  for(;;){
    led_green = state_button; 
  }
  return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////      ////////////////////
////////////////////////////////////////////////////////////////////////////////
#pragma vector = ADC1_EOC_vector
__interrupt void ADC1_EOC_handler(void)
{
  unsigned int buff;
  if(( ADC_CSR_bit.EOC == 1 ) && ( ADC_CSR_bit.EOCIE == 1 )){
    ADC_CSR_bit.EOC = 0;
    ADC_CR1_bit.ADON = 0;
    buff = ADC_DB0RH;
    buff = buff+ADC_DB1RH;
    buff = buff+ADC_DB2RH;
    buff = buff+ADC_DB3RH;
    buff = buff+ADC_DB4RH;
    buff = buff+ADC_DB5RH;
    buff = buff+ADC_DB6RH;
    buff = buff+ADC_DB7RH;
    buff = buff+ADC_DB8RH;
    buff = buff+ADC_DB9RH;
    buff = buff/10;
    switch( ADC_CSR_bit.CH ){
    case channal_op:
      Vdd = ( width_ad * Vop ) / buff;
      if( ( Vdd <= out_max ) && ( Vdd >= out_min ) ){
        led_red = 1;
      }
      else{
        led_red = 0;
      }
      ADC_CSR_bit.CH = channal_res;
      break;
    case channal_res:
      l=( (unsigned long) buff * max_l ) / width_ad;
      ADC_CSR_bit.CH = channal_op;
      break;
    }
    ADC_CR1_bit.ADON = 1;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_CAPCOM_CC2IF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
__interrupt void TIM2_CAPCOM_CC2IF_handler(void)
{
  if (( TIM2_SR1_bit.CC2IF == 1 ) && (TIM2_IER_bit.CC2IE == 1))
  {
    bool new_button;
    tick = TIM2_CCR2L;
    if(tick>130)new_button = 1;
    else new_button = 0;
    if(( new_button ) & ( state_button == 0 ))state_button = 1;
    else if(( new_button ) & ( state_button == 1 ))state_button = 0;
  }
}

46

http://s5.uploads.ru/t/yE9bZ.png

:
#define led_blue   PA_ODR_bit.ODR3 //
#define led_green  PA_ODR_bit.ODR2 // 
#define led_red  PA_ODR_bit.ODR1 //
#define Vop 120 //  (1.2V*100) 
#define channal_op 6 //   1,2.   6 
#define channal_res 5 //  4,7.   3
#define out_min 300 // 3,0  *100
#define out_max 420 // 4,2  *100
#define width_ad 255 //   8 255 
#define max_l 400 //    .
#define sensitivity 9
#define not_touch 100
:
#include "iostm8s103f3.h"
#include <stdbool.h>
#include "define.h"
#define button PD_IDR_bit.IDR3
// AFR0

unsigned int Vdd;
unsigned int l;
unsigned char n_touch=0;
bool state_button = 0;

int main( void )
{
////////////////////////////////////////////////////////////////////////////////
///////////////////////////   ///////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  CLK_SWCR_bit.SWEN = 0;//              
  CLK_CKDIVR_HSIDIV = 0;//     = 1.
  CLK_CKDIVR_CPUDIV = 0;//    = 1.      
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.1   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR1 = 1; //  
  PA_CR1_bit.C11 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C21 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.2   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR2 = 1; //  
  PA_CR1_bit.C12 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C22 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
//////////////////////////  A.3   //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR3 = 1; //  
  PA_CR1_bit.C13 = 1; //     +  - (PUSHPULL)
  PA_CR2_bit.C23 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////   /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  ADC_CSR_bit.CH = channal_op;//
  ADC_CR3_bit.DBUF = 1;//   
  ADC_CR1_bit.CONT = 1;//    
  ADC_CR1_bit.SPSEL = 0;//   4
  ADC_CR2_bit.ALIGN =0;//     
  ADC_CR1_bit.ADON = 1;//      
  ADC_CSR_bit.EOCIE = 1;//     
  ADC_CR1_bit.ADON = 1;//   
   
////////////////////////////////////////////////////////////////////////////////
////////////   2.   - 1.   - 2 ////////////// 
////////////////////////////////////////////////////////////////////////////////  
  TIM2_ARRH = 255;
  TIM2_ARRL = 255; 
  TIM2_PSCR = 0;  
// 1  /   
  TIM2_CCMR1_OC1M = 6; //  
  TIM2_CCR1H = 0;//
  TIM2_CCR1L = not_touch;//
  TIM2_CCER1_CC1E = 1; 
//  
  TIM2_CCMR2_CC2S = 1;// 
  TIM2_CCER1_CC2P = 1;// 
  TIM2_CCER1_CC2E = 1;//  .
  TIM1_EGR_bit.UG = 1;
  asm("nop");
  asm("nop");
  TIM2_SR1 = 0; 
  TIM2_IER_CC2IE = 1;// 
  TIM2_CR1_bit.CEN = 1; // */
////////////////////////////////////////////////////////////////////////////////
  asm("RIM"); //  
  for(;;){
    led_green = state_button; 
  }
  return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////      ////////////////////
////////////////////////////////////////////////////////////////////////////////
#pragma vector = ADC1_EOC_vector
__interrupt void ADC1_EOC_handler(void)
{
  unsigned int buff;
  if(( ADC_CSR_bit.EOC == 1 ) && ( ADC_CSR_bit.EOCIE == 1 )){
    ADC_CSR_bit.EOC = 0;
    ADC_CR1_bit.ADON = 0;
    buff = ADC_DB0RH;
    buff = buff+ADC_DB1RH;
    buff = buff+ADC_DB2RH;
    buff = buff+ADC_DB3RH;
    buff = buff+ADC_DB4RH;
    buff = buff+ADC_DB5RH;
    buff = buff+ADC_DB6RH;
    buff = buff+ADC_DB7RH;
    buff = buff+ADC_DB8RH;
    buff = buff+ADC_DB9RH;
    buff = buff/10;
    switch( ADC_CSR_bit.CH ){
    case channal_op:
      Vdd = ( width_ad * Vop ) / buff;
      if( ( Vdd <= out_max ) && ( Vdd >= out_min ) ){
        led_red = 1;
      }
      else{
        led_red = 0;
      }
      ADC_CSR_bit.CH = channal_res;
      break;
    case channal_res:
      l=( (unsigned long) buff * max_l ) / width_ad;
      ADC_CSR_bit.CH = channal_op;
      break;
    }
    ADC_CR1_bit.ADON = 1;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_CAPCOM_CC2IF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
 __interrupt void TIM2_CAPCOM_CC2IF_handler(void)
{
  if (( TIM2_SR1_bit.CC2IF == 1 ) && (TIM2_IER_bit.CC2IE == 1))
  {
   if(n_touch==100){
      switch(state_button){
    case 0 : state_button = 1;n_touch = 0;break;
    case 1 : state_button = 0;n_touch = 0;break;
      }
    }
    if( (TIM2_CCR2L > (not_touch + sensitivity))){
      ++n_touch;
    }
  }
}

n_touch R8. 100 - 20.

47

.

48

:
#define led_blue   PA_ODR_bit.ODR3 //
#define led_green  PA_ODR_bit.ODR2 // 
#define led_red  PA_ODR_bit.ODR1 //
#define beep_pin  PD_ODR_bit.ODR4 //
#define Vop 120 //  (1.2V*100) 
#define channal_op 6 //   1,2.   6 
#define channal_res 5 //  4,7.   3
#define out_min 300 // 3,0  *100
#define out_max 520 // 4,2  *100
#define width_ad 255 //   8 255 
#define max_l 400 //    .
#define sensitivity 18
#define not_touch 500
#define time_touch 800
#define tone 6800//63000
#define change_tone 200
:
#include "iostm8s103f3.h"
#include <stdbool.h>
#include "define.h"
// AFR0
unsigned int Vdd;
unsigned int l;
unsigned int n_touch=0;
bool state_button = 0;
unsigned int time;
unsigned int buff;

union type_tim1_pscr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_pscr;
union type_tim1_arr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_arr;
union type_tim1_ccr3{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_ccr3;
union type_tim1_ccr2{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_ccr2;
union type_tim2_pscr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_pscr;
union type_tim2_arr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_arr; 
union type_tim2_ccr3{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr3;
union type_tim2_ccr2{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr2;
union type_tim2_ccr1{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr1;
int main( void )
{
////////////////////////////////////////////////////////////////////////////////
///////////////////////////   ///////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  CLK_SWCR_bit.SWEN = 0;//              
  CLK_CKDIVR_HSIDIV = 0;//     = 1.
  CLK_CKDIVR_CPUDIV = 0;//    = 1. 
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  D.4 beep_pin /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PD_DDR_bit.DDR4 = 1; //  
  PD_CR1_bit.C14 = 1; //   PUSHPULL
  PD_CR2_bit.C24 = 0; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.1   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR1 = 1; //  
  PA_CR1_bit.C11 = 1; //   PUSHPULL
  PA_CR2_bit.C21 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.2   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR2 = 1; //  
  PA_CR1_bit.C12 = 1; //   PUSHPULL
  PA_CR2_bit.C22 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
//////////////////////////  A.3   //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR3 = 1; //  
  PA_CR1_bit.C13 = 1; //   PUSHPULL
  PA_CR2_bit.C23 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////   /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  ADC_CSR_bit.CH = channal_op;//
  ADC_CR3_bit.DBUF = 1;//   
  ADC_CR1_bit.CONT = 1;//    
  ADC_CR1_bit.SPSEL = 0;//   
  ADC_CR2_bit.ALIGN =0;//     
  ADC_CR1_bit.ADON = 1;//      
  ADC_CSR_bit.EOCIE = 1;//     
  ADC_CR1_bit.ADON = 1;//   
//////////////////////////////////////////////////////////////////////////////// 
/////  1.  -  1.  L->H  2.  H->L  3. ///////
//////////////////////     3/////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  tim1_arr.full = 50000;
  tim1_pscr.full = 15; 
////////////////////////////////////////////////////////////////////////////////  
  TIM1_ARRH = tim1_arr.h;
  TIM1_ARRL = tim1_arr.l;  
  TIM1_PSCRH = tim1_pscr.h;//
  TIM1_PSCRL = tim1_pscr.l; //
/*      ~50 . */ 
// 1.   
  TIM1_CCMR1_OC1M = 6; //  
  TIM1_CR1_OPM = 1;
//    
  TIM1_CCR1H = 0;//
  TIM1_CCR1L = 10;// 
  TIM1_CCER1_CC1E = 1; //    .
  // 2  /   .  .
  TIM1_CCMR2_CC2S = 1;//     ,    TI2
  TIM1_CCER1_CC2P = 0;//   
  TIM1_CCER1_CC2E = 1;//     
// 3 
  TIM1_CCMR3_CC3S = 1;//     ,    TI3
  TIM1_CCER2_CC3P = 1;//   
  TIM1_CCER2_CC3E = 1;//  .
  
  TIM1_IER_UIE = 1;// 
  TIM1_BKR_bit.MOE = 1;//Main output enable
  TIM1_EGR_bit.UG = 1;  // Update Event
  asm("nop");
  asm("nop");
  TIM1_SR1 = 0;
//   
  TIM1_CR1_bit.CEN = 1;   
////////////////////////////////////////////////////////////////////////////////
//////////   2.   - 1.   - 2 H->L ///////////
//////////////////////     2/////////////////////////// 
//////////////////////////////////////////////////////////////////////////////// 
  tim2_arr.full = tone; //6200
  TIM2_ARRH = tim2_arr.h;
  TIM2_ARRL = tim2_arr.l; 
  TIM2_PSCR = 0;
  TIM2_CR1_bit.ARPE = 1;  
// 1.   
  TIM2_CCMR1_OC1M = 6; //  
//    
  tim2_ccr1.full = not_touch;
  TIM2_CCR1H = tim2_ccr1.h;//
  TIM2_CCR1L = tim2_ccr1.l;//
  TIM2_CCER1_CC1E = 1; //    . 
// 2.     
  TIM2_CCMR2_CC2S = 1;// 
  TIM2_CCER1_CC2P = 1;//   H-LEVEL TO L-LEVEL, .
  TIM2_CCER1_CC2E = 1;//  .
// 3.   
  TIM2_CCMR3_OC3M = 6; //  
//    
  tim2_ccr3.full = ( tone / 2 ); //,   
  TIM2_CCR3H = tim2_ccr3.h;//
  TIM2_CCR3L = tim2_ccr3.l;//
  TIM2_CCER2_CC3E = 0; //     .
  TIM2_IER_bit.CC3IE = 1;
  TIM2_EGR_bit.UG = 1;  // Update Event
  asm("nop");
  asm("nop");
  TIM2_SR1 = 0;
  TIM2_IER_UIE = 1;//      
  TIM2_CR1_bit.CEN = 1; // */
  
  asm("RIM"); //  
  for(;;){
  led_green = state_button; //    LED
  }
  return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////      ////////////////////
////////////////////////////////////////////////////////////////////////////////
#pragma vector = ADC1_EOC_vector
__interrupt void ADC1_EOC_handler(void)
{
  unsigned int buff;
  if(( ADC_CSR_bit.EOC == 1 ) && ( ADC_CSR_bit.EOCIE == 1 )){
    ADC_CSR_bit.EOC = 0;
    ADC_CR1_bit.ADON = 0;
    buff = ADC_DB0RH;
    buff = buff+ADC_DB1RH;
    buff = buff+ADC_DB2RH;
    buff = buff+ADC_DB3RH;
    buff = buff+ADC_DB4RH;
    buff = buff+ADC_DB5RH;
    buff = buff+ADC_DB6RH;
    buff = buff+ADC_DB7RH;
    buff = buff+ADC_DB8RH;
    buff = buff+ADC_DB9RH;
    buff = buff/10;
    switch( ADC_CSR_bit.CH ){
    case channal_op:
      Vdd = ( width_ad * Vop ) / buff;
      if( ( Vdd <= out_max ) && ( Vdd >= out_min ) ){
        led_red = 1;
      }
      else{
        led_red = 0;
      }
      ADC_CSR_bit.CH = channal_res;
      break;
    case channal_res:
      l=( (unsigned long) buff * max_l ) / width_ad;
      ADC_CSR_bit.CH = channal_op;
      break;
    }
    ADC_CR1_bit.ADON = 1;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_OVR_UIF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
 __interrupt void TIM2_OVR_UIF_handler(void)
{
  if(( TIM2_SR1_bit.UIF == 1 ) && (TIM2_IER_bit.UIE == 1))
  {
    unsigned int buff1;
    tim2_ccr2.h = TIM2_CCR2H;
    tim2_ccr2.l = TIM2_CCR2L;
    buff1 = tim2_ccr2.full - not_touch;
    if( buff1 >= sensitivity )++n_touch;
    if( n_touch == time_touch ){
      n_touch = 0;
      switch(state_button){
    case 0 : state_button = 1;break;
    case 1 : state_button = 0;break;
      }
    }    
    if( state_button )beep_pin = 1;
    else beep_pin = 0;
    TIM2_SR1 = 0;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_CAPCOM_CC3IF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
 __interrupt void TIM2_CAPCOM_CC3IF_handler(void)
 {
   if(( TIM2_SR1_bit.CC3IF == 1 ) && (TIM2_IER_bit.CC3IE == 1))
   {
    beep_pin = 0;
    if( state_button ){
      if( tim2_arr.full <= ( tone + change_tone ) )++tim2_arr.full; //6200
      else tim2_arr.full = tone;
      TIM2_ARRH = tim2_arr.h;
      TIM2_ARRL = tim2_arr.l;
    }
    TIM2_SR1_bit.CC3IF = 0;
   }
 }
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM1_OVR_UIF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
__interrupt void TIM1_OVR_UIF_handler(void)
{
  if(( TIM1_SR1_bit.UIF == 1 ) && (TIM1_IER_bit.UIE == 1))
  { 
    tim1_ccr3.h = TIM1_CCR3H;
    tim1_ccr3.l = TIM1_CCR3L;
    tim1_ccr2.h = TIM1_CCR2H;
    tim1_ccr2.l = TIM1_CCR2L;
//        
    time = (tim1_ccr3.full - tim1_ccr2.full)/57;
    TIM1_SR1_bit.UIF = 0;
    TIM1_CR1_bit.CEN = 1;
  }
}

1 . 1 - (, . , , , ... ).2,3 , 2 l->h, 3 h->l, 3 .
2 . 1 - . 2 , 2 h->l 2 1, + ,   ( ). 3 , ( - - . ... :x ) .

49

http://s5.uploads.ru/t/eCcdL.png
http://sd.uploads.ru/t/X9DGL.png

http://sd.uploads.ru/t/k8Ho0.png

:
#include "iostm8s103f3.h"
#include <stdbool.h>
#include "define.h"
// AFR0
unsigned int Vdd;
unsigned int l;
unsigned int n_touch=0;
unsigned int time;
unsigned int buff;

union type_tim1_pscr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_pscr;
union type_tim1_arr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_arr;
union type_tim1_ccr3{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_ccr3;
union type_tim1_ccr2{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim1_ccr2;
union type_tim2_pscr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_pscr;
union type_tim2_arr{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_arr; 
union type_tim2_ccr3{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr3;
union type_tim2_ccr2{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr2;
union type_tim2_ccr1{
  unsigned int full;
  struct{
    unsigned int l : 8;
    unsigned int h : 8;
  };
} tim2_ccr1;
int main( void )
{
////////////////////////////////////////////////////////////////////////////////
///////////////////////////   ///////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  CLK_SWCR_bit.SWEN = 0;//              
  CLK_CKDIVR_HSIDIV = 0;//     = 1.
  CLK_CKDIVR_CPUDIV = 0;//    = 1. 
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  D.4 beep_pin /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PD_DDR_bit.DDR4 = 1; //  
  PD_CR1_bit.C14 = 1; //   PUSHPULL
  PD_CR2_bit.C24 = 0; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.1   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR1 = 1; //  
  PA_CR1_bit.C11 = 1; //   PUSHPULL
  PA_CR2_bit.C21 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
///////////////////////////  A.2   /////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR2 = 1; //  
  PA_CR1_bit.C12 = 1; //   PUSHPULL
  PA_CR2_bit.C22 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
//////////////////////////  A.3   //////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  PA_DDR_bit.DDR3 = 1; //  
  PA_CR1_bit.C13 = 1; //   PUSHPULL
  PA_CR2_bit.C23 = 1; // 10
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////   /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
  ADC_CSR_bit.CH = channal_op;//
  ADC_CR3_bit.DBUF = 1;//   
  ADC_CR1_bit.CONT = 1;//    
  ADC_CR1_bit.SPSEL = 0;//   
  ADC_CR2_bit.ALIGN =0;//     
  ADC_CR1_bit.ADON = 1;//      
  ADC_CSR_bit.EOCIE = 1;//     
  ADC_CR1_bit.ADON = 1;//   
//////////////////////////////////////////////////////////////////////////////// 
/////  1.  -  1.  L->H  2.  H->L  3. ///////
//////////////////////     3/////////////////////////// 
////////////////////////////////////////////////////////////////////////////////
  tim1_arr.full = 50000;//50000*1 == 50
  tim1_pscr.full = 15; // 16(16/16= 1)
////////////////////////////////////////////////////////////////////////////////  
  TIM1_ARRH = tim1_arr.h;
  TIM1_ARRL = tim1_arr.l;  
  TIM1_PSCRH = tim1_pscr.h;//
  TIM1_PSCRL = tim1_pscr.l; //
/*      ~50 . */ 
// 1.   
  TIM1_CCMR1_OC1M = 6; //  
  TIM1_CR1_OPM = 1;
//    
  TIM1_CCR1H = 0;//
  TIM1_CCR1L = 10;// 
  TIM1_CCER1_CC1E = 1; //    .
  // 2  /   .  .
  TIM1_CCMR2_CC2S = 1;//     ,    TI2
  TIM1_CCER1_CC2P = 0;//   
  TIM1_CCER1_CC2E = 1;//     
// 3 
  TIM1_CCMR3_CC3S = 1;//     ,    TI3
  TIM1_CCER2_CC3P = 1;//   
  TIM1_CCER2_CC3E = 1;//  .
  
  TIM1_IER_UIE = 1;// 
  TIM1_BKR_bit.MOE = 1;//Main output enable
  TIM1_EGR_bit.UG = 1;  // Update Event
  asm("nop");
  asm("nop");
  TIM1_SR1 = 0;
//   
  TIM1_CR1_bit.CEN = 1;   
////////////////////////////////////////////////////////////////////////////////
//////////   2.   - 1.   - 2 H->L ///////////
//////////////////////     2/////////////////////////// 
//////////////////////////////////////////////////////////////////////////////// 
  tim2_arr.full = tone; //6200
  TIM2_ARRH = tim2_arr.h;
  TIM2_ARRL = tim2_arr.l; 
  TIM2_PSCR = 0;
  TIM2_CR1_bit.ARPE = 1;  
// 1.   
  TIM2_CCMR1_OC1M = 6; //  
//    
  tim2_ccr1.full = not_touch;
  TIM2_CCR1H = tim2_ccr1.h;//
  TIM2_CCR1L = tim2_ccr1.l;//
  TIM2_CCER1_CC1E = 1; //    . 
// 2.     
  TIM2_CCMR2_CC2S = 1;// 
  TIM2_CCER1_CC2P = 1;//   H-LEVEL TO L-LEVEL, .
  TIM2_CCER1_CC2E = 1;//  .
// 3.   
  TIM2_CCMR3_OC3M = 6; //  
//    
  tim2_ccr3.full = ( tone / 3 ); //,   
  TIM2_CCR3H = tim2_ccr3.h;//
  TIM2_CCR3L = tim2_ccr3.l;//
  TIM2_CCER2_CC3E = 0; //    .
  TIM2_IER_bit.CC3IE = 1;
  TIM2_EGR_bit.UG = 1;  // Update Event
  asm("nop");
  asm("nop");
  TIM2_SR1 = 0;
  TIM2_IER_UIE = 1;//      
  TIM2_CR1_bit.CEN = 1; // */
  
  asm("RIM"); //  
  for(;;){
    
      led_green = TIM2_CCER2_CC3E; //    LED
  }
  return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////      ////////////////////
////////////////////////////////////////////////////////////////////////////////
#pragma vector = ADC1_EOC_vector
__interrupt void ADC1_EOC_handler(void)
{
  unsigned int buff;
  if(( ADC_CSR_bit.EOC == 1 ) && ( ADC_CSR_bit.EOCIE == 1 )){
    ADC_CSR_bit.EOC = 0;
    ADC_CR1_bit.ADON = 0;
    buff = ADC_DB0RH;
    buff = buff+ADC_DB1RH;
    buff = buff+ADC_DB2RH;
    buff = buff+ADC_DB3RH;
    buff = buff+ADC_DB4RH;
    buff = buff+ADC_DB5RH;
    buff = buff+ADC_DB6RH;
    buff = buff+ADC_DB7RH;
    buff = buff+ADC_DB8RH;
    buff = buff+ADC_DB9RH;
    buff = buff/10;
    switch( ADC_CSR_bit.CH ){
    case channal_op:
      Vdd = ( width_ad * Vop ) / buff;
      if( ( Vdd <= out_max ) && ( Vdd >= out_min ) ){
        led_red = 1;
      }
      else{
        led_red = 0;
      }
      ADC_CSR_bit.CH = channal_res;
      break;
    case channal_res:
      l=( (unsigned long) buff * max_l ) / width_ad;
      ADC_CSR_bit.CH = channal_op;
      break;
    }
    ADC_CR1_bit.ADON = 1;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_OVR_UIF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
 __interrupt void TIM2_OVR_UIF_handler(void)
{
  if(( TIM2_SR1_bit.UIF == 1 ) && (TIM2_IER_bit.UIE == 1))
  {
    unsigned int buff1;
    tim2_ccr2.h = TIM2_CCR2H;
    tim2_ccr2.l = TIM2_CCR2L;
    buff1 = tim2_ccr2.full - not_touch;
    if( buff1 >= sensitivity )++n_touch;
    if( n_touch == time_touch ){
      n_touch = 0;
      switch(TIM2_CCER2_CC3E){
    case 0 : TIM2_CCER2_CC3E = 1;break;
    case 1 : TIM2_CCER2_CC3E = 0;break;
      }
    }    
    TIM2_SR1 = 0;
  }
}
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM2_CAPCOM_CC3IF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
 __interrupt void TIM2_CAPCOM_CC3IF_handler(void)
 {
   if(( TIM2_SR1_bit.CC3IF == 1 ) && (TIM2_IER_bit.CC3IE == 1))
   {
    if( TIM2_CCER2_CC3E ){
      if( tim2_arr.full <= ( tone + change_tone ) )++tim2_arr.full; //6200
      else tim2_arr.full = tone;
      TIM2_ARRH = tim2_arr.h;
      TIM2_ARRL = tim2_arr.l;
    }
    TIM2_SR1_bit.CC3IF = 0;
   }
 }
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
#pragma vector = TIM1_OVR_UIF_vector
/*..............................................................................*/
/*..............................................................................*/
/*..............................................................................*/
__interrupt void TIM1_OVR_UIF_handler(void)
{
  if(( TIM1_SR1_bit.UIF == 1 ) && (TIM1_IER_bit.UIE == 1))
  { 
    tim1_ccr3.h = TIM1_CCR3H;
    tim1_ccr3.l = TIM1_CCR3L;
    tim1_ccr2.h = TIM1_CCR2H;
    tim1_ccr2.l = TIM1_CCR2L;
//        
    time = (tim1_ccr3.full - tim1_ccr2.full)/57;
    TIM1_SR1_bit.UIF = 0;
    TIM1_CR1_bit.CEN = 1;
  }
}

!!! . 32. f100rb 1- .
?
( - ) 10- . . - 4-, ... ... , . . . . . .

50

. 2 100 10 . . .

51


»  » RA »  1