;
;    Mecrisp - A native code Forth implementation for MSP430 microcontrollers
;    Copyright (C) 2011  Matthias Koch
;
;    This program is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;


; Zugriff auf den ADC10 - so einfach wie möglich !
 
;
; ADC10 - Register und Konstanten
;

;------------------------------------------------------------------------------
; Register 
;------------------------------------------------------------------------------

ADC10AE0       equ  004Ah    ; input enable register 0, Byte
ADC10AE1       equ  004Bh    ; input enable register 1, Byte
ADC10CTL0      equ  01B0h    ; control register 0
ADC10CTL1      equ  01B2h    ; control register 0
ADC10MEM       equ  01B4h    ; memory
ADC10DTC0      equ  0048h    ; data transfer control register 0
ADC10DTC1      equ  0049h    ; data transfer control register 1
ADC10SA        equ  01BCh    ; data transfer start address

;------------------------------------------------------------------------------
; Konstanten für den Control Register 0
;------------------------------------------------------------------------------

; Auswahl der Spannungsreferenz, Bit 15-13
                 ; 5432109876543210
SREF_0         equ 0000000000000000b ; VR+ = AVCC and VR- = AVSS
SREF_1         equ 0010000000000000b ; VR+ = VREF+ and VR- = AVSS
SREF_2         equ 0100000000000000b ; VR+ = VEREF+ and VR- = AVSS
SREF_3         equ 0110000000000000b ; VR+ = buffered VEREF+ and VR- = AVSS
SREF_4         equ 1000000000000000b ; VR+ = AVCC and VR- = VREF-/VEREF-
SREF_5         equ 1010000000000000b ; VR+ = VREF+ and VR- = VREF-/VEREF-
SREF_6         equ 1100000000000000b ; VR+ = VEREF+ and VR- = VREF-/VEREF-
SREF_7         equ 1110000000000000b ; VR+ = buffered VEREF+ and VR- = VREF-/VEREF-

; sample-and-hold time, Bits 12-11
                 ; 5432109876543210
ADC10SHT_0     equ 0000000000000000b ; 4 x ADC10CLKs
ADC10SHT_1     equ 0000100000000000b ; 8 x ADC10CLKs
ADC10SHT_2     equ 0001000000000000b ; 16 x ADC10CLKs
ADC10SHT_3     equ 0001100000000000b ; 64 x ADC10CLKs
                 ; 5432109876543210
ADC10SC        equ 0000000000000001b
ENC            equ 0000000000000010b
ADC10IFG       equ 0000000000000100b
ADC10IE        equ 0000000000001000b
ADC10ON        equ 0000000000010000b
REFON          equ 0000000000100000b
REF2_5V        equ 0000000001000000b
MSC            equ 0000000010000000b
REFBURST       equ 0000000100000000b
REFOUT         equ 0000001000000000b
ADC10SR        equ 0000010000000000b


;------------------------------------------------------------------------------
; Konstanten für den Control Register 1
;------------------------------------------------------------------------------

; Eingangskanal, Bits 15-12
                 ; 5432109876543210
INCH_0         equ 0000000000000000b
INCH_1         equ 0001000000000000b
INCH_2         equ 0010000000000000b
INCH_3         equ 0011000000000000b
INCH_4         equ 0100000000000000b
INCH_5         equ 0101000000000000b
INCH_6         equ 0110000000000000b
INCH_7         equ 0111000000000000b
INCH_8         equ 1000000000000000b
INCH_9         equ 1001000000000000b
INCH_10        equ 1010000000000000b
INCH_11        equ 1011000000000000b
INCH_12        equ 1100000000000000b ; Selects Channel 11
INCH_13        equ 1101000000000000b ; Selects Channel 11
INCH_14        equ 1110000000000000b ; Selects Channel 11
INCH_15        equ 1111000000000000b ; Selects Channel 11

