📄 1.c
字号:
/*= ex2c.c =====================================================================
*
* Copyright (C) 2003 Nordic VLSI ASA
*
* Author(s) : Ole Saether
* Support mail : nrfprog@nvlsi.no
*
* 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 DIO8 (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: 1 $
*
*==============================================================================
*/
#include <nrf24e1.h>
struct RFConfig
{
unsigned char n;
unsigned char buf[15];
};
typedef struct RFConfig RFConfig;
#define ADDR_INDEX 10 // Index to address bytes in RFConfig.buf
#define ADDR_COUNT 2 // Number of address bytes
const RFConfig tconf =
{
15,
0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xaa, 0xbb, 0x00, 0x09, 0x43, 0x6f, 0x04
};
const RFConfig rconf =
{
15,
0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xaa, 0xbb, 0x12, 0x34, 0x43, 0x6f, 0x05
};
unsigned char buf[4];
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
}
void TransmitPacket(unsigned int b)
{
unsigned int b1;
unsigned char an;
unsigned char i;
b1=b;
buf[0]=(b>>8);
buf[1]=(b1<<8);
CE = 1;
Delay100us(0);
for(i=0;i<ADDR_COUNT;i++)
{
SpiReadWrite(tconf.buf[ADDR_INDEX+i]);
}
for (an=0;an<2;an++)
{
SpiReadWrite(buf[an]);
}
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 int b)
{
PWMDUTY = b;
}
void Receiver(void)
{
unsigned char a,i;
unsigned char b;
CS = 1;
Delay100us(0);
for(b=0;b<rconf.n;b++)
{
SpiReadWrite(rconf.buf[b]);
}
CS = 0;
for(;;)
{
CE = 1;
while(DR1 == 0)
;
for (a=0;a<2;a++)
{
buf[a] = SpiReadWrite(0);
}
CE = 0;
//buf[0]=0x11;
// buf[1]=0xAA;
for(i=0;i<2;i++)
{
SBUF=buf[i];
while(!TI );
TI = 0;
Delay100us(100);
}
//Delay100us(100);
}
}
void qw(void)
{
unsigned char b,i,an;
CS = 1;
Delay100us(0);
for(b=0;b<tconf.n;b++)
{
SpiReadWrite(tconf.buf[b]);
}
CS = 0;
REGX_LSB=0x50;
REGX_MSB=0x09;
buf[0]=0x11;
buf[1]=0x11;
buf[2]=0x11;
buf[3]=0x11;
for(;;)
{
for (an=0;an<4;an++)
{
SBUF=buf[an];
while(!TI);TI=0;
}
// b = ReadADC(); // Read ADC
// TransmitPacket(257); // Transmit data
/* CE = 1;
Delay100us(0);
for(i=0;i<ADDR_COUNT;i++)
{
SpiReadWrite(tconf.buf[ADDR_INDEX+i]);
}
REGX_LSB=0x50;
REGX_MSB=0x09;
for (an=0;an<4;an++)
{
SpiReadWrite(buf[an]);
}
CE = 0;
REGX_LSB=0x50;
REGX_MSB=0x09;
Delay100us(5);
REGX_LSB=0x2c;
REGX_MSB=0x01;
PCON=0x01;*/
}
}
void Init(void)
{
P0_ALT=0x06;
//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;
TMOD = 0x20; // 定时器1工作于8位自动重载模式, 用于产生波特率
TH1 = 0xF7; // 波特率9600
TL1 = 0xF7;
SCON = 0x50; // 设定串行口工作方式
PCON = 0x80; // 波特率不倍增
TR1 = 1; // 启动定时器1 // 禁止任何中// Connect internal SPI controller to Radio
//REGX_CTRL |=0x08;
// REGX_LSB=0x50;
// REGX_MSB=0x09;
}
void main(void)
{
Init();
while(1)
{
qw();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -