1 | #include <avr/io.h>
|
2 | #include <avr/interrupt.h>
|
3 | #include <util/delay.h>
|
4 | #include "spi.h" // definition der funktionen
|
5 | #include "nRF24L01.h"
|
6 | #include "nrf24l01_function.h" // definition der funktion
|
7 |
|
8 |
|
9 | #define BIT(x) (1 << (x))
|
10 | #define SETBITS(x,y) ((x) |= (y))
|
11 | #define CLEARBITS(x,y) ((x) &= (~(y)))
|
12 | #define TOGGLEBITS(x,y) ((x) ^= (y))
|
13 | #define SETBIT(x,y) SETBITS((x), (BIT((y))))
|
14 | #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
|
15 | #define TOGGLEBIT(x,y) TOGGLEBITS((x), (BIT((y))))
|
16 |
|
17 | #define DD_CE 1
|
18 | #define DD_CSN 2
|
19 | #define DD_SCK 7
|
20 | #define DD_MOSI 5
|
21 | #define DD_MISO 6
|
22 | #define DD_IRQ 0
|
23 |
|
24 | ISR (TIMER0_OVF_vect);
|
25 | ISR (INT0_vect);
|
26 | void extern_interrupt_init(void);
|
27 | void timer_interrupt_init(void);
|
28 | void io_init(void);
|
29 | void slave_function(void);
|
30 | void master_function(void);
|
31 |
|
32 | volatile unsigned int counter = 0;
|
33 | volatile unsigned int sekunde_over = 0;
|
34 | volatile unsigned int received_data = 0;
|
35 | volatile unsigned int new_data = 0;
|
36 |
|
37 | int main(void){
|
38 | unsigned int k=0;
|
39 | spi_init();
|
40 | extern_interrupt_init(); // INT0 -> falling edge
|
41 | timer_interrupt_init(); // Timer0 -> 1ms
|
42 | io_init(); // PD5 -> output -> low
|
43 | sei(); // Global Interrupts activate
|
44 | while (1){
|
45 | // steht im ersten ATtiny
|
46 | if(sekunde_over) WriteToNrf(W_TX_PAYLOAD, 0x01);
|
47 |
|
48 | // steht im zweiten ATtiny
|
49 | if(new_data){
|
50 | for(k=0; k<received_data; k++){
|
51 | SETBIT(PORTD, 5); // schaltet LED ein
|
52 | _delay_ms(100);
|
53 | CLEARBIT(PORTD, 5);// schaltet LED aus
|
54 | _delay_ms(100);
|
55 | }
|
56 | new_data = 0;
|
57 | }
|
58 | }
|
59 | }
|
60 |
|
61 |
|
62 | void extern_interrupt_init(void){
|
63 | DDRB &= ~(1 << DDB6); // External Interrupt on INT0
|
64 |
|
65 | MCUCR |= (1 << ISC01); // INT0 falling edge
|
66 | MCUCR &= ~(1 << ISC00); // INT0 falling edge
|
67 |
|
68 | GIMSK |= (1 << INT0); // enable INT0 interrupt
|
69 | }
|
70 |
|
71 | void timer_interrupt_init(void){
|
72 | // Timer 0
|
73 | TCCR0A = (1 << WGM01); // CTC Modus
|
74 | TCCR0B = (1 << CS01) ; // prescaler 8 -> 1Mhz/8 = 125kHz
|
75 | OCR0A = 125-1; // -> 1ms Interrupt
|
76 | // Overflow Interrupt allow
|
77 | TIMSK |= (1 << OCIE0A);
|
78 | }
|
79 |
|
80 | void io_init(void){
|
81 | // LED as output
|
82 | //DDRD |= (1 << PD5);
|
83 | //PORTD &= ~(1 << PD5);
|
84 | SETBIT(DDRD, 5);
|
85 | CLEARBIT(PORTD, 5);
|
86 | }
|
87 |
|
88 |
|
89 | ISR (TIMER0_COMPA_vect){
|
90 | static unsigned int millisekunde = 0;
|
91 | static unsigned int sekunde = 0;
|
92 |
|
93 | millisekunde++;
|
94 |
|
95 | if(millisekunde == 1000){
|
96 | millisekunde = 0;
|
97 | sekunde_over = 1;
|
98 | sekunde++;
|
99 |
|
100 | if(sekunde == 60){
|
101 | sekunde = 0;
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 | ISR(INT0_vect){
|
107 | if(new_data==0){
|
108 | cli(); // Disable global interrupt
|
109 | CLEARBIT(PORTB, DD_CE); // CE low - stop sending/listening
|
110 | new_data = 1;
|
111 | received_data = GetReg(R_RX_PAYLOAD);
|
112 |
|
113 | sei(); // enable global interrupts
|
114 | }
|
115 | }
|