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

📄 interrupt.c

📁 单片无限收发芯片的一些资料
💻 C
字号:
/****************************************************************************/
/* Application note AN015                                                   */
/* Reference design : CC1000 RF Modem                                       */
/*                                                                          */
/* File:      interrupt.c                                                   */
/* Revision:  1.0                                                           */
/*                                                                          */
/* Microcontroller:                                                         */
/*          Microchip PIC16F876                                             */
/*                                                                          */
/* Author:  Karl H. Torvmark, Field Applications Engineer, Chipcon          */
/*                                                                          */
/* Contact: Chipcon AS +47 22 95 85 44                                      */
/*          wireless@chipcon.com                                            */
/****************************************************************************/

/****************************************************************************/
/* This file contains the interrupt handler, which handles the external,    */
/* UART, timer 2 and timer 1 interrupts. Communication with the main        */
/* program occurs via global variables.                                     */
/****************************************************************************/


/*                                                                           *
 * Revision history:                                                         *
 *                                                                           *
 * $Log: interrupt.c,v $
 * Revision 2.1  2003/04/24 12:43:27  tos
 * Cosmetic change: added revision history for CVS.
 *
 *                                                                           *
 ****************************************************************************/


#include "io16f876.h"
#include "modemhw.h"
#include "main.h"
#include "configure.h"
#include <stdio.h>

#define INTERRUPT_VECTOR 0x04

#pragma vector=INTERRUPT_VECTOR 
__interrupt void InterruptHandler(void)
{
  char dummy;
  
  /* This function must execute as fast as possible. Therefore, as much processing as
  possible is left to the main program */
  
  switch (State) {
    case TX_STATE: 
      /* Write data to CC1000 */
      DIO=ShiftRegMSB;
      ShiftReg=ShiftReg<<1;
      BitCounter++;
        
      if (BitCounter==1) {
        if (TXBufferIndex>BytesToSend) {
          NextState=IDLE_STATE;
          break;
        }
      }
       
      if (BitCounter==8) {
        BitCounter=0;
        ShiftReg=TXBuffer[TXBufferIndex++];
      }
        
      break;
    case RX_STATE:
      /* Read data from CC1000 */
      ShiftReg=ShiftReg<<1;
      ShiftRegLSB=~DIO;
      BitCounter++;
        
      /* Byte received? */
      if (BitCounter==8) {
        BitCounter=0;
          
        /* The three first bytes must be handled in a special manner */
          
        switch (ByteCounter) {
          case 0 :  // First byte (second part of SOF)
            if (ShiftReg!=UI2)
              NextState=IDLE_STATE;
            break;
          case 1 : // Second byte is address
            // Addressing not implemented
            break;
          case 2 : // Third byte is packet size, store this
            BytesToReceive=ShiftReg;
            if (BytesToReceive>TX_BUFFER_SIZE) // If invalid length, ignore message
              BytesToReceive=0;
            break;
          default : // Rest of the packet is data, store it in the receive buffer  
            RXBuffer[RXBufferWriteIndex]=ShiftReg;
            // Increase counter, wrap around when we've reached end of buffer
         //   RXBufferWriteIndex=(RXBufferWriteIndex+1)%RX_BUFFER_SIZE;
            RXBufferWriteIndex++;
            RXBufferWriteIndex&=0x3F;
              
        }
        if (ByteCounter>=BytesToReceive+2) {
          UnlockAverage=1;
          NextState=IDLE_STATE;
        }
        ByteCounter++;
      }
      break;
    case IDLE_STATE:
      /* Read data from CC1000 */
      ShiftReg=ShiftReg<<1;
      ShiftRegLSB=~DIO;   // Polarity must be inverted if using high-side LO
      BitCounter++;
      
      /* If preamble has been found */
      if (PreambleFound) {
        /* Look for SOF/unique identifier */
        if (ShiftReg==UI1) { 
          // If SOF found, go into RX mode
          NextState=RX_STATE;
        }
        /* Are we still receiving the preamble? */
        else if ((ShiftReg==0x55)||(ShiftReg==0xAA)) {
          ; /* If so, do nothing */
        }
        /* If we are not receiving a correct preamble, declare an error */
        else if (PreambleError==0) {
          PreambleError++;
        }
          
        if (PreambleError>0)
          /* Increase the error counter regardless of bits read if */
          /* we have found en error */
          PreambleError++;
            
        /* Once an error condition has occurred, a correct SOF must be */
        /* found within 9 bits (we increment by 2 at the first bit), */
        /* otherwise we abort and start looking for the preamble again */
        if (PreambleError>10) {
          PreambleFound=FALSE;
          UnlockAverage=1;
        }
      }
      /* If preamble has not been found */
      else {
        /* If valid preamble, increase counter */
        if ((ShiftReg==0x55)||(ShiftReg==0xAA)) {
          PreambleCount++;
        }
        
        /* If not, reset counter */
        else {
          PreambleCount=0;
        }
        /* If preamble requirement has been reached, declare that preamble */
        /* has been found */   
        if (PreambleCount>=PREAMBLE_REQ) {
          PreambleFound=TRUE;
          LockAverage=1;
          PreambleError=0;
        }
      }
        
      break;
     default:
      // If something goes haywire, get back into a known state
      NextState=IDLE_STATE;
      break;
  }
  INTF=0;  
}

⌨️ 快捷键说明

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