source: avrstuff/libs/usart/usart.c@ 6c47e45

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

libusart: configure as 7E1 when using atmega128.

I'm using a Minitel as a terminal today. It can do only that.

  • Property mode set to 100644
File size: 1.9 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#ifdef __AVR_ATmega128__
11// FIXME there are two USARTs in this case and we should be able to drive either
12#define UBRRH UBRR0H
13#define UBRRL UBRR0L
14#define UCSRA UCSR0A
15#define UCSRC UCSR0C
16#define UCSRB UCSR0B
17#define UDR UDR0
18#endif
19
20//This function is used to initialize the USART
21void USARTInit()
22{
23 //Set Baud rate
24 #include <util/setbaud.h>
25 UBRRH = UBRRH_VALUE;
26 UBRRL = UBRRL_VALUE;
27 #if USE_2X
28 UCSRA |= (1 << U2X);
29 #else
30 UCSRA &= ~(1 << U2X);
31 #endif
32
33 /*Set Frame Format
34 >> Asynchronous mode
35 >> No Parity
36 >> 1 StopBit
37 >> char size 8
38 */
39#ifdef __AVR_ATtiny2313__
40 UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
41#elif defined __AVR_ATmega128__
42 UCSR0C = (2 << UCSZ0) | (2 << UPM0) ; /* 7 bits + even parity.
43 TODO make this configurable. */
44#else
45 UCSRC = (1 << URSEL) | (3 << UCSZ0);
46#endif
47
48 //Enable The receiver and transmitter
49 UCSRB = (1 << RXEN) | (1 << TXEN);
50}
51
52
53//This fuction writes the given "data" to
54//the USART which then transmit it via TX line
55void USARTWriteChar(char data)
56{
57 //Wait untill the transmitter is ready
58 while(!(UCSR0A & (1<<UDRE0)))
59 {
60 //Do nothing
61 }
62
63 //Now write the data to USART buffer
64
65 UDR=data;
66}
67
68
69//This function is used to read the available data
70//from USART. This function will wait untill data is
71//available.
72char USARTReadChar()
73{
74 //Wait untill a data is available
75 while(!(UCSR0A & (1<<RXC0)))
76 {
77 //Do nothing
78 }
79
80 //Now USART has got data from host
81 //and is available is buffer
82
83 return UDR0;
84}
85
86
87void USARTWriteHex(unsigned char i)
88{
89 unsigned char k = i>>4;
90 if (k <=9) k+='0';
91 else k+=('A'-10);
92
93 USARTWriteChar(k);
94
95 k = i&0xF;
96 if (k <=9) k+='0';
97 else k+=('A'-10);
98
99 USARTWriteChar(k);
100}
Note: See TracBrowser for help on using the repository browser.