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

📄 mastermain.c

📁 chipcon公司无线芯片cc1100的主机通信程序 具有一定的参考价值
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                CHIPCON CC1010 Wireless audio project         *
 *      ***   + +   ***                                                      *
 *      ***   +++   ***                     MasterMain                       *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This source file is part of a software project for Full Duplex,           *
 * single-chip, wireless intercom, written for the CC1010 chip               *
 * (RF-transceiver chip with integrated 8051 micro-controller).              *
 *****************************************************************************
 * Author:              OAE                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: MasterMain.c,v $
 * Revision 1.2  2003/08/18 12:19:32  tos
 * Synchronisation with library update (reentry issue).
 *
 * Revision 1.1  2003/08/04 12:34:26  tos
 * Initial version in CVS.
 *
 *                                                                           *
 *                                                                           *
 ****************************************************************************/

#include <Common/Main.h>

// Define preamble constants
#define PREAMBLE_BYTE_COUNT 18
#define PREAMBLE_BITS_SENSE_INIT 81
#define PREAMBLE_BITS_SENSE 16

// Variables
ulong adcperiod = 125;// The ADC sample period in us
ulong pwmperiod = 17;// The PWM-timer period in us
word wait = 5000;// A variable used to pause the program excecution
bit bufferfullflag;
bit initrunflag = 1;
word xdata tCounter; // Reload variable used by interrupt routine

// External constants
extern const byte xdata *transmit_buffer_start_ptr0;
extern const byte xdata *transmit_buffer_start_ptr1;
extern const byte xdata *receive_buffer_start_ptr0;
extern const byte xdata *receive_buffer_start_ptr1;

// External variables
extern bit currenttransmitbuffer;
extern bit currentreceivebuffer;

// Master specific prototypes
bit PacketAssembler(byte sample);
bit AverageFilterUpdate(byte numpreamblebitsense,
    RF_RXTXPAIR_SETTINGS* RF_SETTINGS);


/**************************************************************************
* main() - Main function                                                  *
***************************************************************************
* Description:                                                            *
*   Initiation and system control. This is where the program execution    *
*   starts.                                                               *
***************************************************************************
* Input arguments:                                                        *
*   None.                                                                 *
* Return value:                                                           *
*   None.
**************************************************************************/
void main()
{
    // Perform calibration
    halRFCalib(&RF_SETTINGS_INIT, &RF_CALDATA);

    // Select RF bytemode
    RFCON |= 0x01;

    // Set suitable synch byte
    RF_SET_SYNC_BYTE(RF_SUITABLE_SYNC_BYTE);

    // Disable watchdog timer
    WDT_ENABLE(FALSE);

    // Set optimum settings for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);

    // Initialize DES key at absolute address
    memcpy(DES_KEY, DES_KEY_TEMP, 7);

    // Initialize The leds
    BLED_OE(TRUE); BLED = LED_OFF;
    RLED_OE(TRUE); RLED = LED_OFF;
    YLED_OE(TRUE); YLED = LED_OFF;

    // Turn on interrupts
    INT_ENABLE(INUM_TIMER0, INT_ON);
    INT_GLOBAL_ENABLE(INT_ON);

    // Setup ADC, select AD1 as input
    halConfigADC(ADC_MODE_SINGLE | ADC_REFERENCE_VDD, CLK_FREQ, 0);
    ADC_SELECT_INPUT(ADC_INPUT_AD0);
    // Set the ADC clock divider to its lowest possible value
    ADCON2=0;
    // Power up the ADC from sleep mode
    ADC_POWER(TRUE);


    /**********************************************************************
    *                RF-initiation - average filter update                *
    **********************************************************************/

    while (initrunflag)
    {

    // Start transmission of preambles to update the slaves average filter
    PacketSend(PREAMBLE_BYTE_COUNT, NULL, NULL, &RF_SETTINGS_ACTIVE);

    // Start reception of preambles to update average filter
    initrunflag = AverageFilterUpdate(PREAMBLE_BITS_SENSE_INIT,
        &RF_SETTINGS_INIT);
    
    }// End while

    // Turn on red led to indicate successful initiation
    RLED = LED_ON;

    /*********************************************************************/

    // Configure timer 3 as PWM timer for DA-conversion
    halConfigTimer23(TIMER3 | TIMER23_PWM, pwmperiod, CLK_FREQ);
    TIMER3_RUN(TRUE);

    // Configure timer 0 to generate repeated interrups for ADC and DAC
    halConfigTimer01(TIMER0 | TIMER01_INT_TIMER, adcperiod, CLK_FREQ, &tCounter);
    TIMER0_RUN(TRUE);

    /**********************************************************************
    *                   Main loop - normal operation                      *
    **********************************************************************/

    while (TRUE)
    {
        // Wait until transmit buffer is filled with coded samples
        while(!bufferfullflag);

        // Toggle blue led to indicate packet transmission
        BLED = !BLED;
        
        // Read packet from the correct buffer and send
        if (currenttransmitbuffer == BUFFER0)
            PacketSend(PREAMBLE_BYTE_COUNT, transmit_buffer_start_ptr0,
                PACKET_SIZE, &RF_SETTINGS_ACTIVE);
        else
            PacketSend(PREAMBLE_BYTE_COUNT, transmit_buffer_start_ptr1,
                PACKET_SIZE, &RF_SETTINGS_ACTIVE);

        // Wait for synch then receive and save packet in correct buffer
        if (currentreceivebuffer == BUFFER0)
            PacketReceive(PREAMBLE_BITS_SENSE, receive_buffer_start_ptr1,
                PACKET_SIZE, &RF_SETTINGS_ACTIVE);
        else
            PacketReceive(PREAMBLE_BITS_SENSE, receive_buffer_start_ptr0,
                PACKET_SIZE, &RF_SETTINGS_ACTIVE);

    }// End while

    /*********************************************************************/

}//End main


/**************************************************************************
*                 Timer 0 interrupt service routine                       *
**************************************************************************/

void TIMER0_ISR() interrupt INUM_TIMER0
{
    // Reset the timer to generate another interrupt
    INT_SETFLAG (INUM_TIMER0, INT_CLR);
    ISR_TIMER0_ADJUST(tCounter);

    // Sample the ADC input
    ADCON|=0x04;
    // Continue without waiting for completion

    // Send the previous sampled value to the packet assembler
    bufferfullflag = PacketAssembler(ADC_GET_SAMPLE_8BIT());


    // Read and decode the received and buffered data, update output
    PWM3_SET_DUTY_CYCLE(PacketDisassembler());

}// End ISR

/*************************************************************************/

⌨️ 快捷键说明

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