Last active
December 17, 2015 18:09
-
-
Save ahappyforest/5651555 to your computer and use it in GitHub Desktop.
nRF24L01.c test from http://gizmosnack.blogspot.com/2013/04/tutorial-nrf24l01-and-avr.html, this is a great tutorial!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <stdio.h> | |
#define F_CPU 8000000UL // 8MHZ | |
#include <util/delay.h> | |
#include <avr/interrupt.h> | |
#include "nRF24L01.h" | |
#define DDR_CSN DDB4 // PB4 | |
#define DDR_MOSI DDB5 // PB5 | |
#define DDR_MISO DDB6 // PB6 | |
#define DDR_SCK DDB7 // PB7 | |
#define PORT_CSN PORTB4 | |
#define PORT_MOSI PORTB5 | |
#define PORT_MISO PORTB6 | |
#define PORT_SCK PORTB7 | |
#define DDR_IRQ DDD2 // PD2 | |
#define DDR_CE DDB1 // PB1 | |
#define PORT_IRQ PORTD2 | |
#define PORT_CE PORTB1 | |
#define LED PORTB0 | |
void InitSPI(void) | |
{ | |
DDRB |= (1 << DDR_SCK) | (1 << DDR_MOSI) | (1 << DDR_CSN) | (1 << DDR_CE); | |
SPCR |= (1 << SPE) | (1 << MSTR);// | (1 << SPR0); | |
SETBIT(PORTB, PORT_CSN); | |
CLEARBIT(PORTB, PORT_CE); | |
} | |
char WriteByteSPI(unsigned char cData) | |
{ | |
SPDR = cData; | |
while (!(SPSR & (1 << SPIF))); | |
return SPDR; | |
} | |
void LEDInit(void) | |
{ | |
DDRB |= (1 << LED); | |
} | |
void INT0_interrupt_init(void) | |
{ | |
DDRD &= ~(1 << DDD2); | |
MCUCR |= (1 << ISC01); | |
MCUCR &= ~(1 << ISC00); | |
GICR |= (1 << INT0); | |
sei(); | |
} | |
/********************************/ | |
// nRF24L01 | |
// 1. CSN = 0 | |
// 2. delay 10us | |
// 3. write a byte to SPI | |
// 4. delay 10us | |
// 5. write more bytes | |
// 6. delay 10us | |
// 7. CSN = 1 | |
// 8. return | |
/*********************************/ | |
uint8_t *WriteToNrf(uint8_t ReadWrite, uint8_t reg, uint8_t *val, uint8_t antVal) | |
{ | |
if (ReadWrite == W) { | |
reg = W_REGISTER + reg; | |
} | |
static uint8_t ret[32]; | |
_delay_us(10); | |
CLEARBIT(PORTB, PORT_CSN); | |
_delay_us(10); | |
WriteByteSPI(reg); | |
_delay_us(10); | |
int i; | |
for (i = 0; i < antVal; i++) { | |
if (ReadWrite == R && reg != W_TX_PAYLOAD) { | |
ret[i] = WriteByteSPI(NOP); | |
_delay_us(10); | |
} else { | |
WriteByteSPI(val[i]); | |
_delay_us(10); | |
} | |
} | |
SETBIT(PORTB, PORT_CSN); | |
return ret; | |
} | |
void reset(void) | |
{ | |
_delay_us(10); | |
CLEARBIT(PORTB, PORT_CSN); | |
_delay_us(10); | |
WriteByteSPI(W_REGISTER + STATUS); | |
_delay_us(10); | |
WriteByteSPI(0x07); | |
_delay_us(10); | |
SETBIT(PORTB, PORT_CSN); | |
} | |
uint8_t GetReg(uint8_t reg) | |
{ | |
uint8_t status; | |
_delay_us(10); | |
CLEARBIT(PORTB, PORT_CSN); | |
_delay_us(10); | |
WriteByteSPI(R_REGISTER + reg); | |
_delay_us(10); | |
status = WriteByteSPI(NOP); | |
_delay_us(10); | |
SETBIT(PORTB, PORT_CSN); | |
return status; | |
} | |
void nrf24L01_init(void) | |
{ | |
_delay_ms(100); | |
uint8_t val[5]; | |
val[0] = 0x01; | |
WriteToNrf(W, EN_AA, val, 1); | |
val[0] = 0x01; | |
WriteToNrf(W, EN_RXADDR, val, 1); | |
val[0] = 0x03; | |
WriteToNrf(W, SETUP_AW, val, 1); | |
val[0] = 0x01; | |
WriteToNrf(W, RF_CH, val, 1); | |
val[0] = 0x07; | |
WriteToNrf(W, RF_SETUP, val, 1); | |
int i; | |
for (i = 0; i < 5; i++) { | |
val[i] = 0x12; | |
} | |
WriteToNrf(W, RX_ADDR_P0, val, 5); | |
for (i = 0; i < 5; i++) { | |
val[i] = 0x12; | |
} | |
WriteToNrf(W, TX_ADDR, val, 5); | |
val[0] = 5; | |
WriteToNrf(W, RX_PW_P0, val, 1); | |
val[0] = 0x1E; | |
WriteToNrf(W, CONFIG, val, 1); | |
_delay_ms(100); | |
} | |
void receive_payload(void) | |
{ | |
sei(); | |
SETBIT(PORTB, PORT_CE); | |
_delay_ms(1000); | |
CLEARBIT(PORTB, PORT_CE); | |
cli(); | |
}Kalle | |
void transmit_payload(uint8_t *W_buff) | |
{ | |
WriteToNrf(R, FLUSH_TX, W_buff, 0); | |
WriteToNrf(R, W_TX_PAYLOAD, W_buff, 5); | |
_delay_ms(10); | |
SETBIT(PORTB, PORT_CE); | |
_delay_us(20); | |
CLEARBIT(PORTB, PORT_CE); | |
_delay_us(10); | |
} | |
int main(void) | |
{ | |
InitSPI(); | |
LEDInit(); | |
INT0_interrupt_init(); | |
nrf24L01_init(); | |
reset(); | |
SETBIT(PORTB, LED); // LED | |
_delay_ms(1000); | |
CLEARBIT(PORTB, LED); | |
while (1) { | |
if (GetReg(STATUS) == 0x07) { | |
SETBIT(PORTB, LED); | |
_delay_ms(1000); | |
CLEARBIT(PORTB, LED); | |
} | |
_delay_ms(1000); | |
} | |
return 0; | |
} | |
uint8_t *data; | |
ISR(INT0_vect) | |
{ | |
cli(); | |
CLEARBIT(PORTB, PORT_CE); | |
SETBIT(PORTB, LED); | |
_delay_ms(500); | |
CLEARBIT(PORTB, LED); | |
data = WriteToNrf(R, R_RX_PAYLOAD, data, 5); | |
reset(); | |
for (int i = 0; i < 5; i++) { | |
USART_Transmit(data[i]); | |
} | |
sei(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment