source: avrstuff/libs/usart/usart.c@ cd3a9ac

main
Last change on this file since cd3a9ac was cd3a9ac, checked in by Adrien Destugues <pulkomandy@…>, 10 years ago

Make libusart aware of attiny2313.

git-svn-id: svn://pulkomandy.tk/avrstuff@109 c6672c3c-f6b6-47f9-9001-1fd6b12fecbe

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