Пример #1
0
/**-----------------------------------------------------------------------------------------------*/
_U16 Adc_u16Read(_U08 u8Channel)
{
    if(u8Channel < 13)
    {
        if(u8Channel < 8)
        {/*canales del 0 al 7*/
            CLEAR_8BIT(ANCON0, u8Channel);
        }
        else
        {/*canales del 8 al 12*/
            CLEAR_8BIT(ANCON1, u8Channel - 8);
        }
        ADC_SET_CHANNEL(u8Channel);
        ADC_START();
        while(ADC_FLAG()==1);
        if(gu8Flags == ADC_10BITS)
        {
            return ADC_BUFFER();
        }
        else
        {
            return ADRESH;
        }
    }
    return 0;
}
Пример #2
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);
  	}
}
Пример #3
0
void init_adc()
{
// Initialize values here
    DDRA = 0x80;
    ADC_INIT ();
    ADC_ENABLE ();
    ADC_SET_CHANNEL (0);
}
Пример #4
0
static void adc_start(void)
{
	struct request_t *req = request_dequeue();
	if (req) {
		ADC_SET_CHANNEL(req->channel);
		free(req);
		ADCSRA |= _BV(ADSC);
	}
}
Пример #5
0
void audio_start()
{
    //TIFR = TIFR | BM (OCF0);      // Clear interrupt flag
    //TIMSK = TIMSK | BM (OCIE0);       
    //TCCR0 = BM (WGM01) | BM (CS01);    
    //OCR0 = 248;   //4Khz
    //OCR0 = 124;   // 8Khz
    //TIMSK = TIMSK | BM (OCIE0);  
    
    ADC_SET_CHANNEL (MIC_PIN);  // could move into start_audio later...
    ADC_SAMPLE_SINGLE ();
    printf( "Audio Started\r" );
}
Пример #6
0
void
audio_init()
{
uint8_t i;
// Initialize values here
  DDRA = 0x80;
  ADC_INIT ();
  ADC_ENABLE ();
  ADC_SET_CHANNEL (MIC_PIN);
  audio_index=0;  // set index to 0
  for(i=0; i<AUDIO_BUFS; i++ )
  {
	audio_cnt[i]=0;  // clear buffer counts
  }
  printf( "Audio init\r" );
}
Пример #7
0
// This function  used to initialize the onchip ADC for interrupt mode.
void initOnChipADC()
{
    ADCSRA = BM(ADPS0) | BM(ADPS1) | BM(ADIE); // Enabling the interrupt as well.
    ADMUX = BM(REFS0);  // we are setting the channel to zero initially.

    // enable the ADC now.
    ADC_ENABLE();

    // Delay.
    nrk_spin_wait_us(ADC_SETUP_DELAY);

    ADC_SET_CHANNEL(ACCEL_CHANEL_Z);

    // start the ADC conversion;
    ADCSRA |= BM(ADSC);
}
Пример #8
0
void adc_request(uint8_t channel)
{
	if (!requests && !ADC_BUSY()) {
		ADC_SET_CHANNEL(channel);
		ADCSRA |= _BV(ADSC);
		return;
	}
	struct request_t **p = &requests;
	struct request_t *s = malloc(sizeof(struct request_t));
	s->next = 0;
	s->channel = channel;
	while (*p)
		p = &(*p)->next;
	*p = s;
	if (!ADC_BUSY())
		adc_start();
}
Пример #9
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);
}
Пример #10
0
void init_adc(void)
{
  adcs = NULL;
  next_adc_to_consider = NULL;
  sample_buffer_head = 0;
  sample_buffer_count = 2;
  uint8_t i;
  for (i = 0; i < SAMPLE_BUFFER_SIZE; ++i) {
    sample_buffer[i] = NULL;
  }

  ADC_SET_VREF(AREF);
  ADC_SET_ADJUST(RIGHT);
  ADC_SET_AUTO_TRIGGER_SRC(ADC_TRIGGER_FREERUNNING);
  ADC_AUTO_TRIGGER_ENABLE();
  ADC_SET_PRESCALER_DIV(64);
  ADC_SET_CHANNEL(ADC_CHANNEL_GND);
  ADC_CC_INTERRUPT_ENABLE();
  ADC_ENABLE();
  ADC_START_CONVERSION();

  process_start(&adc_process);
}
Пример #11
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;
                        }

        }

}
Пример #12
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();
}