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

📄 mainc1.c

📁 Atmega8L控制CC1020 TI的另外一款使用很多的RF IC。
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <mega8.h>
#include "CRC.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> 
#include "main.h"
#include "CC1020.h"
#include "MCU_interface.h"  

/********************************************************************************* 
// External Interrupt 0 service routine 
*********************************************************************************/ 
interrupt [EXT_INT0] void ext_int0_isr(void)
{ 

// Receive and send data from and to the CC1020, a bit a time. so prepare the buffer. 
//for send and receive sepreately.
 switch (State)
    {   //Write CC1020 State
        case WR1020_STATE:
             // Write data to CC1020 // On Falling Edge
					if (Wr1020_Buffer_Index<Bytes_to_send)
					{                  
							if ((ShiftReg&0x80)==0x80)
					            {DIO=1;}
							else 
						        {DIO=0;}
							ShiftReg=ShiftReg<<1; //the MSB of the char is sent out first!
					             BitCounter++;
							if (BitCounter==8)
							{
							   BitCounter=0;
							   ShiftReg=Wr1020_Buffer[Wr1020_Buffer_Index++];
							} 
					}
				   else 
					{//Wr1000_Buffer_Index==PREAMBLE_LENGTH+5  
					BitCounter=0;
					Wr1020_Buffer_Index=0;// 
					//Send_ID_Times_count++;//  The variable is handled in the main.c or it can be done here.
					ShiftReg =Wr1020_Buffer[Wr1020_Buffer_Index]; //Initialize the ShiftReg
					}         
        break; 
        
       
       //Read CC1020 State // On Rising Edge
       //What is received here is stored in the Rd1020_Buffer Buffer.
        case Rd1020_STATE: 
				ShiftReg=ShiftReg<<1; 
			    // if (Read_DIO()==1)    // High side LO,Polarity reverse.
				if ((PINC&0x04)==0x04)//  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;
					// 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 :
						Rd1020_Buffer[Rd1020_wr_index++] =ShiftReg;
						if(Rd1020_wr_index>=Rd1020_Buffer_size)           //make sure the buffer size is fourfold.
								{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;}                       //will lose all the data in the buffer.improve.
								{Rd1020_counter--;Rd1020_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==VALID_PREAMBLE_BYTE_1)||(ShiftReg==VALID_PREAMBLE_BYTE_2)) // 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==VALID_PREAMBLE_BYTE_1)||(ShiftReg==VALID_PREAMBLE_BYTE_2)) 
					   {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)
	  break; 
   } //switch 
    
} //interrupt  


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


#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Receiver buffer
#define RX_BUFFER_SIZE 16
volatile char rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE<256
volatile unsigned char rx_wr_index=0,rx_rd_index=0,rx_counter=0;
#else
volatile unsigned int rx_wr_index=0,rx_rd_index=0,rx_counter=0;
#endif

// This flag is set on USART Receiver buffer overflow
volatile bit rx_buffer_overflow=0;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index]=data;
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      };
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART Transmitter buffer
#define TX_BUFFER_SIZE 16
volatile char tx_buffer[TX_BUFFER_SIZE];

#if TX_BUFFER_SIZE<256
volatile unsigned char tx_wr_index=0,tx_rd_index=0,tx_counter=0;
#else
volatile unsigned int tx_wr_index=0,tx_rd_index=0,tx_counter=0;
#endif

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
   {
   --tx_counter;
   UDR=tx_buffer[tx_rd_index];
   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;
   };
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
#asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer[tx_wr_index]=c;
   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;
   ++tx_counter;
   }
else
   UDR=c;
#asm("sei")
}
#pragma used-
#endif

// Standard Input/Output functions

⌨️ 快捷键说明

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