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

📄 main_sdbc_dk3.c

📁 Si4432-T-B1版本资料.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
			if( (ItStatus1 & 0x02) == 0x02 )
			{
 				//Read the length of the received payload
				length = SpiReadRegister(0x4B);												//read the Received Packet Length register
				//check whether the received payload is not longer than the allocated buffer in the MCU
				if(length < 11)
				{
					//Get the reeived payload from the RX FIFO
					for(temp8=0;temp8 < length;temp8++)
					{
						payload[temp8] = SpiReadRegister(0x7F);							//read the FIFO Access register
					}
			
					//check whether the content of the packet is what the demo expects
					if( length == 8 ) 
	            {
	            	if( memcmp(&payload[0], "BUTTON1", 7) == 0 )
					  	{
	               	//blink LED1 to show that the packet received
	     					LED1 = 1;
							for(delay = 0; delay < 10000;delay++);
	  		   			LED1 = 0;
	               }
						if( memcmp(&payload[0], "BUTTON2", 7) == 0 )
					   {
	                 	//blink LED2 to show that the packet received
	     					LED2 = 1;
							for(delay = 0; delay < 10000;delay++);
	  		   			LED2 = 0;
	               }
						if( memcmp(&payload[0], "BUTTON3", 7) == 0 )
					   {
	                 	//blink LED3 to show that the packet received
	     					LED3 = 1;
							for(delay = 0; delay < 10000;delay++);
	  		   			LED3 = 0;
	               }
						if( memcmp(&payload[0], "BUTTON4", 7) == 0 )
					   {
	                 	//blink LED4 to show that the packet received
	     					LED4 = 1;
							for(delay = 0; delay < 10000;delay++);
	  		   			LED4 = 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 
  + 						P0.0  -  skipped, PB0
  + 						P0.1  -  skipped  PB1
  + 						P0.2  -  skipped, analog input, XTAL
  + 						P0.3  -  skipped, analog input, XTAL
  + 						P0.4  -  UART TX, push-pull output
  + 						P0.5  -  UART RX, open drain input
  + 						P0.6  -  skipped
  + 						P0.7  -  skipped
  +
  + 						P1.0  -  SCK  (SPI1), Push-Pull,  Digital
  + 						P1.1  -  MISO (SPI1), Open-Drain, Digital
  + 						P1.2  -  MOSI (SPI1), Push-Pull,  Digital
  + 						P1.3  -  NSS, Push-Pull,  Digital
  + 						P1.4  -  skipped LED1 
  + 						P1.5  -  skipped LED
  + 						P1.6  -  skipped LED
  + 						P1.7  -  skipped LED
  +	
  + 						P2.0  -  skipped, PB3
  + 						P2.1  -  skipped  PB4
  +
  + 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  |= 0xCF;                    // skip P0.0-3 & 0.6-7
   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  |= 0xF0;                    // skip LEDs
   P1MDOUT |= 0xF0;                    // Enable LEDS push pull
   P2SKIP  |= 0x03;                    // skip PB3 & 4
   P2 = 0x03;		
	SFRPAGE = CONFIG_PAGE;
   P1DRV   = 0xFD;		                // MOSI, SCK, NSS, LEDs 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
   LED1 = 0;
   LED2 = 0;
   LED3 = 0;
   LED4 = 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 + -