; sample-and-hold source select, Bits 11-10
                 ; 5432109876543210
SHS_0          equ 0000000000000000b ; ADC10SC - bit
SHS_1          equ 0000010000000000b ; TA OUT1
SHS_2          equ 0000100000000000b ; TA OUT0
SHS_3          equ 0000110000000000b ; TA OUT2

ADC10DF        equ 0000001000000000b ; Data format
ISSH           equ 0000000100000000b ; invert singnal sample-and-hold

; Taktteiler, Bits 7-5
                 ; 5432109876543210
ADC10DIV_0     equ 0000000000000000b
ADC10DIV_1     equ 0000000000100000b
ADC10DIV_2     equ 0000000001000000b
ADC10DIV_3     equ 0000000001100000b
ADC10DIV_4     equ 0000000010000000b
ADC10DIV_5     equ 0000000010100000b
ADC10DIV_6     equ 0000000011000000b
ADC10DIV_7     equ 0000000011100000b

; clock source select, Bits 4-3
                 ; 5432109876543210
ADC10SSEL_0    equ 0000000000000000b ; ADC10OSC
ADC10SSEL_1    equ 0000000000001000b ; ACLK
ADC10SSEL_2    equ 0000000000010000b ; MCLK
ADC10SSEL_3    equ 0000000000011000b ; SMCLK

; conversion sequenze mode select, Bits 2-1
                 ; 5432109876543210
CONSEQ_0       equ 0000000000000000b ; Single channel single conversion
CONSEQ_1       equ 0000000000000010b ; Sequence of channels
CONSEQ_2       equ 0000000000000100b ; Repeat single channel
CONSEQ_3       equ 0000000000000110b ; Repeat sequence of channels

ADC10BUSY      equ 0000000000000001b

;------------------------------------------------------------------------------
; Konstanten für den Data Transfer Control Register 0
;------------------------------------------------------------------------------
                 ; 5432109876543210
ADC10FETCH     equ 0000000000000001b
ADC10B1        equ 0000000000000010b
ADC10CT        equ 0000000000000100b
ADC10TB        equ 0000000000001000b

;------------------------------------------------------------------------------
  Wortbirne Flag_sichtbar, "analog"
AnalogLesen: ; Ließt einen Analogwert ein...
             ; ( Kanal -- Wert )
;------------------------------------------------------------------------------
  
  bic #ENC, &ADC10CTL0 ; Wandler zum Verstellen ausschalten !    
    
  ; Die obersten 4 Bits wählen zwischen den 16 möglichen Eingangskanälen.
  push r10
  mov @r4, r10
  and #1111b, r10
  swpb r10
  rla r10
  rla r10
  rla r10
  rla r10

  bis #ADC10SSEL_0+ADC10DIV_3+CONSEQ_0, r10 ; Einzelnen Wert aufnehmen, Taktquelle auswählen: ADC10OSC / 4
  mov r10, &ADC10CTL1 ; Kanal vom Datenstack holen und im ADC-MUX aktivieren
  pop r10
  
  ; Referenzquelle wählen und einschalten, Wandler einschalten,
  ; Sample-and-Hold-Zeit wählen, lange Messen.
  ;mov #SREF_0+ADC10SHT_3+ADC10ON, &ADC10CTL0                  ; Referenz Vcc
  ;mov #SREF_1+REFON+ADC10SHT_3+ADC10ON, &ADC10CTL0            ; Referenz 1,5V
  mov #SREF_1+REFON+REF2_5V+ADC10SHT_3+ADC10ON, &ADC10CTL0    ; Referenz 2,5V
                                                     
  bis #ENC+ADC10SC, &ADC10CTL0 ; Start sampling/conversion

- bit #ADC10BUSY, &ADC10CTL1   ; ADC10BUSY? Wandlungsende abwarten
  jnz -

  mov &ADC10MEM, @r4 ; Wandlungsergebnis zurückliefern.
  
  ret