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

Use the faster macro version if digital_write() instead.

This commit is contained in:
Mika Tuupola 2011-11-06 20:25:18 +02:00
parent fad28bd803
commit 2b57aba9fe
8 changed files with 75 additions and 144 deletions

View File

@ -11,9 +11,10 @@
* http://www.protostack.com/blog/2011/02/analogue-to-digital-conversion-on-an-atmega168/ * http://www.protostack.com/blog/2011/02/analogue-to-digital-conversion-on-an-atmega168/
* http://www.google.com/search?q=atmel+adc_drv * http://www.google.com/search?q=atmel+adc_drv
* *
* General information on ADXL335. * General information on using ADXL335.
* *
* http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/ * http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/
* http://www.electronicsblog.net/simple-angle-meter-using-adxl335-accelerometer-arduino/
* http://www.sparkfun.com/datasheets/Components/SMD/adxl335.pdf * http://www.sparkfun.com/datasheets/Components/SMD/adxl335.pdf
* *
* To compile and upload run: make clean; make; make program; * To compile and upload run: make clean; make; make program;
@ -27,6 +28,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <avr/io.h> #include <avr/io.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <util/delay.h> #include <util/delay.h>
#include <stdint.h> #include <stdint.h>
@ -40,36 +42,51 @@
#define Y 1 /* Analog 1 */ #define Y 1 /* Analog 1 */
#define Z 2 /* Analog 2 */ #define Z 2 /* Analog 2 */
#define RAD_TO_DEG 57.29578
static void init(void) { static void init(void) {
pin_mode(X, INPUT); pin_mode(X, INPUT);
pin_mode(Y, INPUT); pin_mode(Y, INPUT);
pin_mode(Z, INPUT); pin_mode(Z, INPUT);
} }
int main(void) { int64_t map(int64_t value, int64_t in_min, int64_t in_max, int64_t out_min, int64_t out_max) {
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
uint8_t main(void) {
init(); init();
uart_init(); uart_init();
stdout = &uart_output; stdout = &uart_output;
stdin = &uart_input; stdin = &uart_input;
uint16_t min = 265;
uint16_t max = 413;
while (1) { while (1) {
uint16_t x; uint16_t x = analog_read(X);
uint16_t y; uint16_t y = analog_read(Y);
uint16_t z; uint16_t z = analog_read(Z);
x = analog_read(X);
y = analog_read(Y);
z = analog_read(Z);
printf("word: %d %d %d\n", x, y, z);
uint16_t x2; int8_t x_ang = map(x, min, max, -90, 90);
uint16_t y2; int8_t y_ang = map(y, min, max, -90, 90);
uint16_t z2; int8_t z_ang = map(z, min, max, -90, 90);
x2 = analog_read_byte(X);
y2 = analog_read_byte(Y); uint16_t x_deg = RAD_TO_DEG * (atan2(-y_ang, -z_ang) + M_PI);
z2 = analog_read_byte(Z); uint16_t y_deg = RAD_TO_DEG * (atan2(-x_ang, -z_ang) + M_PI);
uint16_t z_deg = RAD_TO_DEG * (atan2(-y_ang, -x_ang) + M_PI);
printf("word: %d %d %d angle: %d %d %d degrees: %d %d %d\n", x, y, z, x_ang, y_ang, z_ang, x_deg, y_deg, z_deg);
/*
uint16_t x2 = analog_read_byte(X);
uint16_t y2 = analog_read_byte(Y);
uint16_t z2 = analog_read_byte(Z);
printf("byte: %d %d %d\n", x2, y2, z2); printf("byte: %d %d %d\n", x2, y2, z2);
*/
_delay_ms(200); _delay_ms(200);

View File

@ -1,2 +1,3 @@
static void init(void); static void init(void);
int main(void); uint8_t main(void);
int64_t map(int64_t value, int64_t in_min, int64_t in_max, int64_t out_min, int64_t out_max);

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 uart_async.c digital_slow.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

@ -0,0 +1,29 @@
/*
* digital.h
*
* Lightweight macro implementation of Arduino style pin numbering
* for AVR microprocessors. Because only thing I want to use from
* Arduino libraries is the pin numbering scheme.
*
* This file taken 99% from the excellent ArduinoLite project by
* Shikai Chen <csk@live.com>. Some minor changes to suite my personal
* coding taste.
*
* http://code.google.com/p/arduino-lite/
* http://www.csksoft.net/
*
* Copyright (c) 2010-2011 Shikai Chen
*
* Licensed under the LGPL 2.1 license:
* http://www.opensource.org/licenses/lgpl-2.1.php
*/
#ifndef DIGITAL_H
#define DIGITAL_H
#include "pins.h"
#define digital_read(pin) EXPAND_WRAPPER(_D_READ, ARDUINOPIN_TO_PORTID(pin), ARDUINOPIN_TO_PORTMSK(pin) )
#define digital_read_raw(pin) EXPAND_WRAPPER(_D_READ_RAW, ARDUINOPIN_TO_PORTID(pin), ARDUINOPIN_TO_PORTMSK(pin) )
#define digital_write(pin, val) D_WRITE_##val(pin)
#endif /* DIGITAL_H */

View File

@ -1,99 +0,0 @@
#include <stdint.h>
#include "digital_slow.h"
const uint8_t PROGMEM portmask_at_pin[] = {
_BV(0), /* 0, port D */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),
_BV(6),
_BV(7),
_BV(0), /* 8, port B */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),
_BV(0), /* 14, port C */
_BV(1),
_BV(2),
_BV(3),
_BV(4),
_BV(5),
_BV(6),
_BV(7)
};
const uint8_t PROGMEM port_at_pin[] = {
PD, /* 0 */
PD,
PD,
PD,
PD,
PD,
PD,
PD,
PB, /* 8 */
PB,
PB,
PB,
PB,
PB,
PC, /* 14 */
PC,
PC,
PC,
PC,
PC,
PB, /* 20 */
PB
};
const uint16_t PROGMEM output_reg_at_port[] = {
0,
0,
(uint16_t) &PORTB,
(uint16_t) &PORTC,
(uint16_t) &PORTD
};
const uint16_t PROGMEM direction_reg_at_port[] = {
0,
0,
(uint16_t) &DDRB,
(uint16_t) &DDRC,
(uint16_t) &DDRD
};
void pin_mode(uint8_t pin, uint8_t mode) {
uint8_t port_mask = pgm_read_byte(portmask_at_pin + pin);
uint8_t port = pgm_read_byte(port_at_pin + pin);
volatile uint8_t *direction_register = pgm_read_byte(direction_reg_at_port + port);;
if (mode == INPUT) {
*direction_register &= ~port_mask;
} else {
*direction_register |= port_mask;
}
}
void digital_write(uint8_t pin, uint8_t value) {
volatile uint8_t *out;
uint8_t port_mask = pgm_read_byte(portmask_at_pin + pin);
uint8_t port = pgm_read_byte(port_at_pin + pin);
uint8_t *output_register = pgm_read_byte(output_reg_at_port + port);
if (0 == value) {
*output_register &= ~port_mask;
} else {
*output_register |= port_mask;
}
}

View File

@ -1,23 +0,0 @@
#ifndef DIGITAL_SLOW_H
#define DIGITAL_SLOW_H
#include <avr/pgmspace.h>
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1
#define PB 2
#define PC 3
#define PD 4
extern const uint8_t PROGMEM portmask_at_pin[];
extern const uint8_t PROGMEM port_at_pin[];
extern const uint16_t PROGMEM output_reg_at_port[];
extern const uint16_t PROGMEM direction_reg_at_port[];
void pin_mode(uint8_t pin, uint8_t mode);
void digital_write(uint8_t pin, uint8_t value);
#endif /* DIGITAL_SLOW_H */

View File

@ -36,7 +36,8 @@
#include "main.h" #include "main.h"
#include "uart.h" #include "uart.h"
#include "digital_slow.h" #include "pins.h"
#include "digital.h"
#define LATCH 8 /* RCK */ #define LATCH 8 /* RCK */
#define CLOCK 12 /* SRCK */ #define CLOCK 12 /* SRCK */
@ -52,8 +53,11 @@ static void init(void) {
void shift_out(uint8_t data) { void shift_out(uint8_t data) {
for(uint8_t i = 0; i < 8; i++) { for(uint8_t i = 0; i < 8; i++) {
/* Write bit to data port. */ /* Write bit to data port. */
uint8_t bit = data & _BV(7 - i); if (0 == (data & _BV(7 - i))) {
digital_write(DATA, bit); digital_write(DATA, LOW);
} else {
digital_write(DATA, HIGH);
}
/* Pulse clock input to write next bit. */ /* Pulse clock input to write next bit. */
digital_write(CLOCK, LOW); digital_write(CLOCK, LOW);

View File

@ -21,6 +21,8 @@
#ifndef PINS_H #ifndef PINS_H
#define PINS_H #define PINS_H
#include <avr/sfr_defs.h>
#include <avr/io.h>
#define HIGH 0x1 #define HIGH 0x1
#define LOW 0x0 #define LOW 0x0