Hallo,
ich versuche grade den ADC vom dsPIC zum "laufen" zu bringen, leider
bisher erfolglos.
Den Code packe ich hier einfach mal rein:
1 | void Init_ADC(void)
|
2 | {
|
3 | //ADCON1 Register
|
4 | //Set up A/D for Automatic Sampling
|
5 | //Use Timer3 to provide sampling time
|
6 | //Set up A/D conversrion results to be read in 1.15 fractional
|
7 | //number format.
|
8 | //All other bits to their default state
|
9 | ADCON1bits.FORM = 3;
|
10 | ADCON1bits.SSRC = 2;
|
11 | ADCON1bits.ASAM = 1;
|
12 |
|
13 | //ADCON2 Register
|
14 | ADCON2bits.VCFG = 0; //Internal V+ / V-
|
15 | ADCON2bits.SMPI = 15; //Set up A/D for interrupting after 16 samples get filled in the buffer
|
16 |
|
17 | //ADCON3 Register
|
18 | //Total Conversion Time= 1/Sampling Rate = 100 microseconds
|
19 | //For fosc=117.968MHz, Tcy = 33.91 ns = Instruction Cycle Time
|
20 | //We will set up Sampling Time using Timer3 & Tad using ADCS<5:0> bits
|
21 | //All other bits to their default state
|
22 | //Let's set up ADCS arbitrarily to 38
|
23 | //So Tad = Tcy*(ADCS+1)/2 = 661.2 nanoseconds
|
24 | //So, the A/D converter will take 14*Tad periods to convert each sample
|
25 | ADCON3bits.ADCS = 38;
|
26 |
|
27 | //Set up Timer 3 Trigger
|
28 |
|
29 | TMR3 = 0x0000;
|
30 | PR3 = 0x0BAF;
|
31 | IFS0bits.T3IF = 0;
|
32 | IEC0bits.T3IE = 0;
|
33 | T3CONbits.TCS = 0;
|
34 | T3CONbits.TCKPS=0;
|
35 |
|
36 | //ADCHS Register
|
37 | //Set up A/D Channel Select Register to convert AN5 on Mux A input
|
38 | ADCHS = 0x0005;
|
39 |
|
40 |
|
41 | //ADCSSL Register
|
42 | //Channel Scanning is disabled. All bits left to their default state
|
43 | ADCSSL = 0x0000;
|
44 |
|
45 | //ADPCFG Register
|
46 | //Set up channels AN5 as analog inputs and configure rest as digital
|
47 | //entered main() out of reset
|
48 | ADPCFG = 0xFFFF;
|
49 | ADPCFGbits.PCFG5 = 0;
|
50 |
|
51 | //Clear the A/D interrupt flag bit
|
52 | IFS0bits.ADIF = 0;
|
53 |
|
54 | //Set the A/D interrupt enable bit
|
55 | IEC0bits.ADIE = 1;
|
56 |
|
57 | //Turn on the A/D converter
|
58 | //This is typically done after configuring other registers
|
59 | ADCON1bits.ADON = 1;
|
60 |
|
61 | //Start Timer 3
|
62 | T3CONbits.TON = 1;
|
63 | }
|
64 |
|
65 | void __attribute__((__interrupt__)) _ADCInterrupt(void)
|
66 | {
|
67 | //Clear the Timer3 Interrupt Flag
|
68 | IFS0bits.T3IF = 0;
|
69 |
|
70 | int i = 0;
|
71 |
|
72 | //Clear the A/D Interrupt flag bit or else the CPU will
|
73 | //keep vectoring back to the ISR
|
74 | IFS0bits.ADIF = 0;
|
75 |
|
76 | //Put the A/D conversion results to sigCmpx.real
|
77 | adcPtr = &ADCBUF0 ;
|
78 |
|
79 | for (i=0;i<16;i++)
|
80 | {
|
81 | (*p_sigCmpx).imag = 0x0000;
|
82 | (*p_sigCmpx++).real = *adcPtr++;
|
83 | }
|
84 |
|
85 | if (p_sigCmpx > &sigCmpx[FFT_BLOCK_LENGTH-1])
|
86 | {
|
87 | SamplesReadyFlag ++; // Sampling completed
|
88 | IEC0bits.ADIE = 0; // Disable ADC interrupt
|
89 | }
|
90 |
|
91 | }
|
Mein Problem ist, dass meine Ergebnisse einfach nicht stimmen. Im
AD-Buffer stehen immer die gleichen Werte egal was ich an AN5 anstelle.
Ansonsten scheint der Code ganz gut zu "laufen", zumindest kann ich in
die Interrupt-Routine reindebuggen.
Ich weiss leider auch nicht was ich jetzt noch großartig verändern soll,
eigentlich sind alle wichtigen Einstellungen vorgenommen wurden.
Gruß Gustav