-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
117 lines (104 loc) · 3.53 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// MIT License
//
// Copyright (c) 2018 Helvijs Adams
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Software was tested on ATmega328P and ATmega328PB (PB needs few changes in SPI)
// RF module software was tested on - cheap nRF24L01+ from China
// All the relevant settings are defined in nrf24l01.c file
// Some features will be added later, at this moment it is bare minimum to send/receive
//
// Set clock frequency
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
// Set up UART for printf();
#ifndef BAUD
#define BAUD 9600
#endif
#include "STDIO_UART.h"
// Include nRF24L01+ library
#include "nrf24l01.h"
#include "nrf24l01-mnemonics.h"
#include "spi.h"
void print_config(void);
// Used in IRQ ISR
volatile bool message_received = false;
volatile bool status = false;
int main(void)
{
// Set cliche message to send (message cannot exceed 32 characters)
char tx_message[32]; // Define string array
strcpy(tx_message,"Hello World!"); // Copy string into array
// Initialize UART
uart_init();
// Initialize nRF24L01+ and print configuration info
nrf24_init();
print_config();
// Start listening to incoming messages
nrf24_start_listening();
while (1)
{
if (message_received)
{
// Message received, print it
message_received = false;
printf("Received message: %s\n",nrf24_read_message());
// Send message as response
_delay_ms(500);
status = nrf24_send_message(tx_message);
if (status == true) printf("Message sent successfully\n");
}
}
}
// Interrupt on IRQ pin
ISR(INT0_vect)
{
message_received = true;
}
void print_config(void)
{
uint8_t data;
printf("Startup successful\n\n nRF24L01+ configured as:\n");
printf("-------------------------------------------\n");
nrf24_read(CONFIG,&data,1);
printf("CONFIG 0x%x\n",data);
nrf24_read(EN_AA,&data,1);
printf("EN_AA 0x%x\n",data);
nrf24_read(EN_RXADDR,&data,1);
printf("EN_RXADDR 0x%x\n",data);
nrf24_read(SETUP_RETR,&data,1);
printf("SETUP_RETR 0x%x\n",data);
nrf24_read(RF_CH,&data,1);
printf("RF_CH 0x%x\n",data);
nrf24_read(RF_SETUP,&data,1);
printf("RF_SETUP 0x%x\n",data);
nrf24_read(STATUS,&data,1);
printf("STATUS 0x%x\n",data);
nrf24_read(FEATURE,&data,1);
printf("FEATURE 0x%x\n",data);
printf("-------------------------------------------\n\n");
}