static void input_loop(void) { // read initial ADC values BSET(ADC_CR1, 0); // start conversion while (!BCHK(ADC_CSR, 7)) pause(); // wait for end of conversion read_ADC(); // put initial values to all buffers ADC_BUFINIT(0); ADC_BUFINIT(1); ADC_BUFINIT(2); adc_battery = adc_battery_last; adc_battery_filt = (u32)adc_battery * ADC_BAT_FILT; // task CALC must be awaked to compute values before PPM will take on awake(CALC); input_initialized = 1; while (1) { // read ADC only when EOC flag (only for first it will not be ready) if (BCHK(ADC_CSR, 7)) read_ADC(); read_keys(); stop(); } }
/* check 12V supply voltage and warn user if it is too low */ void check_supply_voltage(void) { /* * Voltage threshold: Device is powered by a lead battery. * Warn user if voltage drops below 11.5V * Increase warning intensity if voltage drops below 11V * * The input voltage divider is formed by a 100k and a 22k resistor. * * | V_in | V_div | ADC | * | 13.8V | 2.49V | 249 | * | 11.5V | 2.07V | 207 | * | 11.0V | 1.98V | 198 | * */ uint8_t ADC_result = read_ADC(); int8_t corr = 7; //correction for non-ideal resistors in the divider if(ADC_result > (207+corr)) { switch_LED(0,'R'); //Everything is fine, no need to warn } else if(ADC_result > (198+corr)) { flash_LED(2,100,'R'); //Supply voltage is low, check battery charger }else { flash_LED(2,30,'R'); //Supply voltage is dangerously low, user must act to prevent battery damage } }
int main(void) { int read; float input_voltage, temperature; UART_INIT(); // UART 통신 초기화 ADC_INIT(1); // AD 변환기 초기화 while(1) { // 온도 센서의 출력 전압을 ADC를 거쳐 읽는다. read = read_ADC(); // 0에서 1023 사이의 값을 0V에서 5V 사이 값으로 변환한다. input_voltage = read * 5.0 / 1023.0; // 10mV에 1℃이므로 100을 곱해서 현재 온도를 얻는다. temperature = input_voltage * 100.0; UART_print16bitNumber((int)temperature); // 정수값으로 출력 UART_transmit('\n'); // 줄바꿈 _delay_ms(1000); // 1초에 한 번 출력 } }
void Task_ReadJoystick() { for(;;) { // The button reads ~0 when pushed, and anything from 10 to 1024 when released buttonState = (read_ADC(JOYSTICK_PIN_BUTN) < 10) ? '1' : '0' ; // Read the joystick analog value and save the corresponding direction to joystickDirection, n, e, s, w, and 0 for stop int joy_horz = read_ADC(JOYSTICK_PIN_HORZ); int joy_vert = read_ADC(JOYSTICK_PIN_VERT); /* map joystick values to: * 0 (west or north), 1 (middle), or 2 (east or south) */ int dir_horz = map(joy_horz, JOYSTICK_MIN, JOYSTICK_MAX, 0, 2); int dir_vert = map(joy_vert, JOYSTICK_MIN, JOYSTICK_MAX, 0, 2); // horizontal directions take precedence over vertical if(dir_horz == 0) { joystickDirection = WEST; } else if(dir_horz == 1) { if(dir_vert == 0) { joystickDirection = NORTH; } else if(dir_vert == 1) { joystickDirection = NONE; } else if(dir_vert == 2) { joystickDirection = SOUTH; } } else if(dir_horz == 2) { joystickDirection = EAST; } Task_Sleep(5); } }
int main(void) { ADC_init(); powermgmt(); uint8_t i; DDRB |= (1 << PB5)|(1 << PB4); //Set DDR of PORTB of LED 1 & LED 2-High(output) while(1) { sleep_cpu(); for(i=0; i<=1; i++) { if(i==0) { ADC_converted = read_ADC(0); //Read one ADC channel sleep_disable(); if(ADC_converted < 50) { PORTB |= (1<<PB5); //If ADC value is Below "10"(i.e darkness) turn led on } else { PORTB &= ~(1<<PB5); //Else turn led off } } else if(i==1) { ADC_converted = read_ADC(1); //Read one ADC channel sleep_disable(); if(ADC_converted > 120) { PORTB |= (1<<PB4); //If ADC value is Above 121(i.e temperature higher than room temperature) turn led on } else if(ADC_converted < 120) { PORTB &= ~(1<<PB4); //Else turn led off } } _delay_ms(400); //0.4 second switching between LDR and NTC } } return 0; }
int main(void) { range_select(); init_timer0(); init_ADC(); init_7Seg(); init_led(); while(1) { int r = 0; if(read_ADC()) r = calc_resist(read_ADC()); test_limits(r); printbuf(r); _delay_ms(150); } }
int main(void) { int i, j; uint16_t value; uint8_t adc_value; //variable store value from conversion struct sched_t sched[NUM_LEDS]; init_microcontroller(); for (;;) { adc_value = read_ADC(); //if (adc_value > 155) if (adc_value > ADC_ZERO + (EPSILON / 2)) { // light blue radially from center scheduler_fill(sched, 0, 1, 0); scheduler_schedule_color(&sched[0], 0, 0, 1); value = ADC_ZERO + EPSILON; for (i=1; i<NUM_LEDS && value < 256; i++) { if (adc_value > value) scheduler_schedule_color(&sched[i], 0, 0, 1); else break; value += 10; } } else if (adc_value < ADC_ZERO - (EPSILON / 2)) { // light red radially from center scheduler_fill(sched, 0, 1, 0); scheduler_schedule_color(&sched[0], 1, 0, 0); value = ADC_ZERO - EPSILON; for (i=1; i<NUM_LEDS && value >= 0; i++) { if (adc_value < value) scheduler_schedule_color(&sched[i], 1, 0, 0); else break; value -= 10; } } else { // at rest. light all green scheduler_fill(sched, 0, 1, 0); } for (i=0; i<10; i++) scheduler_run(sched); } return 0; }
int main(void) { int read; ADC_INIT(0); // AD 변환기 초기화 INIT_TIMER1(); while(1) { read = read_ADC(); // 가변저항 값 읽기 OCR1A = PULSE_MIN + (int)(3.48 * read); _delay_ms(ROTATION_DELAY); } }
int main(void) { int read, actualVCC; ADC_INIT(0x0E); // 15번 채널 선택 UART_INIT(); // UART 통신 초기화 while(1) { read = read_ADC(); // 15번 채널 값 읽기 actualVCC = 1125300L / read; // mV 단위로 변환 UART_print16bitNumber(actualVCC); // AVCC 값 출력 UART_printString("\n"); // 줄 바꿈 _delay_ms(1000); // 1초에 한 번 읽음 } }
void main(void) { unsigned int adc_average; unsigned char adc_result, i; init_hardware(); __delay_ms(500); while (1) { //Read the ADC and oversamples to improve accuracy adc_average = 0; for (i = 0; i < ADC_OVERSAMPLES; i++) adc_average += read_ADC(B_SENSE_INPUT); //Averages the read ADC samples adc_result = adc_average / ADC_OVERSAMPLES; //Processes the ADC value and switches outputs accordingly switch (state) { case STATE_ON: if (adc_result < CUTOUT_LEVEL) { PORTC &= ~LEDG_PIN; PORTC |= LEDR_PIN; PORTC &= ~LVCO_PIN; state = STATE_OFF; } else if (adc_result < LOW_BATT_LEVEL) { PORTC |= (LEDR_PIN | LEDG_PIN | LVCO_PIN); } else { PORTC |= ( LEDG_PIN | LVCO_PIN ); PORTC &= ~LEDR_PIN; } break; case STATE_OFF: if (LOW_BATT_LEVEL < adc_result) { PORTC &= ~LEDR_PIN; PORTC |= LEDG_PIN; PORTC |= LVCO_PIN; state = STATE_ON; } break; } //Waits a set time before reading the ADC again __delay_ms(POLL_INTERVAL_MS); } }
int main(void) { powermgmt(); ADC_init(); DDRB |= (1 << PB5)|(1 << PB4); //Set DDR of PORTB of LED 1 & LED 2-High(output) sei(); // Enable Global Interrupts ADCSRA |= 1<<ADSC; // Start Conversion while(1) { ADC_converted = read_ADC(i); //Read one ADC channel } return 0; }
int main(int argc, char **argv) { initialize(); clear_array(); PORTE = 0; ADC_enable(); ADC_set_channel(ADC_MUX_ADC5); ADC_set_prescaler(ADC_PRESCALER_128); ADC_start(); while(1){ set_array_red(read_ADC(ADC_MUX_ADC5)); } }
// Analog input: Returns ADC value (12-bit resolution) // Digital in/out: Returns I/O status to match the bit ordering uint8_t get_user_io_val(uint16_t io_sel, uint16_t * val) { struct __user_io_info *user_io_info = (struct __user_io_info *)&(get_DevConfig_pointer()->user_io_info); uint8_t ret = 0; // I/O Read failed uint8_t idx = 0; uint8_t status = 0; *val = 0; if((user_io_info->user_io_enable & io_sel) ==io_sel) // Enable { idx = get_user_io_bitorder(io_sel); if((user_io_info->user_io_type & io_sel) == io_sel) // IO_ANALOG == 1 { // Analog Input: value *val = read_ADC(USER_IO_ADC_CH[idx]); //printf("===============> Analog input: %d <============\r\n", *val); } else // IO_DIGITAL == 0 { // Digital Input / Output if((user_io_info->user_io_direction & io_sel) == io_sel) // IO_OUTPUT == 1 { // Digital Output: status status = (uint16_t)GPIO_ReadOutputDataBit(USER_IO_PORT[idx], USER_IO_PIN[idx]); //printf("===============> Digital Output: %d <==============\r\n", status); } else // IO_INPUT == 0 { // Digital Input: status status = (uint16_t)GPIO_ReadInputDataBit(USER_IO_PORT[idx], USER_IO_PIN[idx]); //printf("===============> Digital input: %d <==============\r\n", status); } //*val |= (status << i); *val = status; } ret = 1; // I/O Read success } return ret; }
int main(void) { int value; uint8_t on_off; ADC_INIT(0); PORT_INIT(); while(1) { value = read_ADC() >> 7; on_off = 0; for(int i = 0; i <= value; i++){ on_off |= (0x01 << i); } PORTD = on_off; } }
void TIM2_IRQHandler (void) { TIM2->SR &= ~(0x01); bufferE[i%4096]= read_ADC(); switch(etat) { case 0 : DAC->DHR12R1= bufferE[i%4096]; LED_B_ON(1); LED_B_OFF(2); LED_B_OFF(10); LED_B_OFF(11); break; case 1 : DAC->DHR12R1= bufferE[i%4096] + bufferE[(i-1200)%4096] ; // Double LED_B_ON(2); LED_B_OFF(1); LED_B_OFF(10); LED_B_OFF(11); break; case 2 : DAC->DHR12R1= bufferE[i%4096]+0.7*bufferS[(i-2400)%4096]; // Echo bufferS[i%4096]=DAC->DOR1; LED_B_ON(10); LED_B_OFF(2); LED_B_OFF(1); LED_B_OFF(11); break; case 3 : float tmp=0; for(int j=0;j<5;j++) { tmp+= (float)bufferE[(i)%4096]*coeffsFIR[(j)%4096]; // FIR 10khz } DAC->DHR12R1=tmp; LED_B_ON(11); LED_B_OFF(2); LED_B_OFF(10); LED_B_OFF(1); TIM2->SR &= ~(0x01); break; } i++; }
int main(void) { /* Variables */ uint16_t ad_value = 0; char ad_value_text[8]; init_LCD(); init_ADC(); DDRD = 0xFF; PORTD = 0; while(1) { ad_value = read_ADC(); itoa(ad_value,ad_value_text,10); LCDGotoXY(0,1); LCDstring((uint8_t*) ad_value_text, (uint8_t) strlen(ad_value_text)); } }
int main(void) { init_ADC(); // Schalte ADC ein // korrekte Parameterwerte eADCRUNMODE runmode = SINGLESHOT; // Eine Messung start_ADC(runmode); // Messung starten eADCRES res = BIT8; // Auflösung des Digitalwertes unsigned short result; result = read_ADC(res); // 512 bei 50 % bei 10 Bit // 128 bei 50 % bei 8 Bit stop_ADC(); unsigned long a = 835; unsigned long * p = &a; unsigned char b; b = ulong2bcd_unpk(p); // b=5 - richtig & a=83 - richtig // Sonderfall Parameter = 0 unsigned long a2 = 0; unsigned long * p2 = &a2; unsigned char b2; b2 = ulong2bcd_unpk(p2); // b2=0 - richtig & a2=0 - richtig unsigned char c; c = ulong2bcd_pk(89); // Wird 1000 1001 - richtig // inkorrekter Parameterwert unsigned char c2; c2 = ulong2bcd_pk(5); // Wird 0 - richtig unsigned char c3; c3 = ulong2bcd_pk(100); // Wird 0 - richtig while (1) { } }
void GetAudioBandLevel(void) { uint8_t audio_band = 0; DDRD |= (1 << STROBE)|(1 << EQ_RESET); //PORTD bit strobe and reset pins output PORTD &= ~((1 << STROBE)|(1 << EQ_RESET)); //sets strobe and reset low PORTD |= (1 << EQ_RESET); //reset goes high _delay_us(100); //delay 100usec for setup time req PORTD |= (1 << STROBE); //strobe goes high _delay_us(50); //strobe delays PORTD &= ~(1 << STROBE); //strobe goes low _delay_us(50); //strobe delays PORTD &= ~(1 << EQ_RESET); //reset reset PORTD |= (1 << STROBE); //strobe goes high _delay_us(50); for(audio_band = 0; audio_band < 7; audio_band++) { PORTD &= ~(1 << STROBE); //resets (set strobe pin low (active)) _delay_us(30); //setup time for capture AudioLevel[audio_band] = read_ADC(); //reads 8 bit resolution audio level from audio bandpass filter PORTD |= (1 << STROBE); //sets (set strobe pin high again) _delay_us(50); //not sure if needed - check datasheet } }
int main(void){ //startLCD_Show_Credits(); //lcd_check_busy_flag(); //lcd_send_command( LCD_CLEAR ); //You can multiplex these pins by enabling and disabling the LCD display. //lcd_puts( "Lx = 15uH " ); //lcd_check_busy_flag(); //lcd_send_command( LCD_SET_CURSOR_POS | LCD_SECOND_LINE ); //lcd_puts( "Cx = 150uF" ); //lcd_check_busy_flag(); //_delay_ms(2000); //lcd_send_command( LCD_CLEAR ); //lcd_puts("Hello Jenn!"); uart_init(); stdout = &uart_tx; stdin = &uart_rx; init_ADC(); //Set Pin Directions //Lights Pin as output LIGHTS_SIGNAL_DIR |= (1 << LIGHTS_SIGNAL); //Start Button Pin as input START_BUTTON_DIR &= ~(1 << START_BUTTON); //Stop Button Pin as Input STOP_BUTTON_DIR &= ~(1 << STOP_BUTTON); //Buzzer Pin as Output BUZZER_DIR |= (1 << BUZZER); //Trip Switch Pin as Input TRIP_SWITCH_DIR &= ~(1 << TRIP_SWITCH_PIN); //LCD Led Pin as output LCD_LED_DIR |= (1 << LCD_LED_PIN); LCD_LED_PORT |= (1 << LCD_LED_PIN); //Turn it on static long reading; static bool btnStop = false; static bool tripSwitch = false; startLCD_Show_Credits(); displayBlink(3); while (1){ printf("Raw Reading: %d\t",read_ADC(0,&reading)); long mapped_reading = mapRange(0,1023,0,1800,reading); printf("Mapped Reading: %d\t",mapped_reading); display_Selection(mapped_reading); if(START_BUTTON_PIN & (1 << START_BUTTON) && mapped_reading > 0){ lcd_check_busy_flag(); lcd_send_command(LCD_CLEAR); lcd_puts(" STARTING "); displayBlink(2); long count = 0; _delay_ms(1000); //Turn on lights LIGHTS_SIGNAL_PORT |= (1 << LIGHTS_SIGNAL); bool lightsOn = true; for(count = mapped_reading;count >= 0;count--){ //Check for stop button if(STOP_BUTTON_PIN & (1 << STOP_BUTTON)){ lcd_check_busy_flag(); lcd_send_command(LCD_CLEAR); lcd_puts(" STOPPING "); btnStop = true; break; } //Pause if trip switch is high if(!(TRIP_SWITCH_PIN & (1 << TRIP_SWITCH))){ if(!lightsOn){ LIGHTS_SIGNAL_PORT |= (1 << LIGHTS_SIGNAL); } //If the the switch is still closed printf("UV Lights on for: %d more secs...\n",count); lcd_check_busy_flag(); lcd_send_command(LCD_CLEAR); lcd_puts(" Exposing "); lcd_check_busy_flag(); lcd_send_command(LCD_SET_CURSOR_POS | LCD_SECOND_LINE); char * zero = "0"; uint8_t mins = count / 60; uint8_t secs = count % 60; char minsStr[5]; char secsStr[5]; itoa(mins,minsStr,10); itoa(secs,secsStr,10); lcd_check_busy_flag(); lcd_puts(" "); if(mins <= 9){ lcd_check_busy_flag(); lcd_puts(zero); } lcd_check_busy_flag(); lcd_puts(minsStr); lcd_check_busy_flag(); lcd_puts(":"); if(secs <= 9){ lcd_check_busy_flag(); lcd_puts(zero); } lcd_check_busy_flag(); lcd_puts(secsStr); _delay_ms(1000); } else{ lightsOn = false; LIGHTS_SIGNAL_PORT &= ~(1 << LIGHTS_SIGNAL); //Tell user the lid is open printf("LID OPEN, CLOSE IT TO CONTINUE\n"); lcd_check_busy_flag(); lcd_send_command(LCD_CLEAR); lcd_puts(" LID OPEN "); lcd_check_busy_flag(); lcd_send_command(LCD_SET_CURSOR_POS | LCD_SECOND_LINE); lcd_puts(" PLEASE CLOSE IT "); //Increment Count to make up for this one count++; } } //Turn off lights LIGHTS_SIGNAL_PORT &= ~(1 << LIGHTS_SIGNAL); if(btnStop != true){ lcd_check_busy_flag(); lcd_send_command(LCD_CLEAR); lcd_puts(" FINISHED "); alarmSound(2); } else{ buzzerSound(13); btnStop = false; //reset the stop flag } displayBlink(2); } } return 0; }
int main (void){ //Start UART init_UART(); stdout = &uart_tx; stdin = &uart_rx; //Start ADC init_ADC(); //Set pins for display as output //Digit 1 DDRD |= (1 << PD5); //A DDRD |= (1 << PD6); //B DDRD |= (1 << PD7); //C DDRB |= (1 << PB0); //D //Digit 2 DDRC |= (1 << PC1); //A DDRC |= (1 << PC2); //B DDRC |= (1 << PC3); //C DDRC |= (1 << PC4); //D //Lights Pin Output DDRC |= (1 << PC5); //Start/Stop Button DDRB &= ~(1 << PB1); //Start button input DDRB &= ~(1 << PB2); //Stop button input //Buzzer Pin DDRD |= (1 << PD2); //Output int first_digit = 9; int second_digit = 10; static long reading; static bool btnStart; //Blink display to let me know everything is good! displayBlink(5); while(1) { printf("Raw Reading: %d\t",read_ADC(0,&reading)); long mapped_reading = mapRange(0,1023,0,99,reading); printf("Mapped Reading: %d\n",mapped_reading); display_Selection(mapped_reading); if(PINB & (1 << PB1) && mapped_reading > 0 ){ //If the Start button gets pressed then displayBlink(2); int count = 0; _delay_ms(1000); PORTC |= (1 << PC5); for(count = mapped_reading;count >= 0;count--){ //Count Backwards from that number //Check for stop button quit if set if(PINB & (1 << PB2)) break; uint8_t fdigitm = count / 10; uint8_t sdigitm = count % 10; firstDisplay(fdigitm); secondDisplay(sdigitm); printf("UV Lights on for: %d more secs...\n",count); _delay_ms(1000); } PORTC &= ~(1 << PC5); //displayBlink(5); alarmSound(2); displayBlink(2); } } }
/******************************* Main Program Code *************************/ int main(void) { // configure the microprocessor pins for the data lines lcd_D7_ddr |= (1<<lcd_D7_bit); // 4 data lines - output lcd_D6_ddr |= (1<<lcd_D6_bit); lcd_D5_ddr |= (1<<lcd_D5_bit); lcd_D4_ddr |= (1<<lcd_D4_bit); // configure the microprocessor pins for the control lines lcd_E_ddr |= (1<<lcd_E_bit); // E line - output lcd_RS_ddr |= (1<<lcd_RS_bit); // RS line - output // configure the microprocessor pins for the pushbutton pushbutton_ddr &= (1<<pushbutton_bit); pushbutton_port |= (1<<pushbutton_bit); // initialize adc ADMUX = ((1<<REFS0)|(1<<MUX2)|(1<<MUX0)); // Aref = Vcc, select ADC5 ADCSRA = ((1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<ADPS0)); // Prescaler div factor = 128 // initialize the LCD controller as determined by the defines (LCD instructions) lcd_init_4d(); // initialize the LCD display for a 4-bit interface // initialize TWI i2c_init(); // initialize thermometer therm_init(); // display the first line of information lcd_write_string_4d(disp_time); // set cursor to start of second line lcd_write_instruction_4d(lcd_setCursor | lcd_lineTwo); _delay_us(40); // 40 uS delay (min) // Code for interfacing with the serial connection char str[25]; int yy,mm,dd; sei(); // Enable global interrupts uart_init(); // Initialize the USART using baud rate 9600 uart_printstr(sdata); // Print a string from SRAM uart_printstr(fdata); // Print a string from FLASH getDate(&yy,&mm,&dd); // Get date from user sprintf(str,"Date: %d/%d/%d\n",yy,mm,dd); uart_printstr(str); // endless loop while(1) { uart_printstr(sdata); // Print a string from SRAM uart_printstr(fdata); // replace with check for button press function if(bit_is_clear(pushbutton_pin,pushbutton_bit)) { _delay_ms(100); if(bit_is_clear(pushbutton_pin,pushbutton_bit)) mode_new = (mode + 1) % 3; } if(mode_new != mode) { // clear lcd lcd_write_instruction_4d(lcd_clear); // display the first line of information // set cursor to start of first line lcd_write_instruction_4d(lcd_setCursor | lcd_lineOne); _delay_us(40); // 40 uS delay (min lcd_write_string_4d(disp_time); // set cursor to start of second line lcd_write_instruction_4d(lcd_setCursor | lcd_lineTwo); _delay_us(40); // 40 uS delay (min) if (mode_new == 0) { voltage = read_ADC(); lcd_write_string_4d(disp_volt); } else if (mode_new == 1) { frequency = 10 * freq_cntr_get_frequency(); lcd_write_string_4d(disp_freq); } else { temp_calcTemp(); // test thermometer lcd_write_string_4d(disp_temp); } } mode = mode_new; } return 0; }