#51 2016-09-29 10:01:05

NoName
Customer
З Київ
Зареєстрований: 2014-07-08
Повідомлень: 1,446

Re: Управление PWM в определенном алгоритме

ааа,  сбили с толку,
тот код что у вас   не будет ничего опрашивать у него задача  PWM выдать по графику
щас пришлю  другой вариант )
необходимо убрать   из кода,
// #define NO_EXT_KEY
но я кнопки не проверял, это сами

пасиб за мыло, заработался, забыл проверять  все папки )  писем - море  черное ) 


/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 00):
 * <devgate.info эт gmail.com> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer or device ( arduino-ua.com ) in return.   
 * Chingiz Sunlucid 
 * ----------------------------------------------------------------------------
 *  Date create: 2016.09.24  
 *  Date change: 2016.09.29 
 * ----------------------------------------------------------------------------
 */
 
// ARDUINO PIN  CONFIG
// for example
// Arduino leonardo

/*
Input and Output

Each of the 20 digital i/o pins on the Leonardo can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.
TWI: 2 (SDA) and 3 (SCL). Support TWI communication using the Wire library.
External Interrupts: 3 (interrupt 0), 2 (interrupt 1), 0 (interrupt 2), 1 (interrupt 3) and 7 (interrupt 4). These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.
PWM: 3, 5, 6, 9, 10, 11, and 13. Provide 8-bit PWM output with the analogWrite() function.
SPI: on the ICSP header. These pins support SPI communication using the SPI library. Note that the SPI pins are not connected to any of the digital I/O pins as they are on the Uno, They are only available on the ICSP connector. This means that if you have a shield that uses SPI, but does NOT have a 6-pin ICSP connector that connects to the Leonardo's 6-pin ICSP header, the shield will not work.
LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.
Analog Inputs: A0-A5, A6 - A11 (on digital pins 4, 6, 8, 9, 10, and 12). The Leonardo has 12 analog inputs, labeled A0 through A11, all of which can also be used as digital i/o. Pins A0-A5 appear in the same locations as on the Uno; inputs A6-A11 are on digital i/o pins 4, 6, 8, 9, 10, and 12 respectively. Each analog input provide 10 bits of resolution (i.e. 1024 different values). By default the analog inputs measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the analogReference() function.
There are a couple of other pins on the board:

AREF. Reference voltage for the analog inputs. Used with analogReference().
Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board.
*/

// potentiometer wiper (middle terminal) connected to analog pin
//int input_array_pin[] = { A0,A1,A2,A3,A4 };
const int input_array_pin[] = { 4,6,8,9,10 };
const int out_pwm_pin        = 3;
const int digi_start_pin      = 12;
const int LED_PIN_DEMO      = 13;  /* use PWM, for control */



#define DEBUG
#define DEBUG_PWM
// #define NO_EXT_KEY
#define DEBOUNCE_TIME 100
#define TASK_PROCESS_TIME 10


/*
 DEBUG          -    view status exe system  >>  debug uart   
 DEBUG_PWM        -    view change PWM  >>  debug uart   
 NO_EXT_KEY       -    no use user keybord,   for check system 
 DEBOUNCE_TIME 100
 TASK_PROCESS_TIME 10 -    10 ms programm scheduler  
*/

// 8 bit PWM
#define MAX_RESOLUTION_PWM  0xFF
// 16 bit PWM
// #define MAX_RESOLUTION_PWM  0xFFFF

 
typedef enum { // enum name task 
ST_WAIT         = 0,
ST_TIME_R2      = 1,
ST_TIME_R3      = 2,
ST_PWM_R4       = 3,
ST_TIME_R5      = 4,
}TE_STATE;

typedef enum {
TIMER_EXE       = 0,
TIMER_DISABLED  = 1,
TIMER_RUN       = 2,
}TE_TIMER;

 

typedef enum { // noedit, name ( mem data sarray )
R1 = 0,
R2 = 1,
R3 = 2,
R4 = 3,
R5 = 4,
} TE_IO_NAME;


