Add enableCarrier(uint32_t frequency, float duty) and disableCarrier() to RFControl

Allows you to overlay a carrier wave on the RF Signal - in practice this is only used for IR signals (not RF).  Automatically scales frequency to account for 80x difference between APB Clock and Ref Tick Clock depending on which is used.   Checks to ensure resulting parameters (high period and low period) are all in bounds (0,65536) and reports an error if they are not.
This commit is contained in:
Gregg 2021-10-15 22:32:03 -05:00
parent 4ad607951f
commit 5976fd3d0d
3 changed files with 54 additions and 4 deletions

View File

@ -36,6 +36,8 @@ RFControl::RFControl(uint8_t pin, boolean refClock){
// If specified, set the base clock to 1 MHz so tick-units are in microseconds (before any CLK_DIV is applied), otherwise default will be 80 MHz APB clock // If specified, set the base clock to 1 MHz so tick-units are in microseconds (before any CLK_DIV is applied), otherwise default will be 80 MHz APB clock
this->refClock=refClock;
if(refClock) if(refClock)
#ifdef CONFIG_IDF_TARGET_ESP32C3 #ifdef CONFIG_IDF_TARGET_ESP32C3
REG_SET_FIELD(RMT_SYS_CONF_REG,RMT_SCLK_DIV_NUM,79); // ESP32-C3 does not have a 1 MHz REF Tick Clock, but allows the 80 MHz APB clock to be scaled by an additional RMT-specific divider REG_SET_FIELD(RMT_SYS_CONF_REG,RMT_SCLK_DIV_NUM,79); // ESP32-C3 does not have a 1 MHz REF Tick Clock, but allows the 80 MHz APB clock to be scaled by an additional RMT-specific divider
@ -100,4 +102,40 @@ void RFControl::phase(uint32_t nTicks, uint8_t phase){
/////////////////// ///////////////////
void RFControl::enableCarrier(uint32_t freq, float duty){
if(duty<0)
duty=0;
if(duty>1)
duty=1;
if(freq>0){
float period=1.0e6/freq*(refClock?1:80);
uint32_t highTime=period*duty+0.5;
uint32_t lowTime=period*(1.0-duty)+0.5;
if(highTime>0xFFFF || lowTime>0xFFFF){
Serial.printf("\n*** ERROR: Can't enable carrier frequency=%d Hz for RF Control pin=%d, duty=%0.2f. Frequency is too low!\n\n",freq,config->gpio_num,duty);
return;
}
if(highTime==0){
Serial.printf("\n*** ERROR: Can't enable carrier frequency=%d Hz for RF Control pin=%d, duty=%0.2f. Duty is too low or frequency is too high!\n\n",freq,config->gpio_num,duty);
return;
}
if(lowTime==0){
Serial.printf("\n*** ERROR: Can't enable carrier frequency=%d Hz for RF Control pin=%d, duty=%0.2f. Duty is too high or frequency is too high!\n\n",freq,config->gpio_num,duty);
return;
}
// Serial.printf("%d %g %d %d\n",freq,period,highTime,lowTime);
rmt_set_tx_carrier(config->channel,true,highTime,lowTime,RMT_CARRIER_LEVEL_HIGH);
} else {
rmt_set_tx_carrier(config->channel,false,0,0,RMT_CARRIER_LEVEL_HIGH);
}
}
///////////////////
uint8_t RFControl::nChannels=0; uint8_t RFControl::nChannels=0;

View File

@ -13,6 +13,7 @@ class RFControl {
rmt_config_t *config=NULL; rmt_config_t *config=NULL;
vector<uint32_t> data; vector<uint32_t> data;
boolean lowWord=true; boolean lowWord=true;
boolean refClock;
static uint8_t nChannels; static uint8_t nChannels;
public: public:
@ -24,6 +25,8 @@ class RFControl {
void clear(); // clears transmitter memory void clear(); // clears transmitter memory
void add(uint32_t onTime, uint32_t offTime); // adds pulse of onTime ticks HIGH followed by offTime ticks LOW void add(uint32_t onTime, uint32_t offTime); // adds pulse of onTime ticks HIGH followed by offTime ticks LOW
void phase(uint32_t nTicks, uint8_t phase); // adds either a HIGH phase or LOW phase lasting numTicks ticks void phase(uint32_t nTicks, uint8_t phase); // adds either a HIGH phase or LOW phase lasting numTicks ticks
void enableCarrier(uint32_t freq, float duty=0.5); // enables carrier wave if freq>0, else disables carrier wave; duty is a fraction from 0-1
void disableCarrier(){enableCarrier(0);} // disables carrier wave
}; };
// Helper macro for creating your own storage of uint32_t data array elements - used with first variation of start() above // Helper macro for creating your own storage of uint32_t data array elements - used with first variation of start() above

View File

@ -14,12 +14,21 @@ void setup() {
rf.clear(); rf.clear();
rf.add(10000,10000); // rf.add(1000000,1000000);
rf.add(10000,10000); // rf.add(1000000,1000000);
rf.add(10000,30000); rf.add(100000,1);
for(int i=100;i>=0;i-=5){
rf.enableCarrier(38000,i/100.0);
rf.start(1,4);
}
// rf.disableCarrier();
// rf.start(1,200);
while(1);
uint32_t t0=micros(); uint32_t t0=micros();
rf.start(4,1); rf.start(4,80);
uint32_t t1=micros(); uint32_t t1=micros();
Serial.println("End Example"); Serial.println("End Example");