diff --git a/controllers/include/cm510_cfg.h b/controllers/include/cm510_cfg.h index 88cca06cd00138d8c4dc919d5551b578379bc297..f8430c0d1d0563db04f33c99b3ba3340b2dc38d3 100644 --- a/controllers/include/cm510_cfg.h +++ b/controllers/include/cm510_cfg.h @@ -5,7 +5,7 @@ // controller configuration parameters #define ADC_MAX_NUM_SAMPLES 16 -#define ADC_SAMPLE_PERIOD_MS 10 +#define ADC_SAMPLE_PERIOD_MS 40 #define ADC_VOLTAGE_ALARM_TH 559 #define ADC_VOLTAGE_ALARM_WINDOW 20 #define ADC_VOLTAGE_ALARM_NOTE NOTE_LA diff --git a/controllers/src/adc.c b/controllers/src/adc.c index 28ad8059b0a28633ca901bb16debc5b037769331..bc6d5c482790fdee53fd8b0960b9964c79f7c951 100644 --- a/controllers/src/adc.c +++ b/controllers/src/adc.c @@ -22,7 +22,7 @@ void adc_set_channel(uint8_t ch_id) ADMUX&=0xF8; ADMUX|=(ch_id&0x07); if(ch_id==0) - PORTA&=0xFC; + PORTA|=0xFC; else PORTA=~(0x80>>(ch_id-1));// enable the desired channel } @@ -33,7 +33,7 @@ void adc_set_sample_period(void) uint16_t compare_value; uint8_t i; - for(i=0;i<5;i++) + for(i=0;i<7;i++) { compare_value=(((F_CPU/1000)*ADC_SAMPLE_PERIOD_MS)/(prescaler_values[i]*NUM_ADC))-1; if(compare_value<256) @@ -75,49 +75,48 @@ void adc_loop(void) { TIFR2|=(1<<OCF2A)|(1<<OCF2B)|(1<<TOV2);// clear interrupt flag TCNT2=0x00;// start from 0 + // set the channel to sample + adc_set_channel(adc_current_ch); + _delay_us(12); // start a new conversion adc_start_conv(); - } - else - { - if(adc_is_conversion_done()) + // wait for the conversion to end + while(!adc_is_conversion_done()); + ADCSRA|=(1<<ADIF);// clear interrupt flag + data=ADC;//(ADCL | (ADCH<<8)); + adc_values[adc_current_ch]=data; + adc_ch_data[adc_current_ch][adc_current_sample]=data; + // compute the average for the current channel + data=0; + for(i=0;i<ADC_MAX_NUM_SAMPLES;i++) + data+=adc_ch_data[adc_current_ch][i]; + data=data/ADC_MAX_NUM_SAMPLES; + adc_avg_values[adc_current_ch]=data; + if(adc_current_ch==NUM_ADC-1)// last channel + adc_current_sample=(adc_current_sample+1)%ADC_MAX_NUM_SAMPLES; + if(adc_current_ch==ADC_VCC)// monitor the Voltage supply { - ADCSRA|=(1<<ADIF);// clear interrupt flag - data=(ADCL | (ADCH<<8)); - adc_values[adc_current_ch]=data; - adc_ch_data[adc_current_ch][adc_current_sample]=data; - // compute the average for the current channel - data=0; - for(i=0;i<ADC_MAX_NUM_SAMPLES;i++) - data+=adc_ch_data[adc_current_ch][i]; - data=data/ADC_MAX_NUM_SAMPLES; - adc_avg_values[adc_current_ch]=data; - if(adc_current_ch==NUM_ADC-1)// last channel - adc_current_sample=(adc_current_sample+1)%ADC_MAX_NUM_SAMPLES; - if(adc_current_ch==ADC_VCC)// monitor the Voltage supply +// voltage_mv=((uint32_t)data*(uint32_t)5000*(uint32_t)133)/((uint16_t)1024*(uint16_t)33); + // compare with hysteresis + if(adc_voltage_alarm==0x01) { -// voltage_mv=((uint32_t)data*(uint32_t)5000*(uint32_t)133)/((uint16_t)1024*(uint16_t)33); - // compare with hysteresis - if(adc_voltage_alarm==0x01) + if(data>=(ADC_VOLTAGE_ALARM_TH+ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V { - if(data>=(ADC_VOLTAGE_ALARM_TH+ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V - { - buzzer_stop_alarm(); - adc_voltage_alarm=0x00; - } + buzzer_stop_alarm(); + adc_voltage_alarm=0x00; } - else + } + else + { + if(data<(ADC_VOLTAGE_ALARM_TH-ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V { - if(data<(ADC_VOLTAGE_ALARM_TH-ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V - { - buzzer_start_alarm(NOTE_LA,30,30); - adc_voltage_alarm=0x01; - } + buzzer_start_alarm(NOTE_LA,30,30); + adc_voltage_alarm=0x01; } } - adc_current_ch=(adc_current_ch+1)%NUM_ADC; - adc_set_channel(adc_current_ch); } + // select next channel + adc_current_ch=(adc_current_ch+1)%NUM_ADC; } } @@ -155,9 +154,6 @@ void init_adc(void) TIMSK2=0x00;// interrupts not enabled TIFR2=0x07;// clear any pending interrupt adc_set_sample_period();// set the default sample period - - // enable the first ADC channel - adc_set_channel(adc_current_ch); } uint16_t get_adc_channel(adc_t channel_id) diff --git a/controllers/src/cm510.c b/controllers/src/cm510.c index dd68036f9fe963d9ab08e8668037c1e8929373b8..fbe21d184f24bc72cfb0f90c478b1e1cf00e7551 100755 --- a/controllers/src/cm510.c +++ b/controllers/src/cm510.c @@ -42,7 +42,6 @@ void init_cm510(void) init_adc(); init_buzzer(); init_user_time(); - exp_board_init(EXP_BOARD_ID); } int16_t main(void) @@ -51,6 +50,7 @@ int16_t main(void) sei(); /* call the user initialization function */ manager_init(18); + exp_board_init(EXP_BOARD_ID); user_init(); // turn BAT_LED on to indicate the initialization is done turn_led_on(LED_BAT); diff --git a/motion/Makefile b/motion/Makefile index fe1e351817bac42792a869a66be7c6b0dfc93f84..5b1a7eebcbb7df4fc7c92336a70a9c94bd683f90 100755 --- a/motion/Makefile +++ b/motion/Makefile @@ -13,7 +13,7 @@ MMCU=atmega2561 INC_DIRS=-I./include/ -I$(DEV_DIR)include/ -I$(COMM_DIR)include/ -I$(CONT_DIR)include/ -LIBS=$(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a +LIBS=$(CONT_DIR)lib/libcontrollers.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes diff --git a/motion/src/examples/Makefile b/motion/src/examples/Makefile index 686382b1015b6fa6ee484b4dadb4cdd575aaa1e7..847e3b242b9ef3164e143d002b96a37efcf269bd 100644 --- a/motion/src/examples/Makefile +++ b/motion/src/examples/Makefile @@ -14,7 +14,7 @@ CC=avr-gcc OBJCOPY=avr-objcopy MMCU=atmega2561 -LIBS=$(CONT_DIR)lib/libcontrollers.a $(MAN_DIR)lib/libmotion_manager.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a +LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include @@ -48,7 +48,7 @@ motion_manager: $(MAKE) -C $(MAN_DIR) download: $(MAIN_OUT_HEX) - fw_downloader -d /dev/ttyUSB0 -f ./$(PROJECT).hex -p new + fw_downloader -d /dev/ttyUSB0 -f ./$(PROJECT).hex -p cm510 clean: -rm $(PROJECT).*