#define BIT(x)        (1 << (x))
#define CHECKBIT(x,b)   (x&(b))
#define SETBIT(x,b)     x|=(b)
#define CLEARBIT(x,b)   x&=~(b)
#define TOGGLEBIT(x,b)  x^=(b)
#define count_element_array(x)  sizeof(x)/sizeof(x[0])


char debug_str[100];

 
// uncomment for disable debug info, or delete  line #define DEBUG
// #undef DEBUG

typedef struct {
 unsigned long    time_unit[5];  
 unsigned short   pwm_unit [5];  
 unsigned short   current_pwm;   // for 16bit pwm chip
 unsigned long    time_start_key_press;
 unsigned long    time_start_key_un_press;
 uint16_t       key_status;
 TE_STATE     state;  
 } td_system; 

typedef struct { // float not optimal, easy code
 float      pwm_start;  
 float      pwm_end;  
 float      pwm_delta;  
 float      pwm_now;  
 unsigned long  count_step; 
} td_temp_falling; 

 td_system    system_demo_pwm; 
 td_temp_falling  temp_falling; 
 
 unsigned long get_value_from_percent ( unsigned long percent, unsigned long  max_value );
unsigned long get_percent_from_value ( unsigned long xxx, unsigned long  max_value );
void set_new_pwm  (  unsigned short data );
void prepare_new_state  ( TE_STATE new_state );
void decode_input_data ( void );
void manual_stop_system ( void );
TE_TIMER programm_count_down_uint16 ( unsigned short *timer , unsigned char shift );
TE_TIMER programm_count_down_uint32 ( unsigned long *timer , unsigned long shift );
TE_TIMER system_delay_uint32 ( unsigned long *timer , unsigned long shift );
void key_function ( void );
void event_task();
void get_analog_config_system (void);

 
// R1 - value PWM1                ( pwm_unit[0] )
// R2 - state1  time  ( 0..10 sec ), PWM1   ( pwm_unit[1] )
// R3 - state2  time  ( 0..10 sec )     ( pwm_unit[2] )
// R4 - state3  value PWM2          ( pwm_unit[3] )
// R5 - state4 time  ( 0..10 sec )      ( pwm_unit[4] )
//---------------------------------------------------
int in_r_value[count_element_array(input_array_pin)];           // variable to store the value read
//---------------------------------------------------
// funtion provide hw setup system demo pwm.
void setup()
{
 int foo;
 system_demo_pwm.time_start_key_press    = DEBOUNCE_TIME;
 system_demo_pwm.time_start_key_un_press  = DEBOUNCE_TIME;
 
 for ( foo = 0; foo < count_element_array(input_array_pin); foo ++ )
 {
  // set mode pin input 
 }

  pinMode     (out_pwm_pin, OUTPUT);   // sets the pin as output
  pinMode     (LED_PIN_DEMO, OUTPUT);   // sets the pin as output
 pinMode     (digi_start_pin, INPUT);   // sets the pin as input
 Serial.begin(9600);          //  setup serial
 
 prepare_new_state ( ST_WAIT );

}

  
unsigned long get_value_from_percent ( unsigned long percent, unsigned long  max_value )
{
  unsigned long  ret;
  // max_value - 100 %
  //  x      percent
  ret = (unsigned long)((unsigned long)max_value*(unsigned long)percent)/(unsigned long)100;
  return  (ret);
}

unsigned long get_percent_from_value ( unsigned long xxx, unsigned long  max_value )
{
  unsigned long  ret;
  // max_value - 100 %
  //  xxx     percent
  ret = (unsigned long)((unsigned long)xxx*(unsigned long)100/(unsigned long)max_value);
  return  (ret);
}
 

void set_new_pwm  (  unsigned short data )
{
#ifdef DEBUG_PWM
 static  unsigned short old_pwm = 0xFFFF;
 
  if ( data != old_pwm )
  {
   sprintf  ( debug_str,"pwm: %d " ,data ); 
   Serial.println  ( debug_str ); 
   old_pwm = data;
  }
#endif
  
  system_demo_pwm.current_pwm      = data;
  analogWrite ( out_pwm_pin,  data );
#ifdef DEBUG 
  analogWrite ( LED_PIN_DEMO,   data );
#endif
}
 
