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); } }
int8 Adc_initialize(uint32 clk, Adc_Pin pin, Adc_BurstMode burstMode) { ADC_ENABLE_POWER(); // Power on the ADC ADC_SET_CORE_CLK(); // Set the ADC core clock ADC_CLEAR_PINSEL(); // clear pin selection bits switch (pin) { case 0: ADC_ENABLE_PIN0(); // Enable pin 0 ADC_SET_PIN(0); // set sample pin break; case 1: ADC_ENABLE_PIN1(); //Enable pin 1 ADC_SET_PIN(1); // set sample pin break; case 2: ADC_ENABLE_PIN2(); //Enable pin 2 ADC_SET_PIN(2); // set sample pin break; case 3: ADC_ENABLE_PIN3(); //Enable pin 3 ADC_SET_PIN(3); // set sample pin break; case 4: ADC_ENABLE_PIN4(); //Enable pin 4 ADC_SET_PIN(4); // set sample pin break; case 5: ADC_ENABLE_PIN5(); //Enable pin 5 ADC_SET_PIN(5); // set sample pin break; case 6: ADC_ENABLE_PIN6(); //Enable pin 6 ADC_SET_PIN(6); // set sample pin break; case 7: ADC_ENABLE_PIN7(); //Enable pin 7 ADC_SET_PIN(7); // set sample pin break; default: break; } adcPin = pin; ADC_SET_CLK(clk); // Set ADC clock ADC_CONFIGURE(); // Configure ADC specific settings ADC_ENABLE(); // Enable the ADC adcBurstEnabled = burstMode; if (burstMode == Adc_BurstMode_Enabled) { ADC_SET_BURSTMODE(); } ADC_ENABLE_IRQ(); // Enable ADC interrupt if (burstMode == Adc_BurstMode_Enabled) { ADC_SET_ALL_IRQS_BURST(); // Enable all interrupts } else { ADC_SET_ALL_IRQS(); // Enable all interrupts } return 0; }
void power_init () { ADC_INIT (); ADC_ENABLE (); ADC_VREF_VCC(); nrk_timer_int_configure( NRK_APP_TIMER_0, 1, 7373, calc_power); ticks=0; cycle_state=CYCLE_HIGH; //cycle_state_last=CYCLE_UNKNOWN; cycle_cnt=0; cycle_avg=0; cycle_started=0; c1_center=496; v_p2p_low=2000; v_p2p_high=0; c_p2p_low=2000; c_p2p_high=0; c_p2p_low2=2000; c_p2p_high2=0; rms_current=0; rms_current2=0; rms_voltage=0; energy_total2=0; energy_total=0; energy_cycle2=0; energy_cycle=0; cummulative_energy2=0; cummulative_energy=0; total_secs=0; v_last=VOLTAGE_LOW_THRESHOLD+10; triggered=0; nrk_gpio_direction(NRK_DEBUG_2, NRK_PIN_OUTPUT); //startup_sock_state=nrk_eeprom_read_byte(0x100); //if((startup_sock_state&0x01)==0x01) // { nrk_timer_int_start(NRK_APP_TIMER_0); power_mon_enable(); socket_0_enable(); socket_0_active=1; // } //else { //socket_0_active=0; //power_mon_disable(); // nrk_kprintf( PSTR("Socket inactive\r\n")); //} //if((startup_sock_state&0x02)==0x02) // { socket_1_enable(); socket_1_active=1; // } // else { // socket_1_active=0; // } }
void init_adc() { // Initialize values here DDRA = 0x80; ADC_INIT (); ADC_ENABLE (); ADC_SET_CHANNEL (0); }
/************************************************************************* Function: ADC_init Purpose: set up the ADC to be used in IR sensor **************************************************************************/ void ADC_init(void) { SENSOR_IR_DDR &= ~_BV(SENSOR_IR_BIT); //ADC in 10bits : used in IR sensor routine : without shift left or right ADC_REFERENCE_AREF(); //ADC reference in 5V ADC_CLOCK_PRESCALER_128(); /* the original ADC frequency of this project was 125KHz (Prescaler = 128), thus, I changed it to sample faster, in 1MHz (Prescaler16) * I have some loss in precision, working in 10 bits with a frequency bigger than 200KHz, but in this case this do not matters * */ ADC_ENABLE(); ADC_SELECT_CHANNEL_2(); ADC_DIGITAL_INPUT_2_DISABLE(); ADC_START_CONVERSION(); //I discard the first sample, which takes 25 clock cycles ADC_WAIT_CONVERSION_FINISH(); }
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" ); }
// 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); }
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); }
int main(void) { /* Set A7 as an output. (Needed for PWM.) */ DDRA |= _BV(DD7); PORTA = 0; /* Let input power stabilize... */ _delay_ms(500); /* * Configure Timer0 as a fast PWM. It will * - turn on the output pin at the start of each cycle * - turn it off when the value hits DUTY_CYCLE_REG * - wrap to 0 when it hits OCR0A */ TCCR0A = _BV(COM0B1) | _BV(WGM01) | _BV(WGM00); OCR0A = PWM_RESOLUTION; /* Start with 40% duty cycle and ramp up to avoid inrush. */ DUTY_CYCLE_REG = (uint8_t)(PWM_RESOLUTION * 0.40); /* Set Timer0 clock source to be main oscillator. This enables the timer. */ TCCR0B = _BV(CS00) | _BV(WGM02); /* * Turn on the ADC, * - use internal voltage ref. * - configure ADC0 as our source * - left-adjust the result, 8-bits is enough for us * - disable digital input buffer on pin * - enable the ADC. */ ADMUX = /* REF = */ _BV(REFS1) | /* INPUT = */ 0; ADCSRA |= /* PRESCALER = 16 = 2^ */ 4; ADCSRB |= /* LEFT-ADJUST */ _BV(ADLAR); DDRA &= ~_BV(DD0); DIDR0 |= _BV(ADC0D); ADC_ENABLE(); _delay_ms(1); /* * Now enter our main loop. Monitor the output voltage and manipulate * the duty cycle to control it. */ while (1) { /* Wait for the Timer0 to overflow... */ loop_until_bit_is_set(TIFR0, TOV0); /* End of our OFF period, should be peak voltage... */ TIFR0 |= _BV(TOV0); /* Clear the flag. */ /* Check the output voltage. */ ADC_START_CONVERSION(); loop_until_bit_is_clear(ADCSRA, ADSC); uint8_t adc_result = ADCH; if (adc_result < DESIRED_ADC_RESULT && DUTY_CYCLE_REG < MAX_PWM_LEVEL) { DUTY_CYCLE_REG++; } else if (adc_result > DESIRED_ADC_RESULT && DUTY_CYCLE_REG > MIN_PWM_LEVEL) { DUTY_CYCLE_REG--; } } }