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

📄 main.c

📁 CodevisionAVR 开发环境。 用AVR控制TI/Chipcon非常常用的CC1000射频收发芯片。
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "mega48.h"
#include <configure.h>
#include <CC1000.h>
#include "main.h"
#include <stdio.h>  
//#include <simpleio.h>  
#include "CalculateCRC.h"

void SetupCC1000All(void)
{ 
  unsigned char counter;
  char value;
  
  for (counter=0x01;counter<=0x1C;counter++)
      {
       value=EEPROMCC1000Config[counter]; //????
       WriteToCC1000Register(counter,value);
      }
  
 // WriteToCC1000Register(0X42,EEPROMCC1000Config[0x1F]);
  for (counter=0x40;counter<=0x44;counter++) 
      {
       value=EEPROMCC1000Config[counter-0x40+0x1D];
       WriteToCC1000Register(counter,value);
     }

}

/**********************************************************************************************  
The DATA to be RFed
***********************************************************************************************/


//发送数据之前调用以下初始化函数
void Init_Wr1000_Buffer(void)
{
  char i;
  unsigned int CRC16_code;  
  // Put preamble into buffer
  for (i=0;i<PREAMBLE_LENGTH;i++) 
      {
      Wr1000_Buffer[i]=0xAA;                       //如过不需要Preamble就 set PREAMBLE_LENGTH=0                       
      }
  Wr1000_Buffer[PREAMBLE_LENGTH]=UI1;              // First byte of unique identifier 
  Wr1000_Buffer[PREAMBLE_LENGTH+1]=UI2;            // Second byte of unique identifier  
  //Wr1000_Buffer[PREAMBLE_LENGTH]=((char)((Card_ID&0x3fff)>>8))|0b00000000;// ID高位加前缀00 
  Wr1000_Buffer[PREAMBLE_LENGTH+2]=(char)((Card_ID&0x3fff)>>8);// ID高位加前缀00
  Wr1000_Buffer[PREAMBLE_LENGTH+3]=(char)(Card_ID&0x00ff);//ID低8位 
  CRC16_code=Calcu_CRC(Wr1000_Buffer+PREAMBLE_LENGTH+2,2);
  Wr1000_Buffer[PREAMBLE_LENGTH+4]=(unsigned char)((CRC16_code&0xff00)>>8);      // CRCH CRC校验的高位; 待实现
  Wr1000_Buffer[PREAMBLE_LENGTH+5]=(unsigned char)(CRC16_code&0x00ff);           // CRCL CRC校验的低位
  for(i=0;i<PREAMBLE_LENGTH+6;i++)
  {putchar(Wr1000_Buffer[i]);}  
}