void prepare_new_state  ( TE_STATE new_state )
{

 #ifdef DEBUG 
  Serial.print("set new state "); Serial.println(new_state);
 #endif

 // system_demo_pwm.pwm_unit  [R1];
 // system_demo_pwm.time_unit [R2];
 // system_demo_pwm.time_unit [R3];
 // system_demo_pwm.pwm_unit  [R4];
 // system_demo_pwm.time_unit [R5];
 switch ( new_state )
 {
  case  ST_WAIT : // disable all 
     set_new_pwm ( 0 );
  break;
  
  case  ST_TIME_R2 : // start 
     set_new_pwm          ( system_demo_pwm.pwm_unit[R1] );
     temp_falling.pwm_start     = system_demo_pwm.pwm_unit[R1];
     temp_falling.pwm_end       = system_demo_pwm.pwm_unit[R1];  
     temp_falling.pwm_delta     = 0;
     break;
   
  case  ST_TIME_R3 : // falling 
     set_new_pwm          ( system_demo_pwm.pwm_unit[R1] );
     temp_falling.pwm_now     = system_demo_pwm.current_pwm;
     temp_falling.count_step    = system_demo_pwm.time_unit[R3] / TASK_PROCESS_TIME; 
     temp_falling.pwm_start     = system_demo_pwm.pwm_unit[R1];
     temp_falling.pwm_end       = system_demo_pwm.pwm_unit[R4];
     temp_falling.pwm_delta     = (temp_falling.pwm_start - temp_falling.pwm_end ) / (float)temp_falling.count_step;
  break;  

  case  ST_PWM_R4: // line 
    set_new_pwm           ( system_demo_pwm.pwm_unit[R4] );
  break;  
  
  case ST_TIME_R5:
    set_new_pwm           ( system_demo_pwm.current_pwm );
    temp_falling.pwm_now    = system_demo_pwm.current_pwm;
  temp_falling.count_step     = system_demo_pwm.time_unit[R5] / TASK_PROCESS_TIME; 
    temp_falling.pwm_start      = system_demo_pwm.current_pwm;
    temp_falling.pwm_end      = 0;
    temp_falling.pwm_delta      = temp_falling.pwm_start / (float)temp_falling.count_step;
  break;  
  } 
  system_demo_pwm.state = new_state;
 }

//-------------------------------------------------------

void decode_input_data ( void )
{ 
 int foo;

//  R1 (0-1023) value PWM1 
 unsigned long  ui_temp;
#define max_analog_digi_value ( 0x3FF )  
 
 
#ifdef NO_EXT_KEY
in_r_value[R1] = max_analog_digi_value;
in_r_value[R2] = 512;
in_r_value[R3] = 128;
in_r_value[R4] = 512;
in_r_value[R5] = 512;
#endif 

 
 // get pwm1
 ui_temp      = get_percent_from_value( in_r_value[R1],max_analog_digi_value );
 system_demo_pwm.pwm_unit[R1] = get_value_from_percent( ui_temp, MAX_RESOLUTION_PWM        );
 
 // get time1   R2
 ui_temp      = get_percent_from_value( in_r_value[R2],max_analog_digi_value );
 system_demo_pwm.time_unit[R2] = get_value_from_percent( ui_temp,10000     ); // 10 sec
 
 // get time2   R3
 ui_temp      = get_percent_from_value( in_r_value[R3],max_analog_digi_value );
 system_demo_pwm.time_unit[R3] = get_value_from_percent( ui_temp,10000     ); // 10 sec

  // disable incorrect time setting  
 if ( (system_demo_pwm.time_unit[R2] + system_demo_pwm.time_unit[R3]) > 10000 )
 system_demo_pwm.time_unit[R3] = system_demo_pwm.time_unit[R2] / 2;
 //-----------------------

 // get pwm R4
 ui_temp      = get_percent_from_value( in_r_value[R4],max_analog_digi_value );
 system_demo_pwm.pwm_unit[R4] = get_value_from_percent( ui_temp,MAX_RESOLUTION_PWM   );

 // get time   R5
 ui_temp      = get_percent_from_value( in_r_value[R5],max_analog_digi_value );
 system_demo_pwm.time_unit[R5] = get_value_from_percent( ui_temp,10000     ); // 10 sec

#ifdef DEBUG
for ( foo = 0; foo < 5; foo ++ )
{
 switch ( foo )
 {
 case R2:
 case R3:
 case R5:
  // Serial.print("time_unit[");  Serial.print(foo); Serial.print("] = ");
  // Serial.println( system_demo_pwm.time_unit[foo]);
  sprintf  ( debug_str,"time_unit[%d] = %d " ,foo, system_demo_pwm.time_unit[foo] ); 
  Serial.println  ( debug_str ); 
 break;
 
 default:
  sprintf  ( debug_str,"pwm_unit[%d] = %d " ,foo, system_demo_pwm.pwm_unit[foo] ); 
  Serial.println  ( debug_str ); 
 break;
 }
}
#endif 
}

