1 | /*
|
2 | * File: usart26K80.c
|
3 | * Author: vloki
|
4 | *
|
5 | * Created on 22. Juni 2017, 09:30
|
6 | */
|
7 |
|
8 |
|
9 | #include <xc.h>
|
10 |
|
11 | // #pragma config statements should precede project file includes.
|
12 | // Use project enums instead of #define for ON and OFF.
|
13 |
|
14 | // CONFIG1L
|
15 | #pragma config RETEN = OFF // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
|
16 | #pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
|
17 | #pragma config SOSCSEL = HIGH // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
|
18 | #pragma config XINST = OFF // Extended Instruction Set (Disabled)
|
19 |
|
20 | // CONFIG1H
|
21 | #pragma config FOSC = INTIO2 // Oscillator (Internal RC oscillator)
|
22 | #pragma config PLLCFG = ON // PLL x4 Enable bit (Enabled)
|
23 | #pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
|
24 | #pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
|
25 |
|
26 | // CONFIG2L
|
27 | #pragma config PWRTEN = ON // Power Up Timer (Enabled)
|
28 | #pragma config BOREN = OFF // Brown Out Detect (Disabled in hardware, SBOREN disabled)
|
29 | #pragma config BORV = 3 // Brown-out Reset Voltage bits (1.8V)
|
30 | #pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
|
31 |
|
32 | // CONFIG2H
|
33 | #pragma config WDTEN = SWDTDIS // Watchdog Timer (WDT enabled in hardware; SWDTEN bit disabled)
|
34 | #pragma config WDTPS = 1048576 // Watchdog Postscaler (1:1048576)
|
35 |
|
36 | // CONFIG3H
|
37 | #pragma config CANMX = PORTB // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
|
38 | #pragma config MSSPMSK = MSK7 // MSSP address masking (7 Bit address masking mode)
|
39 | #pragma config MCLRE = ON // Master Clear Enable (MCLR Enabled, RE3 Disabled)
|
40 |
|
41 | // CONFIG4L
|
42 | #pragma config STVREN = ON // Stack Overflow Reset (Enabled)
|
43 | #pragma config BBSIZ = BB2K // Boot Block Size (2K word Boot Block size)
|
44 |
|
45 | // CONFIG5L
|
46 | ...
|
47 |
|
48 |
|
49 | #define _XTAL_FREQ 64000000UL
|
50 |
|
51 | void putByte(char data);
|
52 | void writeString(char *str);
|
53 | void UARTInit(void);
|
54 |
|
55 | void main(void) {
|
56 | OSCCONbits.IRCF = 0b111; // interner Oszillator 16MHz
|
57 | OSCTUNEbits.PLLEN = 1;
|
58 | // while(OSCCONbits.HFIOFS == 0){;} // wait until stable
|
59 | __delay_ms(1);
|
60 |
|
61 | UARTInit();
|
62 |
|
63 | volatile char a = 'a';
|
64 | volatile char d;
|
65 | volatile char c;
|
66 | while(1)
|
67 | {
|
68 | while(TXSTA1bits.TRMT!=1 || PIR1bits.TX1IF!=1);
|
69 | TXREG1 = a; // -> funktioniert
|
70 | __delay_ms(500);
|
71 |
|
72 | while(TXSTA1bits.TRMT!=1 || PIR1bits.TX1IF!=1);
|
73 | TXREG1 = 'b'; // -> funktioniert
|
74 |
|
75 | __delay_ms(200);
|
76 |
|
77 | c = 'c';
|
78 | while(TXSTA1bits.TRMT!=1 || PIR1bits.TX1IF!=1);
|
79 | TXREG1 = c; // -> funktioniert
|
80 |
|
81 |
|
82 | d = 'd';
|
83 | putByte(d); // -> funktioniert
|
84 | __delay_ms(200);
|
85 |
|
86 | while(TXSTA1bits.TRMT!=1 || PIR1bits.TX1IF!=1);
|
87 | TXREG1 = '\r';
|
88 | __delay_ms(200);
|
89 | writeString("HelloWorld"); // -> funktioniert
|
90 | __delay_ms(1000);
|
91 | char test[]= "Test-String";
|
92 | writeString(test); // -> funktioniert
|
93 |
|
94 | __delay_ms(1000);
|
95 | }
|
96 | // return;
|
97 | }
|
98 |
|
99 | void putByte(char data)
|
100 | {
|
101 | while(TXSTA1bits.TRMT!=1 || PIR1bits.TX1IF!=1);
|
102 | TXREG1 = data;
|
103 | }
|
104 |
|
105 | void writeString(char *str)
|
106 | {
|
107 | while(*str)
|
108 | {
|
109 | putByte(*str);
|
110 | ++str;
|
111 | }
|
112 | }
|
113 |
|
114 | void UARTInit(void)
|
115 | {
|
116 | TRISCbits.TRISC6 = 1;
|
117 | TRISCbits.TRISC7 = 1;
|
118 |
|
119 | // Values from data sheet with Baudrate 9600
|
120 | // Initialising BaudRate value
|
121 | SPBRGH1 = 0x06;
|
122 | SPBRG1 = 0x82;
|
123 |
|
124 | TXSTA1bits.BRGH = 1; // 1 = high speed baud rate
|
125 | // 0 = low speed baud rate
|
126 | BAUDCON1bits.BRG16 = 1; // 1 = 16 bit baud rate generator
|
127 | // 0 = 8 bit baud rate generator
|
128 | TXSTA1bits.TX9 = 0; // 0 = select 8-bit transmission
|
129 | TXSTA1bits.SYNC = 0; // 0 = async mode
|
130 | TXSTA1bits.SENDB = 0; // 1 = send sync break on next transmission
|
131 | // 0 = sync break transmission completed
|
132 | RCSTA1bits.SPEN = 1; // 1 = serial port enable
|
133 | RCSTA1bits.CREN = 1; // 1 = enable receiver
|
134 | // BAUDCON1bits.TXCKP = 0; // 1 = idle state for transmit is HIGH
|
135 | // 0 = idle state for transmit is LOW
|
136 |
|
137 | TXSTA1bits.TXEN = 1; // 1 = transmit is enabled
|
138 | }
|