2011-03-27 00:04:22 +01:00
|
|
|
/*
|
|
|
|
* Digital read 8bit data from PORTD3..7 PORTD0..1 (Arduino digital ports 3..9)
|
|
|
|
* and output it as binary string (10001101) to serial. Floating state is high.
|
|
|
|
* Connect pin to ground to set it low.
|
|
|
|
*
|
|
|
|
* Led at Arduino digital 13 (PORTB5). Cause we all love blinken leds!
|
|
|
|
*
|
|
|
|
* To compile and upload run: make clean; make; make program;
|
|
|
|
*
|
|
|
|
* Copyright 2011 Mika Tuupola
|
|
|
|
*
|
|
|
|
* Licensed under the MIT license:
|
|
|
|
* http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#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) {
|
2011-09-16 21:50:37 +02:00
|
|
|
UBRR0H = UBRRH_VALUE;
|
|
|
|
UBRR0L = UBRRL_VALUE;
|
2011-03-27 00:04:22 +01:00
|
|
|
|
2011-09-16 21:50:37 +02:00
|
|
|
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
|
|
|
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
|
2011-03-27 00:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int uart_putchar(char c, FILE *stream) {
|
2011-09-16 21:50:37 +02:00
|
|
|
loop_until_bit_is_set(UCSR0A, UDRE0);
|
|
|
|
UDR0 = c;
|
|
|
|
return 0;
|
2011-03-27 00:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int uart_getchar(FILE *stream) {
|
2011-09-16 21:50:37 +02:00
|
|
|
loop_until_bit_is_set(UCSR0A, RXC0);
|
|
|
|
return UDR0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int digital_read(sfr, bit) {
|
|
|
|
return bit_is_set(sft, bit) && 1;
|
2011-03-27 00:04:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void init(void) {
|
|
|
|
|
|
|
|
/* Make PORTD3..7 PORTD0..1 (Arduino digital 3..9) input by clearing bits in DDR */
|
2011-03-27 00:06:45 +01:00
|
|
|
DDRD &= ~(_BV(PORTD2) | _BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5) | _BV(PORTD6) | _BV(PORTD7));
|
|
|
|
DDRB &= ~(_BV(PORTB0) | _BV(PORTB1));
|
2011-03-27 00:04:22 +01:00
|
|
|
|
2011-03-27 00:06:45 +01:00
|
|
|
/* 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));
|
|
|
|
*/
|
2011-09-16 21:50:37 +02:00
|
|
|
|
2011-03-27 00:06:45 +01:00
|
|
|
/* 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));
|
2011-03-27 00:04:22 +01:00
|
|
|
|
|
|
|
/* Make PORTB5 (Arduino digital 13) an output by setting bit in DDR. */
|
|
|
|
DDRB |= _BV(PORTB5);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-16 21:50:37 +02:00
|
|
|
int main(void) {
|
|
|
|
|
2011-03-27 00:04:22 +01:00
|
|
|
init();
|
2011-09-16 21:50:37 +02:00
|
|
|
uart_init();
|
2011-03-27 00:04:22 +01:00
|
|
|
stdout = &output;
|
2011-09-16 21:50:37 +02:00
|
|
|
stdin = &input;
|
|
|
|
|
2011-03-27 00:04:22 +01:00
|
|
|
uint8_t value;
|
|
|
|
char buffer[9];
|
2011-09-16 21:50:37 +02:00
|
|
|
|
|
|
|
while (1) {
|
2011-03-27 00:04:22 +01:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
itoa(value, buffer, 2);
|
|
|
|
puts(buffer);
|
|
|
|
|
|
|
|
_delay_ms(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|