Hi all,
I am trying to use a nRF2401 module to get air rabish as my first test case.
I am using the Atmel AVR ATMega128, running at 3.3v at 8Mhz and the nRF2401 is connected to the GPIO A port of the avr like this:
PortA (GPIO) nRF2401
0----------------------------->CS
1----------------------------->CLK
2----------------------------->Data
3<-----------------------------DR1
6----------------------------->CE
7<-----------------------------data
Vcc-----------------------------Vcc
Gdn-----------------------------Gnd
As nRF2401 data pin is I/O I used to avr pins to drive it, one to read and other one to write to it.
Here is the source code (gcc):
#include <avr\io.h>
void delay_ms(unsigned char x)
{
unsigned char y, z;
for ( ; x > 0 ; x–)
for ( y = 0 ; y < 4 ; y++)
for ( z = 0 ; z < 176 ; z++);
}
#define CE_LOW (PORTA = PORTA & 0b10111111);
#define CE_HIGH (PORTA = PORTA | 0b01000000);
#define CLK1_LOW (PORTA = PORTA & 0b11111101);
#define CLK1_HIGH (PORTA = PORTA | 0b00000010);
#define CS_LOW (PORTA = PORTA & 0b11111110);
#define CS_HIGH (PORTA = PORTA| 0b00000001);
#define DATAOUT_LOW (PORTA = PORTA & 0b11111011);
#define DATAOUT_HIGH (PORTA = PORTA | 0b00000100);
unsigned char BitIsHigh(unsigned char var, unsigned char index)
{
if(index==7)
{
return ( (var & 10000000) == 10000000 );
}
else if(index==6)
{
return ( (var & 01000000) == 01000000 );
}
else if(index==5)
{
return ( (var & 00100000) == 00100000 );
}
else if(index==4)
{
return ( (var & 00010000) == 00010000 );
}
else if(index==3)
{
return ( (var & 00001000) == 00001000 );
}
else if(index==2)
{
return ( (var & 00000100) == 00000100 );
}
else if(index==1)
{
return ( (var & 00000010) == 00000010 );
}
else //0
{
return ( (var & 00000001) == 00000001 );
}
}
unsigned char IsHighDATAIN(void)
{
unsigned char temp=0;
temp=PORTA;
if(BitIsHigh(temp,7))
{
return 1;
}
return 0;
}
unsigned char IsHighDR1(void)
{
unsigned char temp=0;
temp=PORTA;
if(BitIsHigh(temp,3))
{
return 1;
}
return 0;
}
void SetBit(unsigned char *b,unsigned char p)
{
if(p==7)
{
*b |= 10000000;
}
else if(p==6)
{
*b |= 01000000;
}
else if(p==5)
{
*b |= 00100000;
}
else if(p==4)
{
*b |= 00010000;
}
else if(p==3)
{
*b |= 00001000;
}
else if(p==2)
{
*b |= 00000100;
}
else if(p==1)
{
*b |= 00000010;
}
else //0
{
*b |= 00000001;
}
}
void ClearBit(unsigned char *b,unsigned char p)
{
if(p==7)
{
*b &= 01111111;
}
else if(p==6)
{
*b &= 10111111;
}
else if(p==5)
{
*b &= 11011111;
}
else if(p==4)
{
*b &= 11101111;
}
else if(p==3)
{
*b &= 11110111;
}
else if(p==2)
{
*b &= 11111011;
}
else if(p==1)
{
*b &= 11111101;
}
else //0
{
*b &= 11111110;
}
}
#define CS PINA0 //out
#define CLK1 PINA1 //out
#define DATAOUT PINA2 //out
#define CE PINA6 //out
#define DR1 PINA3 //in
#define DATAIN PINA7 //in
unsigned char data_array[4];
void configure_receiver(void);
void receive_data(void);
void main()
{
unsigned char x;
//HARDWARE INITIALIZATION
//nRF2401 controller port
PORTA = 0b00000000;
DDRA = 0b01000111; //0 = Input, 1 = Output
//LEDS
PORTB = 0b00000000;
DDRB = 0b11111111; //0 = Input, 1 = Output
//END OF HARDWARE INITIALIZATION
PORTB = 0xFF; //Clear leds
//leds wellcome
for (x = 0; x < 3; x++)
{
PORTB = 0xFE;
delay_ms(140);
PORTB = 0xFD;
delay_ms(140);
PORTB = 0xFB;
delay_ms(140);
PORTB = 0xFF;
}
PORTB = 0xFF; //leds off
while(1)
{
configure_receiver();
delay_ms(150);
if(IsHighDR1()) //We have data!
{
PORTB=0x00; //turn all leds on
delay_ms(500);
}
}
}
void receive_data(void)
{
unsigned char i, j, temp;
CE_LOW; //Power down RF Front end
//Erase the current data array so that we know we are looking at actual received data
data_array[0] = 0x00;
data_array[1] = 0x00;
data_array[2] = 0x00;
data_array[3] = 0x00;
//Clock in data, we are setup for 32-bit payloads
for(i = 0 ; i < 4 ; i++) //4 bytes
{
for(j = 7 ; j >=0 ; j–) //8 bits each
{
if(IsHighDATAIN())
{
SetBit(&temp,j);
}
else
{
ClearBit(&temp,j);
}
CLK1_HIGH;
CLK1_LOW;
}
data_array = temp; //Store this byte
}
CE_HIGH; //Power up RF Front end
}
void configure_receiver(void)
{
unsigned char i,j,temp;
unsigned char config_setup[3];
//Config Mode
CE_LOW;
- CS_HIGH;*
delay_ms(50);
config_setup[0] = 0b00000101;
config_setup[1] = 0b01001110;
config_setup[2] = 0b00100010; //most right bit is crc off!
//Download to nrf2401
for (j = 3; j > 0; j–)
{
- temp=config_setup[j-1];*
for(i = 7 ; i >=0 ; i–) //8 bits each
*{ * - if(BitIsHigh(temp,i))*
- {*
- DATAOUT_HIGH;*
- }*
- else*
- {*
- DATAOUT_LOW;*
- }*
- CLK1_HIGH;*
- CLK1_LOW;*
}
}
//Configuration is actived on falling edge of CS (page 10)
CE_LOW; - CS_LOW;*
delay_ms(70);
//Start monitoring the air
CE_HIGH;
- CS_LOW;*
}
The leds never flash, or no data is received…
Please, could anyone help me ?
JonhR