Skip to content

Instantly share code, notes, and snippets.

@Jacajack
Last active October 3, 2016 17:35
Show Gist options
  • Save Jacajack/02f1080df4cdb79b5ba5fed9f069eefe to your computer and use it in GitHub Desktop.
Save Jacajack/02f1080df4cdb79b5ba5fed9f069eefe to your computer and use it in GitHub Desktop.
ATmega328p ADC snippet
#include <avr/io.h>
#include <inttypes.h>
#include "adc.h"
void adcenable( )
{
//Enable ADC
ADCSRA = ( 1 << ADEN );
}
void adcdisable( )
{
//Disable ADC
ADCSRA &= ~( 1 << ADEN );
}
uint16_t adcread( uint8_t mux )
{
//Read data from selected ADC (VCC as reference volatge)
ADMUX = ( mux << MUX0 ) | ( 1 << REFS0 );
ADCSRA |= ( 1 << ADSC );
while ( ADCSRA & ( 1 << ADSC ) );
return ADC;
}
#ifndef ADC_H
#define ADC_H
#include <avr/io.h>
#include <inttypes.h>
extern void adcenable( );
extern void adcdisable( );
extern uint16_t adcread( uint8_t mux );
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment