Basic boilerplate for programming AVR (arduino) in C.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
avr-c-boilerplate/lib/usart.h

85 lines
1.6 KiB

9 years ago
#pragma once
//
// Utilities for UART communication.
//
// First, init uart with usart_init().
// Then enable interrupts you want with usart_XXX_isr_enable().
//
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdint.h>
#include "calc.h"
/* USART BAUD RATE REGISTER values at 16 MHz */
enum {
BAUD_9600 = 103,
BAUD_14400 = 68,
BAUD_19200 = 51,
BAUD_28800 = 34,
BAUD_38400 = 25,
BAUD_57600 = 16,
BAUD_76800 = 12,
BAUD_115200 = 8,
BAUD_250k = 3,
BAUD_500k = 1,
BAUD_1M = 0,
};
9 years ago
/** Init UART with a UBRR value */
void usart_init(uint16_t ubrr);
/** Check if there's a byte in the RX register */
#define usart_rx_ready() (0 != (UCSR0A & (1 << RXC0)))
/** Check if transmission of everything is done */
#define usart_tx_ready() (0 != (UCSR0A & (1 << UDRE0)))
// ---- Enable UART interrupts ------------
/** Enable or disable RX ISR */
#define usart_rx_isr_enable(yes) set_bit(UCSR0B, RXCIE0, (yes))
9 years ago
/** Enable or disable TX ISR (1 byte is sent) */
#define usart_tx_isr_enable(yes) set_bit(UCSR0B, TXCIE0, (yes))
9 years ago
/** Enable or disable DRE ISR (all is sent) */
#define usart_dre_isr_enable(yes) set_bit(UCSR0B, UDRIE0, (yes))
9 years ago
// ---- Basic IO --------------------------
/** Send byte over USART */
void usart_tx(uint8_t data);
9 years ago
/** Receive one byte over USART */
uint8_t usart_rx(void);
9 years ago
/** Send byte over UART */
#define usart_putc(data) usart_tx((data))
9 years ago
/** Clear receive buffer */
void usart_clear_rx(void);
9 years ago
// ---- Strings ---------------------------
/** Send string over UART */
void usart_puts(const char* str);
9 years ago
/** Send progmem string over UART */
void usart_puts_P(const char* str);