-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialTest.c
69 lines (55 loc) · 1.85 KB
/
SerialTest.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
/*
* SerialTest.c
*
* Created: 11/25/2012 1:11:50 PM
* Author: Nukesforbreakfast
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/AVRXlib/AVRX_Clocks.h>
#include <avr/AVRXlib/AVRX_Serial.h>
volatile XUSARTst serialStruct;
ISR(USARTD0_RXC_vect)
{
Rx_Handler(&serialStruct);
}
ISR(USARTD0_TXC_vect)
{
Tx_Handler(&serialStruct);
}
void main(void)
{
unsigned long sClk, pClk;
char recieveString[100];
cli(); //
SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc,
CLK_PSBCDIV_1_1_gc);
GetSystemClocks(&sClk, &pClk);
/*
* Programmable interrupt controller configuration
*/
PMIC_CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; //enable all levels of interrupts
PORTH_DIR = 0xFF;
PORTQ_DIR = 0x0F; //port q lower 3 bits control access to usb and other stuff so get access with these two lines
PORTQ_OUT = 0x07; //if using port F make this hex 5.
/*
* Serial set up
*/
//initialize the usart d0 for 57600 baud with 8 data bits, no parity, and 1 stop bit, interrupts on low (porth set to this for debugging purposes)
USART_init(&serialStruct, 0xD0, pClk, (_USART_RXCIL_LO | _USART_TXCIL_LO), 576, -4, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT);
USART_buffer_init(&serialStruct, 100, 100); //initialize the circular buffers
USART_enable(&serialStruct, USART_TXEN_bm | USART_RXEN_bm); //enable the USART
serialStruct.fOutMode = _OUTPUT_CRLF; //append a carriage return and a line feed to every output.
serialStruct.fInMode = _INPUT_CR | _INPUT_TTY | _INPUT_ECHO; //echo input back to the terminal and set up for keyboard input.
sei();
while(1)
{
//USART_send(&serialStruct, "Hey, am I working?");
if(serialStruct.serStatus & _USART_RX_DONE)
{
USART_read(&serialStruct, recieveString);
USART_send(&serialStruct, recieveString);
}
while (!(serialStruct.serStatus & _USART_TX_EMPTY)) { ; }
}
}