-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUART.txt
More file actions
49 lines (36 loc) · 1.21 KB
/
UART.txt
File metadata and controls
49 lines (36 loc) · 1.21 KB
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
#include<p18F4550.h>
#include<stdio.h>
#define Fosc 48000000UL
void InitUART(unsigned int baudrate)
{
TRISCbits.RC6 = 0; //TX pin set as output
TRISCbits.RC7 = 1; //RX pin set as input
SPBRG = (unsigned char)(((Fosc /64)/baudrate)-1);
BAUDCON = 0b00000000; //Non-inverted data; 8-bit baudrate generator
TXSTA = 0b00100000;
RCSTA = 0b10010000; //Serial port enabled; 8-bit data; single receive enabled
}
void SendChar(unsigned char data)
{
while(TXSTAbits.TRMT == 0); //Wait while transmit register is empty
TXREG = data; //Transmit data
}
void putch(unsigned char data)
{
SendChar(data);
}
unsigned char GetChar(void)
{
while(!PIR1bits.RCIF); //Wait till receive buffer becomes full
return RCREG; //Returned received data
}
void main(void)
{
InitUART(9600);
printf("\r\nHello MicroPIC-18F: Enter any Key from Keyboard\r\n");
while(1)
{
printf("%c! ",GetChar()); //Receive character from PC and echo back
}
while(1);
}