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

📄 main_ezlink.c

📁 Si4432-T-B1版本资料.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
			{
				SpiWriteRegister(0x7F,tx_packet[96+temp8]);	
			}

			//Disable all other interrupts and enable the packet sent interrupt only.
			//This will be used for indicating the successfull packet transmission for the MCU
			SpiWriteRegister(0x05, 0x04);														//write 0x04 to the Interrupt Enable 1 register	
			//Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
			ItStatus1 = SpiReadRegister(0x03);												//read the Interrupt Status1 register
			ItStatus2 = SpiReadRegister(0x04);												//read the Interrupt Status2 register

			/*wait for the packet sent interrupt*/
			//The MCU just needs to wait for the 'ipksent' interrupt.
			while(NIRQ == 1);
			//Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
			ItStatus1 = SpiReadRegister(0x03);												//read the Interrupt Status1 register
			ItStatus2 = SpiReadRegister(0x04);												//read the Interrupt Status2 register

			//wait a bit for showing the LED a bit longer
			for(delay = 0; delay < 10000;delay++);
			//turn off the LED
			TX_LED = 0; 
			
			//after packet transmission set the interrupt enable bits according receiving mode
			//Enable three interrupts: 
			// a) one which shows that a valid packet received: 'ipkval'
			// b) second shows if the packet received with incorrect CRC: 'icrcerror' 
			// c) third shows if the RX fifo almost full: 'irxffafull
			SpiWriteRegister(0x05, 0x13); 													//write 0x13 to the Interrupt Enable 1 register
			SpiWriteRegister(0x06, 0x00); 													//write 0x00 to the Interrupt Enable 2 register
			//Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
			ItStatus1 = SpiReadRegister(0x03);												//read the Interrupt Status1 register
			ItStatus2 = SpiReadRegister(0x04);												//read the Interrupt Status2 register

			/*enable receiver chain again*/
			SpiWriteRegister(0x07, 0x05);														//write 0x05 to the Operating Function Control 1 register				
		}

		
	    //wait for the interrupt event
		//If it occurs, then it means a packet received or CRC error happened
		if( NIRQ == 0 )
		{
		
			//read interrupt status registers 
			ItStatus1 = SpiReadRegister(0x03);												//read the Interrupt Status1 register
			ItStatus2 = SpiReadRegister(0x04);												//read the Interrupt Status2 register

			/*CRC Error interrupt occured*/
			if( (ItStatus1 & 0x01) == 0x01 )
			{
				//reset the RX FIFO
       	   SpiWriteRegister(0x08, 0x02);													//write 0x02 to the Operating Function Control 2 register
        	   SpiWriteRegister(0x08, 0x00);													//write 0x00 to the Operating Function Control 2 register
				//blink all LEDs to show the error
				TX_LED = 1;
				RX_LED = 1;
				for(delay = 0; delay < 10000;delay++);
				TX_LED = 0;
				RX_LED = 0;
			}
			
			/*RX FIFO almost full interrupt occured*/
			if( (ItStatus1 & 0x10) == 0x10 )
			{
           	//read 32bytes from the FIFO
				for(temp8=pointer;temp8<pointer+32;temp8++)				
				{
					rx_packet[temp8] = SpiReadRegister(0x7F);
				}
				//update receive buffer pointer
				pointer = pointer + 32;
			}


			/*packet received interrupt occured*/
			if( (ItStatus1 & 0x02) == 0x02 )
			{
				//disable the receiver chain 
				SpiWriteRegister(0x07, 0x01);													//write 0x01 to the Operating Function Control 1 register

 				//Read the length of the received payload
				length = SpiReadRegister(0x4B);												//read the Received Packet Length register
				
				//get the remaining 32bytes from the RX FIFO    
				for(temp8=pointer;temp8<pointer+32;temp8++)			
				{
					rx_packet[temp8] = SpiReadRegister(0x7F);
    			}
	
				//clear receive buffer pointer
				pointer = 0;

				
				//check whether the content of the packet is valid
				if(( length == 128 ) && ( rx_packet[127] == 127 )) 
				{
               //turn on the LED
    				RX_LED = 1;
    				//wait a bit for showing the LED a bit longer
					for(delay = 0; delay < 10000;delay++);
					//turn off the LED
					RX_LED = 0; 
				}
				//reset the RX FIFO
       	   SpiWriteRegister(0x08, 0x02);												//write 0x02 to the Operating Function Control 2 register
        	   SpiWriteRegister(0x08, 0x00);												//write 0x00 to the Operating Function Control 2 register
				//enable the receiver chain again
				SpiWriteRegister(0x07, 0x05);												//write 0x05 to the Operating Function Control 1 register
			}
		} 	
	}
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME:  void MCU_Init(void)
  +
  + DESCRIPTION:   	This function configures the MCU 
  + 		
  + INPUT:			None
  +
  + RETURN:         None
  +
  + NOTES:          None
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void MCU_Init(void)
{
	//Disable the Watch Dog timer of the MCU
  	PCA0MD   &= ~0x40;                  
	
	// Set the clock source of the MCU: 10MHz, using the internal RC osc.
  	CLKSEL    = 0x14;


  	// Initialize the the IO ports and the cross bar
  	P0SKIP  |= 0x40;                    // skip P0.6
  	XBR1    |= 0x40;                    // Enable SPI1 (3 wire mode)
  	P1MDOUT |= 0x01;                    // Enable SCK push pull
  	P1MDOUT |= 0x04;                    // Enable MOSI push pull
  	P1SKIP  |= 0x08;                    // skip NSS
  	P1MDOUT |= 0x08;                    // Enable NSS push pull
  	P1SKIP  |= 0x40;                    // skip TX_LED
  	P1MDOUT |= 0x40;                    // Enable TX_LED push pull
  	P2SKIP  |= 0x01;                    // skip RX_LED
  	P2MDOUT |= 0x01;                    // Enable RX_LED push pull
	P0SKIP  |= 0x02; 							// skip SDN
	P0MDOUT |= 0x02;							// Enable SDN push pull
  	P0SKIP  |= 0x80;                    // skip PB
  	SFRPAGE  = CONFIG_PAGE;
  	P0DRV    = 0x12;                  	// TX high current mode
  	P1DRV    = 0x4D;                  	// MOSI, SCK, NSS, TX_LED high current mode
	P2DRV	   = 0x01;							// RX_LED high current mode	
  	SFRPAGE  = LEGACY_PAGE;
  	XBR2    |= 0x40;                    // enable Crossbar

  	// For the SPI communication the hardware peripheral of the MCU is used 
  	//in 3 wires Single Master Mode. The select pin of the radio is controlled
	//from software
	SPI1CFG   = 0x40;							//Master SPI, CKPHA=0, CKPOL=0
  	SPI1CN    = 0x00;							//3-wire Single Master, SPI enabled
  	SPI1CKR   = 0x00;
  	SPI1EN 	 = 1;                     	// Enable SPI interrupt
  	NSS = 1;
	
	// Turn off the LEDs
	TX_LED = 0;
	RX_LED = 0;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME: void SpiWriteRegister(U8 reg, U8 value)
  +
  + DESCRIPTION:   This function writes the registers 
  + 					
  + INPUT:			 U8 reg - register address   
  +					 U8 value - value write to register	
  +
  + RETURN:        None
  +
  + NOTES:         Write uses a Double buffered transfer
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiWriteRegister (U8 reg, U8 value)
{
  	// Send SPI data using double buffered write

	//Select the radio by pulling the nSEL pin to low
  	NSS = 0;                            
   
  	//write the address of the register into the SPI buffer of the MCU
	//(important to set the MSB bit)
	SPI1DAT = (reg|0x80);				//write data into the SPI register
	//wait until the MCU finishes sending the byte
	while( SPIF1 == 0);					
	SPIF1 = 0;
	//write the new value of the radio register into the SPI buffer of the MCU
	SPI1DAT = value;					//write data into the SPI register
	//wait until the MCU finishes sending the byte
	while( SPIF1 == 0);					//wait for sending the data
	SPIF1 = 0;	       
  	//Deselect the radio by pulling high the nSEL pin
	NSS = 1;                            

}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  +
  + FUNCTION NAME: U8 SpiReadRegister(U8 reg)
  +
  + DESCRIPTION:   This function reads the registers 
  + 					
  + INPUT:			 U8 reg - register address   
  +
  + RETURN:        SPI1DAT - the register content 
  +
  + NOTES:         none
  +
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
U8 SpiReadRegister (U8 reg)
{

	//Select the radio by pulling the nSEL pin to low
	NSS = 0;                            
	//Write the address of the register into the SPI buffer of the MCU 
	//(important to clear the MSB bit)
  	SPI1DAT = reg;								//write data into the SPI register
	//Wait untill the MCU finishes sending the byte
	while( SPIF1 == 0);						
	SPIF1 = 0;
	//Write a dummy data byte into the SPI buffer of the MCU. During sending 
	//this byte the MCU will read the value of the radio register and save it 
	//in its SPI buffer.
	SPI1DAT = 0xFF;							//write dummy data into the SPI register
	//Wait untill the MCU finishes sending the byte
	while( SPIF1 == 0);						
	SPIF1 = 0;
	//Deselect the radio by pulling high the nSEL pin
	NSS = 1;                            

	//Read the received radio register value and return with it
  	return SPI1DAT;
}

⌨️ 快捷键说明

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