Skip to content

Learning to use the analog to digital converter on an ATmega328p

Notifications You must be signed in to change notification settings

Andrew-H3/AVR-ADC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AVR-ADC

Purpose

To learn how to use the ADC on the ATmega328p.

I attempt to control the frequency of an led by sending the ADC the output from a potentiometer configured as a voltage divider. By turning the pot, the frequency should change.

Final Product

Final Product (should be picture here)

Setup

The Programmer

My programming setup consists of a Waveshare USB AVRISP XPII programmer conected to a breadboard. The chip is powered via an arduino board, and has a pull up resistor connected to the reset pin.

USB AVRISP XPII (should be picture here)

The Software

I use avr-gcc, avr-objcopy, and avrdude to compile, convert to hex, and then upload to the microcontroller. This is all run on Ubuntu 18.04. avr-gcc and avr-objcopy can be found in the Ubuntu repo as avr-libc and avrdude as avrdude. All the commands are layed out in the included makefile.

The makefile included is for general purpose avr programming. For this specific repo you need to give make the name of the C source file to use. In this case using adc.c you need to tell make adc. To build and upload (using the same programming setup) run the following:

make file=adc; make upload file=adc

Code

define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

void main(void)
{
	//setup port
	//using B1 as output
	DDRB = 0x02;
	PORTB = 0x00;
	//setup adc
	//power adc
	PRR = 0xFE;
	//set voltage reference & input channel
	//use AREF & ADC0 (PC0), bits left adjusted
	ADMUX = 0x20;
	//enable adc, set division factor of 8
	ADCSRA |= 0x83;

	while(1){
		//start adc conversion
		ADCSRA |= 0x40;
		//wait for conversion to finish
		while(ADCSRA & 0x40) {}
		// read value from adc
		unsigned int adcVal = ADCH;
		PINB = 0x02;
		for (int x = 0; x < adcVal*2; x++) {
			_delay_ms(1);
		}
	}

}

Diagram

Diagram Of Circuit (should be picture here)

Made with www.circuit-diagram.org

Demo

Video Link

YouTube Link

Useful Links

328p datasheet

avr-libc library reference

AVRFREAKS: Bit manipulation

oscilloscope

About

Learning to use the analog to digital converter on an ATmega328p

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published