source: avrstuff/libs/usart/usart.c@ 6390cc3

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

chiptest: Start to hack on ATmega128 and STK500 devboard.

  • Set the proper clock for it (generated by the STK500 master MCU, did

not get crystal working reliably yet)

  • Instead of just 'H', generate the whole alphabet for more action!
  • Use ATmega128 since that's what happens to be seated on my board for

now.

  • Property mode set to 100644
File size: 1.8 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 UCSRC = (3 << UCSZ0);
43#else
44 UCSRC = (1 << URSEL) | (3 << UCSZ0);
45#endif
46
47 //Enable The receiver and transmitter
48 UCSRB = (1 << RXEN) | (1 << TXEN);
49}
50
51
52//This fuction writes the given "data" to
53//the USART which then transmit it via TX line
54void USARTWriteChar(char data)
55{
56 //Wait untill the transmitter is ready
57 while(!(UCSRA & (1<<UDRE)))
58 {
59 //Do nothing
60 }
61
62 //Now write the data to USART buffer
63
64 UDR=data;
65}
66
67
68//This function is used to read the available data
69//from USART. This function will wait untill data is
70//available.
71char USARTReadChar()
72{
73 //Wait untill a data is available
74 while(!(UCSRA & (1<<RXC)))
75 {
76 //Do nothing
77 }
78
79 //Now USART has got data from host
80 //and is available is buffer
81
82 return UDR;
83}
84
85
86void USARTWriteHex(unsigned char i)
87{
88 unsigned char k = i>>4;
89 if (k <=9) k+='0';
90 else k+=('A'-10);
91
92 USARTWriteChar(k);
93
94 k = i&0xF;
95 if (k <=9) k+='0';
96 else k+=('A'-10);
97
98 USARTWriteChar(k);
99}
Note: See TracBrowser for help on using the repository browser.