1 | /*
|
2 | *
|
3 | * ----------------------------------------------------------------------------
|
4 | * THE COFFEEWARE LICENSE (Revision 1):
|
5 | * <ihsan@kehribar.me> wrote this file. As long as you retain this notice you
|
6 | * can do whatever you want with this stuff. If we meet some day, and you think
|
7 | * this stuff is worth it, you can buy me a coffee in return.
|
8 | * -----------------------------------------------------------------------------
|
9 | */
|
10 |
|
11 | #ifndef F_CPU //Define F_CPU if not done
|
12 | #define F_CPU 16000000UL
|
13 | #endif
|
14 | #define BAUD 9600UL
|
15 | #define MYURBB F_CPU/16/BAUD-1
|
16 | #define UART_PUFFER_MAX 33
|
17 |
|
18 | #include <avr/io.h>
|
19 | #include <stdint.h>
|
20 | #include <stdlib.h>
|
21 | #include <util/delay.h>
|
22 | #include "SPI.h"
|
23 | #include "nrf24.h"
|
24 |
|
25 | /* ------------------------------------------------------------------------- */
|
26 | //UART Settings
|
27 | void USART_Init(unsigned int ubrr)
|
28 | { // set baud rate
|
29 | UBRR0H = (uint8_t) (ubrr >> 8);
|
30 | UBRR0L = (uint8_t) (ubrr);
|
31 | // activate Rx and Tx
|
32 | UCSR0B = /*(1<<RXEN0) | (1<<RXCIE0) | */(1<<TXEN0);
|
33 | // select async 8N1
|
34 | UCSR0C = (3<<UCSZ00);
|
35 | }
|
36 | void uart_putchar(char c)
|
37 | { while (!(UCSR0A & (1 << UDRE0))); // wait for buffer ready
|
38 | UDR0 = c;
|
39 | }
|
40 | void uart_write(char *str)
|
41 | { while(*str) {uart_putchar(*str); str++;}
|
42 | }
|
43 |
|
44 | /* ------------------------------------------------------------------------- */
|
45 | uint8_t temp;
|
46 | uint8_t q = 0;
|
47 | char itoabuffer[33];
|
48 | uint8_t var =0;
|
49 | uint8_t data_array[4];
|
50 | uint8_t tx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
|
51 | uint8_t rx_address[5] = {0xD7,0xD7,0xD7,0xD7,0xD7};
|
52 | /* ------------------------------------------------------------------------- */
|
53 | int main()
|
54 | {
|
55 | /* init the hw uart */
|
56 | USART_Init(MYURBB);
|
57 |
|
58 | /* simple greeting message */
|
59 | uart_write("\r\n> TX device ready\r\n");
|
60 | /*
|
61 | uart_write("send_complete?\r\n");
|
62 | itoa(var,itoabuffer,10);
|
63 | uart_write(itoabuffer);
|
64 | uart_write("\r\n");
|
65 | */
|
66 | /* init hardware pins */
|
67 | nrf24_init();
|
68 |
|
69 | /* Channel #2 , payload length: 4 */
|
70 | nrf24_config(2,4);
|
71 |
|
72 | /* Set the device addresses */
|
73 | nrf24_tx_address(tx_address);
|
74 | nrf24_rx_address(rx_address);
|
75 |
|
76 | while(1)
|
77 | {
|
78 | /* Fill the data buffer */
|
79 | data_array[0] = 0x00;
|
80 | data_array[1] = 0xAA;
|
81 | data_array[2] = 0x55;
|
82 | data_array[3] = q++;
|
83 |
|
84 | /* Automatically goes to TX mode */
|
85 | nrf24_send(data_array);
|
86 |
|
87 | /* Wait for transmission to end */
|
88 | while(nrf24_isSending());
|
89 |
|
90 | /* Make analysis on last tranmission attempt */
|
91 | temp = nrf24_lastMessageStatus();
|
92 |
|
93 | if(temp == NRF24_TRANSMISSON_OK)
|
94 | {
|
95 | uart_write("> Tranmission went OK\r\n");
|
96 | }
|
97 | else if(temp == NRF24_MESSAGE_LOST)
|
98 | {
|
99 | uart_write("> Message is lost ...\r\n");
|
100 | }
|
101 |
|
102 | /* Retranmission count indicates the tranmission quality */
|
103 | temp = nrf24_retransmissionCount();
|
104 | uart_write("> Retranmission count: ");
|
105 | itoa(temp,itoabuffer,10);
|
106 | uart_write(itoabuffer);
|
107 | uart_write("\r\n");
|
108 |
|
109 | /* Optionally, go back to RX mode ... */
|
110 | nrf24_powerUpRx();
|
111 |
|
112 | /* Or you might want to power down after TX */
|
113 | // nrf24_powerDown();
|
114 |
|
115 | /* Wait a little ... */
|
116 | _delay_ms(1000);
|
117 | }
|
118 | }
|
119 | /* ------------------------------------------------------------------------- */
|