ece312/final_project/lcd_disp/periph.c
2019-12-03 00:39:32 -07:00

71 lines
1.6 KiB
C

#include "periph.h"
/** @brief Handles incoming Serial commands from the main player.
*/
ISR(USART_RX_vect) {
cli();
// Push incoming data to FIFO
fifo_push(UDR);
// Flag is cleared by reading data from UDR
sei();
}
/** @brief Display update timer
*/
ISR(TIMER0_COMPA_vect) {
cli();
update_display = true;
// Flag is cleared by calling the vector
sei();
}
void gpio_init() {
// Initialise button pins as inputs
BUTTONS_LEFT_DDR &= ~(1 << BUTTONS_LEFT);
BUTTONS_RIGHT_DDR &= ~(1 << BUTTONS_RIGHT);
BUTTONS_PLAYPAUSE_DDR &= ~(1 << BUTTONS_PLAYPAUSE);
// Turn on button pullups
BUTTONS_LEFT_PORT |= (1 << BUTTONS_LEFT);
BUTTONS_RIGHT_PORT |= (1 << BUTTONS_RIGHT);
BUTTONS_PLAYPAUSE_PORT |= (1 << BUTTONS_PLAYPAUSE);
}
void timer_init() {
/**
* Need to verify the math here.
* But with a prescaler of 1024, f_cpu at 8MHz, an OCRA of 195 gives
* 20Hz refresh rate.
*/
// COM0x can be set to defaults
// To set to CTC, WGM = 0b010
TCCR0A = (1 << WGM01);
// Set to ~20Hz
OCR0A = 195;
// CS = 0b101 -> 1024 prescaler
// Everything else should be 0.
// TCCR0B = 0b00000101; <- can't use this because binary stuff is a GCC extension
TCCR0B = 5;
// Enable interrupt
TIMSK = (1 << OCIE0A);
}
void usart_init() {
// initialize USART
UBRRL = UBRR_VALUE & 255;
UBRRH = UBRR_VALUE >> 8;
UCSRB = (1 << TXEN) | (1 << RXEN); // fire-up USART
UCSRC = (1 << UCSZ1) | (1 << UCSZ0); // fire-up USART
// Enable RX complete interrupt
UCSRB |= (1 << RXCIE);
}