Hallo zusammen Ich beschaeftige mich erst seit kurzem mit PICs und habe nun folgendes Problem bei dem ich einfach nicht weiterkomme. Ich habe einen PIC16F690 und einen seperaten MCP3551 AD Wandler mit 22 Bit, beides vom Hersteller Microchip. Verbunden ist das ganze ueber die SPI Schnittstelle. Also CLK, CS und SDO des AD Wandlers mit den entsprechenden uC Pins verbunden, Vref vom AD Wandler liegt direkt parallel mit Vdd an +5V. Leider liefert mein Programm einfach keinen Wert zurueck, egal was ich am AD Wandler fuer eine Spannung messe, der Rueckgabewert ist immer null. Vermutlich habe ich einen Fehler bei der Programmierung der SPI Schnittstelle gemacht, oder etwas falsch initialisiert?! ich hoffe es faellt jemandem etwas ein... Vielen Dank!! Gruesse!
1 | #include <pic.h> |
2 | |
3 | |
4 | __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \ |
5 | & UNPROTECT & BORDIS & IESODIS & FCMDIS); |
6 | |
7 | |
8 | // Variables
|
9 | long int whatIwant; |
10 | |
11 | #define FOSC 8000000L // Internal Oscillator to 8Mhz
|
12 | #define CS RA4 // AD Converter Pin
|
13 | #define SDI RB4 // AD Converter Pin
|
14 | #define SCK RB6 // AD Converter Pin
|
15 | |
16 | |
17 | |
18 | |
19 | |
20 | // Read AD Converter Function
|
21 | long int Read3551(void) |
22 | {
|
23 | short i; |
24 | long int result=0; |
25 | char byte[3], TrashByte; // Temp Bytes for reading |
26 | |
27 | CS=0; // AD Chip Select low to read |
28 | while (SDI==1); // Wait for SDO/RDY to become low (AD internal conversion complete) |
29 | for (i=0; i<3; i++) // Loop for reading Byte 1, 2 and 3 |
30 | {
|
31 | SSPBUF=0b00001111; // Garbage send to start clocking in AD Data Byte 3 |
32 | while (BF==0); // Wait for Buffer Full Status Bit to be set |
33 | byte[i]=SSPBUF; // get byte |
34 | }
|
35 | |
36 | if (byte[2] < 32) // Last 3 Bits must be zero (no overflow and no negative sign) |
37 | result = ((byte[0] * 65536) + (byte[1] * 256) + (byte[2] * 1)); //Add the Bytes to get the whole Value |
38 | |
39 | CS=1; // AD Chip select high to stop AD sending |
40 | return result; // return the Value |
41 | }
|
42 | |
43 | |
44 | |
45 | |
46 | |
47 | main() // ------- MAIN ------- |
48 | {
|
49 | |
50 | // declaration
|
51 | |
52 | OSCCON = 0x75; // Oscillator to 8 Mhz |
53 | ANSEL = 0; // Turn off ADC |
54 | TRISB6= 0; // SCK (AD) is Output (Master Mode) |
55 | TRISA4= 0; // RA4 (CS from AD) is Output |
56 | CS=1; // CS high (AD stops sending) |
57 | SSPCON= 0b00110000; // 00, Enable SPI (SSPEN) , Idle State for SDI clock is high level (CKP), Fosc:4 |
58 | SMP= 0; // SPI Input Data sampled at middle of data output time |
59 | CKE= 0; // SDI Data transmitted on rising Edge of SCK |
60 | |
61 | |
62 | |
63 | // Infinite loop with main program
|
64 | |
65 | for (;;) |
66 | whatIwant=Read3551(); |
67 | |
68 | } // End of MAIN |