/******************************************************************************  
// Pin change 8-14 interrupt service routine  
******************************************************************************/ 
/*interrupt [PCINT1] void pin_change_isr1(void)
{
//Data is Transmitted or Received at the the Rising edge of DCLK which is provided by the  CC1000.      
if ( (PINC&0x10)==0x00)//先判断是falling Edge, 
{      
          //在main里面应该设置好DIO引脚的状态
           //Write data to CC1000  
          if (Send_TD_Times_count<REQ_Send_ID_Times)
          { 
          
             if((ShiftReg&0x80)==0x80) //Error!//if (ShiftReg&0x80==0x80)优先级 error!//if (ShiftReg&0x80==1) //Problem 
			{SET_DIO();
			}//PORTC.DIO=1; //可能的问题2
             else 
		        {CLEAR_DIO();      
		        }// PORTC.DIO=0;  
		        
	    ShiftReg=ShiftReg<<1; //the LSB of the char is sent out first!
	    BitCounter++;	        
           if (BitCounter==8)
			    {
				BitCounter=0; 
				Wr1000_Buffer_Index++;
				
				if (Wr1000_Buffer_Index>=(PREAMBLE_LENGTH+6))
                                 { 
                                  Send_TD_Times_count++;
                                  Wr1000_Buffer_Index=0;
                                 } 
                                 
				ShiftReg=Wr1000_Buffer[Wr1000_Buffer_Index];
			
			    }  
			     
           }  //if (Send_TD_Times_count<REQ_Send_ID_Times){ 
 
} // if ( (PINC&0x10)==0x00)//先判断是falling Edge,

} //interrupt
*/   
interrupt [PCINT1] void pin_change_isr1(void)
{
//Data is Transmitted or Received at the the Rising edge of DCLK which is provided by the  CC1000.      
if ((PINC&0x10)==0x00)//if (((PINC&0x10)==0x00)&&(State==WR1000_STATE))//????falling Edge,  
{      
          //?main???????DIO?????
          if(State==WR1000_STATE){
          if (Send_TD_Times_count<REQ_Send_ID_Times)
          { 
          
             if((ShiftReg&0x80)==0x80) //Error!//if (ShiftReg&0x80==0x80)??? error!//if (ShiftReg&0x80==1) //Problem 
			{SET_DIO();
			}//PORTC.DIO=1; //?????2
             else 
		        {CLEAR_DIO();      
		        }// PORTC.DIO=0;  
		        
	    ShiftReg=ShiftReg<<1; //the LSB of the char is sent out first!
	    BitCounter++;	        
            if (BitCounter==8)
			    {
				BitCounter=0; 
				Wr1000_Buffer_Index++;
				
				if (Wr1000_Buffer_Index>=(PREAMBLE_LENGTH+6))
                                 { 
                                  Send_TD_Times_count++;
                                  //putchar(Send_TD_Times_count);
                                  Wr1000_Buffer_Index=0;
                                 } 
                                 
				ShiftReg=Wr1000_Buffer[Wr1000_Buffer_Index];
			
			    }  
			     
           }  //if (Send_TD_Times_count<REQ_Send_ID_Times){ 
          }   //if(State==WR1000_STATE)
 
} // if ( (PINC&0x10)==0x00)//????falling Edge,

//if ((PINC&0x10)==0x10)//Rising edge
//{
else if( State==Rd1000_STATE)
{ //rising edge,do receiving routine
   // the rising edge , do receving.
 /*   ShiftReg=ShiftReg<<1; 
			    // if (Read_DIO()==1)    // High side LO,Polarity reverse.
				if (Read_DIO()==1)//  if (DIO==0)    DIO==PINC.2
					{
					ShiftReg|=0x01;
					}
				else    {
					ShiftReg&=~0x01;
					} 
				BitCounter++; 
				
			
			 if(UI1_Found)//UI1 has just found,
			  {  // If received 8bits=1byte
				 if(BitCounter==8)
					{
					BitCounter=0; 
					//Reset_TC1();
					// Process received RF data:
					switch(ByteCounter)
					{
							// Byte-0 = SOF part 1:
					case 0 :
						if(ShiftReg!=UI2)//we abort and start looking for preamble again
						{PreambleFound=FALSE;
						 UI1_Found=FALSE;
						}
						break;

					// Rest of the packet is data, store it in the receive buffer  
					default :
						Rd1000_Buffer[Rd1000_wr_index++] =ShiftReg;
						if(Rd1000_wr_index>=Rd1000_Buffer_size)           //make sure the buffer size is fourfold.
								{Rd1000_wr_index=0;}   // Circular Buffer,// we won't know if the buffer is overwritten or not!!              
						if (++Rd1000_counter>Rd1000_Buffer_size)
								//{Rd1000_counter=0;}                       //will lose all the data in the buffer.improve.
								{Rd1000_counter--;Rd1000_wr_index++;}   // will this be better?
						break;
					} //switch(ByteCounter)

					if(ByteCounter>=4) //we've get what we want, so start looking for new Preamble.
					{
					PreambleFound=FALSE;
					UI1_Found=FALSE;
					}
					ByteCounter++;
				                               
				} //if (BitCounter==8 )

		     }
			else if(PreambleFound) //if(UI1_Found)
             {
					if(ShiftReg==UI1)
						{ //READY=1;  // HW Handshake : Not Ready
							UI1_Found=TRUE;
                            //PreambleFound=FALSE;
							BitCounter=0;
							ByteCounter=0;
						}// Else if we are still receiving preamble, do nothing
					else if((ShiftReg==0xAA)||(ShiftReg==0x55)) // Here,the redundant Preamble is ignored!
						{}// Else if we are not receiving a correct preamble, declare an error
					else if(PreambleError==0)
						{PreambleError++;
						}
					//else
					//	{}

					// If preamble error found, increase the error counter regardless of bits read
					if(PreambleError>0)
						{PreambleError++;
						}
						// Once an error condition has occurred, a correct SOF must be found
						// within 9 bits (error counter is initially incremented by 2), otherwise
						// we abort and start looking for preamble again
					if(PreambleError>10) //if(PreambleError>8)??
						{PreambleFound=FALSE;
						}
			
			 }
		    else //else if(PreambleFound) //if(UI1_Found)
		     {    // Else (preamble has not been found) : else if(PreambleFound) //if(UI1_Found)
				  // If valid preamble, increase counter
					if ((ShiftReg==0xAA)||(ShiftReg==0x55)) 
					   {PreambleBitCount++;
					   }
					// Else (not valid preamble), reset counter
					else
					   {PreambleBitCount=0;
					   }

					// If required preamble reached, indicate preamble found
					if(PreambleBitCount>=PREAMBLE_BIT_REQ)
					{
					PreambleFound=TRUE;
					PreambleBitCount=0;// reset once finished looking
					PreambleError=0;
					}

			  }//else //else if(PreambleFound) //if(UI1_Found)        */
			  
			     ShiftReg=ShiftReg<<1; 
			    // if (Read_DIO()==1)    // High side LO,Polarity reverse.
				if (Read_DIO()==1)//  if (DIO==0)    DIO==PINC.2
					{
					ShiftReg|=0x01;
					}
				else    {
					ShiftReg&=~0x01;
					} 
				BitCounter++; 
				/*******************  for test only  ***********************************
				if (BitCounter==8 ) 
					{ BitCounter=0; 
					Rd1020_Buffer[Rd1020_wr_index++] =ShiftReg;
		                     
							if(Rd1020_wr_index>=Rd1020_Buffer_size)
								{Rd1020_wr_index=0;}   // Circular Buffer,// we won't know if the buffer is overwritten or not!!              
							if (++Rd1020_counter>Rd1020_Buffer_size)
								{Rd1020_counter=0;}
				*******************    for test only  ************************************/
			
			 if(UI1_Found)//UI1 has just found,
			  {  // If received 8bits=1byte
				 if(BitCounter==8)
					{
					BitCounter=0;
					//Reset_TC1();
					// Process received RF data:
					switch(ByteCounter)
					{
							// Byte-0 = SOF part 1:
					case 0 :
						if(ShiftReg!=UI2)//we abort and start looking for preamble again
						{PreambleFound=FALSE;
						 UI1_Found=FALSE;
						}
						break;
                    /*
					// Byte-1 = address:
					case 1 :
						// Addressing not implemented
						break;
					// Byte-2 = packet length/size:
					case 2 :
						BytesToReceive=ShiftReg;
						if(BytesToReceive>TX_BUFFER_SIZE)
						{
						BytesToReceive=0;
						}
						break;
                    */
					// Rest of the packet is data, store it in the receive buffer  
					default :
						Rd1000_Buffer[Rd1000_wr_index++] =ShiftReg;
						if(Rd1000_wr_index>=Rd1000_Buffer_size)           //make sure the buffer size is fourfold.
								{Rd1000_wr_index=0;}   // Circular Buffer,// we won't know if the buffer is overwritten or not!!              
						if (++Rd1000_counter>Rd1000_Buffer_size)
								//{Rd1000_counter=0;}                       //will lose all the data in the buffer.improve.
								{Rd1000_counter--;Rd1000_wr_index++;}   // will this be better?
						break;
					} //switch(ByteCounter)

					if(ByteCounter>=4) //we've get what we want, so start looking for new Preamble.
					{
					PreambleFound=FALSE;
					UI1_Found=FALSE;
					}
					ByteCounter++;
				                               
				} //if (BitCounter==8 )

		     }
			else if(PreambleFound) //if(UI1_Found)
             {
					if(ShiftReg==UI1)
						{ //READY=1;  // HW Handshake : Not Ready
							UI1_Found=TRUE;
                            //PreambleFound=FALSE;
							BitCounter=0;
							ByteCounter=0;
						}// Else if we are still receiving preamble, do nothing
					else if((ShiftReg==0xAA)||(ShiftReg==0x55)) // Here,the redundant Preamble is ignored!
						{}// Else if we are not receiving a correct preamble, declare an error
					else if(PreambleError==0)
						{PreambleError++;
						}
					//else
					//	{}

					// If preamble error found, increase the error counter regardless of bits read
					if(PreambleError>0)
						{PreambleError++;
						}
						// Once an error condition has occurred, a correct SOF must be found
						// within 9 bits (error counter is initially incremented by 2), otherwise
						// we abort and start looking for preamble again
					if(PreambleError>8) //if(PreambleError>8)??
						{PreambleFound=FALSE;
						}
			
			 }
		    else //else if(PreambleFound) //if(UI1_Found)
		     {    // Else (preamble has not been found) : else if(PreambleFound) //if(UI1_Found)
				  // If valid preamble, increase counter
					if ((ShiftReg==0xAA)||(ShiftReg==0x55)) 
					   {PreambleBitCount++;
					   }
					// Else (not valid preamble), reset counter
					else
					   {PreambleBitCount=0;
					   }

					// If required preamble reached, indicate preamble found
					if(PreambleBitCount>=PREAMBLE_BIT_REQ)
					{
					PreambleFound=TRUE;
					PreambleBitCount=0;// reset once finished looking
					PreambleError=0;
					}

			  }//else //else if(PreambleFound) //if(UI1_Found) 
			  
      } //if( State==Rd1000_STATE)

//} //if ((PINC&0x10)==0x10)//Rising edge



} //interrupt 

