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

📄 int_others.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                   OTHER INTERRUPTS                   *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * Demonstrates the CC1010 interrupt sources DES, ADC, FLASH, RTC. Uses:     *
 * - INT_ENABLE, INT_PRIORITY, INT_GLOBAL_ENABLE, INT_SETFLAG, INT_GETFLAG,  *
 *   INT_FLASHDEBUG_ENABLE macros                                            *
 * - various other HAL functions and macros                                  *
 *                                                                           *
 * The program cannot be run with single-step operation in the debugger,     *
 * because the debugger's use of interrupts interfere with the operation.    *
 * The last part concerns testing the Flash interrupt. The interrupt is used *
 * for debugging too, hence this part of the program cannot be run in the    *
 * debugger at all.                                                          *
 *                                                                           *
 * The interrupt sources are configured. First the ADC interrupt is run. The *
 * interrupt triggers on a high sample ADC value. Turn the potmeter knob     *
 * clockwise until the red LED lights up, this shows that the interrupt has  *
 * occurred. The yellow LED will then light up when the DES interrupt occurs.*
 * Next the real-time clock interrupt is triggered after 10 seconds, and the *
 * green LED shall light up. 5 seconds afterwards a Flash page is written.   *
 * The Flash interrupt will light the blue LED and switch off the others.    *
 *****************************************************************************
 * Author:              ARR                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/04/25      First public release                                 *
 *                                                                           *
 * $Log: int_others.c,v $
 * Revision 1.2  2002/11/18 10:53:33  kht
 * Added startup macros
 *
 * Revision 1.1  2002/10/11 15:06:20  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/hal.h>
#include <chipcon/cc1010eb.h>

// Define constants for DES
#define RND_LENGTH 100
#define KEY_LENGTH 8
#define RAMBUF_ADDRESS 0x0000
#define CRPKEY_ADDRESS 0x0100

// Define constants for Flash
#define FLASHPAGE_ADDRESS   0x6000

// DES RAM buffers
byte xdata ramBuf[RND_LENGTH] _at_ RAMBUF_ADDRESS;
byte xdata keyBuf[KEY_LENGTH] _at_ CRPKEY_ADDRESS;

// Other global variables for the test
bool adc_checked = FALSE;
bool des_checked = FALSE;
bool rtc_checked = FALSE;
bool flash_checked = FALSE;

