forked from UWARG/RC-Car-Bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UART1.c
124 lines (108 loc) · 3.52 KB
/
UART1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* File: debug.c
*
* Created on August 26, 2014, 12:27 PM
*/
#include <p33FJ256GP710A.h>
#include "UART1.h"
void initUART1()
{
// This is an EXAMPLE, so brutal typing goes into explaining all bit sets
// The HPC16 board has a DB9 connector wired to UART2, so we will
// be configuring this port only
// configure U2MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U2MODEbits.notimplemented; // Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U2MODEbits.notimplemented; // Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.URXINV = 0; // Bit4 IdleState = 1 (for dsPIC)
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0b00; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
//***NOTE That Mitch Changed this code and the Baud Rate can be automatically calculated at the top
// Load a value into Baud Rate Generator. Example is for 9600.
// See section 19.3.1 of datasheet.
// U1BRG = (Fcy/(16*BaudRate))-1
// U1BRG = (3.7M/(16*9600))-1
// U1BRG = 23
//UART Baud Rate Calculated Values...
//9600 = 23
//19200 = 11
//38400 = 5
//115200 = 1
U1BRG = 1;//Baud_Rate; // 40Mhz osc, ___ Baud
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
//U2STAbits.notimplemented = 0; //Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 2; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
IPC7 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 0; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 0; // Enable Recieve Interrupts
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
// I think I have the thing working now.
/* wait at least 104 usec (1/9600) before sending first char */
int i = 0;
while (i < 4160)
{
Nop();
i++;
}
}
void UART1_SendChar(char data)
{
U1TXREG = data;
while(U1STAbits.TRMT == 0);
U1STAbits.TRMT = 0;
}
void UART1_SendString(char *s)
{
do
{
UART1_SendChar(*s);
s++;
//Delay10TCYx(250);
}while(*s != 0);
UART1_SendChar('\n');
}
//void UART1_SendStringNum(char *s, double value)
//{
// do
// {
// UART1_SendChar(*s);
// s++;
// //Delay10TCYx(250);
// }while(*s != 0);
//
// char str[20];
// sprintf(str, "%f", value);
//
// do
// {
// UART1_SendChar(*str);
// str++;
// //Delay10TCYx(250);
// }while(*str != 0);
//
// UART1_SendChar('\n');
//}