void manual_stop_system ( void )
{

  prepare_new_state (ST_TIME_R5);

#ifdef DEBUG
  int foo = ST_TIME_R5;
  sprintf  ( debug_str,"time_unit[%d] = %d " ,foo, system_demo_pwm.time_unit[foo] ); 
  Serial.println  ( debug_str ); 
  sprintf  ( debug_str,"pwm_unit[%d] = %d " ,foo, system_demo_pwm.pwm_unit[foo] ); 
  Serial.println  ( debug_str ); 
#endif 

}


TE_TIMER programm_count_down_uint16 ( unsigned short *timer , unsigned char shift )
{
    if (*timer > 0 )
    {
      if (*timer > shift )
      {
        *timer -= shift;
        return TIMER_RUN;
      }
       else
      {
         *timer = 0;
         return TIMER_EXE;
      }
    }
  return TIMER_DISABLED;
}

TE_TIMER programm_count_down_uint32 ( unsigned long *timer , unsigned long shift )
{
    if (*timer > 0 )
    {
      if (*timer > shift )
      {
        *timer -= shift;
        return TIMER_RUN;
      }
       else
      {
         *timer = 0;
         return TIMER_EXE;
      }
    }
  return TIMER_DISABLED;
}


TE_TIMER system_delay_uint32 ( unsigned long *timer , unsigned long shift )
{
    if ( *timer > 0 )
    {
      if (*timer > shift )
      {
        *timer -= shift;
        return TIMER_RUN;
      }
       else
      { // save delta time
         *timer =  ( shift - *timer );
         return TIMER_EXE;
      }
    }
  return TIMER_DISABLED;
}


void key_function ( void )
{
 #ifndef NO_EXT_KEY
 
   TE_TIMER  status; 
   
  if ( digitalRead(digi_start_pin))  
  {
   status = programm_count_down_uint32 (&system_demo_pwm.time_start_key_press, TASK_PROCESS_TIME);
   switch ( status )
   {
    case TIMER_EXE:
    case TIMER_DISABLED:
    system_demo_pwm.key_status = 1;
    break;
   }
  } else {
   
   system_demo_pwm.time_start_key_press = DEBOUNCE_TIME;
   status = programm_count_down_uint32 (&system_demo_pwm.time_start_key_un_press, TASK_PROCESS_TIME);

   switch ( status )
   {
    case TIMER_EXE:
      case TIMER_DISABLED:
    system_demo_pwm.key_status = 0;
    break;
   }
  }
  #endif
}
 
 
void event_task()
{
   TE_TIMER  status; 
   key_function ();
   
   if ( system_demo_pwm.state == ST_WAIT  ) return;
   
   switch  ( system_demo_pwm.state )
   {
   //----------------------------------------
   case ST_TIME_R2:  
   
//     sprintf  ( debug_str,"time [%d]" , system_demo_pwm.time_unit[R2] ); 
//     Serial.println  ( debug_str ); 

   status = programm_count_down_uint32 (&system_demo_pwm.time_unit[R2], TASK_PROCESS_TIME);
   switch ( status )
   {
    case TIMER_EXE:
    case TIMER_DISABLED:
      prepare_new_state(ST_TIME_R3);
    break;
    case TIMER_RUN:
     break;   
   }
   break;  
     //----------------------------------------
   case ST_TIME_R3: 
   status = programm_count_down_uint32 (&temp_falling.count_step, 1);
   switch ( status )
   {
    case TIMER_EXE:
    case TIMER_DISABLED:
      prepare_new_state(ST_PWM_R4);
    break;
    
    case TIMER_RUN:
    if ( temp_falling.pwm_now > temp_falling.pwm_delta )
    {
     temp_falling.pwm_now -= temp_falling.pwm_delta;
    } else temp_falling.pwm_now = 0; 
   set_new_pwm ( temp_falling.pwm_now );
     break;   
   }
   break;  
   //----------------------------------------
   case ST_PWM_R4: 
      // analogWrite(out_pwm_pin, system_demo_pwm.pwm_unit[R4])
   break;  
   //----------------------------------------
   case ST_TIME_R5: 
  
  status = programm_count_down_uint32 (&temp_falling.count_step, 1);
   switch ( status )
   {
    case TIMER_EXE:
    case TIMER_DISABLED:
      prepare_new_state(ST_WAIT);
    break;
    
    case TIMER_RUN:
    if ( temp_falling.pwm_now > temp_falling.pwm_delta )
    {
     temp_falling.pwm_now -= temp_falling.pwm_delta;
    } else temp_falling.pwm_now = 0; 
  set_new_pwm ( temp_falling.pwm_now );
     break;   
   }
  break;  
  //----------------------------------------
   }
 }

