54 lines
1.6 KiB
C
54 lines
1.6 KiB
C
//setup adc for an attiny85
|
||
//
|
||
#include<avr/io.h>
|
||
#include"avr_adc.h"
|
||
#define _BS(bit) (1<<bit)
|
||
#define _BC(bit) ~(1<<bit)
|
||
#define STARTADC ADCSRA |= _BS(ADSC)
|
||
|
||
|
||
//1.setup_prescaler
|
||
/* this function initialises the ADC
|
||
|
||
ADC Prescaler Notes:
|
||
--------------------
|
||
|
||
ADC Prescaler needs to be set so that the ADC input frequency is between 50 - 200kHz.
|
||
|
||
For more information, see table 17.5 "ADC Prescaler Selections" in
|
||
chapter 17.13.2 "ADCSRA – ADC Control and Status Register A"
|
||
(pages 140 and 141 on the complete ATtiny25/45/85 datasheet, Rev. 2586M–AVR–07/10)
|
||
|
||
Valid prescaler values for various clock speeds
|
||
|
||
Clock Available prescaler values
|
||
---------------------------------------
|
||
1 MHz 8 (125kHz), 16 (62.5kHz)
|
||
4 MHz 32 (125kHz), 64 (62.5kHz)
|
||
8 MHz 64 (125kHz), 128 (62.5kHz)
|
||
16 MHz 128 (125kHz)
|
||
|
||
Below example set prescaler to 128 for mcu running at 8MHz
|
||
(check the datasheet for the proper bit values to set the prescaler)
|
||
*/
|
||
void setupADC(void)
|
||
{
|
||
ADMUX = //_BC(REFS0) | //Set_Vref
|
||
//_BC(REFS1) | //Set_Vref
|
||
//_BC(MUX3) | //
|
||
//_BC(MUX2) | //
|
||
// _BS(MUX1) | //
|
||
_BS(MUX0) | //PORT Select PB3
|
||
_BS(ADLAR); //left shift conversion results(used in 8bit mode)
|
||
// ADCSRB = _BS(
|
||
ADCSRA = _BS(ADEN) | //Enable ADC
|
||
//_BC(ADATE) | //Disable AutoTrigger aka Freerunning mode
|
||
// _BS(ADPS1) | //Select Prescaler
|
||
_BS(ADPS0) | //prescaler 128
|
||
//_BS(ADPS1) |
|
||
_BS(ADPS2);
|
||
ADCSRA &= ~(1<<ADATE);
|
||
}
|
||
|
||
|