Esempio n. 1
0
void Task1()
{
uint16_t cnt;
int8_t fd,val,chan;
uint16_t sample;


   printf( "Task1 PID=%d\r\n",nrk_get_pid());

  nrk_gpio_direction(NRK_BUTTON, NRK_PIN_INPUT);
  nrk_gpio_direction(NRK_DEBUG_0, NRK_PIN_OUTPUT);

  nrk_led_set(RED_LED);
  do{} while(nrk_gpio_get(NRK_BUTTON)==1);
  nrk_led_clr(RED_LED);
  nrk_led_set(GREEN_LED);

   // Initialize values here
   ADC_INIT ();
   ADC_ENABLE ();
   ADC_SET_CHANNEL (2);

  while(1) {
	ADC_SAMPLE_SINGLE();
	ADC_GET_SAMPLE_10(sample);
  	// Send sync byte
  	putchar(0x55);
	putchar(sample>>8);
	putchar(sample&0xff);
  	}
}
Esempio n. 2
0
uint16_t get_adc_val()
{
    uint16_t adc_val;
    ADC_SAMPLE_SINGLE();
    delay();
    ADC_GET_SAMPLE_10(adc_val);
    return adc_val;
}
Esempio n. 3
0
void audio_sample(uint8_t state,uint8_t opt,uint8_t *buff,uint8_t size)
{
  uint16_t sample;
  //ff_set_led(1);
  //DISABLE_GLOBAL_INT();
    ADC_SET_CHANNEL (MIC_PIN);  // could move into start_audio later...
    ADC_SAMPLE_SINGLE ();
  ADC_GET_SAMPLE_10 (sample);
  //sample = sample &0xFF;
  sample=sample>>2;
  sample=sample&0xFF;
  if(audio_cnt[audio_index]<MAX_AUDIO_BUF)
  {
	audio_buffers[audio_index][audio_cnt[audio_index]]=sample;
	audio_cnt[audio_index]++;
  }
  //nrk_event_signal(AUDIO_SIGNAL);
  //ENABLE_GLOBAL_INT();
  //ff_clr_led(1);
}
Esempio n. 4
0
/********************************************************************************************* 
* calc_power()
*
* This function is called at 1000Hz NRK_APP_TIMER_0.  It counts ticks starting from the first
* zero crossing point and updates the user power variables once per second (when ticks==1000).
**********************************************************************************************/ 
void calc_power()
{
    // FIXME: Disable interrupt later if power off to save energy

#ifdef CT_METER
    ADC_SET_CHANNEL (1);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(v);

    ADC_SET_CHANNEL (2);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c1);

    ADC_SET_CHANNEL (3);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c2);

    ADC_SET_CHANNEL (5);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(center_chan);
    c1_center=(uint16_t)center_chan;

#else
    if(socket_0_active==0) return;
    ADC_SET_CHANNEL (4);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(v);

    ADC_SET_CHANNEL (5);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c1);

    ADC_SET_CHANNEL (6);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c2);

    ADC_SET_CHANNEL (7);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(center_chan);
    c1_center=(uint16_t)(((uint32_t)center_chan*647)/1000);
#endif

