1 | #include <p24HJ128GP202.h>
|
2 |
|
3 | #include <spi.h>
|
4 |
|
5 |
|
6 |
|
7 | /********
|
8 | ********************** ConfigIntSPI1 *************************/
|
9 |
|
10 |
|
11 | void ConfigIntSPI1( unsigned int config)
|
12 |
|
13 | {
|
14 |
|
15 | _SPI1IF = 0; // Clear IF bit
|
16 |
|
17 | _SPI1IP = (config &0x0007); // Assign interrupt priority
|
18 |
|
19 | _SPI1IE = (config &0x0008)>>3; // Interrupt Enable/Disable bit
|
20 |
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 | /******************************* SPI init ********************************/
|
26 |
|
27 |
|
28 | void SPI_init (char CSS )
|
29 | {
|
30 | // SPI1STATbits.SPIEN = 1; // Enables module and configures SCKx, SDOx, SDIx and SSx as serial port pins
|
31 | // SPI1STATbits.SPISIDL = 0; // Continue module operation in Idle mode
|
32 | SPI1STATbits.SPIROV = 0; // No overflow has occured => clear the receive overflow flag bit
|
33 | // SPI1STATbits.SPIRBF = 0; // Recieve is not complete, SPIxRXB is empty
|
34 |
|
35 | SPI1CON1bits.DISSDO = 1; // SDOx pin is not used by module; pin funktions as I/O
|
36 | SPI1CON1bits.MODE16 = 1; // Communication is word-wide (16 bits)
|
37 | SPI1CON1bits.SMP = 0; // input data sampled at middle of data output time
|
38 | SPI1CON1bits.CKE = 1; // Serial output data changes on transition from active clock state to idle clock state
|
39 | SPI1CON1bits.SSEN = 0; // Not used in Master mode
|
40 | SPI1CON1bits.CKP = 1; // Idle state for clock is a high level; active state is a low level
|
41 | SPI1CON1bits.MSTEN = 1; // Master mode
|
42 | // Prescaler Config = 512:1
|
43 | SPI1CON1bits.SPRE = 0; // secondary prescaler 8:1
|
44 | SPI1CON1bits.PPRE = 0; // primary prescaler 64:1
|
45 |
|
46 | SPI1CON2bits.FRMEN = 1; // Framed SPIx support disabled
|
47 | SPI1CON2bits.SPIFSD = 0; // Framed sync pulse output (master)
|
48 | SPI1CON2bits.FRMPOL = 0; // Frame sync pulse is active-low
|
49 | SPI1CON2bits.FRMDLY = 0; // Frame sync pulse precedes first bit clock
|
50 |
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | int main (void)
|
57 | {
|
58 |
|
59 | /************************** Clocksetting 80MIPS **********************************/
|
60 |
|
61 | CLKDIVbits.PLLPOST = 0b00; // N2 = 2
|
62 | CLKDIVbits.PLLPRE = 0b00010; // N1 = 4
|
63 | PLLFBDbits.PLLDIV = 0b000100000; // M = 32
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | TRISB = 0b0000001000000000; //set Input/Output Pins of RB
|
69 |
|
70 |
|
71 |
|
72 | /************************* SPI Remapping ************************************/
|
73 |
|
74 | RPINR20bits.SDI1R = 0b1001; // Setze SPI1 Data In auf Pin RP9
|
75 |
|
76 | RPOR7bits.RP14R = 0b01000; // Setze SPI1 Clock out auf Pin RP14
|
77 | RPOR6bits.RP13R = 0b01001; // Setze SPI1 SS out auf Pin RP13
|
78 |
|
79 |
|
80 | /************************* SPI Lesen ****************************************/
|
81 |
|
82 | int SPI_Read_Temp_1( void)
|
83 | {
|
84 | short Temp1 = 0x0000;
|
85 | if(SPI1STATbits.SPIRBF = 1)
|
86 | {
|
87 | SPI1STATbits.SPIROV = 0;
|
88 | SPI1BUF = Temp1;
|
89 | while(!SPI1STATbits.SPIRBF);
|
90 | Temp1 = SPI1BUF;
|
91 | return(Temp1);
|
92 | }
|
93 | }
|
94 |
|
95 | }
|