Funkschalter/Reciver/Reciver/Reciver.c
2013-03-31 16:50:14 +00:00

169 lines
3.3 KiB
C
Raw Blame History

/*
* Reciver.c
*
* Created: 25.03.2013 21:03:07
* Author: netz
*/
#include "global.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "rf12.h"
#include "codes.h"
volatile int status = SLEEP;
volatile int pwm_led = 0;
volatile int servopos = RIGHT;
void turn() {
if(status == RUN)
return;
status = RUN;
servopos = RIGHT-60;
_delay_ms(10+DEVICE);
rf12_txpacket(MASTER, DEVICE, status);
_delay_ms(700);
servopos = RIGHT-120;
_delay_ms(700);
servopos = RIGHT;
_delay_ms(700);
}
void poll() {
DDRD &= ~(1<<PIND1);
if(status == ACTIVE) {
if(PIND & (1<<PIND1)) {
rf12_endasyncrx();
turn();
rf12_beginasyncrx();
}
}
if(status == SLEEP) {
if(PIND & (1<<PIND1)) {
rf12_endasyncrx();
rf12_txpacket(MASTER, DEVICE, DEDECT);
rf12_beginasyncrx();
}
}
}
void recive() {
rf12_beginasyncrx();
while(rf12_hasdata()) {
poll();
}
uint8_t addr = rf12_rxbyte();
if(addr == ALL || addr == DEVICE || addr == GROUP) {
while(rf12_hasdata()) {
poll();
}
uint8_t from = rf12_rxbyte();
while(rf12_hasdata()) {
poll();
}
if(from == MASTER) {
uint8_t data = rf12_rxbyte();
switch(data) {
case PING: {
break;
}
case SETSLEEP: {
status = SLEEP;
break;
}
case SETACTIVE: {
status = ACTIVE;
break;
}
case SETRUN: {
rf12_endasyncrx();
turn();
rf12_beginasyncrx();
return;
}
default:
{
rf12_endasyncrx();
return;
}
}
rf12_endasyncrx();
_delay_ms(10+DEVICE);
rf12_txpacket(MASTER, DEVICE, status);
return;
}
}
}
void init_timer() {
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7,813 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
// Timer Period: 21,504 ms
TCCR0=(0<<WGM00) | (0<<COM01) | (0<<COM00) | (0<<WGM01) | (1<<CS02) | (0<<CS01) | (1<<CS00);
TCNT0=0x58;
OCR0=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (1<<TOIE0);
}
ISR(TIMER0_OVF_vect) {
// Reinitialize Timer 0 value
TCNT0=0x58;
if(status == SLEEP) {
if(pwm_led > 50) {
PORTB ^= (1<<PINB0);
pwm_led = 0;
}
} else if(status == ACTIVE) {
if(pwm_led > 5) {
PORTB ^= (1<<PINB0);
pwm_led = 0;
}
} else if(status == RUN) {
if(pwm_led > 1) {
PORTB ^= (1<<PINB0);
pwm_led = 0;
}
}
pwm_led++;
DDRB |= (1<<PB1);
PORTB |= (1<<PB1);
_delay_us(LEFT);
for(int i=0;i<servopos;i++) {
_delay_us(STEP);
}
PORTB &= ~(1<<PB1);
}
int main(void)
{
rf12_init(); // ein paar Register setzen (z.B. CLK auf 10MHz)
rf12_setfreq(RF12FREQ(433.92)); // Sende/Empfangsfrequenz auf 433,92MHz einstellen
rf12_setbandwidth(1, 0, 7); // 400kHz Bandbreite, 0dB Verst<73>rkung, DRSSI threshold: -61dBm
rf12_setbaud(9600); // 19200 baud
rf12_setpower(0, 6); // 1mW Ausgangsleistung, 120kHz Frequenzshift
init_timer();
DDRB |= (1<<PINB0);
sei();
//DDRB |= (1<<PINB0);
while(1)
{
recive();
/*PORTB |= (1<<PINB0);
_delay_ms(100);
PORTB &= ~(1<<PINB0);
_delay_ms(900);*/
}
}