Diese Beispiel zeigt wie der A/D Wandler verwendet wird.

Ich hatte zu Beginn große Probleme weil OpenADC nicht mit | sondern mit & Verknüpft wurde.

 

#include <p18cxxx.h>
#include <usart.h>
#include <delays.h>
#include <adc.h>
#include <stdlib.h>

//-------------------------------Configuration setting ----------------------------------------------

// BAUD_RATE_GEN is calculated as  = [Fosc / (64 * Desired Baudrate)] - 1
// It needs to be changed depending upon oscillator frequency.
//  20.000.000 Hz / (16 * 9600 Baud) - 1 = 129 (approx.)
#define BAUD_RATE_GEN 129                    // Fosc = 20MHz, Baud Rate = 9600 bps

void main(void)
{
//-------------------------define pins------------------------------------------
    // We need to set port D, bit 0 as an output
    // Using TRISDbits instead of TRISD allows isolating a single bit leaving the other bits unchanged
    TRISDbits.TRISD3=1; // 0 = output, 1 = input
    TRISDbits.TRISD4=0; // 0 = output, 1 = input
    TRISEbits.TRISE0=1; // A/D input
   
    // Set port D, bit 3 to off
    PORTDbits.RD4=1;    // turn on LED

    //-------------------------configure USART ---------------------------------------------------------
    // API configures USART for desired parameters:
    //  - TX/RX interrupts turned off
    //  - Asynchronous mode
    //  - 8 bits
    //  - Continuous Receive Enabled
    //  - Low speed baud rate generator mode (Fosc / 16)
    OpenUSART(USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_HIGH, BAUD_RATE_GEN);

    //------------------------configure A/D converter--------------------------
    //
    // Last set ANSEL CH5 to Analog
    OpenADC(ADC_FOSC_16 | ADC_RIGHT_JUST | ADC_20_TAD, ADC_CH5 | ADC_INT_OFF | ADC_REF_VDD_VSS , 0b0000000000100000);
    int result = 0;

    unsigned char buffer[20];

    //------------------------start main loop----------------------------------
    while(1)                                // end of program
    {
        if( !PORTDbits.RD3 )
        {
            Delay1KTCYx(10);                // Entprellen
            while( !PORTDbits.RD3 );        // Entprellen

            SetChanADC(ADC_CH5);            // WICHTIG
            ConvertADC();                   // Starte konvertierung
            while( BusyADC() );             // warte bis fertig
            result = ReadADC();
           
            itoa( buffer, result, 10 );
            putrsUSART( buffer );
            putrsUSART( "\r\n" );

            PORTDbits.RD4 ^= 1;
        }

    }
    CloseUSART();
    CloseADC();
}

 

Das Programm sendet den gemessenen Wert auf Knopfdruck über die USART.

1 1 1 1 1 1 1 1 1 1 Rating 0.00 (0 Votes)