1 | /*******************************************************
|
2 | * Ansteuerung des 12 Kanal AD-Wandlers auf dem Messmodul
|
3 | *******************************************************
|
4 | * Filename: Messmodul.c
|
5 | * Author:
|
6 | * History:
|
7 | * 17.05.2011 CL v1.0 PIC18
|
8 | *******************************************************/
|
9 |
|
10 | // Adds support for PIC18 Peripheral library functions and macros
|
11 | #include <p18f2580.h>
|
12 | #include <MAX1228.h>
|
13 | #include <Defines.h>
|
14 | #include <math.h>
|
15 | #include <ctype.h>
|
16 | #include <stdlib.h>
|
17 | #include <reset.h>
|
18 | #include <stdio.h>
|
19 | #include <delays.h>
|
20 | #include <spi.h>
|
21 |
|
22 | // Configuration Bits
|
23 | #pragma config WDT = OFF
|
24 | #pragma config OSC = HS
|
25 | #pragma config FCMEN = ON
|
26 | #pragma config XINST = OFF
|
27 | #pragma config LVP = OFF
|
28 | #pragma config MCLRE = ON
|
29 |
|
30 |
|
31 | unsigned char conv_byte = 0;
|
32 | unsigned char setup_byte = 0;
|
33 | int average_byte = (MAX1228_AVERAGE_4);
|
34 | unsigned char SPI_Send;
|
35 | unsigned char SPI_Recv;
|
36 | unsigned char sync_mode=0;
|
37 | unsigned char bus_mode=0;
|
38 | unsigned char smp_phase=0;
|
39 | unsigned char w=0;
|
40 | unsigned char TempVar;
|
41 |
|
42 | void main(void)
|
43 | {
|
44 | TRISAbits.TRISA5 = 0;
|
45 | TRISBbits.TRISB0 = 0;
|
46 | TRISCbits.TRISC1 = 0;
|
47 | TRISCbits.TRISC6 = 1;
|
48 | PORTBbits.RB0 = 1;
|
49 | PORTCbits.RC1 = 0;
|
50 | CS = 1;
|
51 | // conv_byte = (MAX1228_CONV_AIN11 | MAX1228_CONV_SCAN_00_N);
|
52 | // setup_byte = (MAX1228_SETUP_INTCLK | MAX1228_SETUP_INTREF_ACTIVE);
|
53 |
|
54 | sync_mode = SPI_FOSC_16 ;
|
55 | bus_mode = MODE_01;
|
56 | smp_phase = SMPMID;
|
57 |
|
58 | OpenSPI(sync_mode,bus_mode,smp_phase);
|
59 |
|
60 | TempVar = SSPBUF; // Clears BF
|
61 | SSPCON1bits.WCOL = 0;
|
62 | CS = 0;
|
63 | SSPBUF =0b00010000; //Reset Byte
|
64 | while(!SSPSTATbits.BF);
|
65 | CS = 1;
|
66 | Delay10TCY();
|
67 | Delay10TCY();
|
68 | Delay10TCY();
|
69 | Delay10TCY();
|
70 |
|
71 |
|
72 | TempVar = SSPBUF; // Clears BF
|
73 | SSPCON1bits.WCOL = 0;
|
74 | CS = 0;
|
75 | SSPBUF =0b01101000; //Setup Byte
|
76 | while(!SSPSTATbits.BF);
|
77 | CS = 1;
|
78 | Delay10TCY();
|
79 | Delay10TCY();
|
80 | while(1)
|
81 | {
|
82 | TempVar = SSPBUF; // Clears BF
|
83 | SSPCON1bits.WCOL = 0;
|
84 | CS = 0;
|
85 | SSPBUF = 0b11011000; //Conversion Byte
|
86 | //SSPBUF = 0b00011011;
|
87 | while(!SSPSTATbits.BF);
|
88 | CS = 1;
|
89 | Delay10TCY();
|
90 | Delay10TCY();
|
91 | Delay10TCY();
|
92 | Delay10TCY();
|
93 | }
|
94 | }
|