1
0
mirror of git://projects.qi-hardware.com/antorcha.git synced 2024-11-24 23:00:37 +02:00

fw/: sample the acceleration sensor (untested)

This commit is contained in:
Werner Almesberger 2012-06-21 08:56:56 -03:00
parent 7045109090
commit eaa11b110c
5 changed files with 101 additions and 1 deletions

View File

@ -30,7 +30,8 @@ OBJCOPY = $(AVR_PREFIX)objcopy
#OBJDUMP = $(AVR_PREFIX)objdump
SIZE = $(AVR_PREFIX)size
OBJS = $(NAME).o dispatch.o hash.o image.o reset.o sweep.o $(COMMON_OBJS)
OBJS = $(NAME).o accel.o dispatch.o hash.o image.o reset.o sweep.o \
$(COMMON_OBJS)
BOOT_OBJS = boot.o flash.o fw.o $(COMMON_OBJS)
COMMON_OBJS = rf.o spi.o

68
fw/accel.c Normal file
View File

@ -0,0 +1,68 @@
/*
* fw/accel.c - Acceleration sensor
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "io.h"
#include "accel.h"
void (*sample)(bool x, uint16_t v) = NULL;
static bool chan_x;
static inline void admux(bool x)
{
ADMUX =
1 << REFS0 | /* Vref is AVcc */
(x ? ADC_X : ADC_Y);
}
static inline void adcsra(bool start)
{
ADCSRA =
1 << ADEN | /* enable ADC */
(start ? 1 << ADSC : 0) |
1 << ADIE | /* enable ADC interrupts */
7; /* clkADC = clk/128 -> 62.5 kHz */
}
ISR(ADC_vect)
{
uint16_t v;
v = ADC;
if (sample)
sample(chan_x, v);
chan_x = !chan_x;
admux(chan_x);
adcsra(1);
}
void accel_start(void)
{
adcsra(0);
admux(1);
chan_x = 1;
}

26
fw/accel.h Normal file
View File

@ -0,0 +1,26 @@
/*
* fw/accel.h - Acceleration sensor
*
* Written 2012 by Werner Almesberger
* Copyright 2012 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef ACCEL_H
#define ACCEL_H
#include <stdbool.h>
#include <stdint.h>
extern void (*sample)(bool x, uint16_t v);
void accel_start(void);
#endif /* !ACCEL_H */

View File

@ -17,6 +17,7 @@
#include "rf.h"
#include "dispatch.h"
#include "sweep.h"
#include "accel.h"
#include "image.h"
@ -47,6 +48,7 @@ int main(void)
*/
sweep_init();
accel_start();
while (1) {
got = rf_recv(buf, sizeof(buf));

View File

@ -34,6 +34,9 @@
#define LED_B7 D, 6
#define LED_B8 D, 7
#define ADC_X 6
#define ADC_Y 7
#define RF_SCLK B, 5
#define RF_MISO B, 4
#define RF_MOSI B, 3