'--------------------------------------------------------------------
'                  ADC_INT.BAS
'  demonstration of GETADC() function in combintion with the idle mode
'  for a better noise immunity
'  Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------
$regfile = "4433def.dat"
$crystal = 4000000
$baud = 19200
'configure single mode and auto prescaler setting
'The single mode must be used with the GETADC() function

'The prescaler divides the internal clock by 2,4,8,15,32,64 or 128
'Because the ADC needs a clock from 50-200 KHz
'The AUTO feature, will select the highest clockrate possible
Config Adc = Single , Prescaler = Auto
'Now give power to the chip
On Adc Adc_isr Nosave
Enable Adc
Enable Interrupts



Dim W As Word , Channel As Byte

Channel = 0
'now read A/D value from channel 0
Do
  Channel = 0
  'idle will put the micro into sleep.
  'an interrupt will wake the micro.
  Start Adc
  Idle
  Stop Adc

  Print "Channel " ; Channel ; " value " ; W
  Waitms 500
Loop
End

Adc_isr:
  push r24
  in r24,sreg
  push r24
  push r25
  W = Getadc(channel)
  pop r25
  pop r24
  !out sreg,r24
  pop r24
Return
'The new M163 has options for the reference voltage
'For this chip you can use the additional param :
'Config Adc = Single , Prescaler = Auto, Reference = Internal
'The reference param may be :
'OFF      : AREF, internal reference turned off
'AVCC     : AVCC, with external capacitor at AREF pin
'INTERNAL : Internal 2.56 voltage reference with external capacitor ar AREF pin

'Using the additional param on chip that do not have the internal reference will have no effect.