📄 ex2c.c
字号:
/*= ex2c.c =====================================================================
*
* Written by Greg Hunter, University of Technology, Sydney, 2007 for sdcc compiler
* Based in the program ex2c.c by Nordic Semiconductor (author: Ole Saether)
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* WARRANTY OF ANY KIND.
*
* Author: Greg Hunter
*
* 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. Jumper P0.6 to P0.4 for the receiver and
* to GND for the transmitter.
*
* 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.
*
* Greg Hunter: The program has been modified to use my modules for interfacing
* to the radio, spi bus, and a/d converter.
*
* Modules added:
* radio.h, radio.c: interface to radio
* spi.h, spi.c: interface to spi bus
* adc.h, adc.c: interface to a/d converter
* delay.h, delay.c: delay generator routines
* uart.h, uart.c: UART functions for SDCC by Chin-Shiuh, modified by Greg Hunter
* for nRF24E1. Used for debugging
*
* COMPILER:
*
* SDCC compiler for 8051 V2.5.0.
*
* $Revision: 0.1
*
*==============================================================================
*/
#define INITIALIZE
#include "regs24e1.h"
#include "delay.h"
#include "uart.h"
#include "adc.h"
#include "spi.h"
#include "radio.h"
void InitPWM(void)
{
P0_ALT |= 0x80; // Enable PWM output
PWMCON = 0xc0; // Enable 8 bit PWM with minimum prescaler
}
unsigned char ReceivePacket()
{
unsigned char b;
while(RadioRXBuf(ComRXBufPt) == 0) ;
b = ComParm.pwmData;
return b;
}
void WritePWM(unsigned char b)
{
PWMDUTY = b;
}
void Receiver(void)
{
unsigned char b;
for(;;)
{
b = ReceivePacket();
WritePWM(b);
}
}
void Transmitter(void)
{
unsigned char b;
ComParm.TXAddress3 = 0x00; // Set up transmit address
ComParm.TXAddress2 = 0x00;
ComParm.TXAddress1 = 0x00;
ComParm.TXAddress0 = 0x04;
for(;;)
{
b = AdcRead(0) >> 8; // Read ADC
ComParm.adcData = b;
RadioTXBuf(ComTXBufPt,COM_TX_BYTES); // 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
RadioInit();
RadioOn();
RadioCfg();
}
void main(void)
{
uart_init();
uart_puts("\r\nuart output enabled for debugging");
Init();
if(P0 & 0x40)
{
InitPWM();
Receiver();
}
else
{
AdcInit();
Transmitter();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -