' Analog to Digital Conversion (ADC) routine for the DARC Board ' uses the ATMega8535 chip ' author: kerwin lumpkins ' this file calls an init_adc sub that initializes the adc module ' then it runs a loop that gets the adc value on channel 7 (pin A7) ' every 1/2 second (500 ms) ' put in the cal value for your part here (marked on the 8535 case ' most are hex 9F, some 91's and some in between Osccal = &H9f $regfile = "m8535.dat" Declare Sub Init_adc Declare Function Get_darc_adc(byval Channel As Byte) As Integer Dim Adc_value As Integer Dim Channel As Byte Call Init_adc Do Channel = 7 Print "getting the adc" Adc_value = Get_darc_adc(channel) Print "Channel " ; Channel ; " value " ; Adc_value Print " " Waitms 500 Loop Sub Init_adc ' set for AVCC reference voltage,Right justified,channel 0 Admux = &H40 'set ad status and control register for conversion disabled, conversion 'not started,auto trigger disabled,interrupts disabled,and divide 'by 64 prescaler Adcsra = &H06 End Sub Init_adc Function Get_darc_adc(byval Channel As Byte) As Integer Dim Flag As Byte Dim Adcnotdone As Bit 'init the admux for channel 0 Admux = Admux And &HE0 Admux = Admux + Channel 'enable the adc Adcsra = &H86 'start the conversion Adcsra = &HC6 Adcnotdone = 1 While Adcnotdone = 1 Flag = Adcsra And &H40 'wait until the adc interrupt flag is set If Flag = 0 Then Adcnotdone = 0 Wend Dim Adcvalh As Integer Dim Adcvall As Integer Dim Adcval As Integer Adcvalh = 0 Adcvall = 0 Adcval = 0 'get the 2 adc hi and lo bytes and combine into one integer value Adcvall = Adcl Adcvalh = Adch Shift Adcvalh , Left , 8 Adcval = Adcvalh + Adcvall 'Print "adcvalh:" ; Adcvalh ; " adcvall: " ; Adcvall ; " adcval = " ; Adcval 'disable conversion Adcsra = &H06 Get_darc_adc = Adcval End Function Get_darc_adc(byval Channel As Byte)