nRF24L01 無線模組 2.4G 24L01+升級版
發表於 : 2012-09-22 22:04:08
CodeCharge Studio, DemoCharge, SuperPDF
https://bug.551.com.tw/
代碼: 選擇全部
void setup()
{
SPI_DIR = ( CE + SCK + CSN + MOSI);
SPI_DIR &=~ ( IRQ + MISO);
// attachInterrupt(1, _ISR, LOW);// interrupt enable
Serial.begin(9600);
init_io(); // Initialize IO port
unsigned char status=SPI_Read(STATUS);
Serial.print("status = ");
Serial.println(status,HEX); // read the mode’s status register, the default value should be ‘E’
Serial.println("*******************TX_Mode Start****************************");
TX_Mode(); // set TX mode
}
void loop()
{
int k = 0;
for(;;)
{
for(int i=0; i<32; i++)
tx_buf[i] = k++;
unsigned char status = SPI_Read(STATUS); // read register STATUS's value
if(status&TX_DS) // if receive data ready (TX_DS) interrupt
{
SPI_RW_Reg(FLUSH_TX,0);
SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH); // write playload to TX_FIFO
}
if(status&MAX_RT) // this is retransmit than SETUP_RETR
{
SPI_RW_Reg(FLUSH_TX,0);
SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH); // disable standy-mode
}
SPI_RW_Reg(WRITE_REG+STATUS,status); // clear RX_DR or TX_DS or MAX_RT interrupt flag
delay(1000);
}
}
代碼: 選擇全部
//MISO -> D12
// * MOSI ->D11
// * SCK ->D13
//* CE ->D8
//* CSN ->D7
#include "SPI.h" //24L01库文件
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"
int aaa;
int aaaa;
int bbb;
int bbbb;
void setup(){
Mirf.spi = &MirfHardwareSpi; //加载24L01 SPI
Mirf.init(); //开始
Mirf.setTADDR((byte *)"serv1");//发送到"接收地址"
Mirf.payload = sizeof(int); //数据类型(整数)
Mirf.config(); //发送通道??
}
void loop(){
aaa=analogRead(A4)+1; //读取 X 向电平PWM(1至1024)
bbb= -analogRead(A5)-1; //读取 Y向电平PWM(-1至-1024)
if (aaa !=aaaa) //如果 X 向电平与之前电平不一样则执行
{
Mirf.send((byte *)&aaa); //发送X向电平
aaaa=aaa; //保存X 向当前电平
while(Mirf.isSending()){ //等待或继续发送??
}
}
if (bbb !=bbbb) //如果 Y 向电平与之前电平不一样则执行
{
Mirf.send((byte *)&bbb); //发送Y向电平
bbbb=bbb; //保存Y 向当前电平
while(Mirf.isSending()){ //等待或继续发送?
}
}
delay(10); // 等待0.01秒
}
代碼: 選擇全部
//MISO -> D12
// * MOSI ->D11
// * SCK ->D13
//* CE ->D8
//* CSN ->D7
//LCD 1602 IIC SDA---A4 328
//LCD 1602 IIC SCL---A5 328
#include "Wire.h" //LCD1602 IIC 库文件
#include ;
LiquidCrystal_I2C lcd(0x27,16,2);
#include "SPI.h" //24L01库文件
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"
int data;
int aaa;
int bbb;
void setup(){
lcd.init(); //加载LCD1602 IIC
lcd.backlight();
//lcd.setBacklight(120); //设置LCD背光亮度,好象没作用
delay(20);
Mirf.spi = &MirfHardwareSpi; //加载24L01 SPI
Mirf.init();
Mirf.setRADDR((byte *)"serv1"); //接收地址" "
Mirf.payload = sizeof(int); //数据类型(整数)
Mirf.config();
}
void loop(){
if(Mirf.dataReady()){ //如果接收到数据则执行
Mirf.getData((byte *)&data); //接收数据
if (data>=1) //如果大于或等于1为X向 (如果设为 >=0就会 X,Y通道冲突,所以设为1开始)
{
aaa=data-1;
}
if (data<=-1) //如果小于或等于-1为Y向(............)
{
bbb=data+1;
}
Mirf.rxFifoEmpty(); //清理24L01援存??
}
lcd.clear(); //清屏
lcd.setCursor(0, 0);
lcd.print(aaa); //显示X向 1至1024
lcd.setCursor(6, 0);
lcd.print(0-bbb); //显示Y向,并将负数变为正数 1至1024
delay(10);
}