// Catch the rising edge after the voltage dips below a low threshold
// Then ignore that case until the voltage goes up again.
// This filter is used to detect the zero crossing point
if(triggered==0 && v<VOLTAGE_LOW_THRESHOLD && v>v_last)
{
if(cycle_state==CYCLE_HIGH) cycle_state=CYCLE_LOW;
else {
        // Low to High transition
        cycle_state=CYCLE_HIGH;
        cycle_cnt++;
        if(energy_cycle<0) energy_cycle*=-1;
        energy_total+=energy_cycle;
        if(energy_cycle2<0) energy_cycle2*=-1;
        energy_total2+=energy_cycle2;
        energy_cycle=0;
        energy_cycle2=0;
        }
cycle_started=1;
triggered=1;
}
// Reset filter trap after the voltage goes up
if(v>VOLTAGE_LOW_THRESHOLD) triggered=0;
v_last=v;

    if(cycle_started==1)
        {
        ticks++;
        if(c1<c_p2p_low) c_p2p_low=c1;
        if(c1>c_p2p_high) c_p2p_high=c1;
        if(v<v_p2p_low) v_p2p_low=v;
        if(v>v_p2p_high) v_p2p_high=v;
        c1-=c1_center;

        if(c2<c_p2p_low2) c_p2p_low2=c2;
        if(c2>c_p2p_high2) c_p2p_high2=c2;
        c2-=c1_center;


        // remove noise at floor
        //if(c1<10) c1=0;

        current_total+=((int32_t)c1*(int32_t)c1);
        voltage_total+=((int32_t)v*(int32_t)v);
        if(cycle_state==CYCLE_LOW) v*=-1;
        energy_cycle+=((int32_t)c1*(int32_t)v);

        current_total2+=((int32_t)c2*(int32_t)c2);
        energy_cycle2+=((int32_t)c2*(int32_t)v);
        //printf( "c1=%u current=%lu\r\n",c1, current_total );
        if(ticks>=1000)
                        {

                        tmp_d=current_total / ticks;
                        if(tmp_d<0) tmp_d=0;
                        tmp_d=sqrt(tmp_d);
                        rms_current=(uint16_t)tmp_d;

                        tmp_d=current_total2 / ticks;
                        if(tmp_d<0) tmp_d=0;
                        tmp_d=sqrt(tmp_d);
                        rms_current2=(uint16_t)tmp_d;

                        tmp_d=voltage_total / ticks;
                        if(tmp_d<0) tmp_d=0;
                        tmp_d=sqrt(tmp_d);
                        rms_voltage=(uint16_t)tmp_d;

                        if(energy_total<0) energy_total=0;
                        true_power=energy_total / ticks;
                        if(true_power>300) cummulative_energy+=true_power;

                        if(energy_total2<0) energy_total2=0;
                        true_power2=energy_total2 / ticks;
                        if(true_power2>300) cummulative_energy2+=true_power2;

                        total_secs++;
                        // FIXME: divide by time
                        tmp_energy=cummulative_energy;
                        tmp_energy2=cummulative_energy2;
                        // Auto-center to acount for thermal drift
                        //c1_center=(c_p2p_high-c_p2p_low)/2;   
                        //v1_center=(v_p2p_high-v_p2p_low)/2;   
                        l_v_p2p_high=v_p2p_high;
                        l_v_p2p_low=v_p2p_low;
                        l_c_p2p_high=c_p2p_high;
                        l_c_p2p_low=c_p2p_low;

                        l_c_p2p_high2=c_p2p_high2;
                        l_c_p2p_low2=c_p2p_low2;
                        if(cycle_cnt==0) nrk_led_toggle(RED_LED);
                        else
                           cycle_avg=ticks/cycle_cnt;



                        freq=cycle_cnt;
                        ticks=0;
                        cycle_cnt=0;
                        voltage_total=0;
                        energy_total=0;
                        energy_total2=0;
                        current_total=0;
                        current_total2=0;
                        v_p2p_low=2000;
                        v_p2p_high=0;
                        c_p2p_low=2000;
                        c_p2p_high=0;
                        c_p2p_low2=2000;
                        c_p2p_high2=0;
                        }

        }

}
Esempio n. 5
0
void calc_power()
{
    // FIXME: Disable interrupt later if power off to save energy
//    if(socket_0_active==0 && socket_1_active==0) return;
 //  nrk_int_disable();

    ADC_SET_CHANNEL (VOLTAGE_CHAN);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(v);

    ADC_SET_CHANNEL (CURRENT_LOW);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c1);

    ADC_SET_CHANNEL (CURRENT_HIGH);
    nrk_spin_wait_us(ADC_SETUP_DELAY);
    ADC_SAMPLE_SINGLE();
    ADC_GET_SAMPLE_10(c2);

    //ADC_SET_CHANNEL (5);
    //nrk_spin_wait_us(ADC_SETUP_DELAY);
    //ADC_SAMPLE_SINGLE();
    //ADC_GET_SAMPLE_10(center_chan);
    //c1_center=(uint16_t)center_chan;

