Achieved PWM control way over 10MHz with 1Hz step ! )

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Achieved PWM control way over 10MHz with 1Hz step ! )

Postby Deouss » Sat Jun 09, 2018 11:39 pm

I was experimenting with RMT and did a small trick to generate very high frequency square wave

Here is a code snippet:

Code: Select all

#define RMT_TX_CHANNEL          (rmt_channel_t)1           /* RMT channel for transmitter */
#define RMT_TX_GPIO_NUM         PIN_PWM /* GPIO number for transmitter signal */
#define RMT_TX_CARRIER_DISABLE  0           /* Disable carrier  */

#define RMT_CLK_DIV             80         /* RMT counter clock divider */
#define rmt_item32_tIMEOUT_US   9500        /*!< RMT receiver timeout value(us) */

#define RMT_TICK_10_US          (80000000/RMT_CLK_DIV/100000)   /* RMT counter value for 10 us.(Source clock is APB clock) */
#define ITEM_DURATION(d)  (d*10.0/RMT_TICK_10_US)

#define PIN_PWM (gpio_num_t)(your pin #)


 //initialize RMT

    rmt_config_t rmt_tx;

    rmt_tx.channel =  RMT_TX_CHANNEL;
    rmt_tx.mem_block_num = 1;
    rmt_tx.clk_div = RMT_CLK_DIV;
    rmt_tx.gpio_num = PIN_PWM;
    rmt_tx.tx_config.loop_en = true;
    rmt_tx.tx_config.carrier_duty_percent = 50;
    rmt_tx.tx_config.carrier_freq_hz = 600;
    rmt_tx.tx_config.carrier_level = (rmt_carrier_level_t)0;
    rmt_tx.tx_config.carrier_en = 1;//RMT_TX_CARRIER_DISABLE;
    rmt_tx.tx_config.idle_level = (rmt_idle_level_t)0;
    rmt_tx.tx_config.idle_output_en = true;
    rmt_tx.rmt_mode = RMT_MODE_TX;
    rmt_config(&rmt_tx);
    rmt_driver_install(rmt_tx.channel, 0, 0);


    rmt_item32_t item;
    item.level0 = 10;
    item.duration0 = 1;
    item.level1 = 10;
    item.duration1 = 1;

    rmt_tx_start(RMT_TX_CHANNEL,true);
    rmt_write_items(RMT_TX_CHANNEL, &item, 1, true);
    rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);

in the control loop code:

		rmt_tx_stop(RMT_TX_CHANNEL);
             	rmt_tx.tx_config.carrier_freq_hz= desired_freq; // set your frequency here
          	rmt_config(&rmt_tx);
                rmt_tx_start(RMT_TX_CHANNEL,true);

                    xTaskCreatePinnedToCore(coreTask, "coreTask", 2048, (void*)rmt_tx.tx_config.carrier_freq_hz, 0, &xHandle, 1);
                    rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);


I used rotary encoder for increasing/decreasing the frequency by desired granularity
The scope went way above 1Mhz with full control
Around 10-15MHz weird things started to happen.
When I reached 40Mhz my PC locked for unknown reason - maybe USB interference

Cheers

Who is online

Users browsing this forum: No registered users and 50 guests