Forum: Mikrocontroller und Digitale Elektronik [PIC] 18F46K20 A/D Wandler Problem


von Andreas R. (blackpuma)


Lesenswert?

Hallo Forum!

Ich habe ein Problem bei meinem PIC. Ich verwende einen PIC18F46K20 und 
möchte die Spannung an Kanal 5 messen. Es liegt eine Spannung von 1,29V 
an. Leider misst mein Controller irgendwas. Kann mir jemand sagen was 
ich beachten muss bzw. was an meinem Programm falsch ist.
1
/** I N C L U D E S ********************************/
2
#include <p18f46K20.h>
3
#include <usart.h>
4
#include <adc.h>
5
#include <delays.h>
6
#include <stdlib.h>
7
8
/** Configuration Word ******************************
9
 *
10
 * PIC18F46K20 mit externem Quarz.
11
 *
12
 */
13
        #pragma config FOSC     = HS
14
        #pragma config FCMEN    = OFF
15
        #pragma config IESO     = OFF
16
        #pragma config PWRT     = OFF
17
  #pragma config BOREN  = OFF
18
        #pragma config BORV     = 30
19
        #pragma config WDTEN  = OFF
20
        #pragma config WDTPS    = 32768
21
        #pragma config MCLRE    = ON
22
        #pragma config LPT1OSC  = OFF
23
        #pragma config PBADEN   = OFF
24
//      #pragma config CCP2MX   = ON
25
        #pragma config STVREN   = ON
26
        #pragma config LVP      = OFF
27
//      #pragma config ICPRT    = OFF       // Dedicated In-CircuitDebug/Programming
28
        #pragma config XINST    = OFF       // Extended Instruction Set
29
        #pragma config CP0      = OFF
30
        #pragma config CP1      = OFF
31
//      #pragma config CP2      = OFF
32
//      #pragma config CP3      = OFF
33
        #pragma config CPB      = OFF
34
//      #pragma config CPD      = OFF
35
        #pragma config WRT0     = OFF
36
        #pragma config WRT1     = OFF
37
//      #pragma config WRT2     = OFF
38
//      #pragma config WRT3     = OFF
39
        #pragma config WRTB     = OFF       // Boot Block Write Protection
40
        #pragma config WRTC     = OFF
41
//      #pragma config WRTD     = OFF
42
        #pragma config EBTR0    = OFF
43
        #pragma config EBTR1    = OFF
44
//      #pragma config EBTR2    = OFF
45
//      #pragma config EBTR3    = OFF
46
        #pragma config EBTRB    = OFF
47
48
/** D E C L A R A T I O N S ********************/
49
#pragma code
50
51
/** F U N C T I O N S **************************/
52
53
// **********************************************
54
//
55
// Hauptprogramm
56
//
57
// **********************************************
58
void main()
59
{
60
  char str[20];
61
62
//Einstellen der Ports
63
// 0 = Output
64
// 1 = Input
65
TRISD = 0x0F; // PORT D
66
TRISC = 0x00; // PORT C Ausgang
67
68
// USART Einstellen
69
OpenUSART(  USART_TX_INT_OFF &
70
      USART_RX_INT_OFF &
71
      USART_ASYNCH_MODE &
72
      USART_EIGHT_BIT &
73
      USART_CONT_RX &
74
      USART_BRGH_LOW, 18 );
75
76
// ADC Einstellen
77
OpenADC( ADC_FOSC_32    &
78
         ADC_RIGHT_JUST &
79
         ADC_12_TAD,
80
         ADC_CH5        &
81
         ADC_INT_OFF, 15 );
82
83
while(1) {
84
85
  // Nur ausführen wenn Taster gedrückt wird.
86
  if( PORTDbits.RD0 == 0 )
87
  {
88
    SetChanADC( ADC_CH5 );
89
90
    Delay10TCYx( 5 );
91
    ConvertADC();
92
    while( BusyADC() );        // Warten bis A/D Wandler fertig ist.
93
94
    ultoa( ADRESH, str );
95
    putsUSART( str );
96
    while( BusyUSART() );
97
    ultoa( ADRESL, str );
98
    putsUSART( str );
99
    while( BusyUSART() );
100
    WriteUSART( 0x0D );        // Zeilenanfang
101
    while( BusyUSART() );
102
    WriteUSART( 0x0A );        // Neue Zeile
103
    while( BusyUSART() );
104
105
    while( PORTDbits.RD0 == 0);   // Erst weiter wenn Taste Losgelassen wird.
106
    Delay10TCYx( 5 );         // Entprellen
107
    PORTDbits.RD7 ^= 1;
108
  }
109
}
110
111
// Programmende
112
}

BG
Andreas

von Andreas R. (blackpuma)


Lesenswert?

Hallo!

Ich habe gestern Abend noch ein wenig herum probiert aber ich bin auf 
keine Lösung gekommen. Bei openADC habe ich die 15 durch die 32 ersetzt. 
Jetzt ist AN5 als Analoger Eingang konfiguriert. Ich "messe", wenn das 
überhaupt ein Messergebnis ist, noch immer den selben Wert.

Wo liegt der Fehler?

BG
Andreas

von Andreas R. (blackpuma)


Lesenswert?

Fehler gefunden!

Laut Datenblatt sollen die Referenzspannungen nach einem Reset auf VSS 
und VDD liegen. Bei mir ist es aber so das sie auf den 
Referenzspannungspins liegen. Komische sache.

BG
Andreas

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.