⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slave._c

📁 这是用C编的nrf905的通讯程序
💻 _C
字号:
//SLAVE

//最关键的是SPI的初始化,MSB首位和极性方式00,极性方式错误,数据则不正确


#include "config.h"
#include "nrf905.h"

#define MASTER  //编译控制是主机还是从机


#define T0N 25
void DelayUs(uint);
void DelayMs(uchar);
//32字节数据包发送时间 
//=650us_StartUp+200us_Preamble+(4+32+2)Byts*8Bit/50000bps*1000000us=6.6ms
#define RFTN 25  //发送测试间隔 10*20ms

bit bTimer,bRfSend;
uchar T0Cnt,RfCnt;

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0xff;
 PORTB = BIT(AM)|BIT(CD)|BIT(DR)|BIT(MISO);
 DDRB  = BIT(CSN)|BIT(MOSI)|BIT(SCK);
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = BIT(uPCLK);
 DDRD  = BIT(TRXCE)|BIT(TX_EN)|BIT(PWR)|BIT(LED)|0x07;
}

//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 20mSec
// actual value: 19.861mSec (0.7%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0x71; //set count
 OCR0  = 0x8F;  //set compare
 TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 TCNT0 = 0x29; //reload counter value
 
 if (--T0Cnt==0)
   {T0Cnt=T0N;
    bTimer=1;
    }
if (--RfCnt==0)
   {RfCnt=RFTN;
    bRfSend=1;
    }
}



//SPI initialize
void spi_init(void)
{uchar temp;

 SPCR = 0x51; //不使用SPI中断,SPI允许,主机模式,MSB,极性方式00,1/16系统时钟速率
 SPSR = 0x00; //setup SPI
 
 temp = SPSR; //!!!
 temp = SPDR; //清空SPI,和中断标志,使SPI空闲    
}




//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer0_init();
 spi_init();
 init_lcd();
 DISLCD();

 MCUCR = BIT(ISC01); //下降沿触发
 GICR  = 0x00;
 TIMSK = 0x05; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}




//接收处理
void RfRecvProc()
{uchar i;

   if ((PINB&(1<<AM))==0) return;//一般先AM=1指示地址匹配对
    
   if ((PINB&(1<<DR))==0) return;//DR=1时表示数据接收对而且Crc正确
   
   
   //已经接收到数据
   nrf905_ReadData();//读出...
  
 
   //数据接收正确...灯指示
   CPL(PORTD,LED);
   for (i=0;i<SIZE;i++){seg(RxBuf[i]);}
   LCD(RxBuf[0]);
   
   
   //从机回送数据包,这样双方都能指示是否收到有效数据包
   #ifndef MASTER
     //RfSendProc();
   #endif
}

//发送测试
void RfSendProc()
{uchar i;
   for (i=0;i<SIZE;i++) TxBuf[i]=2*i;
   nrf905_SendData();//发送测试数据
   nrf905_RxOn();//重新回到接收状态
}

void TimerFunc()
{
   bTimer=0;
   //WDR();//clear WDT
   
   CPL(PORTD,LED);
   //SPDR=66;
}

void SystemIni()
{
  T0Cnt=T0N;
  RfCnt=RFTN;
}

void DelayMs(uchar ms)
{char i;
for (i=0;i<ms;i++)
  {DelayUs(1000);
  }
  return;
}

void DelayUs(uint us)
{uint i;
  for (i=0;i<us;i++)
  {NOP();NOP();NOP();NOP();NOP();NOP();
  }
}

void main()
{
   init_devices();
   SystemIni();
   nrf905_Init();
   ENLCD();
   PutChar('W');
   PutChar('e');
   PutChar('l');
   PutChar('c');
   PutChar('o');
   PutChar('m');
   PutChar('e');
   PutChar(' ');
   PutChar('t');
   PutChar('o');
   PutChar(' ');
   PutChar('X');
   PutChar('G');
   PutChar('C');
   DelayMs(1000);
   DelayMs(1000);
   DelayMs(1000);
   DelayMs(1000);

 
   
   clear_lcd();


   DISLCD();

   while (1)
   {  
     // if (bTimer) TimerFunc();
	  #ifdef MASTER
	    if (bRfSend)
		{ bRfSend=0;
		  RfSendProc();//发送测试
		}
	  #endif
	  
	  RfRecvProc();//接收处理
   }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -