German English
Peripherals

ADC - Sample anlog values of multiple channels time triggered

The used microcontroller on the radio module Preon32 provides a several ADCs (analog-to-digital converters) instances which can be used to sample analog values to digital ones.

This example uses one of the available ADC instances. Each ADC instance has multiple channels. Each channel is internally connected to a GPIO pin. 6 of the channels are used to sample values. All used channels are sampled each 50 milliseconds. The values are stored to a value buffer. The value buffer is filled continuously. Events are generated if the buffer is half full or full.
/*
 * Copyright (c) 2011., Virtenio GmbH
 * All rights reserved.
 */

package com.virtenio.webdemo;

import com.virtenio.drivers.adc.*;
import com.virtenio.drivers.timer.NativeTimer;
import com.virtenio.drivers.timer.PeriodicTimer;

import java.io.IOException;

public class multiAdc {

    public static void main() throws IOException, InterruptedException {

        // ADC-Instanz aus dem Mikrocontroller
        com.virtenio.drivers.adc.ADC adc = NativeADC.getInstance(0);

        // Kennung der ADC-Kanäle
        short[] channels = {0, 1, 2, 3, 4, 5};
        ADCChannelList channelList = new ADCChannelList(adc, channels);

        // Speicherplatz für Messwerte
        short[] buffer = new short[1024];

        // Ereignis-Listener für Buffer-Füllstand
        ADCSamplerListener listener = new ADCSamplerListener() {
            public void adcSamplerNotify(int event) {
                if (event == ADCSamplerListener.HALF_FULL) {
                    System.out.println("HALF FULL");
                } else {
                    System.out.println("FULL");
                }
            }
        };
        channelList.openChannels();

        // zeitgesteuerte Datenerfassung
        TriggeredADCSampler triggerSampler
                = new TriggeredADCSampler(channelList, buffer, listener);
        int millis = 50;
        PeriodicTimer timer = new PeriodicTimer(NativeTimer.getInstance(0), millis, 0);
        TimerADCSampler timerSampler = new TimerADCSampler(triggerSampler, timer);
        timerSampler.start();

        // Datenerfassung läuft nebenläufig, so dass das Hauptprogramm andere Funktionen
        // durchführen kann
        while (true) {
            System.out.println(triggerSampler.getOffset());
            Thread.sleep(500);
        }
    }
}