void get_analog_config_system (void)
{
 int foo;
 for ( foo = 0; foo < count_element_array(input_array_pin); foo ++ )
 { 
  in_r_value[foo] = analogRead ( input_array_pin[foo] );    // read the input pin
 }
}


void loop()
{
 static unsigned long  task_time; 
 static unsigned long  key_time_start_press; 
 static unsigned long  key_time_start_unpress; 
 
 TE_TIMER  status; 


 static unsigned long  hw_old_system_timer = 0; 
        unsigned long  hw_current_system_timer = 0; 

 unsigned long  delta_task_timer; 


 if (  system_demo_pwm.state  == ST_WAIT ) 
 {
    if ( system_demo_pwm.key_status ) 
    {
     get_analog_config_system();
     decode_input_data ( );
     prepare_new_state (1);
    }
 }
 else 
 {
   if (  system_demo_pwm.state != ST_TIME_R5 ) 
   if ( !system_demo_pwm.key_status )  manual_stop_system();
 }

 hw_current_system_timer  = millis();
 delta_task_timer         = hw_current_system_timer - hw_old_system_timer;
 hw_old_system_timer      = hw_current_system_timer;

 status = system_delay_uint32 ( &task_time, delta_task_timer );
 switch ( status )
 {
    case TIMER_EXE:
    case TIMER_DISABLED:
        task_time += TASK_PROCESS_TIME; 
        event_task();
        // Serial.println("exe");
    break;
    
    case TIMER_RUN:
     break;   
 }
 
 // - - - - - - - - - - -
#ifdef NO_EXT_KEY
 if (  system_demo_pwm.state  == ST_WAIT ) 
 if ( system_demo_pwm.key_status == 0 )
  {
  // Serial.println(" key 0");
 status = programm_count_down_uint32 ( &key_time_start_press, delta_task_timer );
 switch ( status )
 {
    case TIMER_EXE:
    case TIMER_DISABLED:
    system_demo_pwm.key_status = 1;
    key_time_start_unpress = 10000;
    Serial.println("set key start");
    break;
    
    case TIMER_RUN:
     break;   
 }
 }


 if (  system_demo_pwm.state == ST_PWM_R4 ) 
 if ( system_demo_pwm.key_status == 1 )
 {
 status = programm_count_down_uint32 ( &key_time_start_unpress, delta_task_timer );
 switch ( status )
 {
    case TIMER_EXE:
    case TIMER_DISABLED:
     system_demo_pwm.key_status = 0;
     key_time_start_press = 5000;
     Serial.println("set key end");
    break;
    
    case TIMER_RUN:
     break;   
  }
  }
  

#endif

 

}

Неактивний

Швидке повідомлення

Введіть повідомлення і натисніть Надіслати

Підвал форуму