Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes

AD Converter (ATmega168)

Last-modified: 2012-08-20 (Mon) 00:29:30 (2160d)
Top / AD Converter (ATmega168)
  • adc.h
    void ad_init(char chnum);	// ADCを初期化
    int ad_get(void);
  • adc.c
    //-----------------------------------------------------------------------------
    // ADC関係
    //-----------------------------------------------------------------------------
    #include <avr/io.h>
    #include "adc.h"
    
    //----- ADCを初期化 -----------------------------------------------------------
    void ad_init(char chnum)
    {
    	// ADC Control and Status Register
    	// ADC許可、64分周
    	ADCSRA = ((1<<ADEN) | 6);
    
    	// Channel number
    	ADMUX = chnum;
    }
    
    //----- ADCの結果を取得 -------------------------------------------------------
    int ad_get(void)
    {
    	ADCSRA |= _BV(ADIF);	// ADC完了割り込み要求フラグをクリア
    	ADCSRA |= _BV(ADSC);	// ADCスタート
    
    	loop_until_bit_is_set(ADCSRA, ADIF);	// ADC完了まで待つ
    
    	return ADCW;
    }