24Receiver.c


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 i = 0;
47
char itoabuffer[33];
48
uint8_t var =0;
49
uint8_t data_array[4];
50
uint8_t tx_address[5] = {0xD7,0xD7,0xD7,0xD7,0xD7};
51
uint8_t rx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
52
/* ------------------------------------------------------------------------- */
53
int main()
54
{
55
  /* init the hw uart */
56
  USART_Init(MYURBB);
57
58
  /* simple greeting message */
59
  uart_write("\r\n> RX device ready\r\n");
60
61
  /* init hardware pins */
62
  nrf24_init();
63
  
64
  /* Channel #2 , payload length: 4 */
65
  nrf24_config(2,4);
66
  
67
  /* Set the device addresses */
68
  nrf24_tx_address(tx_address);
69
  nrf24_rx_address(rx_address);
70
71
  while(1)
72
  {
73
    if(nrf24_dataReady())
74
    {
75
      nrf24_getData(data_array);
76
      uart_write("> ");
77
      for (i=0; i<4; i++)
78
      {
79
        var = data_array[i];
80
        itoa(var,itoabuffer,16);
81
        uart_write(itoabuffer);
82
        uart_write(" ");
83
      }
84
      uart_write(" \r\n");
85
    }
86
  }
87
}
88
/* ------------------------------------------------------------------------- */