int main() {

    int i;

    // Disable watchdog timer
    WDT_ENABLE(FALSE);

    // Startup macros for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);

    // All I/Os are inputs after reset. Make I/Os connected to LEDs outputs
    RLED=YLED=GLED=BLED=LED_OFF;
    RLED_OE(TRUE); YLED_OE(TRUE); GLED_OE(TRUE); BLED_OE(TRUE);

    ////////////////////////////////////////////////////////////////////
    //                         SETUP INTERRUPTS                       //
    ////////////////////////////////////////////////////////////////////

    // Setup ADC and DES as interrupt sources
    INT_ENABLE(INUM_DES_ADC, INT_ON);
    INT_ENABLE(INUM_ADC, INT_ON);
    INT_ENABLE(INUM_DES, INT_ON);
    INT_PRIORITY(INUM_DES_ADC, INT_HIGH);

    // Setup RTC as an interrupt source
    INT_ENABLE(INUM_RTC, INT_ON);
    INT_PRIORITY(INUM_RTC, INT_LOW);

    // Setup FLASH as an interrupt source
    INT_ENABLE(INUM_FLASH, INT_ON);
    INT_FLASHDEBUG_ENABLE(INT_ON);

    // Turn on interrupts
    INT_GLOBAL_ENABLE(INT_ON);

    ////////////////////////////////////////////////////////////////////
    //                     TEST ADC INTERRUPT                         //
    ////////////////////////////////////////////////////////////////////

    // Configure and run ADC
    halConfigADC(ADC_MODE_SINGLE | ADC_INTERRUPT_ENABLE | ADC_REFERENCE_VDD,
        CC1010EB_CLKFREQ, 200); // threshold set quite high
    ADC_SELECT_INPUT(ADC_INPUT_AD0);
    ADC_POWER(TRUE);
    while(!adc_checked) ADC_SAMPLE_SINGLE();
    ADC_POWER(FALSE);

    ////////////////////////////////////////////////////////////////////
    //                     TEST DES INTERRUPT                         //
    ////////////////////////////////////////////////////////////////////

    // Do simple encryption
    // NOTE: Cannot use halDES, function disables the DES interrupt
    CRPKEY=CRPKEY_ADDRESS>>3;
    CRPDAT=RAMBUF_ADDRESS>>3;
    CRPINI7=CRPINI6=CRPINI5=CRPINI4=0;
    CRPINI3=CRPINI2=CRPINI1=CRPINI0=0;
    CRPCNT=RND_LENGTH;
    CRPCON|=0x11;
    while(!des_checked);

    ////////////////////////////////////////////////////////////////////
    //                     TEST RTC INTERRUPT                         //
    ////////////////////////////////////////////////////////////////////

    // Switch to 32kHz clock source
    X32_INPUT_SOURCE(X32_USING_CRYSTAL);
    X32_ENABLE(TRUE);
    halWait(250, CC1010EB_CLKFREQ);
    halWait(250, CC1010EB_CLKFREQ);
    MAIN_CLOCK_SET_SOURCE(CLOCK_X32);
    XOSC_ENABLE(FALSE);

    // Config real time clock, interrupts after 10 seconds
    halConfigRealTimeClock(10);
    RTC_RUN(TRUE);
    while(!rtc_checked);

    // switch back to high-speed XOSC
    XOSC_ENABLE(TRUE);
    MAIN_CLOCK_SET_SOURCE(CLOCK_XOSC);
    X32_ENABLE(FALSE);

    // Wait a while until next operation
    for (i=0; i<20; i++) halWait(250, CC1010EB_CLKFREQ);

    ////////////////////////////////////////////////////////////////////
    //                     TEST FLASH INTERRUPT                       //
    ////////////////////////////////////////////////////////////////////

    // Setup FLASH for writing
    // NOTE: Cannot use halFlashWritePage, function disables Flash interrupt
    FLTIM=(byte)(CC1010EB_CLKFREQ>>8)/3;
    FLADR=(word)FLASHPAGE_ADDRESS>>7;
    FLCON=(FLCON&0xF0) | (((word)RAMBUF_ADDRESS>>7)&0x0F) | 0x10;
    RESERVED|=0x80;
    ENTER_IDLE_MODE();
    RESERVED&=~0x80;
    FLCON &= ~0x10;

    // Wait for interrupt
    while(!flash_checked);

    // Infinite loop
    while(1);
}

// ISR (interrupt service routine) for DES and ADC, priority 9
// The interrupt must be cleared by software
void isr_des_adc() interrupt INUM_DES_ADC {
    INT_SETFLAG(INUM_DES_ADC, INT_CLR);
    if (INT_GETFLAG(INUM_ADC)) {
        INT_SETFLAG(INUM_ADC, INT_CLR);
        RLED=LED_ON;
        adc_checked = TRUE;
    } else if (INT_GETFLAG(INUM_DES)) {
        INT_SETFLAG(INUM_DES, INT_CLR);
        YLED=LED_ON;
        des_checked = TRUE;
    }
} //end isr_des_adc()

// ISR (interrupt service routine) for RTC, priority 11
// The interrupt must be cleared by software
void isr_rtc() interrupt INUM_RTC {
    INT_SETFLAG(INUM_RTC, INT_CLR);
    GLED=LED_ON;
    RTC_RUN(FALSE); // stop RTC
    rtc_checked=TRUE;
} //end isr_rtc()

// ISR (interrupt service routine) for Flash, priority 1
// The interrupt must be cleared by software
void isr_flash() interrupt INUM_FLASH {
    INT_SETFLAG(INUM_FLASH, INT_CLR);
    GLED=LED_OFF;
    YLED=LED_OFF;
    RLED=LED_OFF;
    BLED=LED_ON;
    rtc_checked=TRUE;
} //end isr_flash()

⌨️ 快捷键说明

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