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

📄 main.c

📁 C8051F320+Nrf24L01无线模块的实现
💻 C
字号:
#include "c8051f320.h"
#include "typedef.h"
#include "misc.h"
#include "io.h"
//#include "reg51.H"
#include "api.h"
#define uchar unsigned char
#define uint unsigned int
 void init_io();		//声明外部函数
 void RX_Mode();
 void init_int0();
 void CheckButtons();
 uchar SPI_RW_REG(BYTE reg,BYTE value);
 //extern  uchar SPI_RW_REG123(BYTE reg,BYTE value);
uchar flag;
#define uchar unsigned char
#define TX_ADR_WIDTH    5   // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH  20  // 20 bytes TX payload

uchar const TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
/*uchar Original_ADDRESS[TX_ADR_WIDTH] ={0x32,0x43,0x10,0x10,0x01};	 //Define Original address 
uchar OnePress_ADDRESS[TX_ADR_WIDTH] ={0x33,0x43,0x10,0x10,0x01};
/*BYTE const TX_PAYLOAD[TX_PLOAD_WIDTH] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
                                         0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; */
uchar rx_buf[TX_PLOAD_WIDTH];
uchar tx_buf[TX_PLOAD_WIDTH];


sbit key=P3^0;
sbit led=P2^4;
/**************************************************/
sbit CE =  P0^4;
sbit CSN=  P0^3;
sbit SCK=  P0^0;
sbit MOSI= P0^2;
sbit MISO= P0^1;
sbit IRQ = P0^5;
/**************************************************/
uchar 	bdata sta;
sbit	RX_DR	=sta^6;
sbit	TX_DS	=sta^5;
sbit	MAX_RT	=sta^4;
uchar tf=0; // Send interrut enabled flag
//	uchar t;   //record the number of times you press the button
	uchar i;   //recond the number of times led shake
/*void main(void)	// 主程序
{
	bit light;	 //位变量
	uint n;
	PCA0MD &=  0xb9;
	while(1) //程序循环
	{
		if(!K1)	 //如果读到K1为0
		{
			for(n=0;n<1000;n++); //等待按键稳定
			light=~light;  //每按键一次,标记变化一次
			while(!K1);	//等待按键松开
			for(n=0;n<1000;n++);  //等待按键稳定松开
		}
		P0=light; //直接把位变量赋值给LED灯
	}
}	 

*/	

