pyrorf/main.c
2019-10-22 00:56:22 +02:00

123 lines
1.7 KiB
C

#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/pgmspace.h>
//#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "defines.h"
#include "lcd.h"
#include "spi.h"
#include "rf24.h"
#include "Hamming.h"
#include "tools.h"
int main (void);
void setup (void);
void loop (void);
void _lcd();
void _radio();
uint8_t radiobuffer[32];
volatile uint8_t skip_sleep;
volatile uint8_t t_lcd=0,t_radio=0;
uint8_t p=0;
int main(void)
{
setup();
while (1)
loop();
}
void setup(void)
{
#if defined(USBCON)
USBCON=0;
#endif
// Setup ports
// Port A => Input, no pull-ups
DDRA = 0; PORTA=0;
// Port B => Input, Pull up on INT2(PB2) , SLAVE-SELECT (PB3)
DDRB = 0; PORTB = _BV(PB2)|_BV(PB3);
// Port C => All outputs Low
PORTC=0; DDRC=0xFF;
// Port D => Output : BATT_ENABLE (PD6), pullup on (PD2-4)
PORTD= _BV(PD2)|_BV(PD3)|_BV(PD4);
lcd_init(20,4);
lcd_backlight(1);
lcd_clear();
lcd_home();
lcd_display();
lcd_setCursor(1,1);
lcdprint("** Pyro RF ! **");
spi_init_master();
_delay_ms(500);
lcd_setCursor(3,2);
lcdprint("Init");
GICR|=_BV(INT2);
// Timer1 => 5ms
#if F_CPU == 8000000UL
#define TIMERLOOP 5000
#else
#define TIMERLOOP 10000
#endif
#if defined(TIMSK1)
TCCR1A = 0;
TCNT1=0;
OCR1A = TIMERLOOP;
TIMSK1 = _BV(OCIE1A);
TCCR1B = _BV(WGM12)|_BV(CS11);
#else
TCCR1A = 0;
TCNT1=0;
OCR1A = TIMERLOOP;
TIMSK = _BV(OCIE1A);
TCCR1B = _BV(WGM12)|_BV(CS11);
#endif
sei();
}
void loop(void)
{
skip_sleep=0;
if(!skip_sleep){
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
}
ISR(TIMER1_COMPA_vect){
skip_sleep=1;
}
ISR(INT2_vect){
skip_sleep=1;
}