Move uart stuff to external file. Working digital_read() function.
This commit is contained in:
parent
adae63b499
commit
780bec6cb9
@ -50,8 +50,8 @@ FORMAT = ihex
|
||||
# Target file name (without extension).
|
||||
TARGET = main
|
||||
|
||||
# List C source files here. (C dependencies are automatically generated.)
|
||||
SRC = $(TARGET).c
|
||||
# List C source files here. (C dependencies are automatically generated.)
|
||||
SRC = $(TARGET).c uart_async.c
|
||||
|
||||
# List Assembler source files here.
|
||||
# Make them always end in a capital .S. Files ending in a lowercase .s
|
||||
|
@ -14,6 +14,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU 16000000UL
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
@ -22,80 +27,88 @@
|
||||
#include <avr/sfr_defs.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU 16000000UL
|
||||
#endif
|
||||
|
||||
#define BAUD 9600
|
||||
#include <util/setbaud.h>
|
||||
|
||||
volatile uint8_t interrupts;
|
||||
char buffer[1];
|
||||
|
||||
static void uart_init(void) {
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
|
||||
}
|
||||
|
||||
static int uart_putchar(char c, FILE *stream) {
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uart_getchar(FILE *stream) {
|
||||
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||
return UDR0;
|
||||
}
|
||||
|
||||
static int digital_read(sfr, bit) {
|
||||
return bit_is_set(sft, bit) && 1;
|
||||
}
|
||||
#include "uart.h"
|
||||
|
||||
void init(void) {
|
||||
|
||||
/* Make PORTD3..7 PORTD0..1 (Arduino digital 3..9) input by clearing bits in DDR */
|
||||
/* Make PORTD2..7 PORTD0..1 (Arduino digital 3..11) input by clearing bits in DDR */
|
||||
DDRD &= ~(_BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
|
||||
DDRB &= ~(_BV(PORTB0) | _BV(PORTB1));
|
||||
|
||||
/* Disable pullups by clearing bits in PORT. Default state is now low. */
|
||||
/*
|
||||
PORTD &= ~(_BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
|
||||
PORTB &= ~(_BV(PORTB0) | _BV(PORTB1));
|
||||
*/
|
||||
|
||||
/* In input mode, when pull-up is enabled, default state of pin becomes ’1′. So even if */
|
||||
/* you don’t connect anything to pin and if you try to read it, it will read as 1. Now, */
|
||||
/* when you externally drive that pin to zero(i.e. connect to ground / or pull-down), */
|
||||
/* only then it will be read as 0. */
|
||||
|
||||
/* Enable pullups by setting bits in PORT. Default state is now high. */
|
||||
PORTD |= (_BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
|
||||
PORTB |= (_BV(PORTB0) | _BV(PORTB1));
|
||||
PORTB |= (_BV(PORTB0) | _BV(PORTB1));
|
||||
|
||||
/* Disable pullups by clearing bits in PORT. Default state is now low. */
|
||||
/*
|
||||
PORTD &= ~(_BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
|
||||
PORTB &= ~(_BV(PORTB0) | _BV(PORTB1));
|
||||
*/
|
||||
|
||||
/* Make PORTB5 (Arduino digital 13) an output by setting bit in DDR. */
|
||||
DDRB |= _BV(PORTB5);
|
||||
|
||||
}
|
||||
|
||||
int digital_read(int input_register, int pin) {
|
||||
return bit_is_set(input_register, pin) != 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
void digital_write(int input_register, int pin) {
|
||||
input_register |= _BV(pin);
|
||||
}
|
||||
*/
|
||||
|
||||
int main(void) {
|
||||
|
||||
init();
|
||||
uart_init();
|
||||
stdout = &output;
|
||||
stdin = &input;
|
||||
|
||||
uint8_t value;
|
||||
char buffer[9];
|
||||
stdout = &uart_output;
|
||||
stdin = &uart_input;
|
||||
|
||||
int value;
|
||||
//char buffer[9];
|
||||
|
||||
while (1) {
|
||||
/* Blink led by toggling state of PORTB5 (Arduino digital 13). */
|
||||
PORTB ^= _BV(PORTB5);
|
||||
|
||||
/* Take 8bit value from PORTD3..7 PORTD0..1 */
|
||||
value = (PIND >> 2) + (PINB << 6);
|
||||
|
||||
/*
|
||||
puts("PINB");
|
||||
value = PINB;
|
||||
itoa(value, buffer, 2);
|
||||
puts(buffer);
|
||||
*/
|
||||
|
||||
value = digital_read(PIND, PIND2);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PIND, PIND3);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PIND, PIND4);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PIND, PIND5);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PIND, PIND6);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PIND, PIND7);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PINB, PINB0);
|
||||
printf("%d", value);
|
||||
|
||||
value = digital_read(PINB, PINB1);
|
||||
printf("%d\n", value);
|
||||
|
||||
_delay_ms(500);
|
||||
}
|
||||
|
@ -1,8 +1,2 @@
|
||||
static int uart_putchar(char c, FILE *stream);
|
||||
static int uart_getchar(FILE *stream);
|
||||
|
||||
static void init_uart(void);
|
||||
static void uart_init(void);
|
||||
static void init(void);
|
||||
|
||||
static FILE output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
static FILE input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ );
|
12
digital_read/uart.h
Normal file
12
digital_read/uart.h
Normal file
@ -0,0 +1,12 @@
|
||||
int uart_putchar(char c, FILE *stream);
|
||||
int uart_getchar(FILE *stream);
|
||||
|
||||
void uart_init(void);
|
||||
|
||||
struct rx_ring;
|
||||
struct tx_ring;
|
||||
|
||||
/* http://www.ermicro.com/blog/?p=325 */
|
||||
|
||||
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
|
102
digital_read/uart_async.c
Normal file
102
digital_read/uart_async.c
Normal file
@ -0,0 +1,102 @@
|
||||
/* Based on Atmel Application Note AVR 306 */
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef BAUD
|
||||
#define BAUD 9600
|
||||
#endif
|
||||
#include <util/setbaud.h>
|
||||
|
||||
#ifndef UART_RX_BUFFER_SIZE
|
||||
#define UART_RX_BUFFER_SIZE 64
|
||||
#endif
|
||||
|
||||
#ifndef UART_TX_BUFFER_SIZE
|
||||
#define UART_TX_BUFFER_SIZE 64
|
||||
#endif
|
||||
|
||||
struct tx_ring {
|
||||
int buffer[UART_TX_BUFFER_SIZE];
|
||||
int start;
|
||||
int end;
|
||||
};
|
||||
|
||||
struct rx_ring {
|
||||
int buffer[UART_RX_BUFFER_SIZE];
|
||||
int start;
|
||||
int end;
|
||||
};
|
||||
|
||||
static struct tx_ring tx_buffer;
|
||||
static struct rx_ring rx_buffer;
|
||||
|
||||
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
|
||||
|
||||
void uart_init(void) {
|
||||
|
||||
tx_buffer.start = 0;
|
||||
tx_buffer.end = 0;
|
||||
|
||||
rx_buffer.start = 0;
|
||||
rx_buffer.end = 0;
|
||||
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
|
||||
|
||||
sei();
|
||||
}
|
||||
|
||||
int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
|
||||
int write_pointer = (tx_buffer.end + 1) % UART_TX_BUFFER_SIZE;
|
||||
|
||||
if (write_pointer != tx_buffer.start){
|
||||
tx_buffer.buffer[tx_buffer.end] = c;
|
||||
tx_buffer.end = write_pointer;
|
||||
|
||||
/* Data available. Enable the transmit interrupt for serial port 0. */
|
||||
UCSR0B |= _BV(UDRIE0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_getchar(FILE *stream) {
|
||||
int read_pointer = (rx_buffer.start + 1) % UART_RX_BUFFER_SIZE;
|
||||
|
||||
rx_buffer.start = read_pointer;
|
||||
return rx_buffer.buffer[read_pointer];
|
||||
}
|
||||
|
||||
ISR(USART_RX_vect) {
|
||||
int write_pointer = (rx_buffer.end + 1) % UART_RX_BUFFER_SIZE;
|
||||
|
||||
/* Add next byte to ringbuffer if it has space available. */
|
||||
if (write_pointer != rx_buffer.start){
|
||||
rx_buffer.buffer[rx_buffer.end] = UDR0;
|
||||
rx_buffer.end = write_pointer;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(USART_UDRE_vect){
|
||||
int read_pointer = (tx_buffer.start + 1) % UART_TX_BUFFER_SIZE;
|
||||
|
||||
/* Transmit next byte if data available in ringbuffer. */
|
||||
if (read_pointer != tx_buffer.end) {
|
||||
UDR0 = tx_buffer.buffer[read_pointer];
|
||||
tx_buffer.start = read_pointer;
|
||||
} else {
|
||||
/* Nothing to send. Disable the transmit interrupt for serial port 0. */
|
||||
UCSR0B &= ~_BV(UDRIE0);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user