diff --git a/src/extras/RFControl.cpp b/src/extras/RFControl.cpp index 7f62c81..ce2ce88 100644 --- a/src/extras/RFControl.cpp +++ b/src/extras/RFControl.cpp @@ -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 + this->refClock=refClock; + if(refClock) #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 @@ -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; diff --git a/src/extras/RFControl.h b/src/extras/RFControl.h index 5ca787f..c099935 100644 --- a/src/extras/RFControl.h +++ b/src/extras/RFControl.h @@ -13,6 +13,7 @@ class RFControl { rmt_config_t *config=NULL; vector data; boolean lowWord=true; + boolean refClock; static uint8_t nChannels; public: @@ -24,6 +25,8 @@ class RFControl { 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 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 diff --git a/src/extras/extras.ino b/src/extras/extras.ino index f104b67..4640ae7 100644 --- a/src/extras/extras.ino +++ b/src/extras/extras.ino @@ -14,12 +14,21 @@ void setup() { rf.clear(); - rf.add(10000,10000); - rf.add(10000,10000); - rf.add(10000,30000); +// rf.add(1000000,1000000); +// rf.add(1000000,1000000); + 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(); - rf.start(4,1); + rf.start(4,80); uint32_t t1=micros(); Serial.println("End Example");