1 | #include "libpic30.h"
|
2 |
|
3 | #include <stdio.h>
|
4 | #include <stdlib.h>
|
5 | #include <stddef.h>
|
6 | #include <stdbool.h>
|
7 | #include "xc.h"
|
8 |
|
9 | #define FP 3685000
|
10 | #define BAUDRATE 9600
|
11 | #define BRGVAL (((FP/BAUDRATE)/16)-1)
|
12 | unsigned int i;
|
13 | #define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
|
14 |
|
15 | int befehl = 0;
|
16 | int dummy = 0;
|
17 |
|
18 | void InitGPIO(){
|
19 |
|
20 | __builtin_write_OSCCONL(OSCCON & ~(1<<6));
|
21 |
|
22 | RPOR6bits.RP56R = 0b000001;
|
23 | RPINR18bits.U1RXR = 57;
|
24 |
|
25 | ANSELB = 0;
|
26 |
|
27 | //Reset
|
28 | LATBbits.LATB9 = 0;
|
29 | ODCBbits.ODCB9 = 0;
|
30 | TRISBbits.TRISB9 = 0;
|
31 | //DIAG
|
32 | LATCbits.LATC4 = 0;
|
33 | ODCCbits.ODCC4 = 0;
|
34 | TRISCbits.TRISC4 = 0;
|
35 |
|
36 | //CS
|
37 | LATBbits.LATB0 = 0;
|
38 | ODCBbits.ODCB0 = 0;
|
39 | TRISBbits.TRISB0 = 0;
|
40 |
|
41 | __builtin_write_OSCCONL(OSCCON | (1<<6));
|
42 |
|
43 | PORTBbits.RB0 = 1;
|
44 | }
|
45 |
|
46 | void InitSPI(){
|
47 |
|
48 | /* The following code sequence shows SPI register configuration for Master mode */
|
49 | IFS0bits.SPI1IF = 0; // Clear the Interrupt flag
|
50 | IEC0bits.SPI1IE = 0; // Disable the interrupt
|
51 |
|
52 | // SPI1CON1 Register Settings
|
53 | SPI1CON1bits.DISSCK = 0; // Internal serial clock is enabled
|
54 | SPI1CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
|
55 | SPI1CON1bits.MODE16 = 1; // Communication is word-wide (16 bits)
|
56 | SPI1CON1bits.MSTEN = 1; // Master mode enabled
|
57 | SPI1CON1bits.SMP = 0; // Input data is sampled at the middle of data output time
|
58 | SPI1CON1bits.CKE = 0; // Serial output data changes on transition from
|
59 |
|
60 | // Idle clock state to active clock state
|
61 | SPI1CON1bits.CKP = 0; // Idle state for clock is a low level;
|
62 |
|
63 | // active state is a high level
|
64 | SPI1STATbits.SPIEN = 1; // Enable SPI module
|
65 | SPI1CON1bits.SSEN = 0;
|
66 |
|
67 | // Interrupt Controller Settings
|
68 | IFS0bits.SPI1IF = 0; // Clear the Interrupt flag
|
69 | IEC0bits.SPI1IE = 1; // Enable the interrupt
|
70 |
|
71 | DELAY_105uS
|
72 |
|
73 | }
|
74 |
|
75 | void InitUART(){
|
76 |
|
77 | U1MODEbits.STSEL = 0; // 1-Stop bit
|
78 | U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
|
79 | U1MODEbits.ABAUD = 0; // Auto-Baud disabled
|
80 | U1MODEbits.BRGH = 0; // Standard-Speed mode
|
81 | U1BRG = BRGVAL; // Baud Rate setting for 9600
|
82 | U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
|
83 | U1STAbits.UTXISEL1 = 0;
|
84 | IEC0bits.U1TXIE = 1; // Enable UART TX interrupt
|
85 | U1MODEbits.UARTEN = 1; // Enable UART
|
86 | U1STAbits.UTXEN = 1; // Enable UART TX
|
87 | /* Wait at least 105 microseconds (1/9600) before sending first char */
|
88 |
|
89 | DELAY_105uS
|
90 | }
|
91 |
|
92 | void UARTSendString(const char* str, const uint8_t length) {
|
93 | int i = 0;
|
94 | for (i=0 ; i<length && str[i]!='\0' ; i++) {
|
95 | UARTSendChar(str[i]);
|
96 | }
|
97 | }
|
98 |
|
99 | void UARTSendChar(const char c) {
|
100 | while (U1STAbits.TRMT == 0); // Wait for buffer to be empty
|
101 | U1TXREG = c;
|
102 | }
|
103 |
|
104 | int SPIWriteRead(int wert){
|
105 | SPI1BUF = wert;
|
106 | Nop();
|
107 | while (!SPI1STATbits.SPIRBF);
|
108 | return SPI1BUF;
|
109 | }
|
110 |
|
111 | int main(void)
|
112 | {
|
113 |
|
114 | if(OSSCON.HFIOFS == 1)
|
115 |
|
116 | InitGPIO();
|
117 | InitUART();
|
118 | InitSPI();
|
119 |
|
120 |
|
121 | char* str = "Hello\n\r";
|
122 | UARTSendString(str,29);
|
123 |
|
124 | PORTBbits.RB9 = 1;
|
125 | befehl = 0b1010101010111000;
|
126 |
|
127 | PORTBbits.RB0 = 0;
|
128 | dummy = SPIWriteRead(befehl);
|
129 | PORTBbits.RB0 = 1;
|
130 |
|
131 | while(1){
|
132 |
|
133 | }
|
134 |
|
135 | }
|
136 |
|
137 | void __attribute__((interrupt,auto_psv)) _U1TXInterrupt(void)
|
138 | {
|
139 | IFS0bits.U1TXIF = 0; // Clear TX Interrupt flag
|
140 | }
|