main
Line | |
---|
1 | /* AVR USART acces functions - stolen from Extreme Electronics */
|
---|
2 |
|
---|
3 | #include <avr/io.h>
|
---|
4 | #include <inttypes.h>
|
---|
5 |
|
---|
6 |
|
---|
7 | //This function is used to initialize the USART
|
---|
8 | //at a given UBRR value
|
---|
9 | void USARTInit(uint16_t ubrr_value)
|
---|
10 | {
|
---|
11 |
|
---|
12 | //Set Baud rate
|
---|
13 | UBRRL = ubrr_value;
|
---|
14 | UBRRH = (ubrr_value>>8);
|
---|
15 |
|
---|
16 | /*Set Frame Format
|
---|
17 |
|
---|
18 |
|
---|
19 | >> Asynchronous mode
|
---|
20 | >> No Parity
|
---|
21 | >> 1 StopBit
|
---|
22 | >> char size 8
|
---|
23 |
|
---|
24 | */
|
---|
25 |
|
---|
26 | UCSRC=(1<<URSEL)|(3<<UCSZ0);
|
---|
27 |
|
---|
28 | //Enable The receiver and transmitter
|
---|
29 | UCSRB=(1<<RXEN)|(1<<TXEN);
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | //This fuction writes the given "data" to
|
---|
34 | //the USART which then transmit it via TX line
|
---|
35 | void USARTWriteChar(char data)
|
---|
36 | {
|
---|
37 | //Wait untill the transmitter is ready
|
---|
38 | while(!(UCSRA & (1<<UDRE)))
|
---|
39 | {
|
---|
40 | //Do nothing
|
---|
41 | }
|
---|
42 |
|
---|
43 | //Now write the data to USART buffer
|
---|
44 |
|
---|
45 | UDR=data;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | //This function is used to read the available data
|
---|
50 | //from USART. This function will wait untill data is
|
---|
51 | //available.
|
---|
52 | char USARTReadChar()
|
---|
53 | {
|
---|
54 | //Wait untill a data is available
|
---|
55 | while(!(UCSRA & (1<<RXC)))
|
---|
56 | {
|
---|
57 | //Do nothing
|
---|
58 | }
|
---|
59 |
|
---|
60 | //Now USART has got data from host
|
---|
61 | //and is available is buffer
|
---|
62 |
|
---|
63 | return UDR;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | void USARTWriteHex(unsigned char i)
|
---|
68 | {
|
---|
69 | unsigned char k = i>>4;
|
---|
70 | if (k <=9) k+='0';
|
---|
71 | else k+=('A'-10);
|
---|
72 |
|
---|
73 | USARTWriteChar(k);
|
---|
74 |
|
---|
75 | k = i&0xF;
|
---|
76 | if (k <=9) k+='0';
|
---|
77 | else k+=('A'-10);
|
---|
78 |
|
---|
79 | USARTWriteChar(k);
|
---|
80 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.