-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuart.h
53 lines (44 loc) · 1.52 KB
/
uart.h
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
/*
* uart.h
*
* UART example for ATMega328P clocked at 16 MHz
*
* TODO :-
* - Implement string read function
* - Optimize for size
* - Add helper routines and compile to .a file
*
* Created on: 22-Jan-2014
* Author: Shrikant Giridhar
*/
#ifndef UART_H_
#define UART_H_
#include <avr/io.h>
#include <stdint.h>
/* Probably already defined somewhere else. Define here, if isn't. */
#ifndef FOSC
#define FOSC 16000000UL
#endif
/* Settings */
#define _BAUD 57600 // Baud rate (9600 is default)
#define _DATA 0x03 // Number of data bits in frame = byte tranmission
#define _UBRR (FOSC/16)/_BAUD - 1 // Used for UBRRL and UBRRH
#define RX_BUFF 10
/* Useful macros */
#define TX_START() UCSR0B |= _BV(TXEN0) // Enable TX
#define TX_STOP() UCSR0B &= ~_BV(TXEN0) // Disable TX
#define RX_START() UCSR0B |= _BV(RXEN0) // Enable RX
#define RX_STOP() UCSR0B &= ~_BV(RXEN0) // Disable RX
#define COMM_START() TX_START(); RX_START() // Enable communications
/* Interrupt macros; Remember to set the GIE bit in SREG before using (see datasheet) */
#define RX_INTEN() UCSR0B |= _BV(RXCIE0) // Enable interrupt on RX complete
#define RX_INTDIS() UCSR0B &= ~_BV(RXCIE0) // Disable RX interrupt
#define TX_INTEN() UCSR0B |= _BV(TXCIE0) // Enable interrupt on TX complete
#define TX_INTDIS() UCSR0B &= ~_BV(TXCIE0) // Disable TX interrupt
/* Prototypes */
void initUART(void);
uint8_t getByte(void);
void putByte(unsigned char data);
void writeString(const char *str);
const char* readString(void);
#endif /* UART_H_ */