Forum: Mikrocontroller und Digitale Elektronik PIC24 Problem mit dem SPI und dem MCP2301


von Franzi (Gast)


Lesenswert?

Ja Hi,

Ich möchte die 12 Bit von meinem A/D Wandler MCP3201 per SPI rauslesen 
bloß kriege ich immer den Wert 0 raus, kann mir vielleicht jemand 
weiterhelfen und mir sagen woran das liegen könnte.
Ich währe für eure Hilfe sehr dankbar.


1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <p24FJ128GB108.h>
4
#include <spi.h>
5
#include <xc.h>
6
7
// CONFIG3
8
#pragma config WPFP = WPFP511           // Write Protection Flash Page Segment Boundary (Highest Page (same as page 85))
9
#pragma config WPDIS = WPDIS            // Segment Write Protection Disable bit (Segmented code protection disabled)
10
#pragma config WPCFG = WPCFGDIS         // Configuration Word Code Page Protection Select bit (Last page(at the top of program memory) and Flash configuration words are not protected)
11
#pragma config WPEND = WPENDMEM         // Segment Write Protection End Page Select bit (Write Protect from WPFP to the last page of memory)
12
13
// CONFIG2
14
#pragma config POSCMOD = HS             // Primary Oscillator Select (HS oscillator mode selected)
15
#pragma config DISUVREG = OFF           // Internal USB 3.3V Regulator Disable bit (Regulator is disabled)
16
#pragma config IOL1WAY = OFF            // IOLOCK One-Way Set Enable bit (Unlimited Writes To RP Registers)
17
#pragma config OSCIOFNC = OFF           // Primary Oscillator Output Function (OSCO functions as CLKO (FOSC/2))
18
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Both Clock Switching and Fail-safe Clock Monitor are disabled)
19
#pragma config FNOSC = PRI              // Oscillator Select (Primary oscillator (XT, HS, EC))
20
#pragma config PLL_96MHZ = ON           // 96MHz PLL Disable (Enabled)
21
#pragma config PLLDIV = DIV2            // USB 96 MHz PLL Prescaler Select bits (Oscillator input divided by 2 (8MHz input))
22
#pragma config IESO = OFF               // Internal External Switch Over Mode (IESO mode (Two-speed start-up)disabled)
23
24
// CONFIG1
25
#pragma config WDTPS = PS32768          // Watchdog Timer Postscaler (1:32,768)
26
#pragma config FWPSA = PR128            // WDT Prescaler (Prescaler ratio of 1:128)
27
#pragma config WINDIS = OFF             // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
28
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (Watchdog Timer is disabled)
29
#pragma config ICS = PGx1               // Comm Channel Select (Emulator functions are shared with PGEC1/PGED1)
30
#pragma config GWRP = OFF               // General Code Segment Write Protect (Writes to program memory are allowed)
31
#pragma config GCP = OFF                // General Code Segment Code Protect (Code protection is disabled)
32
#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG port is disabled
33
34
void wait()
35
    {
36
      int del;
37
        for(del = 500; del>= 0; del--);
38
    }
39
40
void INITCSMCP3201PIN(void)   
41
{
42
    TRISBbits.TRISB4 = 0;
43
    LATBbits.LATB4 = 1;
44
}
45
46
47
void INIT_SPI1(void)
48
{
49
  IFS0bits.SPI1IF = 0;
50
  IEC0bits.SPI1IE = 0;
51
  IPC2bits.SPI1IP = 0;
52
53
  //   SPI1CON1 setting
54
  SPI1CON1bits.DISSCK = 0;      // Internal SPI clock enable
55
  SPI1CON1bits.DISSDO = 0;      //  SDO pin is controlled by the module
56
  SPI1CON1bits.MODE16 = 1;      // 16 bits wide
57
  SPI1CON1bits.SMP = 0;         // input data sampled at the middle of data ouput time
58
  SPI1CON1bits.CKE = 0;         // from idle clock to active clock
59
  SPI1CON1bits.SSEN = 0;        // SSx pin not used by the module
60
  SPI1CON1bits.CKP = 0;         // idle state clock is low level
61
  SPI1CON1bits.MSTEN=1;         // master mode
62
  SPI1CON1bits.SPRE = 7;        // secondary prescal 1:1
63
  SPI1CON1bits.PPRE = 3;        //  primary prescale 1:1
64
65
// SPI1CON2 setting
66
  SPI1CON2bits.FRMEN = 1;       // framed SPI support enable
67
  SPI1CON2bits.SPIFSD = 0;      // frame sync pulse output Master
68
  SPI1CON2bits.SPIFPOL = 0;     // framesync pulse is active low
69
  SPI1CON2bits.SPIFE = 0,       // framesync pulse precedes first bit clock
70
  SPI1CON2bits.SPIBEN = 0;      // enhanced buffer disable
71
72
// SPI1STAT setting
73
 SPI1STATbits.SPIEN = 0;        //inititally disable
74
 SPI1STATbits.SPISIDL = 0;      //continue module operation in idle mode
75
 SPI1STATbits.SPIROV = 0;       //no overflow has occured
76
}
77
78
unsigned int RECEIVED_SPI1(void)
79
{
80
    LATBbits.LATB4 = 0;
81
    unsigned int mrdata=0;
82
    mrdata = SPI1BUF;
83
    while (SPI2STATbits.SPIRBF);
84
    LATBbits.LATB4 = 1;
85
    return mrdata;
86
}
87
88
unsigned int wert;
89
90
91
int main(void) {
92
93
    INITCSMCP3201PIN();
94
    INIT_SPI1();
95
96
    SPI1BUF=0;
97
    SPI1STATbits.SPIEN = 1;
98
99
    while(1)
100
    {
101
    wait();
102
    wert = RECEIVED_SPI1();
103
    wait();
104
    }
105
    return 0;
106
107
}

MFG Franzi

von holger (Gast)


Lesenswert?

>kann mir vielleicht jemand
>weiterhelfen und mir sagen woran das liegen könnte.

Du hast SPI nicht verstanden. Der Slave sendet
dir erst was wenn der Master einen Takt liefert.
Das tut er nur wenn er etwas sendet, und wenn
es nur ein Dummybyte ist.

von Franzi (Gast)


Lesenswert?

Das heißt das ich ihm erst einen dummybyte schicken muss?

von holger (Gast)


Lesenswert?

>Das heißt das ich ihm erst einen dummybyte schicken muss?

Ja.

von Franzi (Gast)


Lesenswert?

Auch wenn ich ein Dummy sende kriege ich nichts zurück!
Kann mir sonst noch jemand helfen?
1
    LATBbits.LATB4 = 0;
2
    unsigned int dummy = 0xFF;
3
    SPI1BUF = dummy;
4
    while (SPI2STATbits.SPIRBF);
5
    LATBbits.LATB4 = 1;
6
    return SPI1BUF;

von Zdenek R. (zdenek)


Lesenswert?

Du hast 16 bit Mod aktiviert.
Nur für "Ordnung" :
unsigned int dummy = 0xFFFF;

Kuge hier:
http://ww1.microchip.com/downloads/en/DeviceDoc/21290D.pdf

Mache ein Test - Überbrücke auf MCU MOSI und MISO (Ohne Wandler...) und 
sende
dummy = 0x1234; oder etwas. Diese Daten musst du auflesen.

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.