void delays(uchar n)
{
 	for(;n>0;n--)
		_nop_();
}
void init_io(void)
{
	delays(100);
	CE=0;			// chip enable
	CSN=1;			// Spi disable	
	SCK=0;		// Spi clock line init high

}	
void init_int0()
{
	EA=1;
	EX0=1;						// Enable int0 interrupt.
}  	
/*The following program is achieve by HardWare SPI*/
void SPI_rf_write(uchar dat)
{
	while(SPI0CFG&0x80);
	SPIF = 0;
	SPI0DAT = dat;
	while(!SPIF);	//wait spi transfer finish
}
uchar SPI_rf_read(void)
{
	while(SPI0CFG&0x80);
	SPIF = 0;
	SPI0DAT = 0x00;
	while(!SPIF);	//wait spi transfer finish
	return (SPI0DAT);
}
void write_rf_reg(uchar address,uchar dat)
{
	CSN=0;
	SPI_rf_write(WRITE_REG + address);
	SPI_rf_write(dat);
	CSN=1;
}
uchar read_rf_reg(uchar address)
{	
	uchar status;
	CSN=0;
	SPI_rf_write(address);
	status = SPI_rf_read();
	CSN=1;
	return (status);
}
void write_rf_data(uchar reg,uchar *data_ptr,uchar nLength)
{
	uchar i;
	CSN=0;
	SPI_rf_write(reg);
	for(i=0;i<nLength;i++)
		SPI_rf_write(*data_ptr++);
	CSN=1;
}				 /*
uchar read_rf_data(uchar reg,uchar *data_ptr,uchar nLength)
{
	uchar i,status;
	CSN=0;
	SPI_rf_write(reg);
	for(i=0;i<nLength;i++)
	data_ptr[nLength]=SPI_rf_read();
	status=SPI_rf_read();
	CSN=1;
	return(status);
}

		   /*
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
	uchar status,byte_ctr;

  	CSN = 0;                    		// Set CSN low, init SPI tranaction
  	status = SPI_RW(reg);       		// Select register to write to and read status byte

  	for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
    	pBuf[byte_ctr] = SPI_RW(0);    // Perform SPI_RW to read byte from nRF24L01

  	CSN = 1;                           // Set CSN high again

  	return(status);                    // return nRF24L01 status byte
}  			 */	  
void init_24L01()
{	 
	CE=0;
	
 	write_rf_data(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);		
 	write_rf_data(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
	
	write_rf_reg(EN_AA,0x01);
	write_rf_reg(EN_RXADDR,0x01);
	write_rf_reg(SETUP_RETR,0x1a);
	write_rf_reg(RF_CH,40);
	write_rf_reg(RF_SETUP,0x0f);
	write_rf_reg(CONFIG,0x0f);

	CE=1;
	delays(130);
//	flush_rf_tx();
//	flush_rf_rx();
}
void Set_TX()
{
	CE=0;

	write_rf_data(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
	write_rf_data(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
	write_rf_reg(CONFIG,0x0e);

	CE=1;
	delays(130);		
}					
void Set_RX()
{
	CE=0;

	write_rf_reg(CONFIG,0x0f);
	write_rf_reg(RX_PW_P0,TX_PLOAD_WIDTH);
	
	CE=1;
	delays(130);
}	   
/*The foregoing program is achieve by HardWare SPI*/
	/*		 
uchar SPI_RW(uint  byte)
{
	uchar bit_ctr;
   	for(bit_ctr=0;bit_ctr<8;bit_ctr++)   // output 8-bit
   	{
   		MOSI = (byte & 0x80);         // output 'byte', MSB to MOSI
   		byte = (byte << 1);           // shift next bit into MSB..
   		SCK = 1;                      // Set SCK high..
   		byte |= MISO;       		  // capture current MISO bit
   		SCK = 0;            		  // ..then set SCK low again
   	}				 
    return(byte);           		  // return read byte
}  
uchar SPI_RW_Reg(BYTE reg, BYTE value)
{
	uchar status;
 
  	CSN = 0;                   // CSN low, init SPI transaction
  	status = SPI_RW(reg);      // select register
  	SPI_RW(value);             // ..and write value to it..
  	CSN = 1;                   // CSN high again

  	return(status);            // return nRF24L01 status byte
}	 
BYTE SPI_Read(BYTE reg)
{
	BYTE reg_val;

  	CSN = 0;                // CSN low, initialize SPI communication...
  	SPI_RW(reg);            // Select register to read from..
  	reg_val = SPI_RW(0);    // ..then read registervalue
  	CSN = 1;                // CSN high, terminate SPI communication

  	return(reg_val);        // return register value
}


  
uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
	uchar status,byte_ctr;

  	CSN = 0;                    		// Set CSN low, init SPI tranaction
  	status = SPI_RW(reg);       		// Select register to write to and read status byte

  	for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
    	pBuf[byte_ctr] = SPI_RW(0);    // Perform SPI_RW to read byte from nRF24L01

  	CSN = 1;                           // Set CSN high again

  	return(status);                    // return nRF24L01 status byte
}  		
uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
	uchar status,byte_ctr;

  	CSN = 0;                   // Set CSN low, init SPI tranaction
  	status = SPI_RW(reg);    // Select register to write to and read status byte
  	for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
    	SPI_RW(*pBuf++);
  	CSN = 1;                 // Set CSN high again
  	return(status);          // return nRF24L01 status byte
} 
void Set_RX()
{
	CE=0;
	SPI_RW_Reg(WRITE_REG + CONFIG,0x0f);
	SPI_RW_Reg(WRITE_REG + RX_PW_P0,TX_PLOAD_WIDTH);
	CE=1;
	delays(130);
}  
void Set_TX()
{
	CE=0;
	
	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
	SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);

	SPI_RW_Reg(WRITE_REG + CONFIG,0x0e);
	
	CE=1;
	delays(130);
}
void init_24L01()
{	CE=0;

  	SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
  	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS, TX_ADR_WIDTH);

	SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  	SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
  	SPI_RW_Reg(WRITE_REG + RF_CH,100);        // Select RF channel 40
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
	CE=1;
}	  
/*void RX_Mode(void)
{
	CE=0;
  	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device

  	SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
  	SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 	SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..

  	CE = 1; // Set CE pin high to enable RX device
	delay(130);
  //  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.

}	
void TX_Mode(void)
{
	CE=0;
	 // RX_Addr0 same as TX_Adr for Auto.Ack
  	SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload

  	SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
  	SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 
  	SPI_RW_Reg(WRITE_REG + RF_CH,40);        // Select RF channel 40
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR		  
	SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
  	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
	CE=1;		 
	delays(130);

*/
 
/*Got a baby mistake,false key set if key equal 1 enter to function -_-! */
void led_shake()
{
	 led=0;
     delays(500);
	 led=1;
	 delays(500);
}			  //实现按键循环,不实现连续按键组合
/*uchar judge_BT(uchar t)
{	
     uchar *a;
	 uchar *b;
	 uchar *c;
	 
	 a=Original_ADDRESS;
   	 b=OnePress_ADDRESS;
	 c=TX_ADDRESS;
	 if(t)
	 {
		*a=*b;
		tf=1;
	 }
	 if(t==2)
	 {
	 	t=0;
	    *a=*c;
		tf=1;
	 }
		if(tf)
		{
			tf=0;
			Set_TX();
		}
		return (t);
}	 	*/
void CheckButtons()
{
	if(key==0)
	{
		delays(100);
		if(key==0)
		{
			led_shake();
			tf=1;
		}
	}
	if(tf)
	{
		tf=0;
		Set_TX();
	}
/*	uchar t;
	 uchar *a;
	 uchar *b;
	 uchar *c;
	 
	 a=Original_ADDRESS;
   	 b=OnePress_ADDRESS;
	 c=TX_ADDRESS;   
	if(key==0)
	{
		delays(100);
		if(key==0)
		{
		   led_shake();
		   t++;
		}		 
	}
	 if(t==1)
	 {
		*a=*b;
		tf=1;
	 }
	 if(t==2)
	 {
	 	t=0;
	    *a=*c;
		tf=1;
	 }	*/
		if(tf)
		{
			tf=0;
			Set_TX();
		}
	//judge_BT(t);
	 
} 	  		 
	
/*Init HardWare SPI*/	   
void Init_SPI(void)
{
	SPI0CFG = 0x40;
	SPI0CN  = 0x01;	 //(This Register can control SCK Pin High Or Low??The function of the pin is Enable/disable SPI..why    add by lsy)
      
	SPI0CKR = 0x01;
} 
void init_mcu(void)
{
//	unsigned int i;	
	P0MDIN  = 0xff;		// 01100010
	P0MDOUT	= 0x9d;                     //0:open-drain, 1:push pull    (important Setting,If The port ain't the open-drain Mode,It won't be receive any data from register)add by lsy
   	P1MDIN 	= 0xff;                     // P1 are digital
   	P1MDOUT = 0xff;                     //0:open-drain, 1:push pull 
   	P2MDIN  = 0xff; 
   	P2MDOUT	= 0xff;					// P2.2 and P2.3 set to push-pull
   	P3MDIN	= 0xff;
	P3MDOUT = 0x00;
//	P0SKIP=0x3f;      (This is a question,Wait for answer    add by lsy)
   	P0 = 0xff;
  	P1 = 0xff;
   	P2 = 0xff;
   	P3 = 0xff;
	IT01CF = 0x65;
	XBR0 = 0x02;						    
	XBR1 = 0x40;                         // Enable Crossbar
	TMOD = 0x11;	//T0,T1 as 16bits Timer mode
	PCON = 0;
	CKCON = 0x76;	  //Clock Control Register
	TMR2CN = 0x00;	  //Timer2 Control Register
	TMR3CN = 0xc0;	  //Timer3 .......
	TMR3RLL = 0x00;	  //Timer3 Reload Register Low Byte
	TMR3RLH = 0x00;	  //.......................High..
	TMR2RLL = 0;
	TMR2RLH = 0;
	TR0 = 1;	  //Enable Mcu Timer
	TR1 = 1;   	
	TR2 = 1;
	ET2 = 1;	  //Timer interrupt Enable flag
	EX0 = 1;
	EX1 = 1;
	IT0 = 1;      //Extern interrput Enable flag
	IT1 = 1;
	PX0 = 1; 
}	  
/*To check the register value if the register value is ture,make sure the communication pipe */
void CheckRS()
{
	uchar reg_val;
	reg_val=read_rf_reg(EN_AA);
	reg_val=read_rf_reg(EN_RXADDR);
	reg_val=read_rf_reg(RF_CH);
	reg_val=read_rf_reg(RF_SETUP);
	reg_val=read_rf_reg(SETUP_RETR);
	reg_val=read_rf_reg(CONFIG);
	reg_val=read_rf_reg(RX_PW_P0);
}
 
/*经过测试,CE,CSN,IRQ三个引脚都可以成功的翻转,输出方波,MOSI输出锯齿波,SCK和MISO则无反应*/
void main (void)
{
	uchar d; 
	u16 i = 0;

   	PCA0MD &= ~0x40;	// Disable Watchdog timer
    VDM0CN 	= 0x80;      // Enable VDD Monitor
    delay(100);		// wait for VDD Monitor stable
 //   RSTSRC |= 0x02;
  RSTSRC = 0x06;
	
	SYSCLK_Init();		//init System Clock			//init mcu -_-!
	init_mcu();								 
	init_io();
	Init_SPI();
	init_int0();	
	init_24L01();	
	CheckRS();
//	Set_RX();
	while(1)
	 {
	 		for(d=0;d<0xff;d++)
				{
					tx_buf[1]='d';
				}
		Set_TX();
		led=0;
		delays(100);
		led=1;
		delays(100);
/*		if(flag)
		{
			led=0;
			delays(100);
			led=1;
			delays(100);
		}	*/
	 	//CheckButtons();
	 }
}
void ISR_int0(void) interrupt 0
{
	sta=read_rf_reg(STATUS);	// read register STATUS's value
	if(RX_DR)				// if receive data ready (RX_DR) interrupt
	{
		CE=0;
	   	SPI_rf_write(TX_PLOAD_WIDTH);
	    SPI_rf_read();
		CE=1;									
		flag=1;
	}
	if(MAX_RT) 
	{
		write_rf_reg(FLUSH_TX,0);
	}
	
	write_rf_reg(STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
}	  		

⌨️ 快捷键说明

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