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

📄 mainc1.c

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

// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here

} 

/****************************************************************************/
/* This routine outputs a string, and also outputs trailing new-line and    */
/* carrage return characters                                                */
/****************************************************************************/

void writeln( char flash *str) //void writeln(const char *str)
{
  while (*str!='\0') {
    putchar(*str++);
  }
  putchar('\n');
  putchar('\r');
}


// Configures all the CC1020 registers according to the values stored in EEPROM
void SetupCC1020All(void)
{ 
  char counter;
  char value;

  for(counter=0x01; counter<=0x20; counter++){
    value=EEPROMCC1020Config[counter-1];
    WriteToCC1020Register(counter,value);
  }
}  

/**********************************************************************************************  
Initialize The DATA to be RFed
***********************************************************************************************/
void Init_Wr1020_Buffer_preamble()   //This is the data to be send out by the BasicStation
{
  char i;
  // Put preamble into buffer
  for (i=0;i<PREAMBLE_LENGTH;i++) 
      {
      Wr1020_Buffer[i]=PREAMBLE_BYTE;                       //?????Preamble? set PREAMBLE_LENGTH=0                       
      }     
} 

void Fill_Wr1020_Buffer(unsigned char idH,unsigned char idL)
{ 

  // the frame consists of 6 bytes excluding the Preamble, which is not fully comply to the Document.
  unsigned int CRC16_code;
         
  Wr1020_Buffer[StationID_i]=StationID;           // StationID, for this project is 0x24 
  Wr1020_Buffer[ALM_i]=ALM_Type;                 // ALM type ahould be added.
  Wr1020_Buffer[IDH_i]=idH;                       // IDH  IDH+IDL=is the Address of the ID_Card
  Wr1020_Buffer[IDL_i]=idL;                       // IDL  to which this data will be sent to 
  
  CRC16_code=Calcu_CRC(Wr1020_Buffer+PREAMBLE_LENGTH,3);  
  Wr1020_Buffer[CRCH_i]=(unsigned char)((CRC16_code&0xff00)>>8);   // CRCH CRC?????; ???
  Wr1020_Buffer[CRCL_i]=(unsigned char)(CRC16_code&0x00ff);        // CRCL CRC?????
  
} 


/***************************************************************************
  Main Function = Entry 
****************************************************************************/
// Declare your global variables here  



void main(void)
{
// Declare your local variables here  

char tAlarm[5];
char temp[4];
char test1;
unsigned int i;
//for(i=0;i<0x3fff;i++);
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x30;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=0 State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x10;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;





// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x02;
GIFR=0x40; 
/*
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x03;
GIFR=0x40;
*/


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x5F;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")   

/************ Initialize the next variables ***************
  Send_ID_Times_count++; 
  ShiftReg
  State
  PreambleFound =FALSE;
  Change to INT0 Rising edge when receive/Read
  Change to INT0 Falling edge when Transmit/Write

**********************************************************/
  writeln("Normal..");
  Init_Wr1020_Buffer_preamble(); 
  PSEL=1;
                  // PSEL Active LOW!
  SetupCC1020PD();
  ResetCC1020();
  SetupCC1020All(); 
  
  WakeUpCC1020ToTX(TXANALOG);
  MCUCR=0x02;   //INT0 is configued to falling edge on the very start.//OPTION=0x80;  // INT on falling edge
  SET_DIO_OUTPUT; //TRISC&=~(0x02);     // Set DIO as output
  if(!CalibrateCC1020(PA_POWER))
    writeln("TX Calibration failed");
    
  else 
    writeln("TX Calibration Succeeded"); 
 // while(tx_counter);
     
  WakeUpCC1020ToRX(RXANALOG);         // Is that right here? should it be SetupCC1020RX();Because it's from TX
  MCUCR=0x03; //OPTION=0xC0;          // INT on rising edge
  SET_DIO_INPUT;//TRISC|=0x02;        // Set DIO as input
  if (!CalibrateCC1020(PA_POWER))
    writeln("RX Calibration failed");
  else 
    writeln("RX Calibration Succeeded");
    
  //while(tx_counter);

   // put CC1020 on Receive mode
   State=Rd1020_STATE;
   MCUCR=0x03; //OPTION=0xC0;          // INT on rising edge
   SET_DIO_INPUT; //TRISC|=0x02;        // Set DIO as input
   if(!SetupCC1020RX(RXANALOG, PA_POWER))
   writeln("SetUP RX failed");  //Enter receive/Read1020 state   
     
   //Initialise the Alarm State.
   SET_Pin_ALARM_OUTPUT;
   Cancel_ALARM; 
   writeln("Working...");     
   
while (1)
      {           
   if(rx_counter!=0){
        
        if (getchar()==0xEE)
           { 
             tAlarm[0] = getchar();
             if (tAlarm[0]==0x41)
                { if (getchar()==0x4C)
                   { if (getchar()==0x0D)
                        { if (getchar()==0xFF)
                            {SET_ALARM;       //I got it! why the LED isn't on/off accordingly. Because it's not the PIN.(JP2.D)
                             writeln("ALARM!");
                            }
                        }         
                   }
                }
              else if (tAlarm[0]==0x4E)   // Problem
                      { if (getchar()==0x4F)
                          { if(getchar()==0x01)
                              { if(getchar()==0xFF)
                                  {Cancel_ALARM;
                                   writeln("Cancel ALARM!");
                                  }
                              }  
                           }
                       }
               
                
            } //if (getchar()==0xEE)
        
         } // if(rx_counter!=0) 

    
     if(Rd1020_counter!=0)
     {   
     putchar(readchar());
     }  
    
           // CRC_16=Calcu_CRC(temp,2); 
           /* if(((unsigned char)((CRC_16&0xff00)>>8)==temp[2])&&( (unsigned char)(CRC_16&0x00ff)==temp[3] ) ) //CRC is OK
               {
                // output to UART
                putchar(StationID);
                putchar(temp[0]);  //ALM/IDH
                putchar(temp[1]);         //IDL  
                // output one positive pulse on JP1_Pin8 SLP pin 
                // to be continued...   
               }                  
           */
          
      } //While
      
} //main()

//Notice: the CRC checksum may be wrong implemented! 

⌨️ 快捷键说明

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