چند وقت پیش قبل از اینکه برد درایور USB ماژول های NRF24L01 سایت ببینم به فکر ساختش بودم که اونو ساختم و بعد از دست کاری
کد برنامه نتیجه این شد :mrgreen: کسی خواست دانلود کنه بسازه . برای مبدل USB به سریال از یک میکرو MEGA8 استفاده کردم
و برای درایو NRF هم از یک مگا 8 دیگه . با این ماژول هم میتونید بفرستین و هم بگیرین فقط دیتا هایی که توی محیط ترمینال مینویسید داخل پرانتز باشه
مثلا (1234)
قسمت usb اون هم روی برد در آوردم که حسابی ارزون بشه . فقط پشت قسمت فیش usb 4 تا ترک هست که روی اون سیم لحیم بکشید تا کلفت بشه لغ نخوره
http://upload7.ir/u/preview/s1/dec7d...SzxcgBhjzmgOuq


[code=c]
#include <mega8.h>
//#define xtal 8000000
#include <delay.h>
#include <stdio.h>
#include "nrf24l01p.h"
#define LEDR_ON PORTC.3=1
#define LEDR_OFF PORTC.3=0
#define LEDT_ON PORTB.7=1
#define LEDT_OFF PORTB.7=0
bit look=1;
char canter=0;
bit Trans=0;
unsigned char DATA_USART[30];
interrupt [USART_RXC] void usart_rx_isr(void)
{
char data;
data=UDR;
if(data=='(' && look==1 )look=0,canter=0,LEDR_ON;
if(look==0) DATA_USART[canter]=data,canter++;
if(data=='
' || canter>31)look=1,Trans=1;
//LEDR_OFF;
}
void main()
{
unsigned char Payload[],i;
DDRC=0b00000000;
PORTD=0b00001100;
DDRB.7=1;
delay_ms(1000);
LEDT_ON;
delay_ms(100);
LEDT_OFF;
delay_ms(100);
LEDR_ON;
delay_ms(100);
LEDR_OFF;
PORTD.0=0;
DDRD.0=0;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(1<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x32;
delay_ms(1000);
#asm("sei"
nrf_config(RX);
nrf_state=0;
// while(1) NRF_SEND(30,"welcom"
,delay_ms(1000);
while(1){
if(Trans==1){
Trans=0;
NRF_SEND(canter,DATA_USART);
LEDR_OFF;
}
//+++++++++++++++++++RECEVE+++++++++++++++++++++++++ ++++++
if(nrf_state==1){
LEDT_ON;
LEDR_ON;
//for(i=1 ; i<=payload[0] ; i++)putchar(payload[i]),delay_us(10);
printf("%s",payload);
LEDT_OFF;
LEDR_OFF;
for(i=0 ; i<=33 ; i++)payload[i]=0;
nrf_state=0;
}
}
} // end main
[/code]
[code=c]
/* Name: nrf24l01p.h
// External Interrupt(s) initialization
// INTx: On
// INTx Mode: Falling Edge
// INTERRUPT_CHEACK();
//nrf_config(char mode) RX OR TX
*/
#include <delay.h>
#define LED PORTC.5
#ifndef __nrf24l01p_h_included__
#define __nrf24l01p_h_included__
#define IRQ PORTD.2
#define DD_IRQ DDRD.2
#define CE PORTC.0
#define DD_CE DDRC.0
#define CSN PORTB.2
#define DD_CSN DDRB.2
#define MOSI PORTB.3
#define DD_MOSI DDRB.3
#define MISO PORTB.4
#define DD_MISO DDRB.4
#define CLK PORTB.5
#define DD_CLK DDRB.5
#define TX 0
#define RX 1
#define NRF_STANDBAY 0 // NRF BI KAR AST
#define NRF_RECEVE 1 // DATA DARYAFT SHODE
#define NRF_T_OK 2 // DATA BE DOROSTI SEND SHODE
#define NRF_T_R 3 // DATA BE DOROSTI SEND SHODE VA CHIZI DARYAFT SHODE
#define NRF_T_ERR 4 // DATA BE DOROSTI ERSAL NASHODE
#define db 11
flash unsigned char base_address[5]={0x00,0x01,0x03,0x07,0x00};
unsigned char temp_address[5]={0x00,0x01,0x03,0x07,0x00};
unsigned char payload[33];
unsigned char command_buff=0,status_buff=0,nrf_state=0;
char nrf_mode;
bit sent_actived=0;
unsigned char spi(unsigned char data)
{
SPDR=data;
while(!(SPSR & 0x80));
data=SPSR;
return SPDR;
}
void nrf_inst(unsigned char ins)
{
int i;
CSN=0;
status_buff=spi(ins);
switch(ins & 0xE0)
{
case 0x00:
{
if((ins & 0x1F)==0x0A || (ins & 0x1F)==0x0B || (ins & 0x1F)==0x10)
{
for(i=4;i>=0;i--)
{
temp_address[i]=spi(0xFF);
}
}
else
{
command_buff=spi(0xFF);
}
break;
}
case 0x20:
{
if((ins & 0x1F)==0x0A || (ins & 0x1F)==0x0B || (ins & 0x1F)==0x10)
{
for(i=4;i>=0;i--)
{
spi(base_address[i]);
}
}
else
{
spi(command_buff);
}
break;
}
case 0x60:
{
if((ins & 0x01)==1)
{
i=payload[0];
while(i!=0)
{
payload[i]=spi(0xFF);
i--;
}
}
else
{
command_buff=spi(0xFF);
}
break;
}
case 0xA0:
{
i=payload[0];
while(i!=0)
{
spi(payload[i]);
i--;
}
break;
}
}
CSN=1;
delay_us(10);
}
void INTERRUPT_CHEACK(){
if(nrf_mode==0)
{
nrf_inst(0xFF);
if(status_buff & 0x20)
{
nrf_state=2;
nrf_inst(0x17);
if((command_buff & 0x01)==0)
{
nrf_inst(0x60);
if(command_buff<=32)
{
payload[0]=command_buff;
nrf_inst(0x61);
nrf_state=3;
}
else
nrf_inst(0xE2);
}
}
else
{
nrf_state=4;
}
}
else
{
nrf_inst(0x60);
if(command_buff>32)
{
nrf_inst(0xE2);
}
else
{
payload[0]=command_buff;
nrf_inst(0x61);
nrf_state=1;
}
}
command_buff=0x7E;
nrf_inst(0x27);
nrf_inst(0xE1);
}
interrupt [EXT_INT0] void ext_int0_isr(void)
{
INTERRUPT_CHEACK();
}
void sent()
{
if(nrf_state!=0)sent_actived=1,nrf_state=0; // faal sazi ersal mojadad
if(sent_actived)
{
sent_actived=0;
if((temp_address[4]==base_address[4]) && (temp_address[3]==base_address[3]) && (temp_address[2]==base_address[2]) && (temp_address[1]==base_address[1]) && (temp_address[0]==base_address[0]))
{
nrf_inst(0xE1);
nrf_inst(0xA0);
delay_ms(1);
CE=1;
delay_us(20);
CE=0;
delay_ms(1);
}
else
nrf_state=5;
} // END if(sent_actived)
}
void NRF_INIT(char mode){
IRQ=1;
CSN=1;
DD_CSN=1;
CLK=0;
DD_CLK=1;
CE=0;
DD_CE=1;
DD_MOSI=1;
DD_MISO=0;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x02;
GIFR=0x40;
#asm("sei"
SPCR=0x50;
delay_ms(100); //110
nrf_mode=mode;
command_buff=0x01;
nrf_inst(0x21);
command_buff=0x01;
nrf_inst(0x22);
command_buff=0x03;
nrf_inst(0x23);
command_buff=0x2f;
nrf_inst(0x24);
command_buff=0x01;
nrf_inst(0x25);
command_buff=0x26;
nrf_inst(0x26);
command_buff=0x06;
nrf_inst(0x16);
nrf_inst(0x2A);
nrf_inst(0x30);
command_buff=0x01;
nrf_inst(0x3C);
command_buff=0x07;
nrf_inst(0x3D);
if(mode==0)
{
command_buff=0x4E;
nrf_inst(0x20);
delay_ms(100);
sent_actived=1;
}
else
{
command_buff=0x3F;
nrf_inst(0x20);
delay_ms(5);
CE=1;
}
}
void nrf_config(char mode)
{
nrf_mode=mode;
command_buff=0x01;
nrf_inst(0x21);
command_buff=0x01;
nrf_inst(0x22);
command_buff=0x03;
nrf_inst(0x23);
command_buff=0x2f;
nrf_inst(0x24);
command_buff=0x01;
nrf_inst(0x25);
command_buff=0x26;
nrf_inst(0x26);
command_buff=0x06;
nrf_inst(0x16);
nrf_inst(0x2A);
nrf_inst(0x30);
command_buff=0x01;
nrf_inst(0x3C);
command_buff=0x07;
nrf_inst(0x3D);
if(mode==0)
{
command_buff=0x4E;
nrf_inst(0x20);
delay_ms(10);
sent_actived=1;
}
else
{
command_buff=0x3F;
nrf_inst(0x20);
delay_ms(5);
CE=1;
}
}
#endif
void NRF_SEND(char NUMBER,const unsigned char *CODE){
unsigned char i;
nrf_config(TX);
payload[0]=NUMBER;
if(nrf_state!=0)sent_actived=1,nrf_state=0; // faal sazi ersal mojadad
for(i=1 ; i<NUMBER ; i++)payload[i]=CODE[i-1];
sent();
nrf_config(RX);
}
[/code]
برای میکرو اول هم که به usb متصل میشه توی گوگل usbpio سرچ کنید درایور و فایل هگز اون هست :mrgreen:
کد برنامه نتیجه این شد :mrgreen: کسی خواست دانلود کنه بسازه . برای مبدل USB به سریال از یک میکرو MEGA8 استفاده کردم
و برای درایو NRF هم از یک مگا 8 دیگه . با این ماژول هم میتونید بفرستین و هم بگیرین فقط دیتا هایی که توی محیط ترمینال مینویسید داخل پرانتز باشه
مثلا (1234)
قسمت usb اون هم روی برد در آوردم که حسابی ارزون بشه . فقط پشت قسمت فیش usb 4 تا ترک هست که روی اون سیم لحیم بکشید تا کلفت بشه لغ نخوره
http://upload7.ir/u/preview/s1/dec7d...SzxcgBhjzmgOuq


[code=c]
#include <mega8.h>
//#define xtal 8000000
#include <delay.h>
#include <stdio.h>
#include "nrf24l01p.h"
#define LEDR_ON PORTC.3=1
#define LEDR_OFF PORTC.3=0
#define LEDT_ON PORTB.7=1
#define LEDT_OFF PORTB.7=0
bit look=1;
char canter=0;
bit Trans=0;
unsigned char DATA_USART[30];
interrupt [USART_RXC] void usart_rx_isr(void)
{
char data;
data=UDR;
if(data=='(' && look==1 )look=0,canter=0,LEDR_ON;
if(look==0) DATA_USART[canter]=data,canter++;
if(data=='

//LEDR_OFF;
}
void main()
{
unsigned char Payload[],i;
DDRC=0b00000000;
PORTD=0b00001100;
DDRB.7=1;
delay_ms(1000);
LEDT_ON;
delay_ms(100);
LEDT_OFF;
delay_ms(100);
LEDR_ON;
delay_ms(100);
LEDR_OFF;
PORTD.0=0;
DDRD.0=0;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(1<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00;
UBRRL=0x32;
delay_ms(1000);
#asm("sei"

nrf_config(RX);
nrf_state=0;
// while(1) NRF_SEND(30,"welcom"

while(1){
if(Trans==1){
Trans=0;
NRF_SEND(canter,DATA_USART);
LEDR_OFF;
}
//+++++++++++++++++++RECEVE+++++++++++++++++++++++++ ++++++
if(nrf_state==1){
LEDT_ON;
LEDR_ON;
//for(i=1 ; i<=payload[0] ; i++)putchar(payload[i]),delay_us(10);
printf("%s",payload);
LEDT_OFF;
LEDR_OFF;
for(i=0 ; i<=33 ; i++)payload[i]=0;
nrf_state=0;
}
}
} // end main
[/code]
[code=c]
/* Name: nrf24l01p.h
// External Interrupt(s) initialization
// INTx: On
// INTx Mode: Falling Edge
// INTERRUPT_CHEACK();
//nrf_config(char mode) RX OR TX
*/
#include <delay.h>
#define LED PORTC.5
#ifndef __nrf24l01p_h_included__
#define __nrf24l01p_h_included__
#define IRQ PORTD.2
#define DD_IRQ DDRD.2
#define CE PORTC.0
#define DD_CE DDRC.0
#define CSN PORTB.2
#define DD_CSN DDRB.2
#define MOSI PORTB.3
#define DD_MOSI DDRB.3
#define MISO PORTB.4
#define DD_MISO DDRB.4
#define CLK PORTB.5
#define DD_CLK DDRB.5
#define TX 0
#define RX 1
#define NRF_STANDBAY 0 // NRF BI KAR AST
#define NRF_RECEVE 1 // DATA DARYAFT SHODE
#define NRF_T_OK 2 // DATA BE DOROSTI SEND SHODE
#define NRF_T_R 3 // DATA BE DOROSTI SEND SHODE VA CHIZI DARYAFT SHODE
#define NRF_T_ERR 4 // DATA BE DOROSTI ERSAL NASHODE
#define db 11
flash unsigned char base_address[5]={0x00,0x01,0x03,0x07,0x00};
unsigned char temp_address[5]={0x00,0x01,0x03,0x07,0x00};
unsigned char payload[33];
unsigned char command_buff=0,status_buff=0,nrf_state=0;
char nrf_mode;
bit sent_actived=0;
unsigned char spi(unsigned char data)
{
SPDR=data;
while(!(SPSR & 0x80));
data=SPSR;
return SPDR;
}
void nrf_inst(unsigned char ins)
{
int i;
CSN=0;
status_buff=spi(ins);
switch(ins & 0xE0)
{
case 0x00:
{
if((ins & 0x1F)==0x0A || (ins & 0x1F)==0x0B || (ins & 0x1F)==0x10)
{
for(i=4;i>=0;i--)
{
temp_address[i]=spi(0xFF);
}
}
else
{
command_buff=spi(0xFF);
}
break;
}
case 0x20:
{
if((ins & 0x1F)==0x0A || (ins & 0x1F)==0x0B || (ins & 0x1F)==0x10)
{
for(i=4;i>=0;i--)
{
spi(base_address[i]);
}
}
else
{
spi(command_buff);
}
break;
}
case 0x60:
{
if((ins & 0x01)==1)
{
i=payload[0];
while(i!=0)
{
payload[i]=spi(0xFF);
i--;
}
}
else
{
command_buff=spi(0xFF);
}
break;
}
case 0xA0:
{
i=payload[0];
while(i!=0)
{
spi(payload[i]);
i--;
}
break;
}
}
CSN=1;
delay_us(10);
}
void INTERRUPT_CHEACK(){
if(nrf_mode==0)
{
nrf_inst(0xFF);
if(status_buff & 0x20)
{
nrf_state=2;
nrf_inst(0x17);
if((command_buff & 0x01)==0)
{
nrf_inst(0x60);
if(command_buff<=32)
{
payload[0]=command_buff;
nrf_inst(0x61);
nrf_state=3;
}
else
nrf_inst(0xE2);
}
}
else
{
nrf_state=4;
}
}
else
{
nrf_inst(0x60);
if(command_buff>32)
{
nrf_inst(0xE2);
}
else
{
payload[0]=command_buff;
nrf_inst(0x61);
nrf_state=1;
}
}
command_buff=0x7E;
nrf_inst(0x27);
nrf_inst(0xE1);
}
interrupt [EXT_INT0] void ext_int0_isr(void)
{
INTERRUPT_CHEACK();
}
void sent()
{
if(nrf_state!=0)sent_actived=1,nrf_state=0; // faal sazi ersal mojadad
if(sent_actived)
{
sent_actived=0;
if((temp_address[4]==base_address[4]) && (temp_address[3]==base_address[3]) && (temp_address[2]==base_address[2]) && (temp_address[1]==base_address[1]) && (temp_address[0]==base_address[0]))
{
nrf_inst(0xE1);
nrf_inst(0xA0);
delay_ms(1);
CE=1;
delay_us(20);
CE=0;
delay_ms(1);
}
else
nrf_state=5;
} // END if(sent_actived)
}
void NRF_INIT(char mode){
IRQ=1;
CSN=1;
DD_CSN=1;
CLK=0;
DD_CLK=1;
CE=0;
DD_CE=1;
DD_MOSI=1;
DD_MISO=0;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x02;
GIFR=0x40;
#asm("sei"

SPCR=0x50;
delay_ms(100); //110
nrf_mode=mode;
command_buff=0x01;
nrf_inst(0x21);
command_buff=0x01;
nrf_inst(0x22);
command_buff=0x03;
nrf_inst(0x23);
command_buff=0x2f;
nrf_inst(0x24);
command_buff=0x01;
nrf_inst(0x25);
command_buff=0x26;
nrf_inst(0x26);
command_buff=0x06;
nrf_inst(0x16);
nrf_inst(0x2A);
nrf_inst(0x30);
command_buff=0x01;
nrf_inst(0x3C);
command_buff=0x07;
nrf_inst(0x3D);
if(mode==0)
{
command_buff=0x4E;
nrf_inst(0x20);
delay_ms(100);
sent_actived=1;
}
else
{
command_buff=0x3F;
nrf_inst(0x20);
delay_ms(5);
CE=1;
}
}
void nrf_config(char mode)
{
nrf_mode=mode;
command_buff=0x01;
nrf_inst(0x21);
command_buff=0x01;
nrf_inst(0x22);
command_buff=0x03;
nrf_inst(0x23);
command_buff=0x2f;
nrf_inst(0x24);
command_buff=0x01;
nrf_inst(0x25);
command_buff=0x26;
nrf_inst(0x26);
command_buff=0x06;
nrf_inst(0x16);
nrf_inst(0x2A);
nrf_inst(0x30);
command_buff=0x01;
nrf_inst(0x3C);
command_buff=0x07;
nrf_inst(0x3D);
if(mode==0)
{
command_buff=0x4E;
nrf_inst(0x20);
delay_ms(10);
sent_actived=1;
}
else
{
command_buff=0x3F;
nrf_inst(0x20);
delay_ms(5);
CE=1;
}
}
#endif
void NRF_SEND(char NUMBER,const unsigned char *CODE){
unsigned char i;
nrf_config(TX);
payload[0]=NUMBER;
if(nrf_state!=0)sent_actived=1,nrf_state=0; // faal sazi ersal mojadad
for(i=1 ; i<NUMBER ; i++)payload[i]=CODE[i-1];
sent();
nrf_config(RX);
}
[/code]
برای میکرو اول هم که به usb متصل میشه توی گوگل usbpio سرچ کنید درایور و فایل هگز اون هست :mrgreen: