20140521-181001

This commit is contained in:
2014-05-21 16:10:07 +00:00
parent a75b3870c8
commit ae3ffd7330
37 changed files with 6285 additions and 0 deletions
@@ -0,0 +1,33 @@
/*
* Led.h
*
* Created: 03.11.2013 17:11:58
* Author: BlubbFish
*/
#ifndef LED_H_
#define LED_H_
#include "hardware/pin.hpp"
template <typename Port, int pin>
class Led {
public:
Led() {
init();
}
void on() {
led::set();
}
void off() {
led::clear();
}
private:
void init() {
led::make_output();
off();
}
const typedef avrlib::pin<Port, pin> led;
};
#endif /* LED_H_ */
@@ -0,0 +1,86 @@
/*
* Spi.h
*
* Created: 06.11.2013 15:41:34
* Author: netz
*/
#ifndef SPI_H_
#define SPI_H_
#include <avr/io.h>
#include <util/delay.h>
#include "hardware/pin.hpp"
template <typename Port, int cspin, int misopin, int mosipin, int sckpin, int mode>
class Spi {
public:
Spi() {
this->init();
}
void CSOn() {
cs::make_low();
}
void CSOff() {
cs::make_high();
}
uint8_t send(uint8_t data) {
if(mode == 0) {
return this->send_hard(data);
}
return this->send_soft(data);
}
uint8_t has_data() {
return !miso::read();
}
private:
const typedef avrlib::pin<Port, cspin> cs;
const typedef avrlib::pin<Port, misopin> miso;
const typedef avrlib::pin<Port, mosipin> mosi;
const typedef avrlib::pin<Port, sckpin> sck;
void init() {
this->init_port();
if(mode == 0) {
this->init_spi();
}
}
void init_port() {
mosi::make_low(); //output und low;
sck::make_low(); //output und low;
cs::make_high(); //output und low;
miso::make_input(); //input und low;
}
void init_spi() {
SPCR = (1<<SPE) | (1<<MSTR);
SPSR = (1<<SPI2X);
}
uint8_t send_soft(uint8_t data) {
uint8_t datain=0;
for (uint8_t i=0; i<8; i++)
{
if (data & 0x80) {
mosi::make_high();
}
else {
mosi::make_low();
}
datain <<= 1;
if(miso::read()) {
datain |= 1;
}
sck::make_high();
data<<=1;
_delay_us(0.3);
sck::make_low();
}
return datain;
}
uint8_t send_hard(uint8_t data) {
SPDR = data; // Sendet ein Byte
loop_until_bit_is_set(SPSR, SPIF); // Wartet bis Byte gesendet wurde
return SPDR;
}
};
#endif /* SPI_H_ */
@@ -0,0 +1,50 @@
#ifndef AVRLIB_PIN_HPP
#define AVRLIB_PIN_HPP
#include <avr/io.h>
namespace avrlib {
template <typename Port, uint8_t Pin>
struct pin
{
static void set(bool value = true)
{
if (value)
Port::port(Port::port() | (1<<Pin));
else
Port::port(Port::port() & ~(1<<Pin));
}
static void clear() { set(false); }
static void toggle() { Port::port(Port::port() ^ (1<<Pin)); }
static bool get() { return (Port::port() & (1<<Pin)) != 0; }
static bool value() { return (Port::pin() & (1<<Pin)) != 0; }
static void output(bool value)
{
if (value)
Port::dir(Port::dir() | (1<<Pin));
else
Port::dir(Port::dir() & ~(1<<Pin));
}
static bool output() { return (Port::dir() & (1<<Pin)) != 0; }
static void make_output() { output(true); }
static void make_input() { output(false); clear(); }
static void make_low() { clear(); output(true); }
static void make_high() { set(); output(true); }
static void set_value(bool value) { set(value); }
static void set_high() { set(true); }
static void set_low() { set(false); }
static bool read() { return value(); }
static void pullup() { set_high(); }
};
}
#endif
@@ -0,0 +1,22 @@
#ifndef AVRLIB_PORTB_HPP
#define AVRLIB_PORTB_HPP
#include <avr/io.h>
namespace avrlib {
struct portb
{
static uint8_t port() { return PORTB; }
static void port(uint8_t v) { PORTB = v; }
static uint8_t pin() { return PINB; }
static void pin(uint8_t v) { PINB = v; }
static uint8_t dir() { return DDRB; }
static void dir(uint8_t v) { DDRB = v; }
};
}
#endif
@@ -0,0 +1,22 @@
#ifndef AVRLIB_PORTC_HPP
#define AVRLIB_PORTC_HPP
#include <avr/io.h>
namespace avrlib {
struct portc
{
static uint8_t port() { return PORTC; }
static void port(uint8_t v) { PORTC = v; }
static uint8_t pin() { return PINC; }
static void pin(uint8_t v) { PINC = v; }
static uint8_t dir() { return DDRC; }
static void dir(uint8_t v) { DDRC = v; }
};
}
#endif
@@ -0,0 +1,22 @@
#ifndef AVRLIB_PORTD_HPP
#define AVRLIB_PORTD_HPP
#include <avr/io.h>
namespace avrlib {
struct portd
{
static uint8_t port() { return PORTD; }
static void port(uint8_t v) { PORTD = v; }
static uint8_t pin() { return PIND; }
static void pin(uint8_t v) { PIND = v; }
static uint8_t dir() { return DDRD; }
static void dir(uint8_t v) { DDRD = v; }
};
}
#endif
@@ -0,0 +1,154 @@
/*
* rfm12.hpp
*
* Created: 08.05.2014 00:06:49
* Author: netz
*/
#ifndef RFM12_H_
#define RFM12_H_
template <typename Spi, uint8_t bandwidth, uint8_t gain, uint8_t drssi, uint32_t frequenz, uint16_t baud, uint8_t power, uint8_t mod>
class Rfm12 {
public:
Rfm12() {
this->init();
}
void ready(void) {
s.CSOn();
while(s.has_data()); // wait until FIFO ready
}
void beginasyncrx() {
this->send(0x82C8); // RX on
this->send(0xCA81); // set FIFO mode
this->send(0xCA83); // enable FIFO
}
uint8_t hasdata() {
s.CSOn();
return s.has_data();
}
uint8_t rxbyte() {
return this->send(0xB000);
}
void endasyncrx() {
this->send(0x8208); // RX off
}
void txdata(uint8_t *data, uint8_t number) {
uint8_t i;
this->send(0x8238); // TX on
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB82D);
this->ready();
this->send(0xB8D4);
for (i=0; i<number; i++)
{
this->ready();
this->send(0xB800|(*data++));
}
this->ready();
this->send(0x8208); // TX off
}
void rxdata(uint8_t *data, uint8_t number) {
uint8_t i;
this->send(0x82C8); // RX on
this->send(0xCA81); // set FIFO mode
this->send(0xCA83); // enable FIFO
for (i=0; i<number; i++)
{
this->ready();
*data++=this->send(0xB000);
}
this->send(0x8208); // RX off
}
void txpacket(uint8_t addr, uint8_t from, uint8_t data) {
this->send(0x8238); // TX on
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB8AA);
this->ready();
this->send(0xB82D);
this->ready();
this->send(0xB8D4);
this->ready();
this->send(0xB800|addr);
this->ready();
this->send(0xB800|from);
this->ready();
this->send(0xB800|data);
this->ready();
this->send(0xB800);
this->ready();
this->send(0x8208); // TX off
_delay_ms(100);
}
private:
Spi s;
uint16_t send(uint16_t wert) {
s.CSOn();
uint16_t werti = s.send((uint8_t)(wert >> 8)) << 8;
werti |= s.send((uint8_t)wert);
s.CSOff();
return werti;
}
void init(void) {
_delay_ms(100);
this->send(0xC0E0); // AVR CLK: 10MHz
this->send(0x80D7); // Enable FIFO
this->send(0xC2AB); // Data Filter: internal
this->send(0xCA81); // Set FIFO mode
this->send(0xE000); // disable wakeuptimer
this->send(0xC800); // disable low duty cycle
this->send(0xC4F7); // AFC settings: autotuning: -10kHz...+7,5kHz
this->setfreq(); // Sende/Empfangsfrequenz auf 433,92MHz einstellen
this->setbandwidth(); // 400kHz Bandbreite, 0dB Verstärkung, DRSSI threshold: -61dBm
this->setbaud(); // 19200 baud
this->setpower(); // 1mW Ausgangsleistung, 120kHz Frequenzshift
}
void setbandwidth() {
this->send( 0x9400 | ( ( bandwidth & 7 ) << 5 ) | ( ( gain & 3 ) << 3 ) | ( drssi & 7 ) );
}
void setfreq() {
uint16_t freq = (uint16_t)(((float)(frequenz-430000)/1000)/0.0025); // macro for calculating frequency value out of frequency in kHz
if( freq < 96 ) { // 430,2400MHz
this->send( 0xA000 | 96 );
} else if( freq > 3903 ) { // 439,7575MHz
this->send( 0xA000 | 3903 );
}
this->send( 0xA000 | freq );
}
void setbaud() {
if (baud < 663) {
return;
}
if (baud < 5400) { // Baudrate= 344827,58621/(R+1)/(1+CS*7)
this->send(0xC680 | ( ( 43104 / baud ) - 1 ) );
} else {
this->send(0xC600 | ( ( 344828UL / baud ) - 1 ) );
}
}
void setpower() {
this->send( 0x9800 | ( power & 7 ) | ( ( mod & 15 ) << 4 ) );
}
};
#endif /* RFM12_H_ */
@@ -0,0 +1,47 @@
/*
* Rs232.h
*
* Created: 04.11.2013 21:31:09
* Author: netz
*/
#ifndef RS232_H_
#define RS232_H_
#include <avr/io.h>
#include <avr/interrupt.h>
template <uint32_t baudrate>
class Uart {
public:
Uart() {
sei();
init();
send("Uart done!\r\n");
}
void send(const char *text) {
while (*text)
{
uart_putchar(*text);
text++;
}
}
void send(uint8_t wert) {
uart_putchar(wert);
}
private:
void init() {
UBRRL = (F_CPU / (baudrate * 16L) - 1); //Teiler wird gesetzt
UCSRB = /*(1<<RXEN1) | (1<<RXCIE1) | */ (1<<TXEN); //Enable TXEN im Register UCR TX-Data Enable
UCSRC = (1<<URSEL) | (3<<UCSZ0); //8N1
}
uint8_t uart_putchar(uint8_t c) {
loop_until_bit_is_set(UCSRA, UDRE); //Ausgabe des Zeichens
UDR = c;
return 0;
}
};
#endif /* RS232_H_ */