/******************************************************************************   
 //Read the received data from the CC1020 
******************************************************************************/ 
 char readchar(void)
{ char data;
  while (Rd1000_counter==0);//if there is no data to read, wait. 
  data=Rd1000_Buffer[Rd1000_rd_index];
  if (++Rd1000_rd_index == Rd1000_Buffer_size) Rd1000_rd_index=0;
  #asm("cli")
  --Rd1000_counter;
  #asm("sei")
  return data;
}
 

/******************************************************************************   
 // Timer1 Compare Match A interrupt service routine 
******************************************************************************/ 
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here      
TC1_interrupt_count++;
Stop_TC1();
}
 

/******************************************************************************   
 // Timer2 Compare Match A interrupt service routine 
******************************************************************************/  
interrupt [TIM2_COMPA] void timer2_compa_isr(void)
{
         WakeUp_SLEEP;    //一旦唤醒,立即清除SE。SMCR.0=SE
         //#asm("cli")
         HignFreq_8MHz();  //Full speed again! // Some times,Maybe you don't have to chage back to full speed.
         //#asm("sei")
         /*
         PRR_Open_USART0(); //Open之后还要初始化, 这与PRR寄存器有关.   
         UCSR0A=0x00;
         UCSR0B=0xD8;
         UCSR0C=0x06;
         UBRR0H=0x00;
         UBRR0L=0x33; */ 
         Stop_TC2();   
                      
}        


/******************************************************************************   
// USART Receiver interrupt service routine
******************************************************************************/ 
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)

⌨️ 快捷键说明

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