การจำลองการทำงานของ ADC
-ADC
-Slider
ไฟล์โปรเจค
; ************************************************************
; PROJECT:
; AUTHOR:
; ************************************************************
; Micro + software running
; ------------------------------------------------------------
.MICRO "ATmega168"
.TOOLCHAIN "GCC"
.GCCPATH "C:\WinAVR-20100110"
.GCCMAKE AUTO
.TARGET "adc-1.hex"
.SOURCE "adc-1.c"
.TRACE ; Activate micro trace
; Following lines are optional; if not included
; exactly these values are taken by default
; ------------------------------------------------------------
.POWER VDD=5 VSS=0 ; Power nodes
.CLOCK 1meg ; Micro clock
.STORE 250m ; Trace (micro+signals) storage time
; Micro nodes: RESET, AREF, PB0-PB7, PC0-PC6, PD0-PD7, ACO, TIM1OVF, ADC6, ADC7
; Define here the hardware around the micro
; ------------------------------------------------------------
R1 VDD AREF 1 ;Set reference voltage
Rin0 PC0 node_v0 100K ; conect Slider-1 to PC0 (Analog ch0)
Vin0 node_v0 VSS SLIDER_1(0 5) ; Slider 1 in Control Panel from 0 to 5V
.plot v(pc0) v(node_v0)
ไฟล์ adc-1.c
#include <avr\io.h> // Most basic include files
#include <avr\interrupt.h> // Add the necessary ones
#include <avr\signal.h> // here
#include <util/delay.h>
unsigned int adc0;
// It is recommended to use this coding style to
// follow better the mixed C-assembly code in the
// Program Memory window
//
// ***********************************************************
// Main program
//
int main(void)
{
ADCSRA = (1<<ADEN)|(0<<ADATE); // ADC Enable & Auto Trigger Disable
ADCSRA |= (0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0); // XTAL/8
while(1)
{ // Infinite loop; define here the
ADMUX = 0b0000000; //Aref,left adjust, select ADC0 (bit 2=0 bit 1 = 0 bit 0 = 0)
ADCSRA |= (1<<ADSC); // ADC Start Conversion
while (!(ADCSRA &(1<<ADIF))); // Wait Coversion completes
adc0 = ADCW; // Read ADC
_delay_ms(10);
}
}