0
votes

I am looking how to develop function similar of function millis of arduino in ForeC language(is a special language and use same syntax of C). It doesn't support time.h library, so i find this method to develop function millis:

int clock;
int millis(){
while(1){
 clock++;
 return clo
}
}

but when i call this function in this function:

void drive_buzzer()
{

  typedef enum { e_sys_wait_buz, e_play_buz, e_end_buz, e_silence_buz} buz_state_t;
  static unsigned char buz_state = e_sys_wait_buz;
  static unsigned long timer = 0;


 switch ( buz_state ) {
  // Waiting for a sound to emit...
  case e_sys_wait_buz:
    if ( g_snd.snd_evt )
    {
      g_snd.repeat--;
      timer = millis()+g_snd.duration;
      //tone(BUZZER_PIN,g_snd.freq);  
     // pwm(g_snd.freq,0.5);
      printf("%s\n","bbbbbbbbbbbbbbbbbbbbbbbbbbbbeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeepppp" );   
      buz_state = e_play_buz;
      printf("%d\n",millis() );
      printf("%ld\n",timer );
    }
    break;

  // Play the sound
  case e_play_buz:

      if ( millis() > timer ) {
        //noTone(BUZZER_PIN);
        //printf("%s\n","e_play_buz" );   
        buz_state = e_end_buz;
      }
    break;

  // Stop the sound
  case e_end_buz:
    if ( g_snd.repeat != 0 ) { 
        g_snd.repeat--;
        //printf("%s\n","e_end_buz" );  
        timer = millis()+g_snd.interval;
        buz_state = e_silence_buz;
    }
    else
      buz_state = e_sys_wait_buz;
    break;

  // Silence interval between two sounds
  case e_silence_buz:
    if ( millis() > timer ) {
      //tone(BUZZER_PIN,g_snd.freq); 
      //printf("%s\n","e_silence_buz" );   
      timer = millis()+g_snd.duration;
      buz_state = e_play_buz;
    }
    break;
  default:
    break;
 }

}

It works only with the first case :( Any idea or useful links how can i simulate the behavior of clock(without using any library) Thanks!

1
This while(1){ clock++; return clo } looks awfully like a useless loop. And a syntax error or undefined variable.Yunnosch
Your millis() simply increments a global variable by 1 and returns it (probably). That has nothing to do with any timing.datafiddler

1 Answers

0
votes

You need to implement a timer interrupt which regularly updates your variable clock. Your function millis() then just simply shall return the value of clock. The read of clock from within millis must be atomic.