Subversion Repositories Code-Repo

Rev

Rev 212 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 212 Rev 215
Line 3... Line 3...
3
#include "defines.h"
3
#include "defines.h"
4
#include "UART1.h"
4
#include "UART1.h"
5
 
5
 
6
static UART1_DATA *uart_data_ptr;
6
static UART1_DATA *uart_data_ptr;
7
 
7
 
-
 
8
/* Note: BRGH values are different from PIC18!
-
 
9
 *
-
 
10
 * Baud Rate Calculation (BRGH = 0):
-
 
11
 * Baud Rate = PerfBusFreq / (16 * (BRG + 1))
-
 
12
 * BRG = PerfBusFreq / (16 * Baud Rate) - 1
-
 
13
 *
-
 
14
 * Baud Rate Calculation (BRGH = 1):
-
 
15
 * Baud Rate = PerfBusFreq / (4 * (BRG + 1))
-
 
16
 * BRG = PerfBusFreq / (4 * Baud Rate) - 1
-
 
17
 */
-
 
18
 
8
void UART1_Init(UART1_DATA *data, void (*rx_callback)(char)) {
19
void UART1_Init(UART1_DATA *data, void (*rx_callback)(char)) {
9
    uart_data_ptr = data;
20
    uart_data_ptr = data;
10
    uart_data_ptr->rx_callback = rx_callback;
21
    uart_data_ptr->rx_callback = rx_callback;
11
    uart_data_ptr->buffer_out_len = 0;
22
    uart_data_ptr->buffer_out_len = 0;
12
    uart_data_ptr->buffer_out_ind = 0;
23
    uart_data_ptr->buffer_out_ind = 0;
Line 17... Line 28...
17
    IFS0CLR   = 0x1C000000; // Clear any existing events
28
    IFS0CLR   = 0x1C000000; // Clear any existing events
18
    IPC6SET   = 0x00000009; // Set Priority = 2, Subpriority = 1
29
    IPC6SET   = 0x00000009; // Set Priority = 2, Subpriority = 1
19
    
30
    
20
    U1MODE    = 0x00008000; // UART enabled, BRGH = 0
31
    U1MODE    = 0x00008000; // UART enabled, BRGH = 0
21
    U1STA     = 0x00009400; // TX interrupt on buffer empty, RX interrupt on buffer not empty
32
    U1STA     = 0x00009400; // TX interrupt on buffer empty, RX interrupt on buffer not empty
22
    U1BRG     = 19;         // Set baud rate to 256000 @ 80MHz
33
    U1BRG     = 42;         // Set baud rate to 115200 @ 80MHz
23
    IEC0SET   = 0x0C000000; // Enable the RX and Error interrupts
34
    IEC0SET   = 0x0C000000; // Enable the RX and Error interrupts
24
 
35
 
25
    INTEnableInterrupts();
36
    INTEnableInterrupts();
26
 
37
 
27
    TRISDbits.TRISD14 = 0;
38
    TRISDbits.TRISD14 = 0;
Line 73... Line 84...
73
        }
84
        }
74
        IFS0CLR = 0x08000000; // Clear the recieve flag
85
        IFS0CLR = 0x08000000; // Clear the recieve flag
75
    }
86
    }
76
 
87
 
77
    // Process UART1 transmit flag
88
    // Process UART1 transmit flag
78
    if (IFS0bits.U1TXIF) {
89
    if (IFS0bits.U1TXIF && IEC0bits.U1TXIE) {
79
        // Disable the transmit interrupt if all data has been sent
90
        // Disable the transmit interrupt if all data has been sent
80
        if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len) {
91
        if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len) {
81
            IEC0CLR = 0x10000000;
92
            IEC0CLR = 0x10000000;
82
            uart_data_ptr->buffer_out_len = 0;
93
            uart_data_ptr->buffer_out_len = 0;
83
        } else {
94
        } else {