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

📄 ex2c.c

📁 nRF24E1 sample transmit & receiver
💻 C
字号:
/*= ex2c.c =====================================================================
 *
 * Copyright (C) 2003, 2004 Nordic Semiconductor
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * WARRANTY OF ANY KIND.
 *
 * Author(s): Ole Saether
 *
 * DESCRIPTION:
 *
 *   This program gives an example of using the ADC and PWM together with the
 *   radio. You need two nrf24E1 evaluation boards to test this program. One
 *   board will act as the transmitter and the other as the receiver determined
 *   by the logical level on pin P0.6.
 * 
 *   After initializing the ADC and the radio, the transmitter enters an
 *   infinite loop continuously reading the ADC and transmitting the read data.
 *
 *   After initializing the PWM and radio, the receiver enters an infinite loop
 *   continuously waiting for a radio packet and updating the PWM with the data
 *   in the received packet.
 *
 *   The functionality is the same as in ex2a.asm.
 *
 * COMPILER:
 *  
 *  This program has been tested with Keil C51 V6.10 and V7.05.
 *
 * $Revision: 5 $
 *
 *==============================================================================
*/
#include <Nordic\reg24e1.h>

struct RFConfig
{
    unsigned char n;
    unsigned char buf[15];
};

typedef struct RFConfig RFConfig;

#define ADDR_INDEX  8   // Index to address bytes in RFConfig.buf
#define ADDR_COUNT  4   // Number of address bytes

const RFConfig txconf =
{
    15,
    0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x12, 0x34, 0x56, 0x78, 0x83, 0x6c, 0x04
};

const RFConfig rxconf =
{
    15,
    0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x87, 0x65, 0x43, 0x21, 0x83, 0x6c, 0x05
};

void Delay100us(volatile unsigned char n)
{
    unsigned char i;
    while(n--)
        for(i=0;i<35;i++)
            ;
}

unsigned char SpiReadWrite(unsigned char b)
{
    EXIF &= ~0x20;                  // Clear SPI interrupt
    SPI_DATA = b;                   // Move byte to send to SPI data register
    while((EXIF & 0x20) == 0x00)    // Wait until SPI hs finished transmitting
        ;
    return SPI_DATA;
}

void InitADC(void)
{
    ADCCON = 0x20;                  // Channel 0, NPD=1, ADCRUN=0, EXTREF=0
    ADCSTATIC &= 0x1c;
    ADCSTATIC |= 0x03;              // 12bit
    ADCCON &= ~0x80;                // Start..
    ADCCON |= 0x80;                 // ..new conversion
}

void InitPWM(void)
{
    P0_ALT |= 0x80;                 // Enable PWM output
    PWMCON = 0xc0;                  // Enable 8 bit PWM with minimum prescaler
}

unsigned char ReceivePacket()
{
    unsigned char b;
    CE = 1;
    while(DR1 == 0)
        ;
    b = SpiReadWrite(0);
    CE = 0;
    return b;
}

void TransmitPacket(unsigned char b)
{
    unsigned char i;
    CE = 1;
    Delay100us(0);
    // Start with the address of the receiver:
    for(i=0;i<ADDR_COUNT;i++)
        SpiReadWrite(rxconf.buf[ADDR_INDEX+i]);
    SpiReadWrite(b);
    CE = 0;
    Delay100us(3);                  // Wait ~300us
}

unsigned char ReadADC(void)
{
    unsigned char b;

    while((EXIF & 0x10) == 0)       // Wait until ADC conversion complete
        ;
    EXIF &= ~0x10;                  // Clear ADC completion bit
    b = ADCDATAH;                   // Read ADC data
    ADCCON &= ~0x80;                // Start..
    ADCCON |= 0x80;                 // ..new conversion
    return b;
}

void WritePWM(unsigned char b)
{
    PWMDUTY = b;
}

void Receiver(void)
{
    unsigned char b;
    CS = 1;
    Delay100us(0);
    for(b=0;b<rxconf.n;b++)
    {
        SpiReadWrite(rxconf.buf[b]);
    }
    CS = 0;
    for(;;)
    {
        b = ReceivePacket();
        WritePWM(b);
    }
}

void Transmitter(void)
{
    unsigned char b;
    
    CS = 1;
    Delay100us(0);
    for(b=0;b<txconf.n;b++)
    {
        SpiReadWrite(txconf.buf[b]);
    }
    CS = 0;
    for(;;)
    {
        b = ReadADC();              // Read ADC
        TransmitPacket(b);          // Transmit data
    }
}

void Init(void)
{
    P0_DIR = 0x40;                  // P0.6 is input, the rest output
    P0 = 0x10;                      // P0.4 = 1 for the rec/tran selection
    PWR_UP = 1;                     // Turn on Radio
    Delay100us(30);                 // Wait > 3ms 
    SPICLK = 0;                     // Max SPI clock (XTAL/8)
    SPI_CTRL = 0x02;                // Connect internal SPI controller to Radio
}    

void main(void)
{
    Init();
    if(P0 & 0x40)
    {
        InitPWM();
        Receiver();
    }
    else
    {
        InitADC();
        Transmitter();
    }
}

⌨️ 快捷键说明

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