'************************************************************************** '* Name : ADC-Summer '* Function : First Steps Example '* Hardware : aTeVaL Evaluation Board - http://www.ehajo.de '************************************************************************** '* Compiler : lunaAVR 2013.r6 (or newer) '************************************************************************** '* Author : rgf, avr@myluna.de '* Date/Version: 06.12.2013 '* www : http://avr.myluna.de '************************************************************************** ' µC-Belegung am Beispiel Atmega328p ' Poti: PortC.0 !The bottom ADC jumper must be plugged on the aTeVaL! ' Summer: PortD.7 !The solder bridge "SPKR" on the solder side must be closed! '************************************************************************** avr.device = atmega328p 'Set controller type avr.clock = 8000000 'To tell the compiler at what speed the controller works. avr.stack = 64 'Set the program stack ' -> See "avr": http://avr.myluna.de/doku.php?id=en:avr #library "Library/Adc.interface" ' -> See "Libraries": http://avr.myluna.de/doku.php?id=en:libraries 'Alias the port pins of potentiometer and the summer with an identifier. 'This for readability in the source code. #define POTI as PortC.0 #define BUZZER as PortD.7 ' -> See "#defines": http://avr.myluna.de/doku.php?id=en:define 'A variable unsigned 16-bit add to receive the ADC reading dim adc_wert as word POTI.mode = input 'poti as input BUZZER.mode = output,low 'buzzer as output ' -> See "Port": http://avr.myluna.de/doku.php?id=en:port 'We want the voltage value of the potentiometer on the ADC0 (pin 23 from controller) 'Configure the analog-digital converter of the controller. 'Division factor of the ADC, ADC frequency should be btw 50 and 200 kHz. 'In this case, divider 64, so then 8MHz / 64 = 125kHz Adc.Clock = 64 'AVCC (also +5V used as a voltage reference) Adc.Reference = avcc 'select channel 0 (ADC0) Adc.Channel = 0 'ADC enable Adc.Enable 'After turning on a dummy conversion is recommended. 'start conversion adc_wert = Adc.Value 'Main loop do 'read value adc_wert = Adc.Value 'Wait so many microseconds as the ADC value is long_delay( adc_wert ) 'Invert pin of buzzer BUZZER.toggle loop 'Sub program procedure long_delay(us as word) 'so many us wait as the function were given while us 'Loop to run as long as us <> 0 waitus 1 'wait 1µs us-- 'Variable "us" decrement wend endproc