// Catch the rising edge after the voltage dips below a low threshold
// Then ignore that case until the voltage goes up again.
// This filter is used to detect the zero crossing point
//if(triggered==0 && v<VOLTAGE_LOW_THRESHOLD && v>v_last)
//if(v>VOLTAGE_ZERO_THRESHOLD && v_last<=VOLTAGE_ZERO_THRESHOLD)
if(v>v_center && v_last<=v_center)
{
//if(cycle_state==CYCLE_HIGH) cycle_state=CYCLE_LOW;
//else { 
	// Low to High transition
	cycle_state=CYCLE_HIGH; 
	cycle_cnt++; 
	if(energy_cycle<0) energy_cycle*=-1;
	energy_total+=energy_cycle;
	if(energy_cycle2<0) energy_cycle2*=-1;
	energy_total2+=energy_cycle2;
	energy_cycle=0;
	energy_cycle2=0;
//	}
cycle_started=1;
//triggered=1;
}

// Reset filter trap after the voltage goes up
//if(v>VOLTAGE_ZERO_THRESHOLD) triggered=0;

v_last=v;

    if(cycle_started==1) 
	{
	ticks++;
	if(c1<c_p2p_low) c_p2p_low=c1;
	if(c1>c_p2p_high) c_p2p_high=c1;
	if(v<v_p2p_low) v_p2p_low=v;
	if(v>v_p2p_high) v_p2p_high=v;
	c1-=c_center;

	if(c2<c_p2p_low2) c_p2p_low2=c2;
	if(c2>c_p2p_high2) c_p2p_high2=c2;
	c2-=c2_center;

        // Do we need this?	
	v-=v_center;
	// remove noise at floor
	//if(c1<10) c1=0;
	
	current_total+=((int32_t)c1*(int32_t)c1);
	voltage_total+=((int32_t)v*(int32_t)v);
	//if(cycle_state==CYCLE_LOW) v*=-1;
	energy_cycle+=((int32_t)c1*(int32_t)v);
	
	current_total2+=((int32_t)c2*(int32_t)c2);
	energy_cycle2+=((int32_t)c2*(int32_t)v);
	//printf( "c1=%u current=%lu\r\n",c1, current_total );

	
	if(ticks>=2000) 
	{

	// Save values to pass to event detector functions that calculate power for
	// packets etc
	freq=cycle_cnt;
	l_v_p2p_high=v_p2p_high;
  	l_v_p2p_low=v_p2p_low;
  	l_c_p2p_high=c_p2p_high;
  	l_c_p2p_low=c_p2p_low;
  	l_c_p2p_high2=c_p2p_high2;
  	l_c_p2p_low2=c_p2p_low2;
	ticks_last=ticks;
	current_total_last=current_total;
	current_total2_last=current_total2;
	energy_total_last=energy_total;
	energy_total2_last=energy_total2;
	voltage_total_last=voltage_total;


	// Reset values for next cycle	
  	ticks=0;
  	cycle_cnt=0;
  	voltage_total=0;
  	energy_total=0;
  	energy_total2=0;
  	current_total=0;
  	current_total2=0;
  	v_p2p_low=2000;
  	v_p2p_high=0;
  	c_p2p_low=2000;
  	c_p2p_high=0;
  	c_p2p_low2=2000;
  	c_p2p_high2=0;

	// Signal event detector task
	nrk_event_signal(update_energy_sig);
	}


	}

//nrk_int_enable();
}