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

📄 main.c

📁 This the code using PIC MCU to control
💻 C
字号:

#include <pic.h>
#include "pic_setting.h"
#include "CC1100.h"
#include "CC1100LIB.C"

BYTE paTable[] = {0xC0, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };
BYTE txBuffer[] = {2, 1,0x01};
BYTE rxBuffer[2];
BYTE dpybuffer[10]={0x11,0xF9,0x23,0x61,0xC9,0x45,0x05,0xF1,0x01,0x41};

//------------------------------------------------------------------
void InitPIC16F72() {
	TRISA=RACNF;
	TRISB=RBCNF;
	TRISC=RCCNF;

	ADCON1=0x06; //ADC OFF
	OPTION=0x87;	
	T1CON=0x31;

	PORTC=0XFF;	//数码管 OFF
	LED1=0;		//LED ON
	LED2=0;
	
	P_SCLK=0;
	P_CSn=1;
	P_SI=0;
	Dly1mS(200);
}

//--------------------------------------------------------------
BYTE KeyScan(void)
{
	TMR0=0; T0IF=0;
	OPTION=0x07;	//portb pull up
	Dly1mS(2);
	while(1)
	{
		if(T0IF) goto	l_exit_key;
		Current_key=PORTB|0xc3;
		if(Current_key!=Old_key)
		{
	  	Dly1mS(20);
	  	Current_key=PORTB|0xc3;
	  	if(Current_key!=Old_key)	//key state change?
			{
		  	Old_key=Current_key;
		  	if(Old_key!=0xff)
				{	
		  		Keyflag=1;	//new key
				goto	l_exit_key;
  				}
		  	else
       	    	Keyflag=0;	//key release		
			}
	   	else
		 	 Keyflag=0;		//niose	
		}
		else
			Keyflag=0;		////key state	do not be changed
	}
l_exit_key:
	LED2=1;
	LED1=1;
	OPTION=0x87;
	return(Keyflag);

	
}


void TxCC1100(void);
unsigned char RxCC1100(void);

//-----------------------------------------------------------
void main() 
{
	BYTE rfstate;
	InitPIC16F72();
	POWER_UP_RESET_CC1100();
	halRfWriteRfSettings();
	halSpiWriteBurstReg(CC1100_PATABLE, paTable, sizeof(paTable));
	halSpiStrobe(CC1100_SCAL);
	Dly1mS(2);
	while(1)
	{
		rfstate=halSpiReadStatus(CC1100_MARCSTATE);
		if((rfstate& 0x1f)==0x00)
		{
			LED1=0;
			LED2=0;
			POWER_UP_RESET_CC1100();
			halRfWriteRfSettings();
			
		}
		KeyScan();
		if(Keyflag)
		{
			txBuffer[1]=0;
			if(Old_key==0xFB)
				txBuffer[1]=1;	
			if(Old_key==0xF7)
				txBuffer[1]=2;
			if(Old_key==0xEF)
				txBuffer[1]=3;
			if(Old_key==0xDF)
				txBuffer[1]=4;
			txBuffer[2]=0x01;
	    	TxCC1100();
		}
		else
		{	
			if(RxCC1100())
			{
				if(rxBuffer[1]==0x01) 
				{
		
					txBuffer[2]=0x07;
					txBuffer[1]=rxBuffer[0];
					halSpiStrobe(CC1100_SIDLE);
					Dly1mS(500);
					TxCC1100();	
				}
				else
					if(rxBuffer[1]==0x07) Dly1mS(500);
					
			} 		
		}
	 }
}

//-------------------------------------------------------------
void TxCC1100()
{
	unsigned char	i;
//	halSpiWriteReg(0x3E,0xC0);	
	halSpiWriteReg(CC1100_FREND0, 0x10);//POWER=10DBM
	Dly1mS(50);	
	for(i=0;i<4;i++)
	{	
    	halRfSendPacket(txBuffer, sizeof(txBuffer));
		Dly1mS(50);
	}
//	halSpiWriteReg(0x3E,0x7f);	//POWER=0DBM
	halSpiWriteReg(CC1100_FREND0, 0x11);
	Dly1mS(50);
   	halRfSendPacket(txBuffer, sizeof(txBuffer));

	Dly1mS(100);
	PORTC=0xFF;	
}

//-----------------------------------------------------------
unsigned char RxCC1100(void)
{	
	unsigned char	length,i;
	length = sizeof(rxBuffer);
    if (halRfReceivePacket(rxBuffer, length))
		{
		 	i=rxBuffer[0];	
		 	PORTC=dpybuffer[i];
		 	LED2=0;
		 	LED1=1;
		 	return 1;
			
		}
	else	return 0;
	
}

⌨️ 快捷键说明

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