#include <avr/io.h>
#include <util/delay.h>
void setup() {
Serial.begin(9600);
ADMUX = 0; // For ADC0
ADCSRA = (1 << ADEN) | (1 << ADATE) | 7; // Enable ADC and auto-trigger
ADCSRB = 0; // Auto-trigger source configuration
}
int main() {
init();
setup();
while(true) { // Infinite Loop
ADCSRA |= (1 << ADSC); // Start the Conversion
while (ADCSRA & (1 << ADSC)); // Wait for Conversion to Complete
uint16_t adcValue = ADC; // Read the ADC Value
adcValue = map(adcValue, 0, 1023, 0, 255); // Map value to 8-bit range
Serial.println(adcValue); // Print Mapped Value
_delay_ms(500); // Delay for demo purposes
}
}
// This code is for an Arduino program written in C++. It involves setting up the Analog-to-Digital Converter (ADC) and serial communication, then repeatedly reading an analog value, mapping it to an 8-bit range, and printing it via serial communication. Let's go through the code step-by-step:
// Header Files
// cpp
// Copy code
// #include <avr/io.h>
// #include <util/delay.h>
// These include directives pull in AVR-specific input/output definitions and utility functions for delays, respectively.
// setup Function
// cpp
// Copy code
// void setup() {
// Serial.begin(9600);
// ADMUX = 0; // For ADC0
// ADCSRA = (1 << ADEN) | (1 << ADATE) | 7; // Enable ADC and auto-trigger
// ADCSRB = 0; // Auto-trigger source configuration
// }
// Serial.begin(9600);: Initializes serial communication at a baud rate of 9600 bits per second.
// ADMUX = 0;: Selects ADC0 (analog input 0) as the input channel for the ADC.
// ADCSRA = (1 << ADEN) | (1 << ADATE) | 7;:
// ADEN: Enables the ADC.
// ADATE: Enables auto-triggering for continuous conversions.
// 7: Sets the ADC prescaler to 128.
// ADCSRB = 0;: Configures the auto-trigger source to free running mode.
// main Function
// cpp
// Copy code
// int main() {
// init();
// setup();
// while(true) { // Infinite Loop
// ADCSRA |= (1 << ADSC); // Start the Conversion
// while (ADCSRA & (1 << ADSC)); // Wait for Conversion to Complete
// uint16_t adcValue = ADC; // Read the ADC Value
// adcValue = map(adcValue, 0, 1023, 0, 255); // Map value to 8-bit range
// Serial.println(adcValue); // Print Mapped Value
// _delay_ms(500); // Delay for demo purposes
// }
// }
// init();: This initializes the Arduino environment (usually provided by the Arduino core libraries).
// setup();: Calls the setup function to configure the ADC and serial communication.
// Infinite Loop
// The while(true) loop runs indefinitely, performing the following steps repeatedly:
// ADCSRA |= (1 << ADSC);: Starts an ADC conversion by setting the ADSC bit in ADCSRA.
// while (ADCSRA & (1 << ADSC));: Waits for the conversion to complete by polling the ADSC bit.
// uint16_t adcValue = ADC;: Reads the 10-bit ADC conversion result from the ADC register.
// adcValue = map(adcValue, 0, 1023, 0, 255);: Maps the 10-bit ADC value (0-1023) to an 8-bit range (0-255). Note: map is not a built-in function in standard AVR libraries; it's typically part of Arduino's core functions.
// Serial.println(adcValue);: Prints the mapped 8-bit value to the serial monitor.
// _delay_ms(500);: Waits for 500 milliseconds before repeating the loop.
// Summary
// This program sets up an ADC on an Arduino to continuously read from analog input 0. It maps the 10-bit ADC values to an 8-bit range and sends these values over the serial port every 500 milliseconds. This setup is useful for monitoring analog sensor data and transmitting it to a computer for further analysis or display.