1
0
mirror of synced 2024-11-21 16:54:25 +01:00

Move uart stuff to external file. Working digital_read() function.

This commit is contained in:
Mika Tuupola 2011-10-09 21:19:14 +03:00
parent adae63b499
commit 780bec6cb9
5 changed files with 179 additions and 58 deletions

View File

@ -51,7 +51,7 @@ FORMAT = ihex
TARGET = main TARGET = main
# List C source files here. (C dependencies are automatically generated.) # List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c SRC = $(TARGET).c uart_async.c
# List Assembler source files here. # List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s # Make them always end in a capital .S. Files ending in a lowercase .s

View File

@ -14,6 +14,11 @@
* *
*/ */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <stdlib.h> #include <stdlib.h>
#include <avr/io.h> #include <avr/io.h>
#include <stdio.h> #include <stdio.h>
@ -22,80 +27,88 @@
#include <avr/sfr_defs.h> #include <avr/sfr_defs.h>
#include "main.h" #include "main.h"
#include "uart.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;
}
void init(void) { 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)); DDRD &= ~(_BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
DDRB &= ~(_BV(PORTB0) | _BV(PORTB1)); DDRB &= ~(_BV(PORTB0) | _BV(PORTB1));
/* Disable pullups by clearing bits in PORT. Default state is now low. */ /* In input mode, when pull-up is enabled, default state of pin becomes 1. So even if */
/* /* you dont connect anything to pin and if you try to read it, it will read as 1. Now, */
PORTD &= ~(_BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7)); /* when you externally drive that pin to zero(i.e. connect to ground / or pull-down), */
PORTB &= ~(_BV(PORTB0) | _BV(PORTB1)); /* only then it will be read as 0. */
*/
/* Enable pullups by setting bits in PORT. Default state is now high. */ /* 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)); 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. */ /* Make PORTB5 (Arduino digital 13) an output by setting bit in DDR. */
DDRB |= _BV(PORTB5); 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) { int main(void) {
init(); init();
uart_init(); uart_init();
stdout = &output; stdout = &uart_output;
stdin = &input; stdin = &uart_input;
uint8_t value; int value;
char buffer[9]; //char buffer[9];
while (1) { while (1) {
/* Blink led by toggling state of PORTB5 (Arduino digital 13). */ /* Blink led by toggling state of PORTB5 (Arduino digital 13). */
PORTB ^= _BV(PORTB5); PORTB ^= _BV(PORTB5);
/* Take 8bit value from PORTD3..7 PORTD0..1 */ /*
value = (PIND >> 2) + (PINB << 6); puts("PINB");
value = PINB;
itoa(value, buffer, 2); itoa(value, buffer, 2);
puts(buffer); 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); _delay_ms(500);
} }

View File

@ -1,8 +1,2 @@
static int uart_putchar(char c, FILE *stream); static void uart_init(void);
static int uart_getchar(FILE *stream);
static void init_uart(void);
static void 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
View 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
View 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);
}
}