uint16_t hardware_analogRead(uint8_t ch) { uint32_t buf = 0; // Initialize the ADC driver before first use ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32); // Must setup the ADC channel to read beforehand if(ch < 15) ADC_SetupChannel(ch); if(ch > 7) ch = (1 << 8 | ((ch - 8) << MUX0)); else ch = ch << MUX0; // Perform a single conversion of the ADC channel 1 // buf = ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ch); // Start reading ADC channel 1 in free running (continuous conversion) mode ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ch); for(uint8_t i = 0; i < ADC_READINGS; i++) { while (!(ADC_IsReadingComplete())); buf += ADC_GetResult(); } // Leave ADC Disabled to save power ADC_Disable(); return (uint16_t)(buf / ADC_READINGS); }
/** Initializes the hardware and software associated with the V2 protocol command handling. */ void V2Protocol_Init(void) { #if defined(ADC) /* Initialize the ADC converter for VTARGET level detection on supported AVR models */ ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128); ADC_SetupChannel(VTARGET_ADC_CHANNEL); ADC_StartReading(VTARGET_REF_MASK | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK); #endif V2Params_LoadNonVolatileParamValues(); #if defined(ENABLE_ISP_PROTOCOL) ISPTarget_ConfigureRescueClock(); #endif }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ LEDs_Init(); ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32); ADC_SetupChannel(MIC_IN_ADC_CHANNEL); USB_Init(); /* Start the ADC conversion in free running mode */ ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | MIC_IN_ADC_MUX_MASK); }
/** Initialises the hardware and software associated with the V2 protocol command handling. */ void V2Protocol_Init(void) { #if defined(ADC) /* Initialize the ADC converter for VTARGET level detection on supported AVR models */ ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128); ADC_SetupChannel(VTARGET_ADC_CHANNEL); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK); #endif /* Timeout timer initialization (10ms period) */ OCR0A = (((F_CPU / 1024) / 100) - 1); TCCR0A = (1 << WGM01); TIMSK0 = (1 << OCIE0A); V2Params_LoadNonVolatileParamValues(); #if defined(ENABLE_ISP_PROTOCOL) ISPTarget_ConfigureRescueClock(); #endif }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Hardware Initialization */ Serial_Init(9600, false); Buttons_Init(); ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_32); ADC_SetupChannel(MIC_IN_ADC_CHANNEL); LEDs_Init(); USB_Init(); /* Create a stdio stream for the serial port for stdin and stdout */ Serial_CreateStream(NULL); /* Start the ADC conversion in free running mode */ ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_GET_CHANNEL_MASK(MIC_IN_ADC_CHANNEL)); }
void ButtonStates(void) { uint16_t port, result; char buffer[64]; PORTD = 0b11111111; /*for (int i=0; i<4; i++) { if (i > 1) { PORTC = (i%1 == 0) ? 0b01000000 : 0b10000000; } else { PORTB = (i%1 == 0) ? 0b01000000 : 0b10000000; } */ // TODO: dry this up ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL0); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "4,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL1); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "0,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL4); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "1,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL5); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "2,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL6); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "3,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL12); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "7,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL13); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "6,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED | ADC_CHANNEL7); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, "5,%d\n", ADC_GetResult()); fputs(buffer, &USBSerialStream); /* ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL5); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, " %d", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL6); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, " %d", ADC_GetResult()); fputs(buffer, &USBSerialStream); ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL7); while(!(ADC_IsReadingComplete())) {} sprintf(buffer, " %d", ADC_GetResult()); fputs(buffer, &USBSerialStream); PORTB = 0; PORTC = 0; //} */ }