void configure_adc(
    UINT8 ref, UINT8 samp_rate, UINT8 samp_time, UINT8 presc,
    UINT8 ressel, UINT8 gain, UINT8 neg_ain, UINT8 ain
){
    configure_global_ports();
    enable_adc_clk();
    adc_ptr = adc;
    bankA_ptr = bankA;

    disable_adc();

    /*
        BIN = (0xFFF)(V_in * GAIN / V_ref)
    */
    adc_ptr->REFCTRL.reg |= ref;        // Select a reference
    adc_ptr->AVGCTRL.reg |= samp_rate;  // Now collect a number
                                        //  of samples at a time.
        // Total sampling time length = (SAMPLEN+1)*(Clk_ADC/2)
    adc_ptr->SAMPCTRL.reg = samp_time;    // Set sampling time
    adc_ptr->CTRLB.reg |= presc << 8u;    // Set clock prescaler
    adc_ptr->CTRLB.reg |= ressel << 4;     // Select resolution
    adc_ptr->INPUTCTRL.reg |= gain << 24u;   // Select Gain
    adc_ptr->INPUTCTRL.reg |=
        neg_ain << 8    // Map the negative terminal
        | ain           // Map the positive terminal
        ;
    
    enable_adc();
}
Пример #2
0
int saradc_disable(void)
{
	disable_adc();
	
	disable_sample_engine();
	
	return 0;
}
Пример #3
0
void sleep_avr()
{
	// Disable analog to digital converter
	disable_adc();

	set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement
	cli();
	sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
	sei();                                  // Enable interrupts
	sleep_cpu();                            // sleep
	sleep_disable();                        // Disable interrupts

	// Enable global interrupts
	sei(); 
}
Пример #4
0
void all_off(){
	stop_timer();
	unlock_stage = 0;
	seconds = 0;
	d_seconds = 0;
	apply_led(0);
	power_mode = MODE_OFF;
	apply_power();
	DRIVER_OFF;
	OCR0B=0;
	led_selected = 0;
	old_led_selected = 0;
	disable_adc();
	clean_times_occured();
}
Пример #5
0
int main ()
{
	// Disable the ADC
	disable_adc();

	// Watchdog timer reset safety check
	wdt_reset_safety();
	
	// Initialise watchdog timer
	init_wdt();

	// Identify input and output pins
	init_pins();

	// Buffer for text
	char s[20];

	// Setup direction and port for debug logging
	STX_PORT |= 1<<STX_BIT;
	STX_DDR |= 1<<STX_BIT;

	// Initialise DSP
	int i = 0;
	for (; i < MAX_READINGS; i++) readings[i] = 0;

	for (;;)
	{
		// Go to sleep
		sleep_avr();
		
		// Read VCC and convert to base 10 number
		long vcc = read_vcc();

		dsp(vcc);
		utoa(average, s, 10);

		// Output VCC to soft serial PIN STX_BIT
		sputs(s);
		sputs("\n\r");

		if (average > FULL_CHARGE_MV)
		{
			init_wdt_05s();
			blink();
		}
		else init_wdt();	
	}
}
Пример #6
0
static inline void saradc_power_control(int on)
{
	if (on) {
#if MESON_CPU_TYPE >= MESON_CPU_TYPE_MESON8
		enable_bandgap();
		udelay(10);
#endif
		enable_adc();
		udelay(5);
		enable_clock();
		enable_sample_engine();
	}
	else {
		disable_sample_engine();
		disable_clock();
		disable_adc();
#if MESON_CPU_TYPE >= MESON_CPU_TYPE_MESON8
		disable_bandgap();
#endif 			
	}
}
Пример #7
0
int main(void){
	// PA0 - ADC IN
	// PA2 - RED
	// PA3 - GREEN
	// PA4 - BLUE
	// PA7(OC0B) - PWM output.
	// PB0 - out (EN)
	// PB1 - hall1, PB2 - hall2

	DDRA   = 0b11111110;
	PORTA  = 0b00000000;

	DDRB   = 0b0001;
	PORTB  = 0b0110;

	TCCR0A = (1 << COM0B1) | (1 << WGM00);// | (1 << WGM01);  		// PWM mode
	TCCR0B = (1 << CS01);							// clock source = CLK/8, start PWM

	PCMSK1 = (1<<PCINT9) | (1<<PCINT10);
	GIMSK = (1<<PCIE1);//interrupt on pin change0

	ADMUX = 0b10000000; // ADC0 - int

	apply_led(1);
	DRIVER_ON;
	OCR0B = 8;
	_delay_ms(500);
	all_off();

	eeprom_read_block(&seconds_on, &nv_seconds_on, sizeof(seconds_on));
	eeprom_read_block(&times_on,   &nv_times_on,   sizeof(times_on)  );
	eeprom_read_block(&max_on,     &nv_max_on,     sizeof(max_on)    );
	prev_power_mode = MODE_OFF;
	sei();

	while(1){
		// ADC8 = 330 - ~70 degrees Celcium
		// 9V =  714
		// 10V = 793
		// 11V = 872
		// 12V = 951
		// 12.6V = 999
		if(power_mode==MODE_OFF && unlock_stage==0){
			seconds = 0;
			d_seconds = 0;
			stop_timer();
			disable_adc();
			set_sleep_mode(SLEEP_MODE_PWR_DOWN);
			sleep_enable();
			sleep_bod_disable();
			sleep_cpu();
			sleep_disable();
		}
		if(seconds>1 && power_mode==MODE_OFF){
			all_off();
		}
		if(unlock_stage==0 && power_mode!=MODE_OFF){
			if(d_seconds==0){
				adc_channel = 0;
				ADMUX = 0b10000000; // ADC0 - int
			}
			if(d_seconds==5){
				adc_channel = 1;
				ADMUX = 0b10100010; // int reference and temp sensor
			}

			if(seconds%2==0){
				if(power_mode==MODE_LOW_VOLTAGE){
					RED_ON;GREEN_OFF;BLUE_OFF;
				}
				if(power_mode==MODE_OVERTEMP){
					RED_ON;GREEN_ON;BLUE_OFF;
				}
			}else{
				if(power_mode==MODE_OVERTEMP||power_mode==MODE_LOW_VOLTAGE){
					apply_led(0);
				}
			}

			// if((seconds%5==0) && ADMUX == 0b10000000){ //measure voltage in active mode
			if(adc_channel ==0 && d_seconds == 4){ //measure voltage in active mode
				if(val<LIMIT_9V){
					if(check_and_set_times(0)){
					  all_off();//completely discharged. turning off
					}
				}
				else if(power_mode!=MODE_OVERTEMP && power_mode!=MODE_LOW_VOLTAGE && power_mode>=MODE_IDLE){ //show voltage always, except overtemp
					if(val<((LIMIT_9V+LIMIT_10V)/2)){
						if(power_mode>=MODE_LOW && check_and_set_times(1)){ // усвловие нужно, чтобы не задалбывать функцию - в ней будут логи
							apply_led(0);
							power_mode = MODE_LOW_VOLTAGE;//low voltage
							apply_power();
						}
					}else if(val<LIMIT_10V){
						if(led_selected==0 || check_and_set_times(2) ){
							apply_led(1);
						}
						led_selected = 1;
					}else if(val<LIMIT_11V){
						if(led_selected==0 || check_and_set_times(3)){
							apply_led(2);
						}
						led_selected = 2;
					}else{
						if(led_selected==0 || check_and_set_times(4)){
							apply_led(3);
						}
						led_selected = 3;
					}
				}
			}
			while(d_seconds==4){}

			if(adc_channel ==1 && d_seconds == 8){ //measure temp in active mode
				if(val>330 && power_mode>=MODE_LOW && check_and_set_times(5)){// усвловие нужно, чтобы не задалбывать функцию - в ней будут логи
					power_mode = MODE_OVERTEMP;
					apply_power(); //overtemp. reduce current cunsumption
				}
			}
			while(d_seconds==8){}

			if(power_mode==MODE_IDLE && seconds>=7200){ //выключение через два часа из режима простоя
				all_off();
			}


			// if(seconds==0 && d_seconds<5 && power_mode>=MODE_LOW){
			// 	GIMSK = 0;//вырубим прерывания на первые 500мс, чтобы не кнопать кнопкой сильно часто
			// }else{
			// 	GIMSK = (1<<PCIE1);//interrupt on pin change0
			// }
		}
	}
}